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 Gargantext.Prelude
31 import Data.Text (Text)
34 import Servant.Multipart
35 --import Servant.Mock (HasMock(mock))
36 import Servant.Swagger (HasSwagger(toSwagger))
37 import Servant.Swagger.Internal
38 -- import qualified Data.ByteString.Lazy as LBS
40 import Control.Monad.IO.Class
41 import Gargantext.API.Types
42 --import Servant.CSV.Cassava (CSV'(..))
43 --import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
45 import Gargantext.API.Ngrams (TODO)
46 import Gargantext.Prelude.Utils (hash)
49 -- TODO Is it possible to adapt the function according to iValue input ?
50 --type API = MultipartForm Mem (MultipartData Mem) :> Post '[JSON] Integer
52 -- instance Generic Mem
54 --instance ToSchema Mem
55 --instance Arbitrary Mem
57 instance ToParamSchema (MultipartData Mem) where
58 toParamSchema _ = toParamSchema (Proxy :: Proxy TODO)
60 --instance Arbitrary ( MultipartData Mem)
62 instance (ToParamSchema a, HasSwagger sub) =>
63 HasSwagger (MultipartForm tag a :> sub) where
65 toSwagger _ = toSwagger (Proxy :: Proxy sub)
70 & schema .~ ParamOther sch
72 & in_ .~ ParamFormData
73 & paramSchema .~ toParamSchema (Proxy :: Proxy a)
74 --declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
75 --instance Arbitrary (MultipartForm Mem (MultipartData Mem))
78 instance (FromMultipart tag a, MultipartBackend tag, Servant.Multipart.LookupContext context (MultipartOptions tag))
79 => HasMock (MultipartForm tag a :> sub) context where
82 instance HasMock (MultipartForm Mem (MultipartData Mem) :> sub) context where
88 type ApiUpload = MultipartForm Mem (MultipartData Mem) :> Post '[JSON] [Hash]
89 -- MultipartData consists in textual inputs,
90 -- accessible through its "inputs" field, as well
91 -- as files, accessible through its "files" field.
92 upload :: GargServer ApiUpload
93 upload multipartData = do
97 putStrLn ("Inputs:" :: Text)
98 forM (inputs multipartData) $ \input -> do
99 putStrLn $ ("iName " :: Text) <> (iName input)
100 <> ("iValue " :: Text) <> (iValue input)
104 _ <- forM (files multipartData) $ \file -> do
105 let content = fdPayload file
106 putStrLn $ ("XXX " :: Text) <> (fdFileName file)
107 putStrLn $ ("YYY " :: Text) <> cs content
109 -- is <- inputs multipartData
112 pure $ map (hash . cs) is
113 -------------------------------------------------------------------------------