upload attachment

This commit is contained in:
Peter Maquiran
2024-08-13 17:05:46 +01:00
parent 251f533a68
commit d7eb6a552b
20 changed files with 436 additions and 152 deletions
@@ -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
})
}
}
}