• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { LogUtil } from './LogUtil';
17import { MediaLibraryAccess } from '../access/MediaLibraryAccess';
18import dataSharePredicates from '@ohos.data.dataSharePredicates';
19import fileio from '@ohos.fileio';
20import photoAccessHelper from '@ohos.file.photoAccessHelper';
21import common from '@ohos.app.ability.common';
22
23const TAG: string = 'OperationUtils';
24let noteContext = AppStorage.Get<common.UIAbilityContext>('noteContext')!;
25
26export class OperationUtils {
27  async copy(uri: string): Promise<string | undefined> {
28    let imagePath: string = '';
29    try {
30      let context = noteContext;
31      let fileAsset = await this.getFileAssetById(uri, context);
32      if (fileAsset == null || fileAsset == undefined) {
33        LogUtil.warn(TAG, "Failed to get fileAsset");
34        return;
35      }
36
37      let fd = await MediaLibraryAccess.openAsset('RW', fileAsset);
38      if (fd !== undefined && fd <= 0) {
39        LogUtil.warn(TAG, "fd is invaild");
40        return;
41      }
42      LogUtil.info(TAG, 'copyAsset read end!');
43
44      // 通过Uri拷贝图片到指定指定的包路径下
45      // 1、获取本地路径
46      let dir = context.filesDir;
47      // 2、生成本地文件名
48      let time = new Date().getTime();
49      imagePath = dir + '/' + time.toString() + '_note' + '.jpg';
50      // 3、拷贝
51      await fileio.copyFile(fd, imagePath);
52      // 4、关闭fd,Asset
53      await MediaLibraryAccess.closeAsset(fd!, fileAsset);
54    } catch (error) {
55      LogUtil.error(TAG, "create error: " + error);
56    }
57    LogUtil.debug(TAG, 'copy end');
58    return imagePath.toString();
59  }
60
61  saveImageData(dataArray: Uint8Array, imageType: string): string {
62    let imagePath: string = '';
63    try {
64      let context = noteContext;
65      // 1、获取本地路径
66      let dir = context.filesDir;
67      let time = new Date().getTime();
68      imagePath = dir + '/' + time.toString() + '_note' + '.' + imageType;
69      let fd = fileio.openSync(imagePath, 0o100 | 0o2, 0o666);
70      // 3、把image数据写入本地图片文件中
71      fileio.writeSync(fd, dataArray.buffer);
72    } catch (error) {
73      LogUtil.error(TAG, "create error: " + error);
74    }
75    LogUtil.info(TAG, 'save image end');
76    return imagePath.toString();
77  }
78
79  saveImage(imageData: string, imageType: string): string {
80    let imagePath: string = '';
81    try {
82      let context = noteContext;
83      // 1、获取本地路径
84      let dir = context.filesDir;
85      // 2、生成本地文件名
86      let time = new Date().getTime();
87      imagePath = dir + '/' + time.toString() + '_note' + '.' + imageType;
88      let fd = fileio.openSync(imagePath, 0o100 | 0o2, 0o666);
89      let arrayBuffer = new ArrayBuffer(imageData.length);
90      let ia = new Uint8Array(arrayBuffer);
91      for (let i = 0; i < imageData.length; i++) {
92        ia[i] = imageData.charCodeAt(i);
93      }
94      // 3、把image数据写入本地图片文件中
95      fileio.writeSync(fd, ia.buffer);
96    } catch (error) {
97      LogUtil.error(TAG, "create error: " + error);
98    }
99    LogUtil.debug(TAG, 'save image end');
100    return imagePath.toString();
101  }
102
103  getIdByUri(uri: string): number {
104    let srcIndex = uri.lastIndexOf('/');
105    let srcEnd = uri.length;
106    let srcId = uri.substring(srcIndex + 1, srcEnd);
107    let numId = new Number(srcId);
108    LogUtil.debug(TAG, `getIdByUri numId: ${numId}`);
109    return numId.valueOf();
110  }
111
112  async getFileAssetById(imagePath: string, context: common.UIAbilityContext) {
113    LogUtil.info(TAG, 'getFileAssetById', imagePath);
114    let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
115    let fetchOpt: photoAccessHelper.FetchOptions = {
116      fetchColumns: [],
117      predicates: predicates.equalTo('uri', imagePath)
118    }
119
120    try {
121      let result = await MediaLibraryAccess.getFirstObject(fetchOpt, context);
122      if (result == null) {
123        LogUtil.info(TAG, 'getFileAssetByUri fail');
124        return null;
125      }
126      return result.obj;
127    } catch (error) {
128      LogUtil.error(TAG, 'getFileAssetById error');
129      return null;
130    }
131  }
132}
133
134let mOperationUtils = new OperationUtils();
135
136export default mOperationUtils as OperationUtils;
137