-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathlocal.ts
More file actions
289 lines (262 loc) · 7.12 KB
/
local.ts
File metadata and controls
289 lines (262 loc) · 7.12 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { ReadStream, createReadStream } from 'fs';
import { extname, join } from 'path';
import fs from 'fs-extra';
import { CannotParseAsJsonException } from '../exceptions/cannotParseAsJson.js';
import { CannotPerformFileOpException } from '../exceptions/cannotPerformFileOp.js';
import { getMimeTypeFromExtention } from '../helpers/index.js';
import {
LocalDiskOptions,
StorageDriver,
StorageDriver$FileMetadataResponse,
StorageDriver$PutFileResponse,
StorageDriver$RenameFileResponse,
} from '../interfaces/index.js';
import { StorageService } from '../service.js';
export class Local implements StorageDriver {
constructor(
private disk: string,
private config: LocalDiskOptions,
) {}
/**
* Put file content to the path specified.
*
* @param path
* @param fileContent
*/
async put(
filePath: string,
fileContent: any,
): Promise<StorageDriver$PutFileResponse> {
await fs.outputFile(
join(this.config.basePath || '', filePath),
fileContent,
);
return { path: join(this.config.basePath || '', filePath), url: '' };
}
/**
* Get file stored at the specified path.
*
* @param path
*/
async get(filePath: string): Promise<Buffer> {
return await fs.readFile(join(this.config.basePath || '', filePath));
}
/**
* Get object's metadata
* @param path
*/
async meta(filePath: string): Promise<StorageDriver$FileMetadataResponse> {
const path = join(this.config.basePath || '', filePath);
const res = await fs.stat(path);
return {
path,
contentLength: res.size,
lastModified: res.mtime,
};
}
/**
* Get Signed Urls
* @param path
*/
async signedUrl(
filePath: string,
expire = 10,
command: 'get' | 'put',
): Promise<string> {
console.log(expire, filePath, command);
return '';
}
/**
* Check if file exists at the path.
*
* @param path
*/
async exists(filePath: string): Promise<boolean> {
return fs.pathExists(join(this.config.basePath || '', filePath));
}
/**
* Check if file is missing at the path.
*
* @param path
*/
async missing(filePath: string): Promise<boolean> {
return !(await this.exists(filePath));
}
/**
* Get URL for path mentioned.
*
* @param path
*/
async url(fileName: string): Promise<string> {
if (this.config.hasOwnProperty('baseUrl')) {
const filePath = join('public', fileName);
return `${this.config.basePath}/${filePath}`;
} else {
return '';
}
}
/**
* Delete file at the given path.
*
* @param path
*/
async delete(filePath: string): Promise<boolean> {
try {
await fs.remove(join(this.config.basePath || '', filePath));
return true;
} catch (e) {
if (this.shouldThrowError())
throw new CannotPerformFileOpException(
`File ${filePath} cannot be deleted due to the reason: ${e['message']}`,
);
}
return false;
}
getAsStream(filePath: string): ReadStream {
return createReadStream(join(this.config.basePath || '', filePath));
}
/**
* Copy file internally in the same disk
*
* @param path
* @param newPath
*/
async copy(
sourcePath: string,
destinationPath: string,
): Promise<StorageDriver$RenameFileResponse> {
await fs.copy(
join(this.config.basePath || '', sourcePath),
join(this.config.basePath || '', destinationPath),
{ overwrite: true },
);
return {
path: join(this.config.basePath || '', destinationPath),
url: await this.url(destinationPath),
};
}
/**
* Move file internally in the same disk
*
* @param path
* @param newPath
*/
async move(
sourcePath: string,
destinationPath: string,
): Promise<StorageDriver$RenameFileResponse> {
await this.copy(sourcePath, destinationPath);
await this.delete(sourcePath);
return {
path: join(this.config.basePath || '', destinationPath),
url: await this.url(destinationPath),
};
}
/**
* Get instance of driver's client.
*/
getClient(): null {
return null;
}
/**
* Get config of the driver's instance.
*/
getConfig(): Record<string, any> {
return this.config;
}
async copyToDisk(
sourcePath: string,
destinationDisk: string,
destinationPath: string,
): Promise<boolean> {
try {
const buffer = await this.get(sourcePath);
const driver = StorageService.getDriver(destinationDisk);
await driver.put(destinationPath, buffer);
return true;
} catch (e) {
if (this.shouldThrowError()) {
throw new CannotPerformFileOpException(
`File cannot be copied from ${sourcePath} to ${destinationDisk} in ${destinationDisk} disk for the reason: ${e['message']}`,
);
}
}
return false;
}
async moveToDisk(
sourcePath: string,
destinationDisk: string,
destinationPath: string,
): Promise<boolean> {
try {
const buffer = await this.get(sourcePath);
const driver = StorageService.getDriver(destinationDisk);
await driver.put(destinationPath, buffer);
await this.delete(sourcePath);
return true;
} catch (e) {
if (this.shouldThrowError()) {
throw new CannotPerformFileOpException(
`File cannot be moved from ${sourcePath} to ${destinationDisk} in ${destinationDisk} disk for the reason: ${e['message']}`,
);
}
console.log('error while copying ===> ', e);
}
return false;
}
async getAsJson(path: string): Promise<Record<string, any>> {
const buffer = await this.get(path);
try {
return JSON.parse(buffer.toString());
} catch (e) {
if (this.shouldThrowError()) {
throw new CannotParseAsJsonException();
}
return null;
}
}
temporaryUrl(
path: string,
ttlInMins: number,
params?: Record<string, any>,
): Promise<string> {
console.log(path, ttlInMins, params);
return null;
}
async size(filePath: string): Promise<number> {
const path = join(this.config.basePath || '', filePath);
const res = await fs.stat(path);
return res.size;
}
async lastModifiedAt(filePath: string): Promise<Date> {
const path = join(this.config.basePath || '', filePath);
const res = await fs.stat(path);
return res.mtime;
}
async mimeType(filePath: string): Promise<string> {
return getMimeTypeFromExtention(filePath);
}
async path(filePath: string): Promise<string> {
return join(this.config.basePath || '', filePath);
}
async listDir(path: string): Promise<Record<string, any>> {
const directory = join(this.config.basePath || '', path);
const fileNames = await fs.readdir(directory);
const listOfFiles = [];
for (const fileName of fileNames) {
const ext = extname(fileName);
const fileStat = await fs.stat(join(directory, fileName));
listOfFiles.push({
name: fileName,
ext: ext,
contentLengthInBytes: fileStat.size,
});
}
return { total: listOfFiles.length, files: listOfFiles };
}
shouldThrowError(): boolean {
return this.config.throwOnFailure === undefined
? true
: this.config.throwOnFailure;
}
}