1 | /// <reference path="../../adonis-typings/index.d.ts" />
|
2 | /// <reference types="node" />
|
3 | import { DriveManagerContract } from '@ioc:Adonis/Core/Drive';
|
4 | import { MultipartStream, FileValidationOptions } from '@ioc:Adonis/Core/BodyParser';
|
5 | import { File } from './File';
|
6 | /**
|
7 | * Part handler handles the progress of a stream and also internally validates
|
8 | * it's size and extension.
|
9 | *
|
10 | * This class offloads the task of validating a file stream, regardless of how
|
11 | * the stream is consumed. For example:
|
12 | *
|
13 | * In classic scanerio, we will process the file stream and write files to the
|
14 | * tmp directory and in more advanced cases, the end user can handle the
|
15 | * stream by themselves and report each chunk to this class.
|
16 | */
|
17 | export declare class PartHandler {
|
18 | private part;
|
19 | private options;
|
20 | private drive;
|
21 | /**
|
22 | * The stream buffer reported by the stream consumer. We hold the buffer until are
|
23 | * able to detect the file extension and then buff memory is released
|
24 | */
|
25 | private buff?;
|
26 | /**
|
27 | * A boolean to know if we can use the magic number to detect the file type. This is how it
|
28 | * works.
|
29 | *
|
30 | * - We begin by extracting the file extension from the file name
|
31 | * - If the extension is something we support via magic numbers, then we ignore the extension
|
32 | * and inspect the buffer
|
33 | * - Otherwise, we have no other option than to trust the extension
|
34 | *
|
35 | * Think of this as using the optimal way for validating the file type
|
36 | */
|
37 | private canFileTypeBeDetected;
|
38 | /**
|
39 | * Creating a new file object for each part inside the multipart
|
40 | * form data
|
41 | */
|
42 | file: File;
|
43 | /**
|
44 | * A boolean to know, if we have emitted the error event after one or
|
45 | * more validation errors. We need this flag, since the race conditions
|
46 | * between `data` and `error` events will trigger multiple `error`
|
47 | * emit.
|
48 | */
|
49 | private emittedValidationError;
|
50 | constructor(part: MultipartStream, options: Partial<FileValidationOptions & {
|
51 | deferValidations: boolean;
|
52 | }>, drive: DriveManagerContract);
|
53 | /**
|
54 | * Detects the file type and extension and also validates it when validations
|
55 | * are not deferred.
|
56 | */
|
57 | private detectFileTypeAndExtension;
|
58 | /**
|
59 | * Skip the stream or end it forcefully. This is invoked when the
|
60 | * streaming consumer reports an error
|
61 | */
|
62 | private skipEndStream;
|
63 | /**
|
64 | * Finish the process of listening for any more events and mark the
|
65 | * file state as consumed.
|
66 | */
|
67 | private finish;
|
68 | /**
|
69 | * Start the process the updating the file state
|
70 | * to streaming mode.
|
71 | */
|
72 | begin(): void;
|
73 | /**
|
74 | * Handles the file upload progress by validating the file size and
|
75 | * extension.
|
76 | */
|
77 | reportProgress(line: Buffer, bufferLength: number): Promise<void>;
|
78 | /**
|
79 | * Report errors encountered while processing the stream. These can be errors
|
80 | * apart from the one reported by this class. For example: The `s3` failure
|
81 | * due to some bad credentails.
|
82 | */
|
83 | reportError(error: any): Promise<void>;
|
84 | /**
|
85 | * Report success data about the file.
|
86 | */
|
87 | reportSuccess(data?: {
|
88 | filePath?: string;
|
89 | tmpPath?: string;
|
90 | } & {
|
91 | [key: string]: any;
|
92 | }): Promise<void>;
|
93 | }
|