Enhance DocumentSchema and MinioService for improved error handling and functionality. Update page index validation to check for undefined or null values. Introduce total page count retrieval in DocumentSchema and adjust senderId handling. Refactor MinioService to correctly read and parse document pages, ensuring robust object management. This update improves clarity and reliability in document synchronization processes.

This commit is contained in:
2024-11-27 07:50:51 +07:00
parent 8fba48ed0f
commit 0bfdd3812d
2 changed files with 20 additions and 12 deletions

View File

@@ -159,14 +159,16 @@ export class DocumentSchema extends PothosSchema {
if (ctx.isSubscription) throw new Error('Not allowed') if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http?.me?.id) throw new Error('User not found') if (!ctx.http?.me?.id) throw new Error('User not found')
if (!args.documentId) throw new Error('Document id not found') if (!args.documentId) throw new Error('Document id not found')
if (!args.pageIndex) throw new Error('Page index not found') if (args.pageIndex === undefined || args.pageIndex === null) throw new Error('Page index not found')
const delta = await this.minio.getDocumentPage(args.documentId, args.pageIndex) const delta = await this.minio.getDocumentPage(args.documentId, args.pageIndex)
if (!delta) throw new Error('Delta not found') if (!delta) throw new Error('Delta not found')
const totalPage = await this.minio.countDocumentPages(args.documentId)
return { return {
documentId: args.documentId, documentId: args.documentId,
pageIndex: args.pageIndex, pageIndex: args.pageIndex,
delta, delta,
senderId: 'server', totalPage,
senderId: ctx.http?.me?.id,
eventType: DocumentEvent.CLIENT_REQUEST_SYNC, eventType: DocumentEvent.CLIENT_REQUEST_SYNC,
} }
}, },
@@ -265,8 +267,7 @@ export class DocumentSchema extends PothosSchema {
const senderId = ctx.http?.me?.id const senderId = ctx.http?.me?.id
if (!args.data.documentId) throw new Error('Document id not found') if (!args.data.documentId) throw new Error('Document id not found')
if (!senderId) throw new Error('User not found') if (!senderId) throw new Error('User not found')
if (!args.data.pageIndex) throw new Error('Page index not found') if (args.data.pageIndex === undefined || args.data.pageIndex === null) throw new Error('Page index not found')
// save delta to minio // save delta to minio
const delta = args.data.delta const delta = args.data.delta
if (!delta) throw new Error('Delta not found') if (!delta) throw new Error('Delta not found')
@@ -275,7 +276,7 @@ export class DocumentSchema extends PothosSchema {
return { return {
...args.data, ...args.data,
totalPage, totalPage,
senderId, senderId: 'server',
eventType: DocumentEvent.SERVER_REQUEST_SYNC, eventType: DocumentEvent.SERVER_REQUEST_SYNC,
} }
}, },

View File

@@ -94,20 +94,27 @@ export class MinioService {
) )
} }
async getDocumentPage(id: string, page: number) { async getDocumentPage(id: string, page: number) {
const delta = (await this.minioClient.getObject( const delta = await this.minioClient.getObject(
this.configService.get('BUCKET_NAME') ?? 'epess', this.configService.get('BUCKET_NAME') ?? 'epess',
`documents/${id}/${page}`, `documents/${id}/${page}`,
)) as unknown as Delta )
return delta const buffer = await delta.read()
return JSON.parse(buffer.toString())
} }
async countDocumentPages(id: string): Promise<number> { async countDocumentPages(id: string): Promise<number> {
return new Promise<number>((resolve, reject) => { return new Promise<number>((resolve, reject) => {
const stream = this.minioClient.listObjects(this.configService.get('BUCKET_NAME') ?? 'epess', `documents/${id}`) const stream = this.minioClient.listObjects(this.configService.get('BUCKET_NAME') ?? 'epess', `documents/${id}/`)
const items: BucketItem[] = [] const items: BucketItem[] = []
stream.on('data', (item) => items.push(item)) stream.on('data', (item) => {
stream.on('end', () => resolve(items.length)) items.push(item)
stream.on('error', (err) => reject(err)) })
stream.on('end', () => {
resolve(items.length)
})
stream.on('error', (err) => {
reject(err)
})
}) })
} }
// export document to docx format by get all pages and convert to docx // export document to docx format by get all pages and convert to docx