]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Types.hs
Merge branch 'dev-charts-update-economy' of ssh://gitlab.iscpif.fr:20022/gargantext...
[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)
17
18 import Gargantext.Core (Lang(..){-, allLangs-})
19 import Gargantext.Core.Utils.Prefix (unPrefixSwagger)
20 import Gargantext.Prelude
21 import qualified Gargantext.Prelude.Utils as GPU
22 import Gargantext.API.Node.Corpus.New.File (FileType)
23
24 -------------------------------------------------------
25 data NewWithForm = NewWithForm
26 { _wf_filetype :: !FileType
27 , _wf_data :: !Text
28 , _wf_lang :: !(Maybe Lang)
29 , _wf_name :: !Text
30 } deriving (Eq, Show, Generic)
31
32 makeLenses ''NewWithForm
33 instance FromForm NewWithForm
34 instance FromJSON NewWithForm where
35 parseJSON = genericParseJSON $ jsonOptions "_wf_"
36 instance ToSchema NewWithForm where
37 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")
38
39 -------------------------------------------------------
40
41 data NewWithFile = NewWithFile
42 { _wfi_b64_data :: !Text
43 , _wfi_lang :: !(Maybe Lang)
44 , _wfi_name :: !Text
45 } deriving (Eq, Show, Generic)
46
47 makeLenses ''NewWithFile
48 instance FromForm NewWithFile
49 instance FromJSON NewWithFile where
50 parseJSON = genericParseJSON $ jsonOptions "_wfi_"
51 instance ToSchema NewWithFile where
52 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wfi_")
53
54 instance GPU.SaveFile NewWithFile where
55 saveFile' fp (NewWithFile b64d _ _) = do
56 let eDecoded = BSB64.decode $ TE.encodeUtf8 b64d
57 case eDecoded of
58 Left err -> panic $ T.pack $ "Error decoding: " <> err
59 Right decoded -> BS.writeFile fp decoded
60 -- BS.writeFile fp $ BSB64.decodeLenient $ TE.encodeUtf8 b64d
61
62 --instance GPU.ReadFile NewWithFile where
63 -- readFile' = TIO.readFile