Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
purescript-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grégoire Locqueville
purescript-gargantext
Commits
9ea7a6c8
Commit
9ea7a6c8
authored
Jan 15, 2020
by
Przemyslaw Kaminski
Committed by
Alexandre Delanoë
Jan 19, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Tree] use multipart form data for async file upload
parent
ffe273a8
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
10 deletions
+43
-10
Upload.purs
...Gargantext/Components/Forest/Tree/Node/Action/Upload.purs
+3
-2
REST.purs
src/Gargantext/Config/REST.purs
+37
-8
Sessions.purs
src/Gargantext/Sessions.purs
+3
-0
No files found.
src/Gargantext/Components/Forest/Tree/Node/Action/Upload.purs
View file @
9ea7a6c8
...
...
@@ -17,7 +17,7 @@ import Web.File.FileReader.Aff (readAsText)
import Gargantext.Components.Forest.Tree.Node.Action
import Gargantext.Routes (SessionRoute(..))
import Gargantext.Sessions (Session, postWwwUrlencoded)
import Gargantext.Sessions (Session, postWwwUrlencoded
, postMultipartFormData
)
import Gargantext.Types (class ToQuery, toQuery, NodeType(..), AsyncTask(..))
import Gargantext.Utils (id)
import Gargantext.Utils.Reactix as R2
...
...
@@ -169,7 +169,8 @@ instance fileUploadQueryToQuery :: ToQuery FileUploadQuery where
uploadFile :: Session -> ID -> FileType -> UploadFileContents -> Aff AsyncTask
uploadFile session id fileType (UploadFileContents fileContents) =
postWwwUrlencoded session p fileContents
--postWwwUrlencoded session p fileContents
postMultipartFormData session p fileContents
where
q = FileUploadQuery { fileType: fileType }
p = NodeAPI Corpus (Just id) $ "add/file/async" <> Q.print (toQuery q)
src/Gargantext/Config/REST.purs
View file @
9ea7a6c8
module Gargantext.Config.REST where
import Prelude (Unit, bind, pure, ($), (<$>), (<<<), (<>))
import Affjax (defaultRequest, printResponseFormatError, request)
import Affjax.RequestBody (RequestBody(..), string)
import Affjax.RequestBody (RequestBody(..),
formData, formURLEncoded,
string)
import Affjax.RequestHeader (RequestHeader(..))
import Affjax.ResponseFormat as ResponseFormat
import
Effect.Class (liftEffect
)
import
DOM.Simple.Console (log
)
import Data.Argonaut (class DecodeJson, decodeJson, class EncodeJson, encodeJson)
import Data.Either (Either(..))
import Data.Foldable (foldMap)
import Data.FormURLEncoded as FormURLEncoded
import Data.HTTP.Method (Method(..))
import Data.Maybe (Maybe(..))
import Data.MediaType.Common (applicationFormURLEncoded, applicationJSON)
import Data.Foldable (foldMap)
import DOM.Simple.Console (log)
import Data.MediaType.Common (applicationFormURLEncoded, applicationJSON, multipartFormData)
import Data.Tuple (Tuple(..))
import Effect.Aff (Aff, throwError)
import Effect.Class (liftEffect)
import Effect.Exception (error)
import Prelude (Unit, bind, pure, ($), (<$>), (<<<), (<>))
import Web.XHR.FormData as XHRFormData
type Token = String
...
...
@@ -80,7 +82,7 @@ postWwwUrlencoded mtoken url body = do
foldMap (\token ->
[RequestHeader "Authorization" $ "Bearer " <> token]
) mtoken
, content = Just $
string b
ody
, content = Just $
formURLEncoded urlEncodedB
ody
}
case affResp.body of
Left err -> do
...
...
@@ -93,3 +95,30 @@ postWwwUrlencoded mtoken url body = do
case decodeJson json of
Left err -> throwError $ error $ "decodeJson affResp.body: " <> err
Right b -> pure b
where
urlEncodedBody = FormURLEncoded.fromArray [Tuple "body" (Just body)]
postMultipartFormData :: forall b. DecodeJson b => Maybe Token -> String -> String -> Aff b
postMultipartFormData mtoken url body = do
fd <- liftEffect $ XHRFormData.new
_ <- liftEffect $ XHRFormData.append (XHRFormData.EntryName "body") body fd
affResp <- request $ defaultRequest
{ url = url
, responseFormat = ResponseFormat.json
, method = Left POST
, headers = [ ContentType multipartFormData
, Accept applicationJSON
] <>
foldMap (\token ->
[ RequestHeader "Authorization" $ "Bearer " <> token ]
) mtoken
, content = Just $ formData fd
}
case affResp.body of
Left err -> do
_ <- liftEffect $ log $ printResponseFormatError err
throwError $ error $ printResponseFormatError err
Right json -> do
case decodeJson json of
Left err -> throwError $ error $ "decodeJson affResp.body: " <> err
Right b -> pure b
src/Gargantext/Sessions.purs
View file @
9ea7a6c8
...
...
@@ -226,5 +226,8 @@ post session@(Session {token}) p = REST.post (Just token) (toUrl session p)
postWwwUrlencoded :: forall b p. DecodeJson b => ToUrl Session p => Session -> p -> String -> Aff b
postWwwUrlencoded session@(Session {token}) p = REST.postWwwUrlencoded (Just token) (toUrl session p)
postMultipartFormData :: forall b p. DecodeJson b => ToUrl Session p => Session -> p -> String -> Aff b
postMultipartFormData session@(Session {token}) p = REST.postMultipartFormData (Just token) (toUrl session p)
getls :: Effect Storage
getls = window >>= localStorage
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment