fix wrong logic uploaded file

This commit is contained in:
2024-11-09 15:49:06 +07:00
parent 797018ed74
commit 2736547b84
6 changed files with 23 additions and 44 deletions

View File

@@ -39,7 +39,7 @@ import { WorkshopModule } from '../Workshop/workshop.module'
import { WorkshopOrganizationModule } from '../WorkshopOrganization/workshoporganization.module' import { WorkshopOrganizationModule } from '../WorkshopOrganization/workshoporganization.module'
import { WorkshopSubscriptionModule } from '../WorkshopSubscription/workshopsubscription.module' import { WorkshopSubscriptionModule } from '../WorkshopSubscription/workshopsubscription.module'
import { initContextCache } from '@pothos/core' import { initContextCache } from '@pothos/core'
import { PubSub } from 'graphql-subscriptions' import { PubSub, PubSubOptions } from 'graphql-subscriptions'
@Global() @Global()
@Module({ @Module({

View File

@@ -8,7 +8,8 @@ import {
import { Builder, SchemaContext } from '../Graphql/graphql.builder' import { Builder, SchemaContext } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service' import { PrismaService } from '../Prisma/prisma.service'
import { Message, MessageContextType, MessageType } from '@prisma/client' import { Message, MessageContextType, MessageType } from '@prisma/client'
import { DateTimeUtils } from 'src/common/utils/datetime.utils' import { DateTimeUtils } from '../common/utils/datetime.utils'
import { PubSubEvent } from '../common/pubsub/pubsub-event'
@Injectable() @Injectable()
export class MessageSchema extends PothosSchema { export class MessageSchema extends PothosSchema {
@@ -106,37 +107,6 @@ export class MessageSchema extends PothosSchema {
// mutations // mutations
this.builder.mutationFields((t) => ({ this.builder.mutationFields((t) => ({
testSendMessage: t.field({
type: this.message(),
description: 'Test sending a message.',
resolve: async (_root, _args, ctx) => {
if (ctx.isSubscription) {
throw new Error('Not allowed')
}
ctx.http.pubSub.publish('MESSAGE_SENT', {
id: '1',
senderId: '1',
recipientId: '2',
chatRoomId: 'b86e840f-81d3-4043-b57a-f9adf719423c',
type: MessageType.TEXT,
content: 'Hello, world!',
context: MessageContextType.CHAT,
metadata: {},
sentAt: DateTimeUtils.now().toJSDate(),
})
return {
id: '1',
senderId: '1',
recipientId: '2',
chatRoomId: 'b86e840f-81d3-4043-b57a-f9adf719423c',
type: MessageType.TEXT,
content: 'Hello, world!',
context: MessageContextType.CHAT,
metadata: {},
sentAt: DateTimeUtils.now().toJSDate(),
}
},
}),
sendMessage: t.prismaField({ sendMessage: t.prismaField({
type: this.message(), type: this.message(),
description: 'Send a message to a chat room.', description: 'Send a message to a chat room.',
@@ -182,7 +152,10 @@ export class MessageSchema extends PothosSchema {
...query, ...query,
data: args.input, data: args.input,
}) })
ctx.http.pubSub.publish(`MESSAGE_SENT_${message.chatRoomId}`, message) ctx.http.pubSub.publish(
`${PubSubEvent.MESSAGE_SENT}.${message.chatRoomId}`,
message,
)
return message return message
}, },
}), }),
@@ -204,7 +177,7 @@ export class MessageSchema extends PothosSchema {
websocket: { pubSub }, websocket: { pubSub },
} = ctx } = ctx
return pubSub.asyncIterator( return pubSub.asyncIterator(
`MESSAGE_SENT_${args.chatRoomId}`, `${PubSubEvent.MESSAGE_SENT}.${args.chatRoomId}`,
) as unknown as AsyncIterable<Message> ) as unknown as AsyncIterable<Message>
}, },
resolve: (payload: Message) => payload, resolve: (payload: Message) => payload,

View File

@@ -1 +0,0 @@

View File

@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common' import { Inject, Injectable, Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config' import { ConfigService } from '@nestjs/config'
import { FileUpload } from 'graphql-upload/processRequest.js' import { FileUpload } from 'graphql-upload/processRequest.js'
import { Client } from 'minio' import { Client } from 'minio'
@@ -31,8 +31,8 @@ export class MinioService {
return { result, filename, mimetype, actualFileName } return { result, filename, mimetype, actualFileName }
} }
async getFileUrl(id: string, category: string, presignUrl?: string) { async getFileUrl(fileName: string, category: string, presignUrl?: string) {
if (!id) { if (!fileName) {
return null return null
} }
let url = null let url = null
@@ -47,14 +47,13 @@ export class MinioService {
url = await this.minioClient.presignedUrl( url = await this.minioClient.presignedUrl(
'GET', 'GET',
this.configService.get('BUCKET_NAME') ?? 'epess', this.configService.get('BUCKET_NAME') ?? 'epess',
`${category}/${id}`, `${category}/${fileName}`,
60 * 60 * 24 * 7, 60 * 60 * 24 * 7,
) )
} catch (error) { } catch (error) {
console.log(error) Logger.error(error, 'MinioService')
} }
} }
console.log(url)
return url return url
} }

View File

@@ -46,7 +46,7 @@ export class UploadedFileSchema extends PothosSchema {
description: 'The URL of the file.', description: 'The URL of the file.',
resolve: async (file) => { resolve: async (file) => {
return await this.minioService.updatePresignUrl( return await this.minioService.updatePresignUrl(
file.id, file.fileName,
'files', 'files',
file.fileUrl, file.fileUrl,
) )
@@ -89,7 +89,10 @@ 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(file.id, 'files') const fileUrl = await this.minioService.getFileUrl(
file.fileName,
'files',
)
if (!fileUrl) { if (!fileUrl) {
throw new Error('Cannot retrieve file url') throw new Error('Cannot retrieve file url')
} }

View File

@@ -0,0 +1,5 @@
export enum PubSubEvent {
SERVICE_CREATED = 'SERVICE_CREATED',
MESSAGE_SENT = 'MESSAGE_SENT',
NOTIFICATION = 'NOTIFICATION',
}