1 {-# LANGUAGE TemplateHaskell #-}
3 module Gargantext.API.Node.Types where
5 import Control.Lens hiding (elements, Empty)
7 import qualified Data.ByteString as BS
8 import qualified Data.ByteString.Base64 as BSB64
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)
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.File (FileType)
24 -------------------------------------------------------
25 data NewWithForm = NewWithForm
26 { _wf_filetype :: !FileType
27 , _wf_data :: !Text -- NOTE for binary files, this represents base-64 data
28 , _wf_lang :: !(Maybe Lang)
30 } deriving (Eq, Show, Generic)
32 makeLenses ''NewWithForm
33 instance FromForm NewWithForm
34 instance ToForm NewWithForm
35 instance FromJSON NewWithForm where
36 parseJSON = genericParseJSON $ jsonOptions "_wf_"
37 instance ToJSON NewWithForm where
38 toJSON = genericToJSON $ jsonOptions "_wf_"
39 instance ToSchema NewWithForm where
40 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")
42 -------------------------------------------------------
44 data NewWithFile = NewWithFile
45 { _wfi_b64_data :: !Text
46 , _wfi_lang :: !(Maybe Lang)
48 } deriving (Eq, Show, Generic)
50 makeLenses ''NewWithFile
51 instance FromForm NewWithFile
52 instance ToForm NewWithFile
53 instance FromJSON NewWithFile where
54 parseJSON = genericParseJSON $ jsonOptions "_wfi_"
55 instance ToJSON NewWithFile where
56 toJSON = genericToJSON $ jsonOptions "_wfi_"
59 instance ToSchema NewWithFile where
60 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wfi_")
62 instance GargDB.SaveFile NewWithFile where
63 saveFile' fp (NewWithFile b64d _ _) = do
64 let eDecoded = BSB64.decode $ TE.encodeUtf8 b64d
66 Left err -> panic $ T.pack $ "Error decoding: " <> err
67 Right decoded -> BS.writeFile fp decoded
68 -- BS.writeFile fp $ BSB64.decodeLenient $ TE.encodeUtf8 b64d
70 --instance GargDB.ReadFile NewWithFile where
71 -- readFile' = TIO.readFile