]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Types.hs
Merge branch 'dev' into 475-dev-node-team-invite
[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 import Gargantext.Core.Text.List.Social (FlowSocialListWith)
24
25 -------------------------------------------------------
26 data NewWithForm = NewWithForm
27 { _wf_filetype :: !FileType
28 , _wf_fileformat :: !FileFormat
29 , _wf_data :: !Text -- NOTE for binary files, this represents base-64 data
30 , _wf_lang :: !(Maybe Lang)
31 , _wf_name :: !Text
32 , _wf_selection :: !FlowSocialListWith
33 } deriving (Eq, Show, Generic)
34
35 makeLenses ''NewWithForm
36 instance FromForm NewWithForm
37 instance ToForm NewWithForm
38 instance FromJSON NewWithForm where
39 parseJSON = genericParseJSON $ jsonOptions "_wf_"
40 instance ToJSON NewWithForm where
41 toJSON = genericToJSON $ jsonOptions "_wf_"
42 instance ToSchema NewWithForm where
43 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")
44
45 -------------------------------------------------------
46
47 data NewWithFile = NewWithFile
48 { _wfi_b64_data :: !Text
49 , _wfi_lang :: !(Maybe Lang)
50 , _wfi_name :: !Text
51 } deriving (Eq, Show, Generic)
52
53 makeLenses ''NewWithFile
54 instance FromForm NewWithFile
55 instance ToForm NewWithFile
56 instance FromJSON NewWithFile where
57 parseJSON = genericParseJSON $ jsonOptions "_wfi_"
58 instance ToJSON NewWithFile where
59 toJSON = genericToJSON $ jsonOptions "_wfi_"
60
61
62 instance ToSchema NewWithFile where
63 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wfi_")
64
65 instance GargDB.SaveFile NewWithFile where
66 saveFile' fp (NewWithFile b64d _ _) = do
67 let eDecoded = BSB64.decode $ TE.encodeUtf8 b64d
68 case eDecoded of
69 Left err -> panic $ T.pack $ "Error decoding: " <> err
70 Right decoded -> BS.writeFile fp decoded
71 -- BS.writeFile fp $ BSB64.decodeLenient $ TE.encodeUtf8 b64d
72
73 --instance GargDB.ReadFile NewWithFile where
74 -- readFile' = TIO.readFile