2 Module : Gargantext.API.Upload
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE DeriveGeneric #-}
16 {-# LANGUAGE FlexibleContexts #-}
17 {-# LANGUAGE FlexibleInstances #-}
18 {-# LANGUAGE NoImplicitPrelude #-}
19 {-# LANGUAGE MultiParamTypeClasses #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE RankNTypes #-}
22 {-# LANGUAGE ScopedTypeVariables #-}
23 {-# LANGUAGE TemplateHaskell #-}
24 {-# LANGUAGE TypeOperators #-}
26 module Gargantext.API.Upload
29 import Control.Lens ((.~), (?~))
30 import qualified Data.Text as Text
31 import GHC.Generics (Generic)
32 import Gargantext.Prelude
33 import Data.Text (Text)
37 import Servant.Multipart
38 --import Servant.Mock (HasMock(mock))
39 import Servant.Swagger (HasSwagger(toSwagger))
40 import Servant.Swagger.Internal
41 -- import qualified Data.ByteString.Lazy as LBS
43 import Control.Monad.IO.Class
44 import Gargantext.API.Types
45 --import Servant.CSV.Cassava (CSV'(..))
46 --import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
48 import Gargantext.API.Ngrams (TODO)
49 import Gargantext.Prelude.Utils (hash)
52 -- TODO Is it possible to adapt the function according to iValue input ?
53 --type API = MultipartForm Mem (MultipartData Mem) :> Post '[JSON] Integer
55 -- instance Generic Mem
57 --instance ToSchema Mem
58 --instance Arbitrary Mem
60 instance ToParamSchema (MultipartData Mem) where
61 toParamSchema _ = toParamSchema (Proxy :: Proxy TODO)
63 --instance Arbitrary ( MultipartData Mem)
65 instance (ToParamSchema a, HasSwagger sub) =>
66 HasSwagger (MultipartForm tag a :> sub) where
68 toSwagger _ = toSwagger (Proxy :: Proxy sub)
73 & schema .~ ParamOther sch
75 & in_ .~ ParamFormData
76 & paramSchema .~ toParamSchema (Proxy :: Proxy a)
77 --declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
78 --instance Arbitrary (MultipartForm Mem (MultipartData Mem))
81 instance (FromMultipart tag a, MultipartBackend tag, Servant.Multipart.LookupContext context (MultipartOptions tag))
82 => HasMock (MultipartForm tag a :> sub) context where
85 instance HasMock (MultipartForm Mem (MultipartData Mem) :> sub) context where
89 data Upload = Upload { up :: [Text] }
92 instance ToJSON Upload
96 type ApiUpload = MultipartForm Mem (MultipartData Mem) :> Post '[JSON] Hash
97 -- MultipartData consists in textual inputs,
98 -- accessible through its "inputs" field, as well
99 -- as files, accessible through its "files" field.
100 upload :: GargServer ApiUpload
101 upload multipartData = do
105 putStrLn ("Inputs:" :: Text)
106 forM (inputs multipartData) $ \input -> do
107 putStrLn $ ("iName " :: Text) <> (iName input)
108 <> ("iValue " :: Text) <> (iValue input)
112 _ <- forM (files multipartData) $ \file -> do
113 let content = fdPayload file
114 putStrLn $ ("XXX " :: Text) <> (fdFileName file)
115 putStrLn $ ("YYY " :: Text) <> cs content
117 -- is <- inputs multipartData
120 pure $ hash $ Text.concat $ map cs is
121 -------------------------------------------------------------------------------