UNPKG

3.41 kBTypeScriptView Raw
1/// <reference path="../../adonis-typings/bodyparser.d.ts" />
2import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
3import { MultipartContract, PartHandler as PartHandlerType } from '@ioc:Adonis/Core/BodyParser';
4import { DriveManagerContract } from '@ioc:Adonis/Core/Drive';
5/**
6 * Multipart class offers a low level API to interact the incoming
7 * HTTP request data as a stream. This makes it super easy to
8 * write files to s3 without saving them to the disk first.
9 */
10export declare class Multipart implements MultipartContract {
11 private ctx;
12 private config;
13 private drive;
14 /**
15 * The registered handlers to handle the file uploads
16 */
17 private handlers;
18 /**
19 * Collected fields from the multipart stream
20 */
21 private fields;
22 /**
23 * Collected files from the multipart stream. Files are only collected
24 * when there is an attached listener for a given file.
25 */
26 private files;
27 /**
28 * We track the finishing of `this.onFile` async handlers
29 * to make sure that `process` promise resolves for all
30 * handlers to finish.
31 */
32 private pendingHandlers;
33 /**
34 * The reference to underlying multiparty form
35 */
36 private form;
37 /**
38 * Total size limit of the multipart stream. If it goes beyond
39 * this limit, then an exception will be raised.
40 */
41 private upperLimit?;
42 /**
43 * A track of total number of file bytes processed so far
44 */
45 private processedBytes;
46 /**
47 * The current state of the multipart form handler
48 */
49 state: 'idle' | 'processing' | 'error' | 'success';
50 constructor(ctx: HttpContextContract, config: Partial<{
51 limit: string | number;
52 maxFields: number;
53 convertEmptyStringsToNull: boolean;
54 }>, drive: DriveManagerContract);
55 /**
56 * Returns a boolean telling whether all streams have been
57 * consumed along with all handlers execution
58 */
59 private isClosed;
60 /**
61 * Removes array like expression from the part name to
62 * find the handler
63 */
64 private getHandlerName;
65 /**
66 * Validates and returns an error when upper limit is defined and
67 * processed bytes is over the upper limit
68 */
69 private validateProcessedBytes;
70 /**
71 * Handles a given part by invoking it's handler or
72 * by resuming the part, if there is no defined
73 * handler
74 */
75 private handlePart;
76 /**
77 * Record the fields inside multipart contract
78 */
79 private handleField;
80 /**
81 * Processes the user config and computes the `upperLimit` value from
82 * it.
83 */
84 private processConfig;
85 /**
86 * Mark the process as finished
87 */
88 private finish;
89 /**
90 * Attach handler for a given file. To handle all files, you
91 * can attach a wildcard handler.
92 *
93 * @example
94 * ```ts
95 * multipart.onFile('package', {}, async (stream) => {
96 * })
97 *
98 * multipart.onFile('*', {}, async (stream) => {
99 * })
100 * ```
101 */
102 onFile(name: string, options: Parameters<MultipartContract['onFile']>[1], handler: PartHandlerType): this;
103 /**
104 * Abort request by emitting error
105 */
106 abort(error: any): void;
107 /**
108 * Process the request by going all the file and field
109 * streams.
110 */
111 process(config?: Parameters<MultipartContract['process']>[0]): Promise<void>;
112}