1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 type common from '@ohos.app.ability.common';
17import photoAccessHelper from '@ohos.file.photoAccessHelper'
18import fs from '@ohos.file.fs';
19import DateTimeUtil from '../utils/DateTimeUtil';
20import Logger from '../utils/Logger';
21
22const TAG = '[MediaModel]';
23
24export default class MediaModel {
25  private photoAccessHelperTest: photoAccessHelper.PhotoAccessHelper = undefined;
26  private static mediaInstance: MediaModel = undefined;
27
28  constructor() {
29    this.photoAccessHelperTest = photoAccessHelper.getPhotoAccessHelper(globalThis.abilityContext);
30  }
31
32  public static getMediaInstance(context: common.Context): MediaModel {
33    if (this.mediaInstance === undefined) {
34      this.mediaInstance = new MediaModel();
35    }
36    return this.mediaInstance;
37  }
38
39  async createAndGetUri(mediaType: photoAccessHelper.PhotoType): Promise<photoAccessHelper.PhotoAsset> {
40    let result = {
41      prefix: 'VID_', suffix: '.mp4'
42    }
43    let info = result;
44    Logger.info(TAG, `createAndGetUri info = ${info}`);
45    let dateTimeUtil = new DateTimeUtil();
46    let name = `${dateTimeUtil.getDate()}_${dateTimeUtil.getTime()}`;
47    let photoAsset = null;
48    let extension: string = '.mp4';
49    let options: photoAccessHelper.CreateOptions = {
50      title: name
51    }
52    try {
53      photoAsset = await this.photoAccessHelperTest.createAsset(mediaType, extension, options);
54    } catch (err) {
55      Logger.info(TAG, `createAndGetUri err = ${err}`);
56    }
57    Logger.info(TAG, `createAndGetUri fileAsset = ${photoAsset}`);
58    return photoAsset;
59  }
60
61  async getFdPath(photoAsset: photoAccessHelper.PhotoAsset): Promise<number> {
62    let fd = await photoAsset.open('Rw');
63    Logger.info(TAG, `fd = ${fd}`);
64    return fd;
65  }
66
67  /**
68   * 复制视频至沙箱
69   * @param fd
70   * @returns
71   */
72  async copyVideo(fd: number): Promise<string> {
73    // 将录制视频存入沙箱路径
74    let mContext: common.Context = globalThis.abilityContext;
75    Logger.info(TAG, `this.fd = ${JSON.stringify(fd)}`);
76    // upload只可访问的沙箱路径:/data/app/el2/100/base/com.samples.appsampled/haps/entry/cache/
77    Logger.info(TAG, `mContext.cacheDir = ${JSON.stringify(mContext.cacheDir)}`);
78    let fileName = new Date().getTime().toString();
79    let imagePath = `${mContext.cacheDir}/${fileName}.mp4`;
80    Logger.info(TAG, `this.imagePath = ${JSON.stringify(imagePath)}`);
81    try {
82      fs.copyFileSync(fd, imagePath);
83      return `${fileName}.mp4`;
84    } catch (err) {
85      Logger.info(TAG, `this.err = ${err}`);
86    }
87    return null;
88  }
89}
90