import { PlatformFile, PreparedFiles } from '../interfaces' import { DuplicatedFileError, IFileService, IFileServiceImplementation, } from '../interfaces/files.interface' import { ApisauceInstance } from '../services/api/apisauce' import { PLATFORM_SERVICES, resolveService } from './platformResolver' export class FileServiceProvider implements IFileService { api: ApisauceInstance implementation?: IFileServiceImplementation constructor(api: ApisauceInstance) { this.api = api } async initialize() { const serviceProvider = await resolveService>( PLATFORM_SERVICES.FILES, this.api, ) if (!serviceProvider) { return } this.implementation = serviceProvider } initialized() { return !!this.implementation } async uploadFiles( type: string, hash: string, organization: string, files: PreparedFiles[], ) { if (!this.initialized) { await this.initialize() } return this.implementation?.uploadFiles( type, hash, organization, files as PreparedFiles[], ) } async getFileHash(fileData: PlatformFile) { if (!this.initialized) { await this.initialize() } if (!this.implementation) { return '' } return this.implementation?.getFileHash(fileData as T) } } export class SignError extends Error { constructor(message: string) { super(message) this.name = 'SignError' } } export class DuplicateError extends Error { public responseData constructor(message: string, responseData: DuplicatedFileError[]) { super(message) this.name = 'DuplicateError' this.responseData = responseData } }