@@ -9,20 +9,14 @@ import { DeviceModelToTypes, DeviceUploadResourceParams } from '../../types';
99import { BaseMethod } from '../BaseMethod' ;
1010import { validateParams } from '../helpers/paramsValidator' ;
1111import { hexToBytes } from '../helpers/hexUtils' ;
12+ import { createUiMessage , UI_REQUEST } from '../../events' ;
1213import { getDeviceType , getDeviceFirmwareVersion } from '../../utils' ;
1314import { PROTO } from '../../constants' ;
1415
1516export default class DeviceUploadResource extends BaseMethod < ResourceUpload > {
1617 paramsData = {
1718 data : new Uint8Array ( ) ,
1819 thumbnailData : new Uint8Array ( ) ,
19- blurData : new Uint8Array ( ) ,
20- } ;
21-
22- private uploadProgress = {
23- totalBytes : 0 ,
24- uploadedBytes : 0 ,
25- currentFile : 'main' as 'main' | 'thumbnail' | 'blur' ,
2620 } ;
2721
2822 getVersionRange ( ) {
@@ -58,28 +52,20 @@ export default class DeviceUploadResource extends BaseMethod<ResourceUpload> {
5852 { name : 'suffix' , type : 'string' , required : true } ,
5953 { name : 'dataHex' , type : 'string' , required : true } ,
6054 { name : 'thumbnailDataHex' , type : 'string' , required : true } ,
61- { name : 'blurDataHex' , type : 'hexString' , required : true } ,
6255 { name : 'resType' , type : 'number' , required : true } ,
6356 { name : 'nftMetaData' , type : 'string' } ,
6457 { name : 'fileNameNoExt' , type : 'string' } ,
6558 ] ) ;
6659
67- const { suffix, dataHex, thumbnailDataHex, blurDataHex , resType, nftMetaData } = this
60+ const { suffix, dataHex, thumbnailDataHex, resType, nftMetaData } = this
6861 . payload as DeviceUploadResourceParams ;
6962
7063 // init params
7164 this . paramsData = {
72- data : new Uint8Array ( hexToBytes ( dataHex ) ) ,
73- thumbnailData : new Uint8Array ( hexToBytes ( thumbnailDataHex ) ) ,
74- blurData : new Uint8Array ( hexToBytes ( blurDataHex ) ) ,
65+ data : hexToBytes ( dataHex ) ,
66+ thumbnailData : hexToBytes ( thumbnailDataHex ) ,
7567 } ;
7668
77- this . uploadProgress . totalBytes =
78- this . paramsData . data . byteLength +
79- this . paramsData . thumbnailData . byteLength +
80- this . paramsData . blurData . byteLength ;
81- this . uploadProgress . uploadedBytes = 0 ;
82-
8369 const fileHash = bytesToHex ( blake2s ( this . payload . dataHex ) ) . slice ( 0 , 8 ) ;
8470 const file_name_no_ext = isEmpty ( this . payload . fileNameNoExt )
8571 ? `${ resType === 0 ? 'wp' : 'nft' } -${ fileHash } -${ Math . floor ( Date . now ( ) / 1000 ) } `
@@ -89,86 +75,48 @@ export default class DeviceUploadResource extends BaseMethod<ResourceUpload> {
8975 extension : suffix ,
9076 data_length : this . paramsData . data . byteLength ,
9177 zoom_data_length : this . paramsData . thumbnailData . byteLength ,
92- blur_data_length : this . paramsData . blurData . byteLength ,
9378 res_type : resType ,
9479 nft_meta_data : nftMetaData ,
9580 file_name_no_ext,
9681 } ;
9782 }
9883
99- private getDataChunk ( sourceData : Uint8Array , offset : number , length : number ) : Uint8Array {
100- const endOffset = Math . min ( offset + length , sourceData . byteLength ) ;
101-
102- return sourceData . subarray ( offset , endOffset ) ;
103- }
104-
105- private updateProgress ( chunkSize : number , requestType : string ) {
106- this . uploadProgress . uploadedBytes += chunkSize ;
107-
108- if ( requestType === 'ResourceRequest' ) {
109- this . uploadProgress . currentFile = 'main' ;
110- } else if ( requestType === 'ZoomRequest' ) {
111- this . uploadProgress . currentFile = 'thumbnail' ;
112- } else {
113- this . uploadProgress . currentFile = 'blur' ;
114- }
115-
116- const progress = Math . round (
117- ( this . uploadProgress . uploadedBytes / this . uploadProgress . totalBytes ) * 100
118- ) ;
119-
120- if ( process . env . NODE_ENV === 'development' ) {
121- console . log ( `Upload progress: ${ progress } % (${ this . uploadProgress . currentFile } )` ) ;
122- }
123- }
124-
12584 processResourceRequest = async (
12685 res :
12786 | TypedResponseMessage < 'ResourceRequest' >
12887 | TypedResponseMessage < 'ZoomRequest' >
129- | TypedResponseMessage < 'BlurRequest' >
13088 | TypedResponseMessage < 'Success' >
13189 ) : Promise < Success > => {
13290 if ( res . type === 'Success' ) {
13391 return res . message ;
13492 }
13593
13694 const { offset, data_length } = res . message ;
137- const { data, thumbnailData, blurData } = this . paramsData ;
95+ const { data, thumbnailData } = this . paramsData ;
13896
13997 if ( offset === undefined ) {
14098 throw new Error ( 'offset is undefined' ) ;
14199 }
142100
143- let sourceData : Uint8Array ;
144-
145- switch ( res . type ) {
146- case 'ResourceRequest' :
147- sourceData = data ;
148- break ;
149- case 'BlurRequest' :
150- sourceData = blurData ;
151- break ;
152- case 'ZoomRequest' :
153- sourceData = thumbnailData ;
154- break ;
155- default :
156- throw new Error ( 'Invalid request type' ) ;
101+ let payload : Uint8Array ;
102+ if ( res . type === 'ResourceRequest' ) {
103+ payload = new Uint8Array ( data . slice ( offset , Math . min ( offset + data_length , data . byteLength ) ) ) ;
104+ } else {
105+ payload = new Uint8Array (
106+ thumbnailData . slice ( offset , Math . min ( offset + data_length , thumbnailData . byteLength ) )
107+ ) ;
157108 }
158109
159- const payload = this . getDataChunk ( sourceData , offset , data_length ) ;
160110 const digest = blake2s ( payload ) ;
161111
162- this . updateProgress ( payload . byteLength , res . type ) ;
163-
164112 const resourceAckParams = {
165113 data_chunk : bytesToHex ( payload ) ,
166114 hash : bytesToHex ( digest ) ,
167115 } ;
168116
169117 const response = await this . device . commands . typedCall (
170118 'ResourceAck' ,
171- [ 'ResourceRequest' , 'ZoomRequest' , 'BlurRequest' , ' Success'] ,
119+ [ 'ResourceRequest' , 'ZoomRequest' , 'Success' ] ,
172120 resourceAckParams
173121 ) ;
174122 return this . processResourceRequest ( response ) ;
@@ -181,10 +129,12 @@ export default class DeviceUploadResource extends BaseMethod<ResourceUpload> {
181129
182130 const res = await this . device . commands . typedCall (
183131 'ResourceUpload' ,
184- [ 'ResourceRequest' , 'ZoomRequest' , 'BlurRequest' , ' Success'] ,
132+ [ 'ResourceRequest' , 'ZoomRequest' , 'Success' ] ,
185133 this . params
186134 ) ;
187135
136+ this . postMessage ( createUiMessage ( UI_REQUEST . CLOSE_UI_WINDOW ) ) ;
137+
188138 return this . processResourceRequest ( res ) ;
189139 }
190140}
0 commit comments