1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE TypeOperators #-}
3 {-# OPTIONS_GHC -fno-warn-orphans -fno-warn-unused-matches -fno-warn-unused-imports #-}
5 module Gargantext.API.Node.File where
7 import Control.Lens ((^.))
8 import qualified Data.ByteString as BS
9 import qualified Data.ByteString.Lazy as BSL
10 import qualified Data.MIME.Types as DMT
11 import Data.Monoid (mempty)
14 import Data.Text.Encoding
15 import qualified Data.Text.IO as TIO
16 import GHC.Generics (Generic)
17 import qualified Network.HTTP.Media as M
18 import Network.Wai.Application.Static
20 import Servant.API.Raw (Raw)
21 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
22 import Servant.Job.Core
23 import Servant.Job.Types
24 import Servant.Job.Utils (jsonOptions)
25 import Servant.Server.Internal
27 import Gargantext.Prelude
28 import qualified Gargantext.Prelude.Utils as GPU
30 import Gargantext.Core.Types (TODO)
31 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..), AsyncJobs)
32 import Gargantext.API.Admin.Settings (HasSettings)
33 import Gargantext.API.Node.Types
34 import Gargantext.API.Prelude
35 import Gargantext.Database.Action.Flow.Types
36 import Gargantext.Database.Action.Node (mkNodeWithParent)
37 import Gargantext.Database.Admin.Types.Hyperdata.File
38 import Gargantext.Database.Admin.Types.Node
39 import Gargantext.Database.Query.Table.Node (getNodeWith)
40 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
41 import Gargantext.Database.Schema.Node (node_hyperdata)
43 data RESPONSE deriving Typeable
45 instance Accept RESPONSE where
46 contentType _ = "text" M.// "*"
48 instance MimeRender RESPONSE BSResponse where
49 mimeRender _ (BSResponse val) = BSL.fromStrict $ val
51 type FileApi = Summary "File download"
53 :> Get '[RESPONSE] (Headers '[Servant.Header "Content-Type" Text] BSResponse)
55 fileApi :: UserId -> NodeId -> GargServer FileApi
56 fileApi uId nId = fileDownload uId nId
58 newtype Contents = Contents BS.ByteString
60 instance GPU.ReadFile Contents where
65 newtype BSResponse = BSResponse BS.ByteString
68 instance ToSchema BSResponse where
69 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
71 fileDownload :: (HasSettings env, FlowCmdM env err m)
74 -> m (Headers '[Servant.Header "Content-Type" Text] BSResponse)
75 fileDownload uId nId = do
76 printDebug "[fileDownload] uId" uId
77 printDebug "[fileDownload] nId" nId
79 node <- getNodeWith nId (Proxy :: Proxy HyperdataFile)
80 let (HyperdataFile { _hff_name = name'
81 , _hff_path = path }) = node ^. node_hyperdata
83 Contents c <- GPU.readFile $ unpack path
85 let (mMime, _) = DMT.guessType DMT.defaultmtd False $ unpack name'
88 Nothing -> "text/plain"
90 pure $ addHeader (pack mime) $ BSResponse c
94 -- let settings = embeddedSettings [("", encodeUtf8 c)]
96 -- Tagged $ staticApp settings
98 -- let settings = embeddedSettings [("", "hello")]
99 -- Tagged $ staticApp settings
101 type FileAsyncApi = Summary "File Async Api"
104 :> AsyncJobs JobLog '[FormUrlEncoded] NewWithFile JobLog
106 fileAsyncApi :: UserId -> NodeId -> GargServer FileAsyncApi
107 fileAsyncApi uId nId =
112 printDebug "addWithFile" x
114 in addWithFile uId nId i log')
117 addWithFile :: (HasSettings env, FlowCmdM env err m)
123 addWithFile uId nId nwf@(NewWithFile _d _l fName) logStatus = do
125 printDebug "[addWithFile] Uploading file: " nId
126 logStatus JobLog { _scst_succeeded = Just 0
127 , _scst_failed = Just 0
128 , _scst_remaining = Just 1
129 , _scst_events = Just []
132 fPath <- GPU.writeFile nwf
133 printDebug "[addWithFile] File saved as: " fPath
135 nIds <- mkNodeWithParent NodeFile (Just nId) uId fName
139 node <- getNodeWith nId' (Proxy :: Proxy HyperdataFile)
140 let hl = node ^. node_hyperdata
141 _ <- updateHyperdata nId' $ hl { _hff_name = fName
142 , _hff_path = pack fPath }
144 printDebug "[addWithFile] Created node with id: " nId'
147 printDebug "[addWithFile] File upload finished: " nId
148 pure $ JobLog { _scst_succeeded = Just 1
149 , _scst_failed = Just 0
150 , _scst_remaining = Just 0
151 , _scst_events = Just []