|
1 | 1 | import http from '@ohos.net.http'; |
2 | 2 | import fileIo from '@ohos.file.fs'; |
3 | 3 | import common from '@ohos.app.ability.common'; |
4 | | -import { buffer } from '@kit.ArkTS'; |
5 | | -import { zlib, BusinessError } from '@kit.BasicServicesKit'; |
| 4 | +import { zlib } from '@kit.BasicServicesKit'; |
6 | 5 | import { EventHub } from './EventHub'; |
7 | 6 | import { DownloadTaskParams } from './DownloadTaskParams'; |
8 | 7 | import Pushy from 'librnupdate.so'; |
@@ -128,53 +127,6 @@ export class DownloadTask { |
128 | 127 | }); |
129 | 128 | } |
130 | 129 |
|
131 | | - private async copyFile(from: string, to: string): Promise<void> { |
132 | | - let reader; |
133 | | - let writer; |
134 | | - try { |
135 | | - reader = fileIo.openSync(from, fileIo.OpenMode.READ_ONLY); |
136 | | - writer = fileIo.openSync( |
137 | | - to, |
138 | | - fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY, |
139 | | - ); |
140 | | - const arrayBuffer = new ArrayBuffer(4096); |
141 | | - let bytesRead: number; |
142 | | - do { |
143 | | - bytesRead = await fileIo |
144 | | - .read(reader.fd, arrayBuffer) |
145 | | - .catch((err: BusinessError) => { |
146 | | - throw Error( |
147 | | - `Error reading file: ${err.message}, code: ${err.code}`, |
148 | | - ); |
149 | | - }); |
150 | | - if (bytesRead > 0) { |
151 | | - const buf = buffer.from(arrayBuffer, 0, bytesRead); |
152 | | - await fileIo |
153 | | - .write(writer.fd, buf.buffer, { |
154 | | - offset: 0, |
155 | | - length: bytesRead, |
156 | | - }) |
157 | | - .catch((err: BusinessError) => { |
158 | | - throw Error( |
159 | | - `Error writing file: ${err.message}, code: ${err.code}`, |
160 | | - ); |
161 | | - }); |
162 | | - } |
163 | | - } while (bytesRead > 0); |
164 | | - console.info('File copied successfully'); |
165 | | - } catch (error) { |
166 | | - console.error('Copy file failed:', error); |
167 | | - throw error; |
168 | | - } finally { |
169 | | - if (reader !== undefined) { |
170 | | - fileIo.closeSync(reader); |
171 | | - } |
172 | | - if (writer !== undefined) { |
173 | | - fileIo.closeSync(writer); |
174 | | - } |
175 | | - } |
176 | | - } |
177 | | - |
178 | 130 | private async doFullPatch(params: DownloadTaskParams): Promise<void> { |
179 | 131 | await this.downloadFile(params); |
180 | 132 | await this.removeDirectory(params.unzipDirectory); |
@@ -243,7 +195,7 @@ export class DownloadTask { |
243 | 195 |
|
244 | 196 | const copies = obj.copies; |
245 | 197 | for (const to in copies) { |
246 | | - let from = copies[to]; |
| 198 | + let from = copies[to].replace('resources/rawfile/', ''); |
247 | 199 | if (from === '') { |
248 | 200 | from = to; |
249 | 201 | } |
@@ -397,29 +349,14 @@ export class DownloadTask { |
397 | 349 | copyList: Map<string, Array<string>>, |
398 | 350 | ): Promise<void> { |
399 | 351 | try { |
400 | | - const bundlePath = this.context.bundleCodeDir; |
401 | | - |
402 | | - const files = await fileIo.listFile(bundlePath); |
403 | | - for (const file of files) { |
404 | | - if (file === '.' || file === '..') { |
405 | | - continue; |
406 | | - } |
407 | | - |
408 | | - const targets = copyList.get(file); |
409 | | - if (targets) { |
410 | | - let lastTarget: string | undefined; |
411 | | - |
412 | | - for (const target of targets) { |
413 | | - console.info(`Copying from resource ${file} to ${target}`); |
414 | | - |
415 | | - if (lastTarget) { |
416 | | - await this.copyFile(lastTarget, target); |
417 | | - } else { |
418 | | - const sourcePath = `${bundlePath}/${file}`; |
419 | | - await this.copyFile(sourcePath, target); |
420 | | - lastTarget = target; |
421 | | - } |
422 | | - } |
| 352 | + const resourceManager = this.context.resourceManager; |
| 353 | + |
| 354 | + for (const [from, targets] of copyList.entries()) { |
| 355 | + const fromContent = await resourceManager.getRawFileContent(from); |
| 356 | + for (const target of targets) { |
| 357 | + const fileStream = fileIo.createStreamSync(target, 'w+'); |
| 358 | + fileStream.writeSync(fromContent.buffer); |
| 359 | + fileStream.close(); |
423 | 360 | } |
424 | 361 | } |
425 | 362 | } catch (error) { |
|
0 commit comments