• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2022-2024 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 fileUri from '@ohos.file.fileuri';
17import fs from '@ohos.file.fs';
18import { BusinessError } from '@ohos.base';
19
20export class FileIoModel {
21  getMediaFileData(mediaUri: string, callback: Function): void {
22    console.info('CertManager FA getMediaFile start');
23    let file: fs.File | undefined = undefined;
24    try {
25      file = fs.openSync(mediaUri, fs.OpenMode.READ_ONLY);
26      let stat = fs.statSync(file.fd);
27      let buf = new ArrayBuffer(Number(stat.size));
28      let num = fs.readSync(file.fd, buf);
29      console.info('CertManager FA getMediaFile success');
30      callback(new Uint8Array(buf));
31    } catch (err) {
32      let e: BusinessError = err as BusinessError;
33      console.error('CertManager FA getMediaFileData failed with err, message: ' + e.message + ', code: ' + e.code);
34      callback(undefined);
35    } finally {
36      try {
37        if (file !== undefined && file !== null) {
38          fs.closeSync(file.fd);
39        }
40      } catch (err) {
41        let e: BusinessError = err as BusinessError;
42        console.error('CertManager FA close io stream failed with err, message: ' + e.message + ', code: ' + e.code);
43      }
44    }
45  }
46
47  getMediaFileSuffix(mediaUri: string, callback: Function): void {
48    try {
49      console.info('CertManager FA getMediaFileSuffix start');
50      let uri = new fileUri.FileUri(mediaUri);
51      let suffix = uri.name.substring(uri.name.lastIndexOf('.') + 1);
52      let suffixLowerCase = suffix.toLowerCase();
53      callback(suffixLowerCase);
54    } catch (err) {
55      let e: BusinessError = err as BusinessError;
56      console.error('CertManager FA getMediaFileSuffix failed with err, message: ' + e.message + ', code: ' + e.code);
57      callback(undefined);
58    }
59  }
60}
61
62let fileIoModel = new FileIoModel();
63
64export default fileIoModel as FileIoModel;
65