@tmlmobilidade/interfaces 20250906.141.55 → 20250908.1551.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -48,7 +48,11 @@ declare class FilesClass extends MongoCollectionClass<File, CreateFileDto, Updat
|
|
|
48
48
|
* @param createFileDto - The file type to create.
|
|
49
49
|
* @returns The file that was uploaded.
|
|
50
50
|
*/
|
|
51
|
-
upload(file: Buffer, createFileDto: CreateFileDto
|
|
51
|
+
upload(file: Buffer, createFileDto: CreateFileDto & {
|
|
52
|
+
_id?: string;
|
|
53
|
+
}, options?: InsertOneOptions & {
|
|
54
|
+
override?: boolean;
|
|
55
|
+
}): Promise<File>;
|
|
52
56
|
protected getCollectionIndexes(): IndexDescription[];
|
|
53
57
|
protected getCollectionName(): string;
|
|
54
58
|
protected getEnvName(): string;
|
|
@@ -156,9 +156,51 @@ class FilesClass extends MongoCollectionClass {
|
|
|
156
156
|
* @returns The file that was uploaded.
|
|
157
157
|
*/
|
|
158
158
|
async upload(file, createFileDto, options) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
159
|
+
//
|
|
160
|
+
//
|
|
161
|
+
// A. Define variables
|
|
162
|
+
if (createFileDto._id && !options?.override) {
|
|
163
|
+
throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'When File ID is provided, override must be true');
|
|
164
|
+
}
|
|
165
|
+
const fileId = createFileDto._id || generateRandomString({ length: 5 });
|
|
166
|
+
const fileExtension = Files.getFileExtension(createFileDto.name);
|
|
167
|
+
const mimeType = Files.getMimeTypeFromFileExtension(createFileDto.name);
|
|
168
|
+
const filePath = `${createFileDto.scope}/${createFileDto.resource_id}/${fileId}.${fileExtension}`;
|
|
169
|
+
//
|
|
170
|
+
// C. Handle database transaction
|
|
171
|
+
const session = this.getMongoConnector().client.startSession();
|
|
172
|
+
let result;
|
|
173
|
+
try {
|
|
174
|
+
session.startTransaction();
|
|
175
|
+
//
|
|
176
|
+
// C.1. Handle file override if specified
|
|
177
|
+
if (options?.override) {
|
|
178
|
+
const existingFile = await this.findOne({ _id: fileId });
|
|
179
|
+
if (existingFile) {
|
|
180
|
+
const existingFileExtension = Files.getFileExtension(existingFile.name);
|
|
181
|
+
const existingFilePath = `${existingFile.scope}/${existingFile.resource_id}/${existingFile._id}.${existingFileExtension}`;
|
|
182
|
+
if (existingFilePath !== filePath) {
|
|
183
|
+
throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'File ID is provided, but the file path is different from the existing file', { cause: { existingFilePath, filePath } });
|
|
184
|
+
}
|
|
185
|
+
await super.deleteById(fileId);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
//
|
|
189
|
+
// C.2. Upload file to storage
|
|
190
|
+
await this.storageService.uploadFile(filePath, file, mimeType);
|
|
191
|
+
//
|
|
192
|
+
// C.3. Insert file record
|
|
193
|
+
result = await this.insertOne({ ...createFileDto, _id: fileId }, { options });
|
|
194
|
+
await session.commitTransaction();
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
await session.abortTransaction();
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
session.endSession();
|
|
202
|
+
}
|
|
203
|
+
return result;
|
|
162
204
|
}
|
|
163
205
|
getCollectionIndexes() {
|
|
164
206
|
return [
|
package/package.json
CHANGED