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'; } from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder'; import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service'; import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from 'src/Minio/minio.service';
@Injectable() @Injectable()
export class CenterSchema extends PothosSchema { export class CenterSchema extends PothosSchema {
constructor( constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder, @Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService, private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) { ) {
super(); super();
} }
@@ -34,8 +36,18 @@ export class CenterSchema extends PothosSchema {
description: t.exposeString('description', { description: t.exposeString('description', {
description: 'The description of the center.', description: 'The description of the center.',
}), }),
logoUrl: t.exposeString('logoUrl', { logoUrl: t.string({
description: 'The URL of the center logo.', 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', { logoFile: t.relation('logoFile', {
description: 'The file associated with the center logo.', description: 'The file associated with the center logo.',

View File

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

View File

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

View File

@@ -7,12 +7,13 @@ import {
} from '@smatch-corp/nestjs-pothos'; } from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder'; import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service'; import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from '../Minio/minio.service';
@Injectable() @Injectable()
export class ServiceSchema extends PothosSchema { export class ServiceSchema extends PothosSchema {
constructor( constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder, @Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService, private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) { ) {
super(); super();
} }
@@ -43,8 +44,18 @@ export class ServiceSchema extends PothosSchema {
imageFileId: t.exposeID('imageFileId', { imageFileId: t.exposeID('imageFileId', {
description: 'The ID of the image file for the service.', 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.', 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', { createdAt: t.expose('createdAt', {
type: 'DateTime', type: 'DateTime',

View File

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

View File

@@ -7,12 +7,14 @@ import {
} from '@smatch-corp/nestjs-pothos'; } from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder'; import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service'; import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from 'src/Minio/minio.service';
@Injectable() @Injectable()
export class WorkshopSchema extends PothosSchema { export class WorkshopSchema extends PothosSchema {
constructor( constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder, @Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService, private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) { ) {
super(); super();
} }
@@ -44,8 +46,18 @@ export class WorkshopSchema extends PothosSchema {
imageFileId: t.exposeID('imageFileId', { imageFileId: t.exposeID('imageFileId', {
description: 'The ID of the image file for the workshop.', 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.', 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', { date: t.expose('date', {
type: 'DateTime', type: 'DateTime',

File diff suppressed because one or more lines are too long