blob: 375d23e714481bec4c177a813e2758a2e050b59b (
plain) (
blame)
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
|
import fs from "fs";
import path, { dirname, join } from "path";
import { Readable } from "stream";
import { Storage } from "./Storage";
//import ExifTransformer = require("exif-be-gone");
import ExifTransformer from "exif-be-gone";
// TODO: split stored files into separate folders named after cloned route
function getPath(filePath: string) {
// STORAGE_LOCATION has a default value in start.ts
const root = process.env.STORAGE_LOCATION || "../";
let filename = join(root, filePath);
if (filePath.indexOf("\0") !== -1 || !filename.startsWith(root)) throw new Error("invalid path");
return filename;
}
export class FileStorage implements Storage {
async get(filePath: string): Promise<Buffer | null> {
filePath = getPath(filePath);
try {
return fs.readFileSync(filePath);
} catch (error) {
try {
const files = fs.readdirSync(filePath);
if (!files.length) return null;
return fs.readFileSync(join(filePath, files[0]));
} catch (error) {
return null;
}
}
}
async set(filePath: string, value: any) {
filePath = getPath(filePath);
//fse.ensureDirSync(dirname(path));
fs.mkdirSync(dirname(filePath), { recursive: true });
value = Readable.from(value);
const cleaned_file = fs.createWriteStream(filePath);
return value.pipe(new ExifTransformer()).pipe(cleaned_file);
}
async delete(filePath: string) {
//TODO: (done?) we should delete the parent directory if empty
fs.unlinkSync(getPath(filePath));
if (fs.readdirSync(path.dirname(filePath)).length == 0) {
fs.rmSync(path.dirname(filePath));
}
}
}
|