2024-05-09 13:42:56 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
|
*
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-01-07 15:50:48 +08:00
|
|
|
|
|
2024-05-09 13:42:56 +08:00
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
import photoAccessHelper from '@ohos.file.photoAccessHelper'
|
|
|
|
|
|
import dataSharePredicates from '@ohos.data.dataSharePredicates'
|
2025-01-07 15:50:48 +08:00
|
|
|
|
import fs from '@ohos.file.fs';
|
|
|
|
|
|
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
|
2024-05-09 13:42:56 +08:00
|
|
|
|
|
2025-01-07 15:50:48 +08:00
|
|
|
|
class FileHelper {
|
2024-05-09 13:42:56 +08:00
|
|
|
|
|
|
|
|
|
|
private userFileMgr: photoAccessHelper.PhotoAccessHelper = undefined;
|
2025-01-07 15:50:48 +08:00
|
|
|
|
private mediaLib: mediaLibrary.MediaLibrary = undefined;
|
|
|
|
|
|
|
|
|
|
|
|
private baseDir: string =""
|
2024-05-09 13:42:56 +08:00
|
|
|
|
FILE_ASSET_FETCH_COLUMNS = [photoAccessHelper.PhotoKeys.URI,
|
2025-01-07 15:50:48 +08:00
|
|
|
|
photoAccessHelper.PhotoKeys.PHOTO_TYPE,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DISPLAY_NAME,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.SIZE,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DATE_ADDED,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DATE_MODIFIED,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DURATION,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.WIDTH,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.HEIGHT,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DATE_TAKEN,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.ORIENTATION,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.FAVORITE,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.TITLE,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.POSITION,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.DATE_TRASHED,
|
|
|
|
|
|
photoAccessHelper.PhotoKeys.HIDDEN
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2024-05-09 13:42:56 +08:00
|
|
|
|
constructor() {
|
2025-01-07 15:50:48 +08:00
|
|
|
|
this.mediaLib = mediaLibrary.getMediaLibrary(globalThis.context);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
this.userFileMgr = photoAccessHelper.getPhotoAccessHelper(globalThis.context);
|
2025-01-07 15:50:48 +08:00
|
|
|
|
this.baseDir = AppStorage.Get('sanBoxFileDir') + '/TextDir';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async queryFile(displayName): Promise<mediaLibrary.FileAsset> {
|
|
|
|
|
|
var ret ;
|
|
|
|
|
|
console.info(` baoyihu queryFile displayName=${displayName}`);
|
|
|
|
|
|
let fetchOp = {
|
|
|
|
|
|
selections: `media_type=? AND display_name = ?`,
|
|
|
|
|
|
selectionArgs: [`${mediaLibrary.MediaType.AUDIO}`,displayName],
|
|
|
|
|
|
};
|
|
|
|
|
|
console.log( 'baoyihu queryFile selections: '+fetchOp.selections +" args:" + fetchOp.selectionArgs);
|
|
|
|
|
|
let fileResult = await this.mediaLib.getFileAssets(fetchOp);
|
|
|
|
|
|
let retCount = fileResult.getCount();
|
|
|
|
|
|
console.log( 'baoyihu queryFile count: '+retCount );
|
|
|
|
|
|
if (retCount > 0) {
|
|
|
|
|
|
ret= fileResult.getFirstObject();
|
|
|
|
|
|
console.log( 'baoyihu queryFile success ' );
|
|
|
|
|
|
}
|
|
|
|
|
|
return Promise.resolve(ret);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-07 15:50:48 +08:00
|
|
|
|
|
2024-05-09 13:42:56 +08:00
|
|
|
|
public async queryPhotoByDisplayName(displayName)
|
|
|
|
|
|
{
|
|
|
|
|
|
console.log( 'baoyihu queryPhotoByDisplayName begin DISPLAY_NAME:' +displayName );
|
|
|
|
|
|
try {
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, displayName)
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
let assetsResult = await this.userFileMgr.getAssets(fetchOptions);
|
|
|
|
|
|
|
|
|
|
|
|
let retCount = assetsResult.getCount();
|
|
|
|
|
|
console.log( 'baoyihu queryPhotoByDisplayName count: '+retCount );
|
|
|
|
|
|
if (retCount > 0) {
|
|
|
|
|
|
let asset = await assetsResult.getFirstObject();
|
|
|
|
|
|
console.log( 'baoyihu queryPhotoByDisplayName one asset uri : ' + asset.uri +', photoType : '+asset.photoType+', displayName : '+asset.displayName);
|
|
|
|
|
|
console.log( 'baoyihu queryPhotoByDisplayName success ' );
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'baoyihu queryPhotoByDisplayName failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async getUserAlbumItemByDisplayName(displayName: string): Promise<photoAccessHelper.Album> {
|
2025-01-07 15:50:48 +08:00
|
|
|
|
let fetchResult = null
|
2024-05-09 13:42:56 +08:00
|
|
|
|
let album: photoAccessHelper.Album = null
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log( 'getUserAlbumItemByDisplayName');
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, displayName)
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: [],
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
fetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions );
|
|
|
|
|
|
console.log( 'get getUserAlbumItemByDisplayName, count: ' + fetchResult.getCount());
|
|
|
|
|
|
if (fetchResult.getCount() > 0) {
|
|
|
|
|
|
album = await fetchResult.getFirstObject();
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'get Album fetchResult failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (fetchResult != null) {
|
|
|
|
|
|
fetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return album;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async addAssetToAlbum(albumName,file_uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
console.log( 'baoyihu addAssetToAlbum begin albumName '+albumName+', file_uri:' +file_uri);
|
|
|
|
|
|
try {
|
|
|
|
|
|
let album = await this.getUserAlbumItemByDisplayName(albumName);
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
predicates.equalTo(photoAccessHelper.PhotoKeys.URI, file_uri)
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
let assetsResult = await this.userFileMgr.getAssets(fetchOptions);
|
|
|
|
|
|
|
|
|
|
|
|
if (assetsResult.getCount() > 0) {
|
|
|
|
|
|
let asset = await assetsResult.getFirstObject();
|
|
|
|
|
|
console.log( 'baoyihu addAssetToAlbum one asset uri : ' + asset.uri +', photoType : '+asset.photoType+', displayName : '+asset.displayName);
|
|
|
|
|
|
await album.addAssets([asset]);
|
|
|
|
|
|
console.log( 'baoyihu addAssetToAlbum success ' );
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'baoyihu addAssetToAlbum failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-07 15:50:48 +08:00
|
|
|
|
private savedPictureFile:string[] = [];
|
|
|
|
|
|
private savedVideoFile:string[] = [];
|
|
|
|
|
|
//1 是图片,2 是视频
|
|
|
|
|
|
public addFile(fileName,file_type)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(file_type==1) {
|
|
|
|
|
|
this.savedPictureFile.push(fileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(file_type==2)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.savedVideoFile.push(fileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async trashPictureFiles(all_fileAsset)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
let albumFetchResult = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
albumFetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
|
|
|
|
|
|
|
|
|
|
|
|
let trashAlbum = await albumFetchResult.getFirstObject();
|
|
|
|
|
|
trashAlbum.deleteAssets(all_fileAsset).then(() => {
|
|
|
|
|
|
console.log('baoyihu deletePictureOfAlbum trash ok : ');
|
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
|
console.log('baoyihu deletePictureOfAlbum trash faild : ');
|
|
|
|
|
|
});
|
|
|
|
|
|
}catch (err) {
|
|
|
|
|
|
console.log('baoyihu deletePictureOfAlbum error: '+err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (albumFetchResult != null) {
|
|
|
|
|
|
albumFetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log( 'baoyihu deletePictureOfAlbum delete end' );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-09 13:42:56 +08:00
|
|
|
|
|
|
|
|
|
|
public async deleteFileOfAlbum(album_Name,file_type): Promise<void> {
|
|
|
|
|
|
console.log('baoyihu deletePictureOfAlbum album_Name:'+album_Name);
|
2025-01-07 15:50:48 +08:00
|
|
|
|
var fileMap =[]
|
|
|
|
|
|
if(file_type==1)
|
|
|
|
|
|
{
|
|
|
|
|
|
fileMap = this.savedPictureFile.slice()
|
|
|
|
|
|
this.savedPictureFile.length = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
fileMap = this.savedVideoFile.slice()
|
|
|
|
|
|
this.savedVideoFile.length = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-09 13:42:56 +08:00
|
|
|
|
var photoFetchResult =null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
let album = await this.getUserAlbumItemByName(album_Name);
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
predicates.equalTo(photoAccessHelper.PhotoKeys.PHOTO_TYPE, file_type)
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
photoFetchResult = await album.getAssets(fetchOptions)
|
|
|
|
|
|
var all_fileAsset = await photoFetchResult.getAllObjects();
|
|
|
|
|
|
var uri_array=[]
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log( 'baoyihu deletePictureOfAlbum iterator begin' +fileMap.length);
|
|
|
|
|
|
// for (let onfile of all_fileAsset)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// console.log( 'baoyihu deletePictureOfAlbum uri : ' + onfile.uri +', photoType : '+onfile.photoType+', displayName : '+onfile.displayName);
|
|
|
|
|
|
// if(fileMap.indexOf(onfile.displayName)>-1)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// console.log( 'baoyihu deletePictureOfAlbum uri find one: ' + onfile.uri +', photoType : '+onfile.photoType+', displayName : '+onfile.displayName);
|
|
|
|
|
|
// uri_array.push(onfile.uri);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// await this.userFileMgr.deleteAssets(uri_array);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
|
|
|
|
|
|
let albumFetchResult = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
albumFetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
|
|
|
|
|
|
|
|
|
|
|
|
let trashAlbum = await albumFetchResult.getFirstObject();
|
|
|
|
|
|
trashAlbum.deleteAssets(all_fileAsset).then(() => {
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log('baoyihu deletePictureOfAlbum trash ok : ');
|
2024-05-09 13:42:56 +08:00
|
|
|
|
}).catch((err) => {
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log('baoyihu deletePictureOfAlbum trash faild : ');
|
2024-05-09 13:42:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
}catch (err) {
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log('baoyihu deletePictureOfAlbum error: '+err);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
if (albumFetchResult != null) {
|
|
|
|
|
|
albumFetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log( 'baoyihu deletePictureOfAlbum delete end' );
|
2024-05-09 13:42:56 +08:00
|
|
|
|
} catch (err) {
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log( 'baoyihu deletePictureOfAlbum failed with err: ' + err);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
if (photoFetchResult != null) {
|
|
|
|
|
|
photoFetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-07 15:50:48 +08:00
|
|
|
|
public async deleteAllPictures(file_type): Promise<void> {
|
|
|
|
|
|
console.log('baoyihu deleteAllPictures enter');
|
|
|
|
|
|
var fileMap =[]
|
|
|
|
|
|
if(file_type==1)
|
|
|
|
|
|
{
|
|
|
|
|
|
fileMap = this.savedPictureFile.slice()
|
|
|
|
|
|
this.savedPictureFile.length = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
fileMap = this.savedVideoFile.slice()
|
|
|
|
|
|
this.savedVideoFile.length = 0;
|
|
|
|
|
|
}
|
2024-05-09 13:42:56 +08:00
|
|
|
|
let photoFetchResult = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
photoFetchResult = await this.userFileMgr.getAssets(fetchOptions);
|
|
|
|
|
|
|
|
|
|
|
|
var all_fileAsset = await photoFetchResult.getAllObjects();
|
|
|
|
|
|
var uri_array=[]
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log( 'baoyihu deleteAllPictures batch delete begin fileMap.length' +fileMap.length);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
for (let onfile of all_fileAsset)
|
|
|
|
|
|
{
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log( 'baoyihu deleteAllPictures find one uri : ' + onfile.uri +', photoType : '+onfile.photoType+', displayName : '+onfile.displayName +', file_size: '+onfile.size);
|
|
|
|
|
|
|
|
|
|
|
|
if(fileMap.indexOf(onfile.displayName)>-1)
|
|
|
|
|
|
{
|
|
|
|
|
|
console.log( 'baoyihu deleteAllPictures uri find one: ' + onfile.uri +', photoType : '+onfile.photoType+', displayName : '+onfile.displayName);
|
|
|
|
|
|
uri_array.push(onfile.uri);
|
|
|
|
|
|
}
|
2024-05-09 13:42:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
// await album.removeAssets(all_fileAsset);
|
|
|
|
|
|
await this.userFileMgr.deleteAssets(uri_array);
|
2025-01-07 15:50:48 +08:00
|
|
|
|
|
|
|
|
|
|
await this.trashPictureFiles(all_fileAsset);
|
|
|
|
|
|
console.log( 'baoyihu deleteAllPictures batch delete end' );
|
2024-05-09 13:42:56 +08:00
|
|
|
|
} catch (err) {
|
2025-01-07 15:50:48 +08:00
|
|
|
|
console.log( 'baoyihu deleteAllPictures failed with err: ' + err);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
if (photoFetchResult != null) {
|
|
|
|
|
|
photoFetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//
|
|
|
|
|
|
// async getUserAlbumItemByUri(uri: string): Promise<photoAccessHelper.Album> {
|
|
|
|
|
|
// let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null
|
|
|
|
|
|
// let album: photoAccessHelper.Album = null
|
|
|
|
|
|
// try {
|
|
|
|
|
|
// console.log( 'getUserAlbumItemByUri');
|
|
|
|
|
|
// let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
// predicates.equalTo(photoAccessHelper.AlbumKeys.URI, uri)
|
|
|
|
|
|
// let fetchOptions = {
|
|
|
|
|
|
// fetchColumns: [],
|
|
|
|
|
|
// predicates: predicates
|
|
|
|
|
|
// };
|
|
|
|
|
|
// fetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions );
|
|
|
|
|
|
// console.log( 'get Album fetchResult, count: ' + fetchResult.getCount());
|
|
|
|
|
|
// if (fetchResult.getCount() > 0) {
|
|
|
|
|
|
// album = await fetchResult.getFirstObject();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
|
// console.log( 'get Album fetchResult failed with err: ' + err);
|
|
|
|
|
|
// } finally {
|
|
|
|
|
|
// if (fetchResult != null) {
|
|
|
|
|
|
// fetchResult.close();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return album;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
async getUserAlbumItemByName(albumName: string): Promise<photoAccessHelper.Album> {
|
2025-01-07 15:50:48 +08:00
|
|
|
|
let fetchResult= null
|
2024-05-09 13:42:56 +08:00
|
|
|
|
let album: photoAccessHelper.Album = null
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log( 'getUserAlbumItemByName');
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, albumName)
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: [],
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
fetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions );
|
|
|
|
|
|
console.log( 'get getUserAlbumItemByName, count: ' + fetchResult.getCount());
|
|
|
|
|
|
if (fetchResult.getCount() > 0) {
|
|
|
|
|
|
album = await fetchResult.getFirstObject();
|
|
|
|
|
|
console.log( 'getUserAlbumItemByName uri: ' + album.albumUri+',albumName: ' + album.albumName);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'getUserAlbumItemByName failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (fetchResult != null) {
|
|
|
|
|
|
fetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return album;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async createAlbum(albumName):Promise<string>
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log( "baoyihu createAlbum beging");
|
|
|
|
|
|
let albumAsset = await this.userFileMgr.createAlbum(albumName);
|
|
|
|
|
|
console.log( 'baoyihu createAlbum success, albumType: ' + albumAsset.albumType +', albumSubtype: '+ albumAsset.albumSubtype
|
2025-01-07 15:50:48 +08:00
|
|
|
|
+', albumName: '+ albumAsset.albumName +', albumUri: '+ albumAsset.albumUri +', coverUri: ' + albumAsset.coverUri);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
return albumAsset.albumUri ;
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'baoyihu createAlbum failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async createPhotoAsset(type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype ): Promise<void>
|
|
|
|
|
|
{
|
|
|
|
|
|
console.log( 'baoyihu createPhotoAsset enter ');
|
|
|
|
|
|
try {
|
|
|
|
|
|
let createOptions = { subtype: photoAccessHelper.PhotoSubtype.SCREENSHOT};
|
|
|
|
|
|
await this.userFileMgr.createAsset("picture2.jpg", createOptions,(err,photoResult)=>{
|
|
|
|
|
|
console.log( 'baoyihu createPhotoAsset return uri: ' + photoResult.uri +', photoType: '+ photoResult.photoType
|
2025-01-07 15:50:48 +08:00
|
|
|
|
+', displayName: '+ photoResult.displayName );
|
2024-05-09 13:42:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'baoyihu createPhotoAsset failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async queryAlbum(type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype ):Promise<string>
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
let firstUri = "";
|
|
|
|
|
|
let fetchResult:photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
fetchResult = await this.userFileMgr.getAlbums(type, subType);
|
|
|
|
|
|
console.log( 'baoyihu queryAlbum success count: ' + fetchResult.getCount());
|
|
|
|
|
|
for (let i = 0; i < fetchResult.getCount(); i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
let albumAsset:photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
|
|
|
|
|
|
console.log( 'queryAlbum albumType: ' + albumAsset.albumType +', Subtype: '+ albumAsset.albumSubtype
|
2025-01-07 15:50:48 +08:00
|
|
|
|
+', Name: '+ albumAsset.albumName +', Uri: '+ albumAsset.albumUri +', coverUri: '+ albumAsset.coverUri);
|
2024-05-09 13:42:56 +08:00
|
|
|
|
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
let photoFetchResult = await albumAsset.getAssets(fetchOptions);
|
|
|
|
|
|
let count = photoFetchResult.getCount();
|
|
|
|
|
|
console.log( 'baoyihu queryAlbum photoFetchResult count: ' + count);
|
|
|
|
|
|
var all_fileAsset = await photoFetchResult.getAllObjects();
|
|
|
|
|
|
// var uri_array=[]
|
|
|
|
|
|
|
|
|
|
|
|
for (let onfile of all_fileAsset)
|
|
|
|
|
|
{
|
|
|
|
|
|
console.log( 'baoyihu queryAlbum one uri : ' + onfile.uri);
|
|
|
|
|
|
firstUri = onfile.uri;
|
|
|
|
|
|
// uri_array.push(onfile.uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log( 'baoyihu queryAlbum return asser:' + firstUri);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'baoyihu queryAlbum failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (fetchResult != null) {
|
|
|
|
|
|
fetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
console.log( 'baoyihu queryAlbum before resolve:' + firstUri);
|
|
|
|
|
|
resolve(firstUri)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-01-07 15:50:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-09 13:42:56 +08:00
|
|
|
|
public async deleteAllVideos(type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype ): Promise<void> {
|
|
|
|
|
|
let fetchResult:photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
fetchResult = await this.userFileMgr.getAlbums(type, subType);
|
|
|
|
|
|
console.log( 'baoyihu_ get Album fetchResult, count: ' + fetchResult.getCount());
|
|
|
|
|
|
for (let i = 0; i < fetchResult.getCount(); i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
let albumAsset:photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
|
|
|
|
|
|
let photoFetchResult:photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
|
|
|
|
|
|
let count = 0;
|
|
|
|
|
|
console.log( 'baoyihu_ get one Album name : ' + albumAsset.albumName);
|
|
|
|
|
|
try {
|
|
|
|
|
|
let predicates = new dataSharePredicates.DataSharePredicates();
|
|
|
|
|
|
let fetchOptions = {
|
|
|
|
|
|
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
|
|
|
|
|
predicates: predicates
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
photoFetchResult = await albumAsset.getAssets(fetchOptions);
|
|
|
|
|
|
count = photoFetchResult.getCount();
|
|
|
|
|
|
console.log( 'baoyihu_ photoFetchResult count: ' + count);
|
|
|
|
|
|
var all_fileAsset = await photoFetchResult.getAllObjects();
|
|
|
|
|
|
/*
|
|
|
|
|
|
var uri_array=[]
|
|
|
|
|
|
console.log( 'baoyihu batch delete begin' );
|
|
|
|
|
|
for (let onfile of all_fileAsset)
|
|
|
|
|
|
{
|
|
|
|
|
|
console.log( 'baoyihu push one uri : ' + onfile.uri);
|
|
|
|
|
|
uri_array.push(onfile.uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
* */
|
|
|
|
|
|
await albumAsset.removeAssets(all_fileAsset);
|
|
|
|
|
|
//await this.userFileMgr.deleteAssets(uri_array);
|
|
|
|
|
|
|
|
|
|
|
|
console.log( 'baoyihu_ batch delete end' );
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'baoyihu_ get Album getPhotoAssets failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (photoFetchResult != null) {
|
|
|
|
|
|
photoFetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log( 'baoyihu_ get Album fetchResult failed with err: ' + err);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (fetchResult != null) {
|
|
|
|
|
|
fetchResult.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-07 15:50:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public isPrime(number):boolean {
|
|
|
|
|
|
if (number < 2) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
for (let i = 2; i * i <= number; i++) {
|
|
|
|
|
|
if (number % i === 0) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public findPrimes(start, end):void
|
|
|
|
|
|
{
|
|
|
|
|
|
console.log(`baoyihu find Prime begin in the range ${start} to ${end} are:`);
|
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
for (let number = start; number <= end; number++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (this.isPrime(number))
|
|
|
|
|
|
{
|
|
|
|
|
|
count++;
|
|
|
|
|
|
//console.log("baoyihu find Number:"+number);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log("baoyihu find Prime count:"+count);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async createRandomFile(fileName): Promise<void> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log("baoyihu createRandomFile enter baseDir:"+this.baseDir);
|
|
|
|
|
|
if (!fs.accessSync(this.baseDir))
|
|
|
|
|
|
{
|
|
|
|
|
|
fs.mkdirSync(this.baseDir);
|
|
|
|
|
|
}
|
|
|
|
|
|
let depth = this.baseDir;
|
|
|
|
|
|
let myFile = depth + `/`+fileName;
|
|
|
|
|
|
console.log("baoyihu createRandomFile begin fileName:"+fileName+", filePath:"+myFile);
|
|
|
|
|
|
let file = fs.openSync(myFile, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
|
|
|
|
|
|
let str: string = "hello, world"+"\n"+"hello,openharmony";
|
|
|
|
|
|
let writeLen = fs.writeSync(file.fd, str);
|
|
|
|
|
|
fs.closeSync(file);
|
|
|
|
|
|
console.log("baoyihu createRandomFile success:"+file);
|
|
|
|
|
|
} catch (e)
|
|
|
|
|
|
{
|
|
|
|
|
|
console.log("baoyihu createRandomFile failed:"+e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-05-09 13:42:56 +08:00
|
|
|
|
}
|
2025-01-07 15:50:48 +08:00
|
|
|
|
const fileHelper = new FileHelper();
|
|
|
|
|
|
export default fileHelper ;
|