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 | CSV_HAL | 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 "CSV_HAL" = pure CSV_HAL
69 parseUrlPiece "PresseRis" = pure PresseRIS
70 parseUrlPiece _ = pure CSV -- TODO error here
73 instance (ToParamSchema a, HasSwagger sub) =>
74 HasSwagger (MultipartForm tag a :> sub) where
76 toSwagger _ = toSwagger (Proxy :: Proxy sub)
81 & schema .~ ParamOther sch
83 & in_ .~ ParamFormData
84 & paramSchema .~ toParamSchema (Proxy :: Proxy a)
87 type WithUpload' = Summary "Upload file(s) to a corpus"
88 :> QueryParam "fileType" FileType
89 :> MultipartForm Mem (MultipartData Mem)
90 :> Post '[JSON] [Hash]
92 --postUpload :: NodeId -> Maybe FileType -> GargServer UploadAPI
93 --postUpload :: NodeId -> GargServer UploadAPI
98 postUpload _ Nothing _ = panic "fileType is a required parameter"
99 postUpload _ (Just fileType) multipartData = do
100 putStrLn $ "File Type: " <> (show fileType)
102 putStrLn ("Inputs:" :: Text)
103 forM (inputs multipartData) $ \input -> do
104 putStrLn $ ("iName " :: Text) <> (iName input)
105 <> ("iValue " :: Text) <> (iValue input)
108 _ <- forM (files multipartData) $ \file -> do
109 let content = fdPayload file
110 putStrLn $ ("XXX " :: Text) <> (fdFileName file)
111 putStrLn $ ("YYY " :: Text) <> cs content
113 -- is <- inputs multipartData
115 pure $ map (sha . cs) is
117 -------------------------------------------------------------------