Skip to content
Newer
Older
100644 110 lines (103 sloc) 4.18 KB
f21f55a @gregorycollins Check in Snap.Util.FileUploads, missing after last refactor.
gregorycollins authored
1 ------------------------------------------------------------------------------
2 -- | This module contains primitives and helper functions for handling
3 -- requests with @Content-type: multipart/form-data@, i.e. HTML forms and file
4 -- uploads.
5 --
6 -- Typically most users will want to use 'handleFileUploads', which writes
7 -- uploaded files to a temporary directory before sending them on to a handler
8 -- specified by the user.
9 --
10 -- Users who wish to handle their file uploads differently can use the
1dab899 @mightybyte Remove mentions of iteratees
mightybyte authored
11 -- lower-level interface called 'handleMultipart'. That function takes
12 -- uploaded files and streams them to a consumer of the user's choosing.
f21f55a @gregorycollins Check in Snap.Util.FileUploads, missing after last refactor.
gregorycollins authored
13 --
14 -- Using these functions requires making \"policy\" decisions which Snap can't
15 -- really make for users, such as \"what's the largest PDF file a user is
16 -- allowed to upload?\" and \"should we read form inputs into the parameters
17 -- mapping?\". Policy is specified on a \"global\" basis (using
18 -- 'UploadPolicy'), and on a per-file basis (using 'PartUploadPolicy', which
19 -- allows you to reject or limit the size of certain uploaded
20 -- @Content-type@s).
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
21 --
22 -- Example usage:
23 --
24 -- @
02c087b @gregorycollins Various code cleanups.
gregorycollins authored
25 -- {-\# LANGUAGE OverloadedStrings #-}
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
26 --
27 -- module Main where
28 --
02c087b @gregorycollins Various code cleanups.
gregorycollins authored
29 -- import qualified Data.ByteString.Char8 as B8
30 -- import Data.Functor ((\<$>))
31 -- import "Snap.Core" ('Snap.Core.Snap', 'Snap.Core.route', 'Snap.Core.writeBS')
32 -- import Snap.Http.Server (quickHttpServe)
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
33 -- import "Snap.Util.FileUploads"
02c087b @gregorycollins Various code cleanups.
gregorycollins authored
34 -- import System.Posix (FileOffset, fileSize, getFileStatus)
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
35 --
02c087b @gregorycollins Various code cleanups.
gregorycollins authored
36 -- uploadForm :: 'Snap.Core.Snap' ()
37 -- uploadForm = 'Snap.Core.writeBS' \"\<form enctype=\\\"multipart\/form-data\\\" action=\\"\/do-upload\\\" method=\\\"POST\\\">\\
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
38 -- \\\<input name=\\\"file\\\" type=\\\"file\\\" \/>\\
39 -- \\\<input type=\\\"submit\\\" value=\\\"Send File\\\" \/>\\
40 -- \\\<\/form>\"
41 --
42 -- getFileSize :: FilePath -> IO FileOffset
43 -- getFileSize path = fileSize \<$> getFileStatus path
44 --
45 -- -- Upload handler that prints out the uploaded file\'s size.
02c087b @gregorycollins Various code cleanups.
gregorycollins authored
46 -- doUpload :: 'Snap.Core.Snap' ()
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
47 -- doUpload = do
48 -- l \<- 'handleFileUploads' \"\/tmp\" 'defaultUploadPolicy'
49 -- (const $ 'allowWithMaximumSize' ('getMaximumFormInputSize' 'defaultUploadPolicy'))
50 -- (\\pinfo mbfname -> do fsize \<- either (const $ return 0) getFileSize mbfname
51 -- return ('partFileName' pinfo, fsize))
02c087b @gregorycollins Various code cleanups.
gregorycollins authored
52 -- 'writeBS' . B8.pack . show $ l
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
53 --
02c087b @gregorycollins Various code cleanups.
gregorycollins authored
54 -- site :: 'Snap.Core.Snap' ()
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
55 -- site = 'Snap.Core.route'
56 -- [ (\"\/upload\", uploadForm)
57 -- , (\"\/do-upload\", doUpload)]
58 --
59 -- main :: IO ()
02c087b @gregorycollins Various code cleanups.
gregorycollins authored
60 -- main = quickHttpServe site
82525fe @23Skidoo Document the 'Snap.Util.FileUploads' module.
23Skidoo authored
61 -- @
f21f55a @gregorycollins Check in Snap.Util.FileUploads, missing after last refactor.
gregorycollins authored
62 module Snap.Util.FileUploads
63 ( -- * Functions
64 handleFileUploads
65 , handleMultipart
66 , PartProcessor
67
68 -- * Uploaded parts
961aee8 @gregorycollins More test coverage for file uploads.
gregorycollins authored
69 , PartInfo
70 , PartDisposition(..)
71 , partFieldName
72 , partFileName
73 , partContentType
74 , partHeaders
75 , partDisposition
f21f55a @gregorycollins Check in Snap.Util.FileUploads, missing after last refactor.
gregorycollins authored
76
77 -- ** Policy
78 -- *** General upload policy
79 , UploadPolicy
80 , defaultUploadPolicy
81 , doProcessFormInputs
82 , setProcessFormInputs
83 , getMaximumFormInputSize
84 , setMaximumFormInputSize
85 , getMaximumNumberOfFormInputs
86 , setMaximumNumberOfFormInputs
87 , getMinimumUploadRate
88 , setMinimumUploadRate
89 , getMinimumUploadSeconds
90 , setMinimumUploadSeconds
91 , getUploadTimeout
92 , setUploadTimeout
93
94 -- *** Per-file upload policy
95 , PartUploadPolicy
96 , disallow
97 , allowWithMaximumSize
98
99 -- * Exceptions
100 , FileUploadException
101 , fileUploadExceptionReason
102 , BadPartException
103 , badPartExceptionReason
104 , PolicyViolationException
105 , policyViolationExceptionReason
106 ) where
107
8ef17b7 @gregorycollins Minimize imports for snap-core.
gregorycollins authored
108
961aee8 @gregorycollins More test coverage for file uploads.
gregorycollins authored
109 import Snap.Internal.Util.FileUploads (BadPartException (badPartExceptionReason), FileUploadException, PartDisposition (..), PartInfo (..), PartProcessor, PartUploadPolicy, PolicyViolationException (policyViolationExceptionReason), UploadPolicy, allowWithMaximumSize, defaultUploadPolicy, disallow, doProcessFormInputs, fileUploadExceptionReason, getMaximumFormInputSize, getMaximumNumberOfFormInputs, getMinimumUploadRate, getMinimumUploadSeconds, getUploadTimeout, handleFileUploads, handleMultipart, setMaximumFormInputSize, setMaximumNumberOfFormInputs, setMinimumUploadRate, setMinimumUploadSeconds, setProcessFormInputs, setUploadTimeout)
Something went wrong with that request. Please try again.