return properly actual file name

This commit is contained in:
2024-10-17 17:25:02 +07:00
parent f9967ac5bd
commit d01fae4788
8 changed files with 61 additions and 25 deletions

View File

@@ -30,6 +30,9 @@ export class UploadedFileSchema extends PothosSchema {
userId: t.exposeID('userId', {
description: 'The ID of the user who uploaded the file.',
}),
actualFileName: t.exposeString('actualFileName', {
description: 'The original name of the file.',
}),
fileName: t.exposeString('fileName', {
description: 'The name of the file.',
}),
@@ -47,7 +50,7 @@ export class UploadedFileSchema extends PothosSchema {
'files',
);
if (!fileUrl) {
throw new Error('Cannot retrieve file url');
return '';
}
return fileUrl;
},
@@ -80,10 +83,7 @@ export class UploadedFileSchema extends PothosSchema {
if (!file) {
throw new Error('File not found');
}
const fileUrl = await this.minioService.getFileUrl(
file.fileName,
'files',
);
const fileUrl = await this.minioService.getFileUrl(file.id, 'files');
if (!fileUrl) {
throw new Error('Cannot retrieve file url');
}
@@ -105,13 +105,11 @@ export class UploadedFileSchema extends PothosSchema {
where: args.filter ?? undefined,
});
const fileUrls = await Promise.all(
files.map((file) =>
this.minioService.getFileUrl(file.fileName, 'files'),
),
files.map((file) => this.minioService.getFileUrl(file.id, 'files')),
);
return files.map((file, index) => ({
...file,
fileUrl: fileUrls[index],
fileUrl: fileUrls[index] ?? '',
}));
},
}),
@@ -145,10 +143,8 @@ export class UploadedFileSchema extends PothosSchema {
if (!user) {
throw new Error('User not found');
}
const { filename, mimetype } = await this.minioService.uploadFile(
args.file,
'files',
);
const { filename, mimetype, actualFileName } =
await this.minioService.uploadFile(args.file, 'files');
if (!mimetype) {
throw new Error('File type not supported');
}
@@ -160,9 +156,10 @@ export class UploadedFileSchema extends PothosSchema {
data: {
userId: user.id,
fileName: filename,
actualFileName: actualFileName,
type: mimetype,
fileType: args.fileType,
fileUrl: fileUrl,
fileUrl: fileUrl ?? '',
uploadedAt: new Date(),
},
});
@@ -213,7 +210,8 @@ export class UploadedFileSchema extends PothosSchema {
fileName: file.filename,
type: file.mimetype,
fileType: args.fileType,
fileUrl: fileUrls[index],
actualFileName: file.actualFileName,
fileUrl: fileUrls[index] ?? '',
uploadedAt: new Date(),
}));
// create files in db