import type { APIClient } from '@wireapp/api-client' ; import type { AssetOptions } from '@wireapp/api-client/src/asset' ; import type { ProgressCallback , RequestCancelable } from '@wireapp/api-client/src/http' ; import type { EncryptedAssetUploaded } from '../cryptography/' ; import { encryptAsset } from '../cryptography/AssetCryptography' ; export interface AssetDataV4 { assetKey : string ; assetToken : string ; assetDomain : string ; forceCaching : boolean ; version : 4 ; } export interface AssetDataV3 { assetKey : string ; assetToken : string ; forceCaching : boolean ; version : 3 ; } export interface AssetDataV2 { assetId : string ; conversationId : string ; forceCaching : boolean ; version : 2 ; } export interface AssetDataV1 { assetId : string ; conversationId : string ; forceCaching : boolean ; version : 1 ; } export type AssetUrlData = AssetDataV1 | AssetDataV2 | AssetDataV3 | AssetDataV4 ; export class AssetService { constructor ( private readonly apiClient ) { } param assetData param progressCallback return public downloadAsset ( assetData , progressCallback ? ) { const { forceCaching } = assetData ; switch ( assetData . version ) { case 1 : return this . apiClient . asset . api . getAssetV1 ( assetData . assetId , assetData . conversationId , forceCaching , progressCallback ) ; case 2 : return this . apiClient . asset . api . getAssetV2 ( assetData . assetId , assetData . conversationId , forceCaching , progressCallback ) ; case 3 : return this . apiClient . asset . api . getAssetV3 ( assetData . assetKey , assetData . assetToken , forceCaching , progressCallback ) ; case 4 : return this . apiClient . asset . api . getAssetV4 ( assetData . assetKey , assetData . assetDomain , assetData . assetToken , forceCaching , progressCallback ) ; } } param plainText param options param progressCallback return public uploadRawAsset ( asset , options ? , progressCallback ? ) { return this . apiClient . asset . api . postAsset ( new Uint8Array ( asset ) , options , progressCallback ) ; } param plainText param options param progressCallback return public async uploadAsset ( plainText , options ? , progressCallback ? ) { const { cipherText , keyBytes , sha256 } = await encryptAsset ( { plainText , algorithm : options ?. algorithm , hash : options ?. hash , } ) ; const request = this . uploadRawAsset ( cipherText , options , progressCallback ) ; return { ... request , response : request . response . then ( response => { const { key , token , domain } = response ; return { cipherText , domain , key , keyBytes , sha256 , token , } ; } ) , } ; } }