import * as fs from "fs"; import { app } from "./app"; export default interface UFS { unlink(path: string, cb: (err: Error) => void): void; stat(path: string): Promise; getToCache(path: string, cachePath: string): Promise; uploadFile(uploadMedia: any): Promise; uploadFileFromCache(path: string, cachePath: string): Promise; } export interface Stat { size: number; } export class LocalUFS implements UFS { unlink(path: string, cb: (err: Error) => void): void { fs.unlink(path, cb); } stat(path: string): Promise { return new Promise((res, rej) => { fs.stat(app.config.uploadPath + "/" + path, (err, r) => { if (err) { return rej(err); } res(r); }); }); } getToCache(path: string, _: string): Promise { return new Promise((res, _) => { return res(app.config.uploadPath + "/" + path); }); } uploadFile(uploadMedia: any): Promise { return new Promise((res, _) => { return res(uploadMedia); }); } uploadFileFromCache(path: string, cachePath: string): Promise { return new Promise((res, rej) => { fs.copyFile( cachePath + "/" + path, app.config.uploadPath + "/" + path, err => { if (err) { return rej(err); } return res(); } ); }); } }