-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile-service.ts
183 lines (159 loc) · 5.35 KB
/
file-service.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* SudoSOS back-end API service.
* Copyright (C) 2020 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UploadedFile } from 'express-fileupload';
import path from 'path';
import { DiskStorage, FileStorage, SIMPLE_FILE_LOCATION } from '../files/storage';
import BaseFile from '../entity/file/base-file';
import SimpleFileRequest from '../controller/request/simple-file-request';
import User from '../entity/user/user';
import Product from '../entity/product/product';
import ProductImage from '../entity/file/product-image';
import Banner from '../entity/banner';
import BannerImage from '../entity/file/banner-image';
/**
* Possible storage methods that can be used
*/
export type StorageMethod = 'disk';
export interface DownloadFileResponse {
file: BaseFile,
data: Buffer,
}
export default class FileService {
private fileStorage: FileStorage;
constructor(workdir?: string, storageMethod?: StorageMethod) {
switch (storageMethod ?? process.env.FILE_STORAGE_METHOD) {
case undefined:
case '':
case 'disk':
this.fileStorage = new DiskStorage(workdir ?? SIMPLE_FILE_LOCATION);
break;
default:
throw new TypeError(`Unknown file storage method: ${process.env.FILE_STORAGE_METHOD}`);
}
}
/**
* Create a new file in storage, given the provided parameters
*/
private async createFile(file: BaseFile, fileData: Buffer): Promise<BaseFile> {
let location: string;
try {
location = await this.fileStorage.saveFile(file.downloadName, fileData);
} catch (error) {
await BaseFile.delete(file.id);
throw new Error(error);
}
// eslint-disable-next-line no-param-reassign
file.location = location;
try {
await file.save();
} catch (error) {
await this.fileStorage.deleteFile(file);
await BaseFile.delete(file.id);
throw new Error(error);
}
return file;
}
/**
* Read and return the given file from storage
*/
private async readFile(file: BaseFile): Promise<Buffer> {
return this.fileStorage.getFile(file);
}
/**
* Remove the given file from storage
*/
private async removeFile(file: BaseFile) {
await this.fileStorage.deleteFile(file);
}
/**
* Upload a simple file to the database and put in storage
*/
public async uploadSimpleFile(
createdBy: User, uploadedFile: UploadedFile, fileEntity: SimpleFileRequest,
) {
const fileExtension = path.extname(uploadedFile.name);
const file = Object.assign(new BaseFile(), {
downloadName: `${fileEntity.name}${fileExtension}`,
createdBy,
location: '',
});
await file.save();
return this.createFile(file, uploadedFile.data);
}
/**
* Get the given simple file object and data from storage
*/
public async getSimpleFile(id: number): Promise<DownloadFileResponse | undefined> {
const file = await BaseFile.findOne({ where: { id } });
if (!file) {
return undefined;
}
const data = await this.readFile(file);
return { file, data };
}
/**
* Delete the simple file with given ID from storage and database
*/
public async deleteSimpleFile(id: number): Promise<void> {
const file = await BaseFile.findOne({ where: { id } });
if (!file) return;
await this.removeFile(file);
await BaseFile.delete(file.id);
}
/**
* Upload an entity image to the given entity and replace the old one, if it exists
*/
public async uploadEntityImage(
entity: Product | Banner, uploadedFile: UploadedFile, createdBy: User,
): Promise<ProductImage> {
let entityImage = entity.image;
if (entityImage == null) {
entityImage = Object.assign(new BaseFile(), {
downloadName: uploadedFile.name,
createdBy,
location: '',
});
await ProductImage.save(entityImage);
} else {
// If the file does exist, we first have to remove it from storage
await this.removeFile(entityImage);
}
// Store the new file in storage.
entityImage = await this.createFile(entityImage, uploadedFile.data);
// Save the file name as the download name.
entityImage.downloadName = path.parse(entityImage.location).base;
// eslint-disable-next-line no-param-reassign
entity.image = entityImage;
if (entity instanceof Product) {
await ProductImage.save(entityImage);
} else if (entity instanceof Banner) {
await BannerImage.save(entityImage);
} else {
throw new Error('Given entity is not a Product or a Banner');
}
await entity.save();
return entityImage;
}
/**
* Delete entity file from database
*/
public async deleteEntityFile(entityFile: ProductImage | BannerImage) {
await this.removeFile(entityFile);
await entityFile.remove();
}
}