Skip to content

Commit e8819cb

Browse files
author
ArktinenSieni
committed
renaming, documenting
1 parent 5270814 commit e8819cb

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/Servant/Multipart.hs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ import qualified Data.ByteString.Lazy as LBS
6161
-- stand now. This also means that 'MultipartForm' can't be used in
6262
-- conjunction with 'ReqBody' in an endpoint.
6363
--
64+
-- The 'tag' type parameter instructs the function to handle data
65+
-- either as data to be saved to temporary storage ('Tmp') or saved to
66+
-- memory ('Mem').
67+
--
6468
-- The 'a' type parameter represents the Haskell type to which
6569
-- you are going to decode the multipart data to, where the
6670
-- multipart data consists in all the usual form inputs along
@@ -73,12 +77,12 @@ import qualified Data.ByteString.Lazy as LBS
7377
-- Example:
7478
--
7579
-- @
76-
-- type API = MultipartForm MultipartData :> Post '[PlainText] String
80+
-- type API = MultipartForm Tmp (MultipartData Tmp) :> Post '[PlainText] String
7781
--
7882
-- api :: Proxy API
7983
-- api = Proxy
8084
--
81-
-- server :: MultipartData -> Handler String
85+
-- server :: MultipartData Tmp -> Handler String
8286
-- server multipartData = return str
8387
--
8488
-- where str = "The form was submitted with "
@@ -100,12 +104,12 @@ import qualified Data.ByteString.Lazy as LBS
100104
-- @
101105
-- data User = User { username :: Text, pic :: FilePath }
102106
--
103-
-- instance FromMultipart User where
107+
-- instance FromMultipart Tmp User where
104108
-- fromMultipart multipartData =
105109
-- User \<$\> lookupInput "username" multipartData
106110
-- \<*\> fmap fileContent (lookupFile "pic" multipartData)
107111
--
108-
-- type API = MultipartForm User :> Post '[PlainText] String
112+
-- type API = MultipartForm Tmp User :> Post '[PlainText] String
109113
--
110114
-- server :: User -> Handler String
111115
-- server usr = return str
@@ -132,6 +136,10 @@ data MultipartForm tag a
132136

133137
-- | What servant gets out of a @multipart/form-data@ form submission.
134138
--
139+
-- The type parameter 'tag' tells if 'MultipartData' is stored as a
140+
-- temporary file or stored in memory. 'tag' is type of either 'Mem'
141+
-- or 'Tmp'.
142+
--
135143
-- The 'inputs' field contains a list of textual 'Input's, where
136144
-- each input for which a value is provided gets to be in this list,
137145
-- represented by the input name and the input value. See haddocks for
@@ -147,17 +155,13 @@ data MultipartData tag = MultipartData
147155
, files :: [FileData tag]
148156
}
149157

150-
151-
152-
-- TODO: this is specific to Tmp. we need a version that
153-
-- can handle Mem as well.
154-
fromRaw :: forall options. ([Network.Wai.Parse.Param], [File (MultipartResult options)]) -> MultipartData options
158+
fromRaw :: forall tag. ([Network.Wai.Parse.Param], [File (MultipartResult tag)]) -> MultipartData tag
155159
fromRaw (inputs, files) = MultipartData is fs
156160

157161
where is = map (\(name, val) -> Input (dec name) (dec val)) inputs
158162
fs = map toFile files
159163

160-
toFile :: File (MultipartResult options) -> FileData options
164+
toFile :: File (MultipartResult tag) -> FileData tag
161165
toFile (iname, fileinfo) =
162166
FileData (dec iname)
163167
(dec $ fileName fileinfo)
@@ -305,7 +309,7 @@ fuzzyMultipartCTCheck ct
305309
--
306310
-- 'generalOptions' lets you specify mostly multipart parsing
307311
-- related options, such as the maximum file size, while
308-
-- 'tmpOptions' lets you configure aspects specific to
312+
-- 'options' lets you configure aspects specific to
309313
-- the temporary file backend. See haddocks for
310314
-- 'ParseRequestBodyOptions' and 'TmpBackendOptions' respectively
311315
-- for more information on what you can tweak.
@@ -328,10 +332,10 @@ class MultipartBackend tag where
328332

329333
defaultBackendOptions :: Proxy tag -> MultipartBackendOptions tag
330334

331-
-- | Tag for temporary files
335+
-- | Tag for data stored as a temporary file
332336
data Tmp
333337

334-
-- | Tag for items in memory
338+
-- | Tag for data stored in memory
335339
data Mem
336340

337341
instance MultipartBackend Tmp where
@@ -373,7 +377,7 @@ defaultTmpBackendOptions = TmpBackendOptions
373377
-- | Default configuration for multipart handling.
374378
--
375379
-- Uses 'defaultParseRequestBodyOptions' and
376-
-- 'defaultTmpBackendOptions' respectively.
380+
-- 'defaultBackendOptions' respectively.
377381
defaultMultipartOptions :: MultipartBackend tag => Proxy tag -> MultipartOptions tag
378382
defaultMultipartOptions pTag = MultipartOptions
379383
{ generalOptions = defaultParseRequestBodyOptions

0 commit comments

Comments
 (0)