• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2023-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 { common } from '@kit.AbilityKit';
17import { fileIo as fs } from '@kit.CoreFileKit';
18import DateTimeUtil from './DateTimeUtils';
19import Logger from './Logger';
20import { photoAccessHelper } from '@kit.MediaLibraryKit';
21import { dataSharePredicates } from '@kit.ArkData';
22
23export default class SaveCameraAsset {
24  private tag: string
25
26  constructor(tag: string) {
27    this.tag = tag;
28  }
29
30  private context = getContext(this) as common.UIAbilityContext;
31  private lastSaveTime: string = '';
32  private saveIndex: number = 0;
33  public VideoPrepareFile?: fs.File;
34  public AudioPrepareFile?: fs.File;
35  public ImagePrepareFile?: fs.File;
36
37  public async createImageFd() {
38    Logger.info(this.tag, 'get Image File Fd');
39    const mDateTimeUtil = new DateTimeUtil();
40    const displayName = this.checkName(`REC_${mDateTimeUtil.getDate()}_${mDateTimeUtil.getTime()}`) + '.jpg';
41    Logger.info(this.tag, 'get Image display name is: ' + displayName);
42    this.ImagePrepareFile = fs.openSync(this.context.filesDir + '/' + displayName, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
43    let fdNumber: number = this.ImagePrepareFile.fd;
44    Logger.info(this.tag, 'get Image File fd is: ' + JSON.stringify(fdNumber));
45    return fdNumber;
46  }
47
48  public async closeImageFile() {
49    if (this.ImagePrepareFile) {
50      await fs.close(this.ImagePrepareFile);
51      Logger.info(this.tag, 'close Image File end');
52    }
53  }
54
55  public async createVideoFd() {
56    // const context = getContext(this);
57    // let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
58    // let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
59    // albumPredicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, 'albumName');
60    // let albumFetchOptions: photoAccessHelper.FetchOptions = {
61    //   fetchColumns: [],
62    //   predicates: albumPredicates
63    // };
64    //
65    // let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
66    // let photoFetchOptions: photoAccessHelper.FetchOptions = {
67    //   fetchColumns: [],
68    //   predicates: photoPredicates
69    // };
70    //
71    // let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset>
72    // let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album>
73    //
74    // try {
75    //   photoFetchResult = await phAccessHelper.getAssets(photoFetchOptions);
76    //   let photoAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
77    //   console.info('getAssets successfully, albumName: ' + photoAsset.displayName);
78    //
79    //   let uri: string = await phAccessHelper.createAsset(photoAccessHelper.PhotoType.VIDEO, "mp4", (err, uri) => {
80    //
81    //   })
82    //
83    //   albumFetchResult =
84    //     await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC,
85    //       albumFetchOptions);
86    //   let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
87    //   console.info('getAlbums successfully, albumName: ' + album.albumName);
88    //   let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest =
89    //     new photoAccessHelper.MediaAlbumChangeRequest(album);
90    //   albumChangeRequest.addAssets([photoAsset]);
91    //   await phAccessHelper.applyChanges(albumChangeRequest);
92    //   console.info('succeed to add ' + photoAsset.displayName + ' to ' + album.albumName);
93    //
94    // } catch (err) {
95    //   console.error('addAssets failed with err: ' + err);
96    // } finally {
97    //   photoFetchResult!.close();
98    //   albumFetchResult!.close();
99    // }
100
101    Logger.info(this.tag, 'get Recorder File Fd');
102    const mDateTimeUtil = new DateTimeUtil();
103    const displayName = this.checkName(`REC_${mDateTimeUtil.getDate()}_${mDateTimeUtil.getTime()}`) + '.mp4';
104    Logger.info(this.tag, 'get Recorder display name is: ' + displayName);
105    this.VideoPrepareFile = fs.openSync(this.context.filesDir + '/' + displayName, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
106    let fdNumber: number = this.VideoPrepareFile.fd;
107    Logger.info(this.tag, 'get Recorder File fd is: ' + JSON.stringify(fdNumber));
108    return fdNumber;
109  }
110
111  // public async createVideoFd() {
112  //   Logger.info(this.tag, 'get Recorder File Fd');
113  //   const mDateTimeUtil = new DateTimeUtil();
114  //   const displayName = this.checkName(`REC_${mDateTimeUtil.getDate()}_${mDateTimeUtil.getTime()}`) + '.mp4';
115  //   Logger.info(this.tag, 'get Recorder display name is: ' + displayName);
116  //   this.VideoPrepareFile = fs.openSync(this.context.filesDir + '/' + displayName, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
117  //   let fdNumber: number = this.VideoPrepareFile.fd;
118  //   Logger.info(this.tag, 'get Recorder File fd is: ' + JSON.stringify(fdNumber));
119  //   return fdNumber;
120  // }
121
122  public async closeVideoFile() {
123    if (this.VideoPrepareFile) {
124      await fs.close(this.VideoPrepareFile);
125      Logger.info(this.tag, 'close Video File end');
126    }
127  }
128
129  public async createAudioFd() {
130    Logger.info(this.tag, 'get Recorder File Fd');
131    const mDateTimeUtil = new DateTimeUtil();
132    const displayName = this.checkName(`REC_${mDateTimeUtil.getDate()}_${mDateTimeUtil.getTime()}`) + '.wav';
133    Logger.info(this.tag, 'get Recorder display name is: ' + displayName);
134    this.AudioPrepareFile = fs.openSync(this.context.filesDir + '/' + displayName, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
135    let fdNumber: number = this.AudioPrepareFile.fd;
136    Logger.info(this.tag, 'get Recorder File fd is: ' + JSON.stringify(fdNumber));
137    return fdNumber;
138  }
139
140  public async closeAudioFile() {
141    if (this.AudioPrepareFile) {
142      await fs.close(this.AudioPrepareFile);
143      Logger.info(this.tag, 'close Audio File end');
144    }
145  }
146
147  private checkName(name: string): string {
148    if (this.lastSaveTime == name) {
149      this.saveIndex += 1;
150      return `${name}_${this.saveIndex}`
151    }
152    this.lastSaveTime = name;
153    this.saveIndex = 0;
154    Logger.info(this.tag, 'get Recorder File name is: ' + name);
155    return name;
156  }
157}