1 {-# OPTIONS_GHC -fno-warn-orphans -fno-warn-unused-matches #-}
3 {-# LANGUAGE AllowAmbiguousTypes #-}
4 {-# LANGUAGE TypeOperators #-}
6 module Gargantext.API.Node.File where
8 import Control.Lens ((^.))
9 import qualified Data.ByteString as BS
10 import qualified Data.ByteString.Lazy as BSL
11 import qualified Data.MIME.Types as DMT
14 import GHC.Generics (Generic)
15 import qualified Network.HTTP.Media as M
17 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
19 import Gargantext.Prelude
20 import qualified Gargantext.Prelude.Utils as GPU
22 import Gargantext.Core.Types (TODO)
23 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..), AsyncJobs)
24 import Gargantext.API.Admin.Types (HasSettings)
25 import Gargantext.API.Node.Types
26 import Gargantext.API.Prelude
27 import Gargantext.Database.Action.Flow.Types
28 import Gargantext.Database.Action.Node (mkNodeWithParent)
29 import Gargantext.Database.Admin.Types.Hyperdata.File
30 import Gargantext.Database.Admin.Types.Node
31 import Gargantext.Database.Query.Table.Node (getNodeWith)
32 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
33 import Gargantext.Database.Schema.Node (node_hyperdata)
35 data RESPONSE deriving Typeable
37 instance Accept RESPONSE where
38 contentType _ = "text" M.// "*"
40 instance MimeRender RESPONSE BSResponse where
41 mimeRender _ (BSResponse val) = BSL.fromStrict $ val
43 type FileApi = Summary "File download"
45 :> Get '[RESPONSE] (Headers '[Servant.Header "Content-Type" Text] BSResponse)
47 fileApi :: UserId -> NodeId -> GargServer FileApi
48 fileApi uId nId = fileDownload uId nId
50 newtype Contents = Contents BS.ByteString
52 instance GPU.ReadFile Contents where
57 newtype BSResponse = BSResponse BS.ByteString
60 instance ToSchema BSResponse where
61 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
63 fileDownload :: (HasSettings env, FlowCmdM env err m)
66 -> m (Headers '[Servant.Header "Content-Type" Text] BSResponse)
67 fileDownload uId nId = do
68 printDebug "[fileDownload] uId" uId
69 printDebug "[fileDownload] nId" nId
71 node <- getNodeWith nId (Proxy :: Proxy HyperdataFile)
72 let (HyperdataFile { _hff_name = name'
73 , _hff_path = path }) = node ^. node_hyperdata
75 Contents c <- GPU.readFile $ unpack path
77 let (mMime, _) = DMT.guessType DMT.defaultmtd False $ unpack name'
80 Nothing -> "text/plain"
82 pure $ addHeader (pack mime) $ BSResponse c
86 -- let settings = embeddedSettings [("", encodeUtf8 c)]
88 -- Tagged $ staticApp settings
90 -- let settings = embeddedSettings [("", "hello")]
91 -- Tagged $ staticApp settings
93 type FileAsyncApi = Summary "File Async Api"
96 :> AsyncJobs JobLog '[FormUrlEncoded] NewWithFile JobLog
98 fileAsyncApi :: UserId -> NodeId -> GargServer FileAsyncApi
99 fileAsyncApi uId nId =
104 printDebug "addWithFile" x
106 in addWithFile uId nId i log')
109 addWithFile :: (HasSettings env, FlowCmdM env err m)
115 addWithFile uId nId nwf@(NewWithFile _d _l fName) logStatus = do
117 printDebug "[addWithFile] Uploading file: " nId
118 logStatus JobLog { _scst_succeeded = Just 0
119 , _scst_failed = Just 0
120 , _scst_remaining = Just 1
121 , _scst_events = Just []
124 fPath <- GPU.writeFile nwf
125 printDebug "[addWithFile] File saved as: " fPath
127 nIds <- mkNodeWithParent NodeFile (Just nId) uId fName
131 node <- getNodeWith nId' (Proxy :: Proxy HyperdataFile)
132 let hl = node ^. node_hyperdata
133 _ <- updateHyperdata nId' $ hl { _hff_name = fName
134 , _hff_path = pack fPath }
136 printDebug "[addWithFile] Created node with id: " nId'
139 printDebug "[addWithFile] File upload finished: " nId
140 pure $ JobLog { _scst_succeeded = Just 1
141 , _scst_failed = Just 0
142 , _scst_remaining = Just 0
143 , _scst_events = Just []