mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
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';
|
|
import { Logger } from 'src/app/services/logger/main/service';
|
|
|
|
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) {
|
|
return getLocalAttachment.map(e => e.file)
|
|
}
|
|
} else {
|
|
const result = await this.AttachmentRemoteDataSourceService.getAttachment(input.id)
|
|
return result.asyncMap(async (e) => {
|
|
|
|
const dataUrl = await convertBlobToDataURL(e)
|
|
Logger.info('downloaded file', {
|
|
data: dataUrl.slice(0, 100)+'...'
|
|
})
|
|
|
|
this.AttachmentLocalDataSource.insert({
|
|
$messageId: input.$messageId,
|
|
id: input.id,
|
|
file: dataUrl
|
|
})
|
|
|
|
return dataUrl
|
|
})
|
|
}
|
|
|
|
}
|
|
}
|