mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 21:35:50 +00:00
upload attachment
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { z } from 'zod';
|
||||
import { AttachmentRemoteDataSourceService } from 'src/app/module/chat/data/data-source/attachment/attachment-remote-data-source.service'
|
||||
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/data-source/attachment/attachment-local-data-source.service'
|
||||
import { convertBlobToDataURL } from 'src/app/utils/ToBase64';
|
||||
import { Result } from 'neverthrow';
|
||||
|
||||
const MessageAttachmentByMessageIdSchema = z.object({
|
||||
$messageId: z.number(),
|
||||
id: z.string()
|
||||
})
|
||||
|
||||
export type MessageAttachmentByMessageIdInput = z.infer<typeof MessageAttachmentByMessageIdSchema>
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MessageAttachmentByMessageIdUseCase {
|
||||
|
||||
constructor(
|
||||
private AttachmentRemoteDataSourceService: AttachmentRemoteDataSourceService,
|
||||
private AttachmentLocalDataSource: AttachmentLocalDataSource
|
||||
) { }
|
||||
|
||||
async execute(input: MessageAttachmentByMessageIdInput): Promise<Result<string, any>> {
|
||||
|
||||
const getLocalAttachment = await this.AttachmentLocalDataSource.findOne({
|
||||
$messageId: input.$messageId
|
||||
})
|
||||
|
||||
if(getLocalAttachment.isOk() && getLocalAttachment.value) {
|
||||
if(getLocalAttachment.value) {
|
||||
console.log('found local', getLocalAttachment.value)
|
||||
return getLocalAttachment.map(e => e.file)
|
||||
}
|
||||
} else {
|
||||
const result = await this.AttachmentRemoteDataSourceService.getAttachment(input)
|
||||
return result.asyncMap(async (e) => {
|
||||
|
||||
const dataUrl = await convertBlobToDataURL(e)
|
||||
|
||||
this.AttachmentLocalDataSource.insert({
|
||||
$messageId: input.$messageId,
|
||||
id: input.id,
|
||||
file: dataUrl
|
||||
})
|
||||
|
||||
return dataUrl
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user