]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Types.hs
[zip] implement zip for parsers
[gargantext.git] / src / Gargantext / API / Node / Types.hs
1 {-# LANGUAGE TemplateHaskell #-}
2
3 module Gargantext.API.Node.Types where
4
5 import Control.Lens hiding (elements, Empty)
6 import Data.Aeson
7 import qualified Data.ByteString as BS
8 import qualified Data.ByteString.Base64 as BSB64
9 import Data.Either
10 import Data.Swagger
11 import Data.Text (Text)
12 import qualified Data.Text as T
13 import qualified Data.Text.Encoding as TE
14 import GHC.Generics (Generic)
15 import Servant.Job.Utils (jsonOptions)
16 import Web.FormUrlEncoded (FromForm, ToForm)
17
18 import Gargantext.Core (Lang(..){-, allLangs-})
19 import Gargantext.Core.Utils.Prefix (unPrefixSwagger)
20 import Gargantext.Prelude
21 import qualified Gargantext.Database.GargDB as GargDB
22 import Gargantext.API.Node.Corpus.New.Types (FileType, FileFormat)
23
24 -------------------------------------------------------
25 data NewWithForm = NewWithForm
26 { _wf_filetype :: !FileType
27 , _wf_fileformat :: !FileFormat
28 , _wf_data :: !Text -- NOTE for binary files, this represents base-64 data
29 , _wf_lang :: !(Maybe Lang)
30 , _wf_name :: !Text
31 } deriving (Eq, Show, Generic)
32
33 makeLenses ''NewWithForm
34 instance FromForm NewWithForm
35 instance ToForm NewWithForm
36 instance FromJSON NewWithForm where
37 parseJSON = genericParseJSON $ jsonOptions "_wf_"
38 instance ToJSON NewWithForm where
39 toJSON = genericToJSON $ jsonOptions "_wf_"
40 instance ToSchema NewWithForm where
41 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")
42
43 -------------------------------------------------------
44
45 data NewWithFile = NewWithFile
46 { _wfi_b64_data :: !Text
47 , _wfi_lang :: !(Maybe Lang)
48 , _wfi_name :: !Text
49 } deriving (Eq, Show, Generic)
50
51 makeLenses ''NewWithFile
52 instance FromForm NewWithFile
53 instance ToForm NewWithFile
54 instance FromJSON NewWithFile where
55 parseJSON = genericParseJSON $ jsonOptions "_wfi_"
56 instance ToJSON NewWithFile where
57 toJSON = genericToJSON $ jsonOptions "_wfi_"
58
59
60 instance ToSchema NewWithFile where
61 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wfi_")
62
63 instance GargDB.SaveFile NewWithFile where
64 saveFile' fp (NewWithFile b64d _ _) = do
65 let eDecoded = BSB64.decode $ TE.encodeUtf8 b64d
66 case eDecoded of
67 Left err -> panic $ T.pack $ "Error decoding: " <> err
68 Right decoded -> BS.writeFile fp decoded
69 -- BS.writeFile fp $ BSB64.decodeLenient $ TE.encodeUtf8 b64d
70
71 --instance GargDB.ReadFile NewWithFile where
72 -- readFile' = TIO.readFile