2 Module : Gargantext.API.Corpus.New.File
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 OverloadedStrings #-}
20 {-# LANGUAGE RankNTypes #-}
21 {-# LANGUAGE ScopedTypeVariables #-}
22 {-# LANGUAGE TemplateHaskell #-}
23 {-# LANGUAGE TypeOperators #-}
25 module Gargantext.API.Corpus.New.File
28 import Control.Lens ((.~), (?~))
29 import Control.Monad (forM)
30 import Control.Monad.IO.Class (liftIO)
33 import Data.Monoid (mempty)
35 import Data.Text (Text())
36 import GHC.Generics (Generic)
37 import Gargantext.API.Ngrams (TODO)
38 import Gargantext.Database.Types.Node
39 import Gargantext.Database.Utils -- (Cmd, CmdM)
40 import Gargantext.Prelude
41 import Gargantext.Prelude.Utils (sha)
43 import Servant.Multipart
44 import Servant.Swagger (HasSwagger(toSwagger))
45 import Servant.Swagger.Internal
46 import Test.QuickCheck (elements)
47 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
49 -------------------------------------------------------------
51 data FileType = CSV | PresseRIS
52 deriving (Eq, Show, Generic)
54 instance ToSchema FileType
55 instance Arbitrary FileType
57 arbitrary = elements [CSV, PresseRIS]
58 instance ToParamSchema FileType
60 instance FromJSON FileType
62 instance ToParamSchema (MultipartData Mem) where
63 toParamSchema _ = toParamSchema (Proxy :: Proxy TODO)
65 instance FromHttpApiData FileType
67 parseUrlPiece "CSV" = pure CSV
68 parseUrlPiece "PresseRis" = pure PresseRIS
69 parseUrlPiece _ = pure CSV -- TODO error here
72 instance (ToParamSchema a, HasSwagger sub) =>
73 HasSwagger (MultipartForm tag a :> sub) where
75 toSwagger _ = toSwagger (Proxy :: Proxy sub)
80 & schema .~ ParamOther sch
82 & in_ .~ ParamFormData
83 & paramSchema .~ toParamSchema (Proxy :: Proxy a)
86 type WithUpload' = Summary "Upload file(s) to a corpus"
87 :> QueryParam "fileType" FileType
88 :> MultipartForm Mem (MultipartData Mem)
89 :> Post '[JSON] [Hash]
91 --postUpload :: NodeId -> Maybe FileType -> GargServer UploadAPI
92 --postUpload :: NodeId -> GargServer UploadAPI
97 postUpload _ Nothing _ = panic "fileType is a required parameter"
98 postUpload _ (Just fileType) multipartData = do
99 putStrLn $ "File Type: " <> (show fileType)
101 putStrLn ("Inputs:" :: Text)
102 forM (inputs multipartData) $ \input -> do
103 putStrLn $ ("iName " :: Text) <> (iName input)
104 <> ("iValue " :: Text) <> (iValue input)
107 _ <- forM (files multipartData) $ \file -> do
108 let content = fdPayload file
109 putStrLn $ ("XXX " :: Text) <> (fdFileName file)
110 putStrLn $ ("YYY " :: Text) <> cs content
112 -- is <- inputs multipartData
114 pure $ map (sha . cs) is
116 -------------------------------------------------------------------