UNPKG

1.64 kBPlain TextView Raw
1import * as fs from "fs";
2import { app } from "./app";
3
4export default interface UFS {
5 unlink(path: string, cb: (err: Error) => void): void;
6 stat(path: string): Promise<Stat>;
7 getToCache(path: string, cachePath: string): Promise<string>;
8 uploadFile(uploadMedia: any): Promise<any>;
9 uploadFileFromCache(path: string, cachePath: string): Promise<void>;
10}
11
12export interface Stat {
13 size: number;
14}
15
16export class LocalUFS implements UFS {
17 unlink(path: string, cb: (err: Error) => void): void {
18 fs.unlink(path, cb);
19 }
20
21 stat(path: string): Promise<Stat> {
22 return new Promise<Stat>((res, rej) => {
23 fs.stat(app.config.uploadPath + "/" + path, (err, r) => {
24 if (err) {
25 return rej(err);
26 }
27 res(r);
28 });
29 });
30 }
31
32 getToCache(path: string, _: string): Promise<string> {
33 return new Promise<string>((res, _) => {
34 return res(app.config.uploadPath + "/" + path);
35 });
36 }
37
38 uploadFile(uploadMedia: any): Promise<any> {
39 return new Promise<any>((res, _) => {
40 return res(uploadMedia);
41 });
42 }
43
44 uploadFileFromCache(path: string, cachePath: string): Promise<void> {
45 return new Promise<void>((res, rej) => {
46 fs.copyFile(
47 cachePath + "/" + path,
48 app.config.uploadPath + "/" + path,
49 err => {
50 if (err) {
51 return rej(err);
52 }
53 return res();
54 }
55 );
56 });
57 }
58}
59
\No newline at end of file