-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.ts
166 lines (151 loc) · 4.68 KB
/
storage.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import * as fs from "fs";
import * as path from "path";
import { Readable } from "stream";
import * as AWS from "aws-sdk";
import { Storage } from "@google-cloud/storage";
/**
* All uploaded files are initially saved in the OS's temp directory in case the files uploaded aren't valid
* A storage engine takes this local path to the file on disk and does something with it to persist it for later reference
*
* The reference DiskStorageEngine simply moves the temporary file to the permanent upload directory
*/
export interface IStorageEngine {
uploadRoot: string;
saveFile(currentPath: string, name: string): Promise<void>;
readFile(name: string): Promise<Readable>;
}
interface ICommonOptions {
uploadDirectory: string;
}
class DiskStorageEngine implements IStorageEngine {
public readonly uploadRoot: string;
private readonly options: ICommonOptions;
constructor(options: ICommonOptions) {
// Values copied via spread operator instead of being passed by reference
this.options = {
...options
};
this.options.uploadDirectory = path.resolve(__dirname, "../", this.options.uploadDirectory);
if (!fs.existsSync(this.options.uploadDirectory)) {
fs.mkdirSync(this.options.uploadDirectory);
}
this.uploadRoot = this.options.uploadDirectory;
}
public saveFile(currentPath: string, name: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
// Apparently fs.rename() won't move across filesystems so we'll copy and delete instead
// Error: EXDEV: cross-device link not permitted
let readStream = fs.createReadStream(currentPath);
let writeStream = fs.createWriteStream(path.join(this.options.uploadDirectory, name));
readStream.on("error", reject);
writeStream.on("error", reject);
writeStream.on("close", () => {
fs.unlink(currentPath, err => {
if (err) {
reject(err);
return;
}
resolve();
});
});
readStream.pipe(writeStream);
});
}
public async readFile(name: string): Promise<Readable> {
return fs.createReadStream(path.join(this.options.uploadDirectory, name));
}
}
interface IS3Options extends ICommonOptions {
bucket: string;
region: string;
accessKey: string;
secretKey: string;
}
class S3StorageEngine implements IStorageEngine {
public readonly uploadRoot: string;
private readonly options: IS3Options;
constructor(options: IS3Options) {
// Values copied via spread operator instead of being passed by reference
this.options = {
...options
};
this.uploadRoot = this.options.uploadDirectory;
}
public saveFile(currentPath: string, name: string): Promise<void> {
AWS.config.update({
region: this.options.region,
credentials: new AWS.Credentials({
accessKeyId: this.options.accessKey,
secretAccessKey: this.options.secretKey
})
});
let s3 = new AWS.S3();
return new Promise<void>((resolve, reject) => {
let readStream = fs.createReadStream(currentPath);
readStream.on("error", reject);
s3.putObject({
Body: readStream,
Bucket: this.options.bucket,
Key: name
}).promise().then((output) => {
resolve();
}).catch(reject);
});
}
public async readFile(name: string): Promise<Readable> {
AWS.config.update({
region: this.options.region,
credentials: new AWS.Credentials({
accessKeyId: this.options.accessKey,
secretAccessKey: this.options.secretKey
})
});
let s3 = new AWS.S3();
const object = {
Bucket: this.options.bucket,
Key: name
};
// Will throw if the object does not exist
await s3.headObject(object).promise();
return s3.getObject(object).createReadStream();
}
}
interface IGCSOptions extends ICommonOptions {
bucket: string;
clientEmail: string;
privateKey: string;
}
class GCSStorageEngine implements IStorageEngine {
public readonly uploadRoot: string;
private readonly options: IGCSOptions;
private readonly storage: Storage;
constructor(options: IGCSOptions) {
// Values copied via spread operator instead of being passed by reference
this.options = {
...options
};
this.uploadRoot = this.options.uploadDirectory;
this.storage = new Storage({
credentials: {
client_email: this.options.clientEmail,
private_key: this.options.privateKey
}
});
}
public async saveFile(currentPath: string, name: string): Promise<void> {
await this.storage.bucket(this.options.bucket).upload(currentPath, {
destination: name
});
}
public async readFile(name: string): Promise<Readable> {
return this.storage.bucket(this.options.bucket).file(name).createReadStream();
}
}
interface IStorageEngines {
[name: string]: new(options: ICommonOptions) => IStorageEngine;
}
export const storageEngines: IStorageEngines = {
"disk": DiskStorageEngine,
"s3": S3StorageEngine,
"gcs": GCSStorageEngine
};