Skip to content

Commit ba00df3

Browse files
committed
Improve multer documentation
...
1 parent 70749b6 commit ba00df3

File tree

6 files changed

+104
-13
lines changed

6 files changed

+104
-13
lines changed

Sources/multer/File.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import struct MacroCore.Buffer
1010

1111
public extension multer {
1212

13+
/**
14+
* Represents a file that got uploaded by ``multer``. It carries the field
15+
* name, the filename, the mime type, and if the ``MemoryStorage`` was used,
16+
* the file contents.
17+
*/
1318
final class File: Equatable {
1419

1520
/// Name in form field
@@ -39,6 +44,17 @@ public extension multer {
3944
*/
4045
public var buffer : Buffer?
4146

47+
/**
48+
* Create a new multer file object.
49+
*
50+
* - Parameters:
51+
* - fieldName: Name of the form field that contains the file
52+
* - originalName: Name of file (filename in content-disposition)
53+
* - mimeType: MIME type of the file, defaults to octet-stream if n/a.
54+
* - path: Path to stored file on the server, if used w/ disk
55+
* storage.
56+
* - buffer: Contents of file, if loaded into memory.
57+
*/
4258
@inlinable
4359
public init(fieldName : String,
4460
originalName : String,

Sources/multer/Limits.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
// MacroExpress / multer
44
//
55
// Created by Helge Heß on 30/05/16.
6-
// Copyright © 2021 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2021-2025 ZeeZide GmbH. All rights reserved.
77
//
88

99
public extension multer {
1010

11+
/**
12+
* Upload limits configuration for multer.
13+
*
14+
* This allows to configure file size or count limits. And various other
15+
* limits.
16+
*/
1117
struct Limits {
1218

1319
/// Maximum size of field names (100 bytes)
@@ -29,6 +35,22 @@ public extension multer {
2935
/// Note: This is not checked yet.
3036
public var headerPairs : Int? = 2000
3137

38+
/**
39+
* Create a new multer limits configuration.
40+
*
41+
* All parameters are optional, defaults:
42+
* - field name size: 100
43+
* - field size: 1MB
44+
* - header pairs: 2000
45+
*
46+
* - Parameters:
47+
* - fieldNameSize: Maximum size of field names (100 bytes)
48+
* - fieldSize: Maximum size of field value (1MB)
49+
* - fields: Maximum number of non-file fields (unlimited)
50+
* - fileSize: Max file size (unlimited)
51+
* - files: Maximum number of file fields (unlimited)
52+
* - headerPairs: Maximum number of header fields (2000)
53+
*/
3254
public init(fieldNameSize : Int? = 100,
3355
fieldSize : Int? = 1024 * 1024,
3456
fields : Int? = nil,

Sources/multer/MemoryStorage.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
// MacroExpress / multer
44
//
55
// Created by Helge Heß on 30/05/16.
6-
// Copyright © 2021 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2021-2025 ZeeZide GmbH. All rights reserved.
77
//
88

99
import struct MacroCore.Buffer
1010

1111
extension multer {
1212

13+
/**
14+
* A ``MulterStorage`` that writes the file contents to the ``File/buffer``
15+
* property of the ``File`` object, i.e. stores it in-memory.
16+
*/
1317
open class MemoryStorage: MulterStorage {
18+
// TBD: does this have to be a class?
1419

1520
public init() {}
1621

Sources/multer/Middleware.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public extension multer {
1717
*
1818
* There are multiple convenience methods to restrict the set of fields to
1919
* accept:
20-
* - `single(fieldName)` (accept just one file for the specified name)
21-
* - `array(fieldName)` (accept just multiple files for the specified name)
22-
* - `none` (accept no file, just form regular fields)
23-
* - `any` (accept all files, careful!)
20+
* - ``single(_:)``: accept just one file for the specified name
21+
* - ``array(_:_:)``: accept just multiple files for the specified name
22+
* - ``none()``: accept no file, just form regular fields
23+
* - ``any()``: accept all files, careful!
2424
*
2525
* All convenience methods call into this middleware.
2626
*

Sources/multer/Multer.swift

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// MacroExpress / multer
44
//
55
// Created by Helge Heß on 30/05/16.
6-
// Copyright © 2021 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2021-2025 ZeeZide GmbH. All rights reserved.
77
//
88

99
import MacroCore
@@ -15,18 +15,29 @@ import connect
1515
* Middleware to parse `multipart/form-data` payloads.
1616
*
1717
* E.g. files submitted using an HTML form like:
18-
*
19-
* <form action="/upload" method="POST" enctype="multipart/form-data">
20-
* <input type="file" name="file" multiple="multiple" />
21-
* <input type="submit" value="Upload" />
22-
* </form>
18+
* ```html
19+
* <form action="/upload" method="POST" enctype="multipart/form-data">
20+
* <input type="file" name="file" multiple="multiple" />
21+
* <input type="submit" value="Upload" />
22+
* </form>
23+
* ```
2324
*
2425
* Roughly designed after the Node
2526
* [multer](https://github.com/expressjs/multer#readme)
2627
* package.
2728
*/
2829
public struct multer {
2930

31+
/**
32+
* A function that controls what files should be uploaded or skipped.
33+
*
34+
* This can be passed to a multer-init. The function gets the
35+
* `IncomingMessage`, the ``File`` and a callback that it **must** invoke to
36+
* continue multer processing.
37+
*
38+
* The filter can issue an error by passing on into the error parameter of the
39+
* callback. And it can pass in `true` or `false` to accept or reject a file.
40+
*/
3041
public typealias FileFilter =
3142
( IncomingMessage, File, @escaping ( Swift.Error?, Bool ) -> Bool ) -> Void
3243

@@ -53,6 +64,20 @@ public struct multer {
5364
}
5465
#endif
5566

67+
/**
68+
* Create a new multer configuration.
69+
*
70+
* This accepts a ``MulterStorage``, ``Limits-swift.struct`` and
71+
* a ``FileFilter-swift.typealias``.
72+
* All of which are optional. The default storage is the ``MemoryStorage``.
73+
*
74+
* - Parameters:
75+
* - storage: Where to put uploaded files, defaults to ``MemoryStorage``
76+
* - limits: What ``Limits-swift.struct`` to apply, checks the
77+
* documentation for the defaults.
78+
* - fileFilter: A function that can filter what files should be uploaded,
79+
* defaults to "no filter", i.e. accept all files.
80+
*/
5681
@inlinable
5782
public init(storage : MulterStorage? = nil,
5883
limits : Limits = Limits(),
@@ -61,12 +86,18 @@ public struct multer {
6186
self.dest = nil
6287
self.fileFilter = fileFilter
6388
self.limits = limits
64-
self.storage = storage ?? MemoryStorage()
89+
self.storage = storage ?? multer.memoryStorage()
6590
}
6691
}
6792

6893
public extension multer { // MARK: - Storage Factory
6994

95+
/**
96+
* Returns a multer storage that writes the file contents to the
97+
* ``File/buffer`` property of the ``File`` object, i.e. stores it in-memory.
98+
*
99+
* - Returns: A new ``MemoryStorage`` object.
100+
*/
70101
@inlinable
71102
static func memoryStorage() -> MemoryStorage {
72103
return MemoryStorage()

Sources/multer/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ package.
1919

2020
**Note**: DiskStorage is prepared, but not working yet.
2121

22+
### Example
23+
24+
[Examples](https://github.com/Macro-swift/Examples/blob/main/Sources/express-simple/main.swift#L48)
25+
```swift
26+
app.post("/multer", multer().array("file", 10)) { req, res, _ in
27+
req.log.info("Got files:", req.files["file"])
28+
res.render("multer", [
29+
"files": req.files["file"]?.map {
30+
[ "name": $0.originalName,
31+
"size": $0.buffer?.length ?? 0,
32+
"mimeType": $0.mimeType ]
33+
} ?? [],
34+
"hasFiles": !(req.files["file"]?.isEmpty ?? true)
35+
])
36+
}
37+
```
38+
2239

2340
### Links
2441

0 commit comments

Comments
 (0)