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

@@ -7,12 +7,14 @@ import {
} from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from 'src/Minio/minio.service';
@Injectable()
export class CenterSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) {
super();
}
@@ -34,8 +36,18 @@ export class CenterSchema extends PothosSchema {
description: t.exposeString('description', {
description: 'The description of the center.',
}),
logoUrl: t.exposeString('logoUrl', {
logoUrl: t.string({
description: 'The URL of the center logo.',
resolve: async (center, args, ctx) => {
const fileUrl = await this.minioService.getFileUrl(
center.uploadedFileId ?? '',
'centers',
);
if (!fileUrl) {
return '';
}
return fileUrl;
},
}),
logoFile: t.relation('logoFile', {
description: 'The file associated with the center logo.',

View File

@@ -13,7 +13,7 @@ export class MinioService {
async uploadFile(file: FileUpload, category: string) {
// sonar ignore next
const { mimetype, createReadStream } = await file;
const { mimetype, createReadStream, filename: actualFileName } = await file;
const filename = this.fileName();
const Name = `${category}/${filename}`;
const fileBuffer = createReadStream();
@@ -27,10 +27,13 @@ export class MinioService {
'Content-Type': mimetype,
},
);
return { result, filename, mimetype };
return { result, filename, mimetype, actualFileName };
}
async getFileUrl(fileName: string, category: string) {
if (!fileName) {
return null;
}
const url = await this.minioClient.presignedUrl(
'GET',
this.configService.get('BUCKET_NAME') ?? 'epess',

View File

@@ -194,7 +194,7 @@ export class ResumeSchema extends PothosSchema {
centerId,
ResumeFile: {
create: {
fileUrl,
fileUrl: fileUrl ?? '',
type: mimetype,
},
},
@@ -202,7 +202,7 @@ export class ResumeSchema extends PothosSchema {
update: {
ResumeFile: {
create: {
fileUrl,
fileUrl: fileUrl ?? '',
type: mimetype,
},
},

View File

@@ -7,12 +7,13 @@ import {
} from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from '../Minio/minio.service';
@Injectable()
export class ServiceSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) {
super();
}
@@ -43,8 +44,18 @@ export class ServiceSchema extends PothosSchema {
imageFileId: t.exposeID('imageFileId', {
description: 'The ID of the image file for the service.',
}),
imageFileUrl: t.exposeString('imageFileUrl', {
imageFileUrl: t.string({
description: 'The URL of the image file for the service.',
resolve: async (service, args, ctx) => {
const fileUrl = await this.minioService.getFileUrl(
service.imageFileId ?? '',
'services',
);
if (!fileUrl) {
return '';
}
return fileUrl;
},
}),
createdAt: t.expose('createdAt', {
type: 'DateTime',

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

View File

@@ -7,12 +7,14 @@ import {
} from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from 'src/Minio/minio.service';
@Injectable()
export class WorkshopSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) {
super();
}
@@ -44,8 +46,18 @@ export class WorkshopSchema extends PothosSchema {
imageFileId: t.exposeID('imageFileId', {
description: 'The ID of the image file for the workshop.',
}),
imageFileUrl: t.exposeString('imageFileUrl', {
imageFileUrl: t.string({
description: 'The URL of the image file for the workshop.',
resolve: async (workshop, args, ctx) => {
const fileUrl = await this.minioService.getFileUrl(
workshop.imageFileId ?? '',
'workshops',
);
if (!fileUrl) {
throw new Error('Cannot retrieve file url');
}
return fileUrl;
},
}),
date: t.expose('date', {
type: 'DateTime',

File diff suppressed because one or more lines are too long