已合并
修复图片接续异常问题 #4
sfchu创建于 7月15日
修复图片接续异常问题 #4
已合并
共 3 个文件变更+49-37
| @@ -74,7 +74,7 @@ export default class EntryAbility extends UIAbility { | |||
| 74 | // Migrate page data using distributed object single-file migration | 74 | // Migrate page data using distributed object single-file migration |
| 75 | AppStorage.setOrCreate('monoFileStr', this.distributedObject['monoFileStr'] ?? ''); | 75 | AppStorage.setOrCreate('monoFileStr', this.distributedObject['monoFileStr'] ?? ''); |
| 76 | AppStorage.setOrCreate('monoFileNum', this.distributedObject['monoFileNum'] ?? undefined); | 76 | AppStorage.setOrCreate('monoFileNum', this.distributedObject['monoFileNum'] ?? undefined); |
| 77 | - AppStorage.setOrCreate('attachment', this.distributedObject['attachment'] ?? []); | 77 | + AppStorage.setOrCreate('attachment', this.distributedObject['attachment'] ?? undefined); |
| 78 | if (this.distributedObject['attachment'] as commonType.Assets !== undefined) { | 78 | if (this.distributedObject['attachment'] as commonType.Assets !== undefined) { |
| 79 | let attachment = this.distributedObject['attachment'] as commonType.Assets; | 79 | let attachment = this.distributedObject['attachment'] as commonType.Assets; |
| 80 | for (const attachments of attachment) { | 80 | for (const attachments of attachment) { |
| @@ -85,7 +85,7 @@ export default class EntryAbility extends UIAbility { | |||
| 85 | // Use the distributed object multi-file migration page data function | 85 | // Use the distributed object multi-file migration page data function |
| 86 | AppStorage.setOrCreate('multiFileStr', this.distributedObject['multiFileStr'] ?? ''); | 86 | AppStorage.setOrCreate('multiFileStr', this.distributedObject['multiFileStr'] ?? ''); |
| 87 | AppStorage.setOrCreate('multiFileNum', this.distributedObject['multiFileNum'] ?? undefined); | 87 | AppStorage.setOrCreate('multiFileNum', this.distributedObject['multiFileNum'] ?? undefined); |
| 88 | - AppStorage.setOrCreate('attachments', this.distributedObject['attachments'] ?? []); | 88 | + AppStorage.setOrCreate('attachments', this.distributedObject['attachments'] ?? undefined); |
| 89 | if (this.distributedObject['attachments'] as commonType.Assets !== undefined) { | 89 | if (this.distributedObject['attachments'] as commonType.Assets !== undefined) { |
| 90 | let attachments = this.distributedObject['attachments'] as commonType.Assets; | 90 | let attachments = this.distributedObject['attachments'] as commonType.Assets; |
| 91 | for (const attachment of attachments) { | 91 | for (const attachment of attachments) { |
| @@ -137,8 +137,8 @@ export default class EntryAbility extends UIAbility { | |||
| 137 | let imageUri: ImageInfo | undefined = AppStorage.get<ImageInfo>('imageUri'); | 137 | let imageUri: ImageInfo | undefined = AppStorage.get<ImageInfo>('imageUri'); |
| 138 | let asset: commonType.Assets | undefined; | 138 | let asset: commonType.Assets | undefined; |
| 139 | if (imageUri !== undefined) { | 139 | if (imageUri !== undefined) { |
| 140 | - this.distributedFileUtil.pixelMapToBuffer(imageUri); | 140 | + await this.distributedFileUtil.pixelMapToBuffer(imageUri); |
| 141 | - asset = this.distributedFileUtil.buildAssetsFromImageInfo(imageUri); | 141 | + asset = await this.distributedFileUtil.buildAssetsFromImageInfo(imageUri); |
| 142 | } | 142 | } |
| 143 | // [StartExclude UniFileDistributedFile] | 143 | // [StartExclude UniFileDistributedFile] |
| 144 | // [Start MultiFileDistributedFile] | 144 | // [Start MultiFileDistributedFile] |
| @@ -146,12 +146,12 @@ export default class EntryAbility extends UIAbility { | |||
| 146 | 146 | ||
| 147 | let assets: commonType.Assets | undefined; | 147 | let assets: commonType.Assets | undefined; |
| 148 | if (imageUriArray !== undefined) { | 148 | if (imageUriArray !== undefined) { |
| 149 | - imageUriArray.forEach((item: ImageInfo) => { | 149 | + for (const item of imageUriArray) { |
| 150 | - this.distributedFileUtil?.pixelMapToBuffer(item); | 150 | + await this.distributedFileUtil.pixelMapToBuffer(item); |
| 151 | - }); | 151 | + } |
| 152 | - assets = this.distributedFileUtil.buildAssetsFromImageArray(AppStorage.get<Array<ImageInfo>>('imageUriArray')); | 152 | + assets = |
| 153 | + await this.distributedFileUtil.buildAssetsFromImageArray(AppStorage.get<Array<ImageInfo>>('imageUriArray')); | ||
| 153 | } | 154 | } |
| 154 | - | ||
| 155 | // [EndExclude UniFileDistributedFile] | 155 | // [EndExclude UniFileDistributedFile] |
| 156 | // [EndExclude DistributedBaseContinue] | 156 | // [EndExclude DistributedBaseContinue] |
| 157 | let contentInfo: ContinuationData = new ContinuationData( | 157 | let contentInfo: ContinuationData = new ContinuationData( |
| @@ -164,17 +164,17 @@ export default class EntryAbility extends UIAbility { | |||
| 164 | // [StartExclude DistributedBaseContinue] | 164 | // [StartExclude DistributedBaseContinue] |
| 165 | AppStorage.get('monoFileStr') ?? '', | 165 | AppStorage.get('monoFileStr') ?? '', |
| 166 | AppStorage.get('monoFileNum') ?? undefined, | 166 | AppStorage.get('monoFileNum') ?? undefined, |
| 167 | - asset, | 167 | + asset ?? undefined, |
| 168 | // [EndExclude MultiFileDistributedFile] | 168 | // [EndExclude MultiFileDistributedFile] |
| 169 | // [StartExclude UniFileDistributedFile] | 169 | // [StartExclude UniFileDistributedFile] |
| 170 | AppStorage.get('multiFileStr') ?? '', | 170 | AppStorage.get('multiFileStr') ?? '', |
| 171 | AppStorage.get('multiFileNum') ?? undefined, | 171 | AppStorage.get('multiFileNum') ?? undefined, |
| 172 | - assets, | 172 | + assets ?? undefined, |
| 173 | // [EndExclude UniFileDistributedFile] | 173 | // [EndExclude UniFileDistributedFile] |
| 174 | // [EndExclude DistributedBaseContinue] | 174 | // [EndExclude DistributedBaseContinue] |
| 175 | (AppStorage.get('pageInfos') as NavPathStack).getAllPathName()[0] | 175 | (AppStorage.get('pageInfos') as NavPathStack).getAllPathName()[0] |
| 176 | ); | 176 | ); |
| 177 | - let source = contentInfo.flatAssets(); | 177 | + let source = await contentInfo.flatAssets(); |
| 178 | this.distributedObject = distributedDataObject.create(this.context, source); | 178 | this.distributedObject = distributedDataObject.create(this.context, source); |
| 179 | // [End UniFileDistributedFile] | 179 | // [End UniFileDistributedFile] |
| 180 | // [End MultiFileDistributedFile] | 180 | // [End MultiFileDistributedFile] |
| @@ -73,14 +73,15 @@ export class ContinuationData { | |||
| 73 | // [EndExclude DistributedBaseData] | 73 | // [EndExclude DistributedBaseData] |
| 74 | } | 74 | } |
| 75 | // [EndExclude DistributedBaseData] | 75 | // [EndExclude DistributedBaseData] |
| 76 | - flatAssets(): object { | 76 | + async flatAssets(): Promise<object> { |
| 77 | let obj: object = this; | 77 | let obj: object = this; |
| 78 | - if (!this.attachments) { | 78 | + if (this.attachment !== undefined) { |
| 79 | - return obj; | 79 | + obj[`attachment${0}`] = this.attachment[0]; |
| 80 | } | 80 | } |
| 81 | - obj[`attachment`] = this.attachment; | 81 | + if (this.attachments !== undefined) { |
| 82 | - for (let i = 0; i < this.attachments.length; i++) { | 82 | + for (let i = 0; i < this.attachments.length; i++) { |
| 83 | - obj[`attachments${i}`] = this.attachments[i]; | 83 | + obj[`attachments${i}`] = this.attachments[i]; |
| 84 | + } | ||
| 84 | } | 85 | } |
| 85 | return obj; | 86 | return obj; |
| 86 | } | 87 | } |
| @@ -33,13 +33,14 @@ export class DistributedFileUtil { | |||
| 33 | constructor(context: common.UIAbilityContext) { | 33 | constructor(context: common.UIAbilityContext) { |
| 34 | this.context = context; | 34 | this.context = context; |
| 35 | } | 35 | } |
| 36 | + | ||
| 36 | // [Start CreateAsset] | 37 | // [Start CreateAsset] |
| 37 | - getAssetInfo(append: ImageInfo): commonType.Asset | undefined { | 38 | + async getAssetInfo(append: ImageInfo): Promise<commonType.Asset | undefined> { |
| 38 | let filePath = this.context.distributedFilesDir + '/' + append.imageName; | 39 | let filePath = this.context.distributedFilesDir + '/' + append.imageName; |
| 39 | let attachment: commonType.Asset; | 40 | let attachment: commonType.Asset; |
| 40 | try { | 41 | try { |
| 41 | let uri: string = fileUri.getUriFromPath(filePath); | 42 | let uri: string = fileUri.getUriFromPath(filePath); |
| 42 | - let stat = fileIo.statSync(filePath); | 43 | + let stat = await fileIo.stat(filePath); |
| 43 | attachment = { | 44 | attachment = { |
| 44 | name: append.imageName, | 45 | name: append.imageName, |
| 45 | uri: uri, | 46 | uri: uri, |
| @@ -51,7 +52,7 @@ export class DistributedFileUtil { | |||
| 51 | hilog.info(DOMAIN, TAG, FORMAT, `[getAssetInfo] attachments name: ${attachment.name}`); | 52 | hilog.info(DOMAIN, TAG, FORMAT, `[getAssetInfo] attachments name: ${attachment.name}`); |
| 52 | return attachment; | 53 | return attachment; |
| 53 | } catch (err) { | 54 | } catch (err) { |
| 54 | - hilog.error(DOMAIN, TAG, FORMAT, `StatSync failed. Cause code: ${err.code}`); | 55 | + hilog.error(DOMAIN, TAG, FORMAT, `Stat failed. Cause code: ${err.code}`); |
| 55 | return undefined; | 56 | return undefined; |
| 56 | } | 57 | } |
| 57 | } | 58 | } |
| @@ -125,10 +126,10 @@ export class DistributedFileUtil { | |||
| 125 | } | 126 | } |
| 126 | } | 127 | } |
| 127 | 128 | ||
| 128 | - buildAssetsFromImageInfo(imageUri: ImageInfo | undefined): commonType.Assets { | 129 | + async buildAssetsFromImageInfo(imageUri: ImageInfo | undefined): Promise<commonType.Assets> { |
| 129 | let assets: commonType.Assets = []; | 130 | let assets: commonType.Assets = []; |
| 130 | if (imageUri) { | 131 | if (imageUri) { |
| 131 | - let attachment = this.getAssetInfo(imageUri); | 132 | + let attachment = await this.getAssetInfo(imageUri); |
| 132 | if (attachment) { | 133 | if (attachment) { |
| 133 | assets.push(attachment); | 134 | assets.push(attachment); |
| 134 | } | 135 | } |
| @@ -136,11 +137,11 @@ export class DistributedFileUtil { | |||
| 136 | return assets; | 137 | return assets; |
| 137 | } | 138 | } |
| 138 | 139 | ||
| 139 | - buildAssetsFromImageArray(imageUriArray: Array<ImageInfo> | undefined): commonType.Assets { | 140 | + async buildAssetsFromImageArray(imageUriArray: Array<ImageInfo> | undefined): Promise<commonType.Assets> { |
| 140 | let assets: commonType.Assets = []; | 141 | let assets: commonType.Assets = []; |
| 141 | if (imageUriArray) { | 142 | if (imageUriArray) { |
| 142 | for (let i = 0; i < imageUriArray.length; i++) { | 143 | for (let i = 0; i < imageUriArray.length; i++) { |
| 143 | - let attachment = this.getAssetInfo(imageUriArray[i]); | 144 | + let attachment = await this.getAssetInfo(imageUriArray[i]); |
| 144 | if (attachment) { | 145 | if (attachment) { |
| 145 | assets.push(attachment); | 146 | assets.push(attachment); |
| 146 | } | 147 | } |
| @@ -149,34 +150,44 @@ export class DistributedFileUtil { | |||
| 149 | return assets; | 150 | return assets; |
| 150 | } | 151 | } |
| 151 | 152 | ||
| 152 | - pixelMapToBuffer(imageInfo:ImageInfo): void { | 153 | + async pixelMapToBuffer(imageInfo: ImageInfo): Promise<void> { |
| 153 | const imagePackerApi: image.ImagePacker = image.createImagePacker(); | 154 | const imagePackerApi: image.ImagePacker = image.createImagePacker(); |
| 154 | let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 }; | 155 | let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 }; |
| 155 | - imagePackerApi.packToData(imageInfo.imagePixelMap, packOpts).then((data: ArrayBuffer) => { | 156 | + try { |
| 156 | - this.writeDistributedFile(data, imageInfo.imageName); | 157 | + const data: ArrayBuffer = await imagePackerApi.packToData(imageInfo.imagePixelMap, packOpts); |
| 157 | - }).catch((err: BusinessError) => { | 158 | + await this.writeDistributedFile(data, imageInfo.imageName); |
| 158 | - hilog.error(DOMAIN, TAG, FORMAT, `PackToData failed. Cause code: ${err.code}`); | 159 | + hilog.info(DOMAIN, TAG, FORMAT, `PixelMap converted to buffer successfully: ${imageInfo.imageName}`); |
| 159 | - }); | 160 | + } catch (err) { |
| 161 | + const error = err as BusinessError; | ||
| 162 | + hilog.error(DOMAIN, TAG, FORMAT, `PackToData failed. Cause code: ${error.code}`); | ||
| 163 | + } finally { | ||
| 164 | + try { | ||
| 165 | + imagePackerApi.release(); | ||
| 166 | + } catch (err) { | ||
| 167 | + hilog.error(DOMAIN, TAG, FORMAT, `Failed to release imagePackerApi. Cause: ${err}`); | ||
| 168 | + } | ||
| 169 | + } | ||
| 160 | } | 170 | } |
| 161 | // [Start WriteDistributedFile] | 171 | // [Start WriteDistributedFile] |
| 162 | - writeDistributedFile(buf: ArrayBuffer, displayName: string): void { | 172 | + async writeDistributedFile(buf: ArrayBuffer, displayName: string): Promise<void> { |
| 163 | let distributedDir: string = this.context.distributedFilesDir; | 173 | let distributedDir: string = this.context.distributedFilesDir; |
| 164 | let fileName: string = '/' + displayName; | 174 | let fileName: string = '/' + displayName; |
| 165 | let filePath: string = distributedDir + fileName; | 175 | let filePath: string = distributedDir + fileName; |
| 166 | let file: fileIo.File | undefined = undefined; | 176 | let file: fileIo.File | undefined = undefined; |
| 167 | try { | 177 | try { |
| 168 | - file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); | 178 | + file = await fileIo.open(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); |
| 169 | hilog.info(DOMAIN, TAG, FORMAT, 'Create file success.'); | 179 | hilog.info(DOMAIN, TAG, FORMAT, 'Create file success.'); |
| 170 | - fileIo.writeSync(file.fd, buf); | 180 | + await fileIo.write(file.fd, buf); |
| 181 | + hilog.info(DOMAIN, TAG, FORMAT, `File written successfully: ${displayName}`); | ||
| 171 | } catch (error) { | 182 | } catch (error) { |
| 172 | let err: BusinessError = error as BusinessError; | 183 | let err: BusinessError = error as BusinessError; |
| 173 | - hilog.error(DOMAIN, TAG, FORMAT, `Failed to openSync / writeSync. Code: ${err.code}`); | 184 | + hilog.error(DOMAIN, TAG, FORMAT, `Failed to open / write. Code: ${err.code}`); |
| 174 | } finally { | 185 | } finally { |
| 175 | if (file) { | 186 | if (file) { |
| 176 | try { | 187 | try { |
| 177 | - fileIo.closeSync(file.fd); | 188 | + await fileIo.close(file); |
| 178 | } catch (err) { | 189 | } catch (err) { |
| 179 | - hilog.error(DOMAIN, TAG, FORMAT, `Failed to closeSync. Cause : ${err}`); | 190 | + hilog.error(DOMAIN, TAG, FORMAT, `Failed to close. Cause: ${err}`); |
| 180 | } | 191 | } |
| 181 | } | 192 | } |
| 182 | } | 193 | } |