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)
32 import Data.Monoid (mempty)
34 import Data.Text (Text())
35 import GHC.Generics (Generic)
36 import Gargantext.API.Ngrams (TODO)
37 import Gargantext.Database.Types.Node
38 import Gargantext.Database.Utils -- (Cmd, CmdM)
39 import Gargantext.Prelude
40 import Gargantext.Prelude.Utils (sha)
42 import Servant.Multipart
43 import Servant.Swagger (HasSwagger(toSwagger))
44 import Servant.Swagger.Internal
45 import Test.QuickCheck (elements)
46 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
48 -------------------------------------------------------------
54 deriving (Eq, Show, Generic)
56 instance ToSchema FileType
57 instance Arbitrary FileType
59 arbitrary = elements [CSV, PresseRIS]
60 instance ToParamSchema FileType
62 instance FromJSON FileType
64 instance ToParamSchema (MultipartData Mem) where
65 toParamSchema _ = toParamSchema (Proxy :: Proxy TODO)
67 instance FromHttpApiData FileType
69 parseUrlPiece "CSV" = pure CSV
70 parseUrlPiece "CSV_HAL" = pure CSV_HAL
71 parseUrlPiece "PresseRis" = pure PresseRIS
72 parseUrlPiece _ = pure CSV -- TODO error here
75 instance (ToParamSchema a, HasSwagger sub) =>
76 HasSwagger (MultipartForm tag a :> sub) where
78 toSwagger _ = toSwagger (Proxy :: Proxy sub)
83 & schema .~ ParamOther sch
85 & in_ .~ ParamFormData
86 & paramSchema .~ toParamSchema (Proxy :: Proxy a)
89 type WithUpload' = Summary "Upload file(s) to a corpus"
90 :> QueryParam "fileType" FileType
91 :> MultipartForm Mem (MultipartData Mem)
92 :> Post '[JSON] [Hash]
94 --postUpload :: NodeId -> Maybe FileType -> GargServer UploadAPI
95 --postUpload :: NodeId -> GargServer UploadAPI
100 postUpload _ Nothing _ = panic "fileType is a required parameter"
101 postUpload _ (Just fileType) multipartData = do
102 printDebug "File Type: " fileType
104 printDebug "Inputs:" ()
105 forM (inputs multipartData) $ \input -> do
106 printDebug "iName " (iName input)
107 printDebug "iValue " (iValue input)
110 _ <- forM (files multipartData) $ \file -> do
111 let content = fdPayload file
112 printDebug "XXX " (fdFileName file)
113 printDebug "YYY " content
115 -- is <- inputs multipartData
117 pure $ map (sha . cs) is
119 -------------------------------------------------------------------