File size: 1,764 Bytes
61d39e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import fs from 'node:fs';
import path_ from 'node:path';
import { TeePromise } from 'teepromise';

const {
    progress_stream,
    size_limit_stream,
} = extension.import('core').util.streamutil;

export default class LocalDiskStorageController {
    constructor () {
        this.path = path_.join(process.cwd(), '/storage');
    }

    async init () {
        await fs.promises.mkdir(this.path, { recursive: true });
    }

    async upload ({ uid, file, storage_api }) {
        const { progress_tracker } = storage_api;

        if ( file.buffer ) {
            const path = this.#getPath(uid);
            await fs.promises.writeFile(path, file.buffer);

            progress_tracker.set_total(file.buffer.length);
            progress_tracker.set(file.buffer.length);
            return;
        }

        let stream = file.stream;
        stream = progress_stream(stream, {
            total: file.size,
            progress_callback: evt => {
                progress_tracker.set_total(file.size);
                progress_tracker.set(evt.uploaded);
            },
        });
        stream = size_limit_stream(stream, {
            limit: file.size,
        });

        const writePromise = new TeePromise();
        const path = this.#getPath(uid);
        const write_stream = fs.createWriteStream(path);

        write_stream.on('error', () => writePromise.reject());
        write_stream.on('finish', () => writePromise.resolve());

        stream.pipe(write_stream);

        // @ts-ignore (it's wrong about this)
        await writePromise;
    }
    copy () {
    }
    delete () {
    }
    read () {
    }

    #getPath (key) {
        return path_.join(this.path, key);
    }
}