379 lines
16 KiB
Plaintext
379 lines
16 KiB
Plaintext
import photoAccessHelper from '@ohos.file.photoAccessHelper';
|
||
import dataSharePredicates from '@ohos.data.dataSharePredicates';
|
||
import common from '@ohos.app.ability.common';
|
||
import { BusinessError } from '@ohos.base';
|
||
import { FileHelpTag } from '../config';
|
||
|
||
export class FileHelper {
|
||
FILE_ASSET_FETCH_COLUMNS = [
|
||
photoAccessHelper.PhotoKeys.URI,
|
||
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
|
||
];
|
||
private userFileMgr!: photoAccessHelper.PhotoAccessHelper
|
||
|
||
constructor(context: common.UIAbilityContext) {
|
||
this.userFileMgr = photoAccessHelper.getPhotoAccessHelper(context);
|
||
}
|
||
|
||
public async queryPhotoByDisplayName(displayName: string) {
|
||
console.log(FileHelpTag, ' queryPhotoByDisplayName begin DISPLAY_NAME:' + displayName);
|
||
try {
|
||
let predicates = new dataSharePredicates.DataSharePredicates();
|
||
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, displayName)
|
||
let fetchOptions: photoAccessHelper.FetchOptions = {
|
||
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
||
predicates: predicates
|
||
};
|
||
let assetsResult = await this.userFileMgr.getAssets(fetchOptions);
|
||
|
||
let retCount = assetsResult.getCount();
|
||
console.log(FileHelpTag, ' queryPhotoByDisplayName count: ' + retCount);
|
||
if (retCount > 0) {
|
||
let asset = await assetsResult.getFirstObject();
|
||
console.log(FileHelpTag, ' queryPhotoByDisplayName one asset uri : ' + asset.uri + ', photoType : ' +
|
||
asset.photoType + ', displayName : ' + asset.displayName);
|
||
console.log(FileHelpTag, ' queryPhotoByDisplayName success ');
|
||
}
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' queryPhotoByDisplayName failed with err: ' + err);
|
||
} finally {
|
||
}
|
||
}
|
||
|
||
async getUserAlbumItemByDisplayName(displayName: string): Promise<photoAccessHelper.Album | undefined> {
|
||
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined
|
||
let album: photoAccessHelper.Album | undefined = undefined
|
||
try {
|
||
console.log(FileHelpTag, 'getUserAlbumItemByDisplayName');
|
||
let predicates = new dataSharePredicates.DataSharePredicates();
|
||
predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, displayName)
|
||
let fetchOptions: photoAccessHelper.FetchOptions = {
|
||
fetchColumns: [],
|
||
predicates: predicates
|
||
};
|
||
fetchResult =
|
||
await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC,
|
||
fetchOptions);
|
||
console.log(FileHelpTag, 'get getUserAlbumItemByDisplayName, count: ' + fetchResult.getCount());
|
||
if (fetchResult.getCount() > 0) {
|
||
album = await fetchResult.getFirstObject();
|
||
}
|
||
} catch (err) {
|
||
console.error(FileHelpTag, 'get Album fetchResult failed with err: ' + err);
|
||
} finally {
|
||
if (fetchResult !== undefined) {
|
||
fetchResult.close();
|
||
}
|
||
}
|
||
return album;
|
||
}
|
||
|
||
public async addAssetToAlbum(albumName: string, file_uri: string) {
|
||
console.log(FileHelpTag, ' 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: photoAccessHelper.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(FileHelpTag, ' addAssetToAlbum one asset uri : ' + asset.uri + ', photoType : ' + asset.photoType +
|
||
', displayName : ' + asset.displayName);
|
||
await album?.addAssets([asset]);
|
||
console.log(FileHelpTag, ' addAssetToAlbum success ');
|
||
}
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' addAssetToAlbum failed with err: ' + err);
|
||
} finally {
|
||
}
|
||
}
|
||
|
||
public async deleteFileOfAlbum(album_Name: string, file_type: number | string | boolean): Promise<void> {
|
||
console.log(FileHelpTag, ' deletePictureOfAlbum album_Name:' + album_Name);
|
||
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> | undefined = undefined;
|
||
try {
|
||
let album = await this.getUserAlbumItemByName(album_Name);
|
||
let predicates = new dataSharePredicates.DataSharePredicates();
|
||
predicates.equalTo(photoAccessHelper.PhotoKeys.PHOTO_TYPE, file_type)
|
||
let fetchOptions: photoAccessHelper.FetchOptions = {
|
||
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
||
predicates: predicates
|
||
};
|
||
photoFetchResult = await album?.getAssets(fetchOptions)
|
||
let all_fileAsset = await photoFetchResult?.getAllObjects();
|
||
|
||
let uri_array: string[] = []
|
||
console.log(FileHelpTag, ' deletePictureOfAlbum iterator begin', album_Name);
|
||
for (let onfile of all_fileAsset!) {
|
||
console.log(FileHelpTag, album_Name,
|
||
' deletePictureOfAlbum uri : ' + onfile.uri + ', photoType : ' + onfile.photoType +
|
||
', displayName : ' + onfile.displayName);
|
||
uri_array.push(onfile.uri);
|
||
}
|
||
await this.userFileMgr.deleteAssets(uri_array);
|
||
|
||
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined;
|
||
try {
|
||
albumFetchResult =
|
||
await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
|
||
|
||
let trashAlbum = await albumFetchResult.getFirstObject();
|
||
trashAlbum.deleteAssets(all_fileAsset).then(() => {
|
||
console.log(FileHelpTag, ' deletePictureOfAlbum trash ok : ', album_Name);
|
||
}).catch((err: BusinessError) => {
|
||
console.error(FileHelpTag, ' deletePictureOfAlbum trash faild : ', album_Name, JSON.stringify(err));
|
||
});
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' deletePictureOfAlbum error: ' + err, album_Name);
|
||
} finally {
|
||
if (albumFetchResult != null) {
|
||
albumFetchResult.close();
|
||
}
|
||
}
|
||
|
||
console.log(FileHelpTag, ' deletePictureOfAlbum delete end', album_Name);
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' deletePictureOfAlbum failed with err: ' + err, album_Name);
|
||
} finally {
|
||
if (photoFetchResult != null) {
|
||
photoFetchResult.close();
|
||
}
|
||
}
|
||
}
|
||
|
||
public async deleteAllPictures(): Promise<void> {
|
||
console.log(FileHelpTag, ' deleteAllPictures');
|
||
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> | undefined = undefined;
|
||
try {
|
||
let predicates = new dataSharePredicates.DataSharePredicates();
|
||
let fetchOptions: photoAccessHelper.FetchOptions = {
|
||
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
||
predicates: predicates
|
||
};
|
||
photoFetchResult = await this.userFileMgr.getAssets(fetchOptions);
|
||
|
||
let all_fileAsset = await photoFetchResult.getAllObjects();
|
||
let uri_array: string[] = []
|
||
console.log(FileHelpTag, ' batch delete begin');
|
||
for (let onfile of all_fileAsset) {
|
||
uri_array.push(onfile.uri);
|
||
}
|
||
// await album.removeAssets(all_fileAsset);
|
||
await this.userFileMgr.deleteAssets(uri_array);
|
||
console.log(FileHelpTag, ' batch delete end');
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' get Album getPhotoAssets failed with err: ' + err);
|
||
} 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 | undefined> {
|
||
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined
|
||
let album: photoAccessHelper.Album | undefined = undefined
|
||
try {
|
||
console.log(FileHelpTag, 'getUserAlbumItemByName');
|
||
let predicates = new dataSharePredicates.DataSharePredicates();
|
||
predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, albumName)
|
||
let fetchOptions: photoAccessHelper.FetchOptions = {
|
||
fetchColumns: [],
|
||
predicates: predicates
|
||
};
|
||
fetchResult =
|
||
await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC,
|
||
fetchOptions);
|
||
console.log(FileHelpTag, 'get getUserAlbumItemByName, count: ' + fetchResult.getCount());
|
||
if (fetchResult.getCount() > 0) {
|
||
album = await fetchResult.getFirstObject();
|
||
console.log(FileHelpTag, 'getUserAlbumItemByName uri: ' + album.albumUri + ',albumName: ' + album.albumName);
|
||
}
|
||
} catch (err) {
|
||
console.error(FileHelpTag, 'getUserAlbumItemByName failed with err: ' + err);
|
||
} finally {
|
||
if (fetchResult != null) {
|
||
fetchResult.close();
|
||
}
|
||
}
|
||
return album;
|
||
}
|
||
|
||
|
||
public async createAlbum(albumName: string): Promise<string | undefined> {
|
||
try {
|
||
console.log(FileHelpTag, " createAlbum beging");
|
||
let albumAsset = await this.userFileMgr.createAlbum(albumName);
|
||
console.log(FileHelpTag, ' createAlbum success, albumType: ' + albumAsset.albumType + ', albumSubtype: ' +
|
||
albumAsset.albumSubtype
|
||
+ ', albumName: ' + albumAsset.albumName + ', albumUri: ' + albumAsset.albumUri + ', coverUri: ' +
|
||
albumAsset.coverUri);
|
||
return albumAsset.albumUri;
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' createAlbum failed with err: ' + err);
|
||
return undefined
|
||
}
|
||
}
|
||
|
||
|
||
public async createPhotoAsset(type: photoAccessHelper.AlbumType,
|
||
subType: photoAccessHelper.AlbumSubtype): Promise<void> {
|
||
console.log(FileHelpTag, ' createPhotoAsset enter ');
|
||
try {
|
||
let createOptions: photoAccessHelper.PhotoCreateOptions = { subtype: photoAccessHelper.PhotoSubtype.SCREENSHOT };
|
||
await this.userFileMgr.createAsset("picture2.jpg", createOptions, (err, photoResult) => {
|
||
console.log('baoyihu createPhotoAsset return uri: ' + photoResult.uri + ', photoType: ' + photoResult.photoType
|
||
+ ', displayName: ' + photoResult.displayName);
|
||
});
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' 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> | undefined = undefined;
|
||
try {
|
||
fetchResult = await this.userFileMgr.getAlbums(type, subType);
|
||
console.log(FileHelpTag, ' queryAlbum success count: ' + fetchResult.getCount());
|
||
for (let i = 0; i < fetchResult.getCount(); i++) {
|
||
let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
|
||
console.log(FileHelpTag,
|
||
'queryAlbum albumType: ' + albumAsset.albumType + ', Subtype: ' + albumAsset.albumSubtype
|
||
+ ', Name: ' + albumAsset.albumName + ', Uri: ' + albumAsset.albumUri + ', coverUri: ' +
|
||
albumAsset.coverUri);
|
||
|
||
let predicates = new dataSharePredicates.DataSharePredicates();
|
||
let fetchOptions: photoAccessHelper.FetchOptions = {
|
||
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
||
predicates: predicates
|
||
};
|
||
let photoFetchResult = await albumAsset.getAssets(fetchOptions);
|
||
let count = photoFetchResult.getCount();
|
||
console.log(FileHelpTag, ' queryAlbum photoFetchResult count: ' + count);
|
||
let all_fileAsset = await photoFetchResult.getAllObjects();
|
||
// var uri_array=[]
|
||
|
||
for (let onfile of all_fileAsset) {
|
||
console.log(FileHelpTag, ' queryAlbum one uri : ' + onfile.uri);
|
||
firstUri = onfile.uri;
|
||
// uri_array.push(onfile.uri);
|
||
}
|
||
|
||
}
|
||
console.log(FileHelpTag, ' queryAlbum return asser:' + firstUri);
|
||
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' queryAlbum failed with err: ' + err);
|
||
} finally {
|
||
if (fetchResult != null) {
|
||
fetchResult.close();
|
||
}
|
||
}
|
||
return new Promise((resolve, reject) => {
|
||
console.log(FileHelpTag, ' queryAlbum before resolve:' + firstUri);
|
||
resolve(firstUri)
|
||
})
|
||
|
||
}
|
||
|
||
public async deleteAllVideos(type: photoAccessHelper.AlbumType,
|
||
subType: photoAccessHelper.AlbumSubtype): Promise<void> {
|
||
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined;
|
||
try {
|
||
fetchResult = await this.userFileMgr.getAlbums(type, subType);
|
||
console.log(FileHelpTag, ' 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> | undefined = undefined;
|
||
let count = 0;
|
||
console.log(FileHelpTag, ' get one Album name : ' + albumAsset.albumName);
|
||
try {
|
||
let predicates = new dataSharePredicates.DataSharePredicates();
|
||
let fetchOptions: photoAccessHelper.FetchOptions = {
|
||
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
|
||
predicates: predicates
|
||
};
|
||
|
||
photoFetchResult = await albumAsset.getAssets(fetchOptions);
|
||
count = photoFetchResult.getCount();
|
||
console.log(FileHelpTag, ' photoFetchResult count: ' + count);
|
||
let 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(FileHelpTag, ' batch delete end');
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' get Album getPhotoAssets failed with err: ' + err);
|
||
} finally {
|
||
if (photoFetchResult != null) {
|
||
photoFetchResult.close();
|
||
}
|
||
}
|
||
}
|
||
} catch (err) {
|
||
console.error(FileHelpTag, ' get Album fetchResult failed with err: ' + err);
|
||
} finally {
|
||
if (fetchResult != null) {
|
||
fetchResult.close();
|
||
}
|
||
}
|
||
}
|
||
} |