• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #include "photo_accesshelper_ffi.h"
17 #include "photo_asset_helper.h"
18 
19 using namespace OHOS::FFI;
20 
21 namespace OHOS {
22 namespace Media {
23 extern "C" {
24 
FfiPhotoAssetGetFileDisplayName(int64_t id)25     char* FfiPhotoAssetGetFileDisplayName(int64_t id)
26     {
27         auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(id);
28         if (photoAssetImpl == nullptr) {
29             LOGE("Invalid object PhotoAssetImpl");
30             return nullptr;
31         }
32         auto fileDisplayName = photoAssetImpl->GetFileDisplayName();
33         return MallocCString(fileDisplayName);
34     }
35 
FfiPhotoAssetGetFileUri(int64_t id)36     char* FfiPhotoAssetGetFileUri(int64_t id)
37     {
38         auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(id);
39         if (photoAssetImpl == nullptr) {
40             LOGE("Invalid object PhotoAssetImpl");
41             return nullptr;
42         }
43         auto fileUri = photoAssetImpl->GetFileUri();
44         return MallocCString(fileUri);
45     }
46 
FfiPhotoAssetGetMediaType(int64_t id)47     int32_t FfiPhotoAssetGetMediaType(int64_t id)
48     {
49         auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(id);
50         if (photoAssetImpl == nullptr) {
51             LOGE("Invalid object PhotoAssetImpl");
52             return 0;
53         }
54         return static_cast<int32_t>(photoAssetImpl->GetMediaType());
55     }
56 
FfiPhotoAssetUserFileMgrGet(int64_t id,char * member,int32_t * errCode)57     PhotoAssetMember FfiPhotoAssetUserFileMgrGet(int64_t id, char* member, int32_t *errCode)
58     {
59         PhotoAssetMember assetMember = {
60             .memberType = -1,
61             .stringValue = nullptr,
62             .boolValue = false
63         };
64         auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(id);
65         if (photoAssetImpl == nullptr) {
66             LOGE("Invalid object PhotoAssetImpl");
67             *errCode = JS_ERR_PARAMETER_INVALID;
68             return assetMember;
69         }
70         std::string inputKey(member);
71         return photoAssetImpl->UserFileMgrGet(inputKey, *errCode);
72     }
73 
FfiPhotoAssetUserFileMgrSet(int64_t id,char * member,char * data,int32_t * errCode)74     void FfiPhotoAssetUserFileMgrSet(int64_t id, char* member, char* data, int32_t* errCode)
75     {
76         auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(id);
77         if (photoAssetImpl == nullptr) {
78             LOGE("Invalid object PhotoAssetImpl");
79             *errCode = JS_ERR_PARAMETER_INVALID;
80             return;
81         }
82         std::string inputKey(member);
83         std::string value(data);
84         photoAssetImpl->UserFileMgrSet(inputKey, value, *errCode);
85     }
86 
FfiPhotoAssetCommitModify(int64_t id,int32_t * errCode)87     void FfiPhotoAssetCommitModify(int64_t id, int32_t* errCode)
88     {
89         auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(id);
90         if (photoAssetImpl == nullptr) {
91             LOGE("Invalid object PhotoAssetImpl");
92             *errCode = JS_ERR_PARAMETER_INVALID;
93             return;
94         }
95         photoAssetImpl->CommitModify(*errCode);
96     }
97 
FfiPhotoAssetGetThumbnail(int64_t id,CSize cSize,int32_t * errCode)98     int64_t FfiPhotoAssetGetThumbnail(int64_t id, CSize cSize, int32_t* errCode)
99     {
100         auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(id);
101         if (photoAssetImpl == nullptr) {
102             LOGE("Invalid object PhotoAssetImpl");
103             *errCode = JS_ERR_PARAMETER_INVALID;
104             return 0;
105         }
106         return photoAssetImpl->GetThumbnail(cSize, *errCode);
107     }
108 
FfiFetchResultGetCount(int64_t id,int32_t * errCode)109     int32_t FfiFetchResultGetCount(int64_t id, int32_t* errCode)
110     {
111         auto fetchResultImpl = FFIData::GetData<FetchResultImpl>(id);
112         if (fetchResultImpl == nullptr) {
113             LOGE("Invalid object FetchResultImpl");
114             *errCode = JS_ERR_PARAMETER_INVALID;
115             return 0;
116         }
117         return fetchResultImpl->GetCount(*errCode);
118     }
119 
FfiFetchResultIsAfterLast(int64_t id,int32_t * errCode)120     bool FfiFetchResultIsAfterLast(int64_t id, int32_t* errCode)
121     {
122         auto fetchResultImpl = FFIData::GetData<FetchResultImpl>(id);
123         if (fetchResultImpl == nullptr) {
124             LOGE("Invalid object FetchResultImpl");
125             *errCode = JS_ERR_PARAMETER_INVALID;
126             return false;
127         }
128         return fetchResultImpl->IsAfterLast(*errCode);
129     }
130 
FfiFetchResultClose(int64_t id)131     void FfiFetchResultClose(int64_t id)
132     {
133         auto fetchResultImpl = FFIData::GetData<FetchResultImpl>(id);
134         if (fetchResultImpl == nullptr) {
135             LOGE("Invalid object FetchResultImpl");
136             return;
137         }
138         fetchResultImpl->Close();
139     }
140 
FfiFetchResultGetFirstObject(int64_t id,int32_t * errCode)141     FetchResultObject FfiFetchResultGetFirstObject(int64_t id, int32_t* errCode)
142     {
143         FetchResultObject fetchResultObject = {
144             .id = -1,
145             .fetchResType = 0
146         };
147         auto fetchResultImpl = FFIData::GetData<FetchResultImpl>(id);
148         if (fetchResultImpl == nullptr) {
149             LOGE("Invalid object FetchResultImpl");
150             *errCode = JS_ERR_PARAMETER_INVALID;
151             return fetchResultObject;
152         }
153         return fetchResultImpl->GetFirstObject(*errCode);
154     }
155 
FfiFetchResultGetNextObject(int64_t id,int32_t * errCode)156     FetchResultObject FfiFetchResultGetNextObject(int64_t id, int32_t* errCode)
157     {
158         FetchResultObject fetchResultObject = {
159             .id = -1,
160             .fetchResType = 0
161         };
162         auto fetchResultImpl = FFIData::GetData<FetchResultImpl>(id);
163         if (fetchResultImpl == nullptr) {
164             LOGE("Invalid object FetchResultImpl");
165             *errCode = JS_ERR_PARAMETER_INVALID;
166             return fetchResultObject;
167         }
168         return fetchResultImpl->GetNextObject(*errCode);
169     }
170 
FfiFetchResultGetLastObject(int64_t id,int32_t * errCode)171     FetchResultObject FfiFetchResultGetLastObject(int64_t id, int32_t* errCode)
172     {
173         FetchResultObject fetchResultObject = {
174             .id = -1,
175             .fetchResType = 0
176         };
177         auto fetchResultImpl = FFIData::GetData<FetchResultImpl>(id);
178         if (fetchResultImpl == nullptr) {
179             LOGE("Invalid object FetchResultImpl");
180             *errCode = JS_ERR_PARAMETER_INVALID;
181             return fetchResultObject;
182         }
183         return fetchResultImpl->GetLastObject(*errCode);
184     }
185 
FfiFetchResultGetObjectAtPosition(int64_t id,int32_t position,int32_t * errCode)186     FetchResultObject FfiFetchResultGetObjectAtPosition(int64_t id, int32_t position, int32_t* errCode)
187     {
188         FetchResultObject fetchResultObject = {
189             .id = -1,
190             .fetchResType = 0
191         };
192         auto fetchResultImpl = FFIData::GetData<FetchResultImpl>(id);
193         if (fetchResultImpl == nullptr) {
194             LOGE("Invalid object FetchResultImpl");
195             *errCode = JS_ERR_PARAMETER_INVALID;
196             return fetchResultObject;
197         }
198         return fetchResultImpl->GetObjectAtPosition(position, *errCode);
199     }
200 
FfiFetchResultGetAllObjects(int64_t id,int32_t * errCode)201     CArrayFetchResultObject FfiFetchResultGetAllObjects(int64_t id, int32_t* errCode)
202     {
203         CArrayFetchResultObject cArrayFetchResultObject = {
204             .head = nullptr,
205             .size = 0
206         };
207         auto fetchResultImpl = FFIData::GetData<FetchResultImpl>(id);
208         if (fetchResultImpl == nullptr) {
209             LOGE("Invalid object FetchResultImpl");
210             *errCode = JS_ERR_PARAMETER_INVALID;
211             return cArrayFetchResultObject;
212         }
213         return fetchResultImpl->GetAllObjects(*errCode);
214     }
215 
FfiPhotoAlbumGetPhotoAlbumType(int64_t id)216     int32_t FfiPhotoAlbumGetPhotoAlbumType(int64_t id)
217     {
218         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
219         if (photoAlbumImpl == nullptr) {
220             LOGE("Invalid object photoAlbumImpl");
221             return 0;
222         }
223         return photoAlbumImpl->GetPhotoAlbumType();
224     }
225 
FfiPhotoAlbumGetPhotoAlbumSubType(int64_t id)226     int32_t FfiPhotoAlbumGetPhotoAlbumSubType(int64_t id)
227     {
228         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
229         if (photoAlbumImpl == nullptr) {
230             LOGE("Invalid object photoAlbumImpl");
231             return 0;
232         }
233         return photoAlbumImpl->GetPhotoAlbumSubType();
234     }
235 
FfiPhotoAlbumGetAlbumName(int64_t id)236     char* FfiPhotoAlbumGetAlbumName(int64_t id)
237     {
238         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
239         if (photoAlbumImpl == nullptr) {
240             LOGE("Invalid object photoAlbumImpl");
241             return nullptr;
242         }
243         auto albumName =  photoAlbumImpl->GetAlbumName();
244         char* cAlbumName = MallocCString(albumName);
245         return cAlbumName;
246     }
247 
FfiPhotoAlbumSetAlbumName(int64_t id,char * cAlbumName)248     void FfiPhotoAlbumSetAlbumName(int64_t id, char* cAlbumName)
249     {
250         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
251         if (photoAlbumImpl == nullptr) {
252             LOGE("Invalid object photoAlbumImpl");
253             return;
254         }
255         photoAlbumImpl->SetAlbumName(cAlbumName);
256     }
257 
FfiPhotoAlbumGetAlbumUri(int64_t id)258     char* FfiPhotoAlbumGetAlbumUri(int64_t id)
259     {
260         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
261         if (photoAlbumImpl == nullptr) {
262             LOGE("Invalid object photoAlbumImpl");
263             return nullptr;
264         }
265         auto albumUri = photoAlbumImpl->GetAlbumUri();
266         char* cAlbumUri = MallocCString(albumUri);
267         return cAlbumUri;
268     }
269 
FfiPhotoAlbumGetCount(int64_t id)270     int32_t FfiPhotoAlbumGetCount(int64_t id)
271     {
272         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
273         if (photoAlbumImpl == nullptr) {
274             LOGE("Invalid object photoAlbumImpl");
275             return 0;
276         }
277         return photoAlbumImpl->GetCount();
278     }
279 
FfiPhotoAlbumGetCoverUri(int64_t id)280     char* FfiPhotoAlbumGetCoverUri(int64_t id)
281     {
282         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
283         if (photoAlbumImpl == nullptr) {
284             LOGE("Invalid object photoAlbumImpl");
285             return nullptr;
286         }
287         auto coverUri = photoAlbumImpl->GetCoverUri();
288         char* cCoverUri = MallocCString(coverUri);
289         return cCoverUri;
290     }
291 
FfiPhotoAlbumGetImageCount(int64_t id)292     int32_t FfiPhotoAlbumGetImageCount(int64_t id)
293     {
294         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
295         if (photoAlbumImpl == nullptr) {
296             LOGE("Invalid object photoAlbumImpl");
297             return 0;
298         }
299         return photoAlbumImpl->GetImageCount();
300     }
301 
FfiPhotoAlbumGetVideoCount(int64_t id)302     int32_t FfiPhotoAlbumGetVideoCount(int64_t id)
303     {
304         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
305         if (photoAlbumImpl == nullptr) {
306             LOGE("Invalid object photoAlbumImpl");
307             return 0;
308         }
309         return photoAlbumImpl->GetVideoCount();
310     }
311 
FfiPhotoAlbumGetAssets(int64_t id,COptions options,int32_t * errCode)312     int64_t FfiPhotoAlbumGetAssets(int64_t id, COptions options, int32_t* errCode)
313     {
314         int64_t result = -1;
315         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
316         if (photoAlbumImpl == nullptr) {
317             LOGE("Invalid object photoAlbumImpl");
318             *errCode = OHOS_INVALID_PARAM_CODE;
319             return result;
320         }
321         auto fetchFileResult = photoAlbumImpl->GetAssets(options, *errCode);
322         if (fetchFileResult == nullptr) {
323             LOGE("GetAssets failed.");
324             return result;
325         }
326         std::unique_ptr<FfiFetchResultProperty> propertyPtr = std::make_unique<FfiFetchResultProperty>();
327         propertyPtr->fetchFileResult_ = fetchFileResult;
328         propertyPtr->fetchResType_ = FetchResType::TYPE_FILE;
329         auto native = FFIData::Create<FetchResultImpl>(std::move(propertyPtr));
330         if (native != nullptr) {
331             result = native->GetID();
332         } else {
333             LOGE("Create FetchResultImpl instance failed.");
334             *errCode = JS_INNER_FAIL;
335         }
336         return result;
337     }
338 
FfiPhotoAlbumCommitModify(int64_t id,int32_t * errCode)339     void FfiPhotoAlbumCommitModify(int64_t id, int32_t* errCode)
340     {
341         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
342         if (photoAlbumImpl == nullptr) {
343             LOGE("Invalid object photoAlbumImpl");
344             *errCode = OHOS_INVALID_PARAM_CODE;
345             return;
346         }
347         photoAlbumImpl->CommitModify(*errCode);
348     }
349 
FfiPhotoAccessHelperGetPhotoAccessHelper(int64_t id)350     int64_t FfiPhotoAccessHelperGetPhotoAccessHelper(int64_t id)
351     {
352         int64_t result = -1;
353         auto ret = FFIData::Create<PhotoAccessHelperImpl>();
354         if (!ret) {
355             LOGE("creat PhotoAccessHelperImpl instance failed.");
356             return result;
357         }
358         if (!ret->GetPhotoAccessHelper(id)) {
359             LOGE("GetPhotoAccessHelper failed.");
360             return result;
361         }
362         result = ret->GetID();
363         return result;
364     }
365 
FfiPhotoAccessHelperGetAssets(int64_t id,COptions options,int32_t * errCode)366     int64_t FfiPhotoAccessHelperGetAssets(int64_t id, COptions options, int32_t* errCode)
367     {
368         int64_t result = -1;
369         auto photoAccessHelperImpl = FFIData::GetData<PhotoAccessHelperImpl>(id);
370         if (photoAccessHelperImpl == nullptr) {
371             LOGE("Invalid object photoAccessHelperImpl");
372             *errCode = JS_ERR_PARAMETER_INVALID;
373             return result;
374         }
375         auto fetchFileResult = photoAccessHelperImpl->GetAssets(options, *errCode);
376         if (fetchFileResult == nullptr) {
377             LOGE("GetAssets failed.");
378             return result;
379         }
380         std::unique_ptr<FfiFetchResultProperty> propertyPtr = std::make_unique<FfiFetchResultProperty>();
381         propertyPtr->fetchFileResult_ = fetchFileResult;
382         propertyPtr->fetchResType_ = FetchResType::TYPE_FILE;
383         auto native = FFIData::Create<FetchResultImpl>(std::move(propertyPtr));
384         if (native != nullptr) {
385             result = native->GetID();
386         } else {
387             LOGE("Create FetchResultImpl instance failed.");
388             *errCode = JS_INNER_FAIL;
389         }
390         return result;
391     }
392 
FfiPhotoAccessHelperGetBurstAssets(int64_t id,char * cBurstKey,COptions options,int32_t * errCode)393     int64_t FfiPhotoAccessHelperGetBurstAssets(int64_t id, char* cBurstKey, COptions options, int32_t* errCode)
394     {
395         int64_t result = -1;
396         auto photoAccessHelperImpl = FFIData::GetData<PhotoAccessHelperImpl>(id);
397         if (photoAccessHelperImpl == nullptr) {
398             LOGE("Invalid object photoAccessHelperImpl");
399             *errCode = JS_ERR_PARAMETER_INVALID;
400             return result;
401         }
402         auto fetchFileResult = photoAccessHelperImpl->GetBurstAssets(cBurstKey, options, *errCode);
403         if (fetchFileResult == nullptr) {
404             LOGE("GetAssets failed.");
405             return result;
406         }
407         std::unique_ptr<FfiFetchResultProperty> propertyPtr = std::make_unique<FfiFetchResultProperty>();
408         propertyPtr->fetchFileResult_ = fetchFileResult;
409         propertyPtr->fetchResType_ = FetchResType::TYPE_FILE;
410         auto native = FFIData::Create<FetchResultImpl>(std::move(propertyPtr));
411         if (native != nullptr) {
412             result = native->GetID();
413         } else {
414             LOGE("Create FetchResultImpl instance failed.");
415             *errCode = JS_INNER_FAIL;
416         }
417         return result;
418     }
419 
FfiPhotoAccessHelperGetAlbums(int64_t id,int32_t type,int32_t subtype,COptions options,int32_t * errCode)420     int64_t FfiPhotoAccessHelperGetAlbums(int64_t id, int32_t type, int32_t subtype,
421         COptions options, int32_t* errCode)
422     {
423         int64_t result = -1;
424         auto photoAccessHelperImpl = FFIData::GetData<PhotoAccessHelperImpl>(id);
425         if (photoAccessHelperImpl == nullptr) {
426             LOGE("Invalid object photoAccessHelperImpl");
427             *errCode = JS_ERR_PARAMETER_INVALID;
428             return result;
429         }
430         auto fetchPhotoAlbumResult = photoAccessHelperImpl->GetAlbums(type, subtype, options, *errCode);
431         if (fetchPhotoAlbumResult == nullptr) {
432             LOGE("GetAlbums failed.");
433             return result;
434         }
435         std::unique_ptr<FfiFetchResultProperty> propertyPtr = std::make_unique<FfiFetchResultProperty>();
436         propertyPtr->fetchPhotoAlbumResult_ = fetchPhotoAlbumResult;
437         propertyPtr->fetchResType_ = FetchResType::TYPE_PHOTOALBUM;
438         auto native = FFIData::Create<FetchResultImpl>(std::move(propertyPtr));
439         if (native != nullptr) {
440             result = native->GetID();
441         } else {
442             LOGE("Create FetchResultImpl instance failed.");
443             *errCode = JS_INNER_FAIL;
444         }
445         return result;
446     }
447 
FfiPhotoAccessHelperRegisterChange(int64_t id,char * uri,bool forChildUris,int64_t funcId,int32_t * errCode)448     void FfiPhotoAccessHelperRegisterChange(int64_t id, char* uri,
449         bool forChildUris, int64_t funcId, int32_t *errCode)
450     {
451         auto photoAccessHelperImpl = FFIData::GetData<PhotoAccessHelperImpl>(id);
452         if (photoAccessHelperImpl == nullptr) {
453             LOGE("Invalid object photoAccessHelperImpl");
454             *errCode = JS_ERR_PARAMETER_INVALID;
455             return;
456         }
457         photoAccessHelperImpl->RegisterChange(uri, forChildUris, funcId, *errCode);
458     }
459 
FfiPhotoAccessHelperUnRegisterChange(int64_t id,char * uri,int64_t funcId,int32_t * errCode)460     void FfiPhotoAccessHelperUnRegisterChange(int64_t id, char* uri, int64_t funcId, int32_t *errCode)
461     {
462         auto photoAccessHelperImpl = FFIData::GetData<PhotoAccessHelperImpl>(id);
463         if (photoAccessHelperImpl == nullptr) {
464             LOGE("Invalid object photoAccessHelperImpl");
465             *errCode = JS_ERR_PARAMETER_INVALID;
466             return;
467         }
468         photoAccessHelperImpl->UnRegisterChange(uri, funcId);
469     }
470 
FfiPhotoAccessHelperRelease(int64_t id,int32_t * errCode)471     void FfiPhotoAccessHelperRelease(int64_t id, int32_t *errCode)
472     {
473         auto photoAccessHelperImpl = FFIData::GetData<PhotoAccessHelperImpl>(id);
474         if (photoAccessHelperImpl == nullptr) {
475             LOGE("Invalid object photoAccessHelperImpl");
476             *errCode = JS_ERR_PARAMETER_INVALID;
477             return;
478         }
479         photoAccessHelperImpl->Release();
480     }
481 
FfiPhotoAccessHelperShowAssetsCreationDialog(int64_t id,CArrString srcFileUris,PhotoCreationConfigs photoCreationConfigs,int64_t funcId,FfiBundleInfo cBundleInfo,int32_t * errCode)482     void FfiPhotoAccessHelperShowAssetsCreationDialog(int64_t id, CArrString srcFileUris,
483         PhotoCreationConfigs photoCreationConfigs, int64_t funcId, FfiBundleInfo cBundleInfo, int32_t *errCode)
484     {
485         auto photoAccessHelperImpl = FFIData::GetData<PhotoAccessHelperImpl>(id);
486         if (photoAccessHelperImpl == nullptr) {
487             LOGE("Invalid object photoAccessHelperImpl");
488             *errCode = JS_ERR_PARAMETER_INVALID;
489             return;
490         }
491         photoAccessHelperImpl->ShowAssetsCreationDialog(srcFileUris,
492             photoCreationConfigs, funcId, cBundleInfo, *errCode);
493     }
494 
FfiPhotoAccessHelperStartPhotoPicker(int64_t id,PhotoSelectOptions option,int32_t * errCode)495     PhotoSelectResult FfiPhotoAccessHelperStartPhotoPicker(int64_t id,
496         PhotoSelectOptions option, int32_t *errCode)
497     {
498         return PhotoAccessHelperImpl::StartPhotoPicker(id, option, *errCode);
499     }
500 
FfiMovingPhotoGetUri(int64_t id,int32_t * errCode)501     char* FfiMovingPhotoGetUri(int64_t id, int32_t* errCode)
502     {
503         auto ffiMovingPhotoImpl = FFIData::GetData<FfiMovingPhotoImpl>(id);
504         if (ffiMovingPhotoImpl == nullptr) {
505             LOGE("Invalid object movingPhotoImpl");
506             *errCode = JS_INNER_FAIL;
507             return nullptr;
508         }
509         auto movingPhotoUri = ffiMovingPhotoImpl->GetUri();
510         char* uri = MallocCString(movingPhotoUri);
511         return uri;
512     }
513 
FfiMovingPhotoRequestContentUri(int64_t id,char * imageFileUri,char * videoFileUri,int32_t * errCode)514     void FfiMovingPhotoRequestContentUri(int64_t id,
515         char* imageFileUri, char* videoFileUri, int32_t* errCode)
516     {
517         auto ffiMovingPhotoImpl = FFIData::GetData<FfiMovingPhotoImpl>(id);
518         if (ffiMovingPhotoImpl == nullptr) {
519             LOGE("Invalid object movingPhotoImpl");
520             *errCode = JS_INNER_FAIL;
521             return;
522         }
523         ffiMovingPhotoImpl->RequestContent(imageFileUri, videoFileUri, *errCode);
524     }
525 
FfiMovingPhotoRequestContentResourceType(int64_t id,int32_t resourceType,char * fileUri,int32_t * errCode)526     void FfiMovingPhotoRequestContentResourceType(int64_t id,
527         int32_t resourceType, char* fileUri, int32_t* errCode)
528     {
529         auto ffiMovingPhotoImpl = FFIData::GetData<FfiMovingPhotoImpl>(id);
530         if (ffiMovingPhotoImpl == nullptr) {
531             LOGE("Invalid object movingPhotoImpl");
532             *errCode = JS_INNER_FAIL;
533             return;
534         }
535         ffiMovingPhotoImpl->RequestContent(resourceType, fileUri, *errCode);
536     }
537 
FfiMovingPhotoRequestContentArrayBuffer(int64_t id,int32_t resourceType,int32_t * errCode)538     CArrUI8 FfiMovingPhotoRequestContentArrayBuffer(int64_t id,
539         int32_t resourceType, int32_t* errCode)
540     {
541         CArrUI8 result = { .head = nullptr, .size = 0};
542         auto ffiMovingPhotoImpl = FFIData::GetData<FfiMovingPhotoImpl>(id);
543         if (ffiMovingPhotoImpl == nullptr) {
544             LOGE("Invalid object movingPhotoImpl");
545             *errCode = JS_INNER_FAIL;
546             return result;
547         }
548         return ffiMovingPhotoImpl->RequestContent(resourceType, *errCode);
549     }
550 
FfiMediaAssetManagerRequestImage(int64_t contextId,int64_t photoAssetId,RequestOptions requestOptions,int64_t funcId,int32_t * errCode)551     char* FfiMediaAssetManagerRequestImage(int64_t contextId, int64_t photoAssetId,
552         RequestOptions requestOptions, int64_t funcId, int32_t* errCode)
553     {
554         return MediaAssetManagerImpl::RequestImage(contextId, photoAssetId,
555             requestOptions, funcId, *errCode);
556     }
557 
FfiMediaAssetManagerRequestImageData(int64_t contextId,int64_t photoAssetId,RequestOptions requestOptions,int64_t funcId,int32_t * errCode)558     char* FfiMediaAssetManagerRequestImageData(int64_t contextId, int64_t photoAssetId,
559         RequestOptions requestOptions, int64_t funcId, int32_t* errCode)
560     {
561         return MediaAssetManagerImpl::RequestImageData(contextId, photoAssetId,
562             requestOptions, funcId, *errCode);
563     }
564 
FfiMediaAssetManagerRequestMovingPhoto(int64_t contextId,int64_t photoAssetId,RequestOptions requestOptions,int64_t funcId,int32_t * errCode)565     char* FfiMediaAssetManagerRequestMovingPhoto(int64_t contextId, int64_t photoAssetId,
566         RequestOptions requestOptions, int64_t funcId, int32_t* errCode)
567     {
568         return MediaAssetManagerImpl::RequestMovingPhoto(contextId, photoAssetId,
569             requestOptions, funcId, *errCode);
570     }
571 
FfiMediaAssetManagerRequestVideoFile(int64_t contextId,int64_t photoAssetId,RequestOptions requestOptions,char * fileUri,int64_t funcId,int32_t * errCode)572     char* FfiMediaAssetManagerRequestVideoFile(int64_t contextId, int64_t photoAssetId,
573         RequestOptions requestOptions, char* fileUri, int64_t funcId, int32_t* errCode)
574     {
575         return MediaAssetManagerImpl::RequestVideoFile(contextId, photoAssetId,
576             requestOptions, fileUri, funcId, *errCode);
577     }
578 
FfiMediaAssetManagerCancelRequest(int64_t contextId,char * cRequestId,int32_t * errCode)579     void FfiMediaAssetManagerCancelRequest(int64_t contextId, char* cRequestId, int32_t* errCode)
580     {
581         MediaAssetManagerImpl::CancelRequest(contextId, cRequestId, *errCode);
582     }
583 
FfiMediaAssetManagerLoadMovingPhoto(int64_t contextId,char * cImageFileUri,char * cVideoFileUri,int32_t * errCode)584     int64_t FfiMediaAssetManagerLoadMovingPhoto(int64_t contextId, char* cImageFileUri,
585         char* cVideoFileUri, int32_t* errCode)
586     {
587         return MediaAssetManagerImpl::LoadMovingPhoto(contextId, cImageFileUri, cVideoFileUri, *errCode);
588     }
589 
590     // MediaAssetChangeRequest
591 
FfiMediaAssetChangeRequestImplConstructor(int64_t id,int32_t * errCode)592     int64_t FfiMediaAssetChangeRequestImplConstructor(int64_t id, int32_t* errCode)
593     {
594         auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(id);
595         if (!photoAssetImpl) {
596             *errCode = JS_INNER_FAIL;
597             return 0;
598         }
599         auto fileAssetPtr = photoAssetImpl->GetFileAssetInstance();
600         if (fileAssetPtr == nullptr) {
601             *errCode = OHOS_INVALID_PARAM_CODE;
602             LOGE("fileAsset is null");
603             return 0;
604         }
605         if (fileAssetPtr->GetMediaType() != MEDIA_TYPE_IMAGE && fileAssetPtr->GetMediaType() != MEDIA_TYPE_VIDEO) {
606             LOGE("Unsupported type of fileAsset");
607             *errCode = OHOS_INVALID_PARAM_CODE;
608             return 0;
609         }
610         auto changeRequest = FFIData::Create<MediaAssetChangeRequestImpl>(fileAssetPtr);
611         if (!changeRequest) {
612             *errCode = JS_INNER_FAIL;
613             return 0;
614         }
615         return changeRequest->GetID();
616     }
617 
FfiMediaAssetChangeRequestImplCreateImageAssetRequest(int64_t id,char * fileUri,int32_t * errCode)618     int64_t FfiMediaAssetChangeRequestImplCreateImageAssetRequest(int64_t id, char* fileUri, int32_t* errCode)
619     {
620         std::string filePath(fileUri);
621         auto changeRequest =
622             FFIData::Create<MediaAssetChangeRequestImpl>(id, filePath, MediaType::MEDIA_TYPE_IMAGE, errCode);
623         if (!changeRequest) {
624             *errCode = JS_INNER_FAIL;
625             return 0;
626         }
627         return changeRequest->GetID();
628     }
629 
FfiMediaAssetChangeRequestImplCreateVideoAssetRequest(int64_t id,char * fileUri,int32_t * errCode)630     int64_t FfiMediaAssetChangeRequestImplCreateVideoAssetRequest(int64_t id, char* fileUri, int32_t* errCode)
631     {
632         std::string filePath(fileUri);
633         auto changeRequest =
634             FFIData::Create<MediaAssetChangeRequestImpl>(id, filePath, MediaType::MEDIA_TYPE_VIDEO, errCode);
635         if (!changeRequest) {
636             *errCode = JS_INNER_FAIL;
637             return 0;
638         }
639         return changeRequest->GetID();
640     }
641 
FfiMediaAssetChangeRequestImplCreateAssetRequest(int64_t id,int32_t photoType,char * extension,char * title,int32_t subType)642     RetDataI64 FfiMediaAssetChangeRequestImplCreateAssetRequest(
643         int64_t id, int32_t photoType, char* extension, char* title, int32_t subType)
644     {
645         RetDataI64 ret = {};
646         auto changeRequest =
647             FFIData::Create<MediaAssetChangeRequestImpl>(id, photoType, extension, title, subType, &ret.code);
648         if (!changeRequest) {
649             ret.code = JS_INNER_FAIL;
650             return ret;
651         }
652         ret.data = changeRequest->GetID();
653         return ret;
654     }
655 
GetUriFromAsset(const OHOS::sptr<PhotoAssetImpl> obj)656     static std::string GetUriFromAsset(const OHOS::sptr<PhotoAssetImpl> obj)
657     {
658         std::string displayName = obj->GetFileDisplayName();
659         // todo
660         std::string filePath = obj->GetFileAssetInstance()->GetFilePath();
661         return MediaFileUtils::GetUriByExtrConditions(PhotoColumn::PHOTO_URI_PREFIX, std::to_string(obj->GetFileId()),
662             MediaFileUtils::GetExtraUri(displayName, filePath));
663     }
664 
ParseAssetArray(CArrI64 & assets,std::vector<std::string> & uriArray)665     static int32_t ParseAssetArray(CArrI64& assets, std::vector<std::string>& uriArray)
666     {
667         if (assets.size == 0) {
668             LOGE("array is empty");
669             return OHOS_INVALID_PARAM_CODE;
670         }
671         for (size_t i = 0; i < static_cast<size_t>(assets.size); i++) {
672             auto photoAssetImpl = FFIData::GetData<PhotoAssetImpl>(assets.head[i]);
673             if (!photoAssetImpl) {
674                 return JS_ERR_PARAMETER_INVALID;
675             }
676             if ((photoAssetImpl->GetMediaType() != MEDIA_TYPE_IMAGE &&
677                     photoAssetImpl->GetMediaType() != MEDIA_TYPE_VIDEO)) {
678                 LOGE("Skip invalid asset, mediaType: %{public}d", photoAssetImpl->GetMediaType());
679                 continue;
680             }
681             uriArray.push_back(GetUriFromAsset(photoAssetImpl));
682         }
683         return 0;
684     }
685 
FfiMediaAssetChangeRequestImplDeleteAssetsByObject(int64_t id,CArrI64 assets)686     int32_t FfiMediaAssetChangeRequestImplDeleteAssetsByObject(int64_t id, CArrI64 assets)
687     {
688         if (assets.size == 0) {
689             LOGE("array is empty");
690             return OHOS_INVALID_PARAM_CODE;
691         }
692         std::vector<std::string> uris;
693         if (ParseAssetArray(assets, uris) != 0) {
694             return OHOS_INVALID_PARAM_CODE;
695         }
696         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
697         if (!changeRequest) {
698             return OHOS_INVALID_PARAM_CODE;
699         }
700         return changeRequest->CJDeleteAssets(id, uris);
701     }
702 
FfiMediaAssetChangeRequestImplDeleteAssetsByString(int64_t id,CArrString assets)703     int32_t FfiMediaAssetChangeRequestImplDeleteAssetsByString(int64_t id, CArrString assets)
704     {
705         if (assets.size == 0) {
706             LOGE("array is empty");
707             return OHOS_INVALID_PARAM_CODE;
708         }
709         std::vector<std::string> uris;
710         for (int64_t i = 0; i < assets.size; i++) {
711             uris.push_back(assets.head[i]);
712         }
713         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
714         if (!changeRequest) {
715             return OHOS_INVALID_PARAM_CODE;
716         }
717         return changeRequest->CJDeleteAssets(id, uris);
718     }
719 
FfiMediaAssetChangeRequestImplGetAsset(int64_t id,int32_t * errCode)720     int64_t FfiMediaAssetChangeRequestImplGetAsset(int64_t id, int32_t* errCode)
721     {
722         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
723         if (!changeRequest) {
724             return OHOS_INVALID_PARAM_CODE;
725         }
726         return changeRequest->CJGetAsset(errCode);
727     }
728 
FfiMediaAssetChangeRequestImplSetTitle(int64_t id,char * title)729     int32_t FfiMediaAssetChangeRequestImplSetTitle(int64_t id, char* title)
730     {
731         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
732         if (!changeRequest) {
733             return OHOS_INVALID_PARAM_CODE;
734         }
735         return changeRequest->CJSetTitle(title);
736     }
737 
FfiMediaAssetChangeRequestImplGetWriteCacheHandler(int64_t id,int32_t * errCode)738     int32_t FfiMediaAssetChangeRequestImplGetWriteCacheHandler(int64_t id, int32_t* errCode)
739     {
740         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
741         if (!changeRequest) {
742             return OHOS_INVALID_PARAM_CODE;
743         }
744         return changeRequest->CJGetWriteCacheHandler(errCode);
745     }
746 
FfiMediaAssetChangeRequestImplAddResourceByString(int64_t id,int32_t resourceType,char * fileUri)747     int32_t FfiMediaAssetChangeRequestImplAddResourceByString(int64_t id, int32_t resourceType, char* fileUri)
748     {
749         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
750         if (!changeRequest) {
751             return OHOS_INVALID_PARAM_CODE;
752         }
753         return changeRequest->CJAddResource(resourceType, fileUri);
754     }
755 
FfiMediaAssetChangeRequestImplAddResourceByBuffer(int64_t id,int32_t resourceType,CArrUI8 data)756     int32_t FfiMediaAssetChangeRequestImplAddResourceByBuffer(int64_t id, int32_t resourceType, CArrUI8 data)
757     {
758         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
759         if (!changeRequest) {
760             return OHOS_INVALID_PARAM_CODE;
761         }
762         return changeRequest->CJAddResource(resourceType, data.head, data.size);
763     }
764 
FfiMediaAssetChangeRequestImplSaveCameraPhoto(int64_t id)765     int32_t FfiMediaAssetChangeRequestImplSaveCameraPhoto(int64_t id)
766     {
767         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
768         if (!changeRequest) {
769             return OHOS_INVALID_PARAM_CODE;
770         }
771         return changeRequest->CJSaveCameraPhoto();
772     }
773 
FfiMediaAssetChangeRequestImplDiscardCameraPhoto(int64_t id)774     int32_t FfiMediaAssetChangeRequestImplDiscardCameraPhoto(int64_t id)
775     {
776         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
777         if (!changeRequest) {
778             return OHOS_INVALID_PARAM_CODE;
779         }
780         return changeRequest->CJDiscardCameraPhoto();
781     }
782 
FfiMediaAssetChangeRequestImplSetOrientation(int64_t id,int32_t orientation)783     int32_t FfiMediaAssetChangeRequestImplSetOrientation(int64_t id, int32_t orientation)
784     {
785         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
786         if (!changeRequest) {
787             return OHOS_INVALID_PARAM_CODE;
788         }
789         return changeRequest->CJSetOrientation(orientation);
790     }
791 
FfiMediaAssetChangeRequestImplApplyChanges(int64_t id)792     int32_t FfiMediaAssetChangeRequestImplApplyChanges(int64_t id)
793     {
794         auto changeRequest = FFIData::GetData<MediaAssetChangeRequestImpl>(id);
795         if (!changeRequest) {
796             return OHOS_INVALID_PARAM_CODE;
797         }
798         return changeRequest->ApplyChanges();
799     }
800 
801     // MediaAlbumChangeRequest
802 
FfiMediaAlbumChangeRequestImplConstructor(int64_t id,int32_t * errCode)803     int64_t FfiMediaAlbumChangeRequestImplConstructor(int64_t id, int32_t* errCode)
804     {
805         auto photoAlbumImpl = FFIData::GetData<PhotoAlbumImpl>(id);
806         if (!photoAlbumImpl) {
807             *errCode = JS_INNER_FAIL;
808             return 0;
809         }
810         auto photoAlbumPtr = photoAlbumImpl->GetPhotoAlbumInstance();
811         if (photoAlbumPtr == nullptr) {
812             LOGE("photoAlbum is null");
813             *errCode = OHOS_INVALID_PARAM_CODE;
814             return 0;
815         }
816         if (!(photoAlbumPtr->GetResultNapiType() == ResultNapiType::TYPE_PHOTOACCESS_HELPER &&
817                 PhotoAlbum::CheckPhotoAlbumType(photoAlbumPtr->GetPhotoAlbumType()) &&
818                 PhotoAlbum::CheckPhotoAlbumSubType(photoAlbumPtr->GetPhotoAlbumSubType()))) {
819             LOGE("Unsupported type of photoAlbum");
820             *errCode = OHOS_INVALID_PARAM_CODE;
821             return 0;
822         }
823         auto changeRequest = FFIData::Create<MediaAlbumChangeRequestImpl>(photoAlbumPtr);
824         if (!changeRequest) {
825             *errCode = JS_INNER_FAIL;
826             return 0;
827         }
828         return changeRequest->GetID();
829     }
830 
FfiMediaAlbumChangeRequestImplGetAlbum(int64_t id,int32_t * errCode)831     int64_t FfiMediaAlbumChangeRequestImplGetAlbum(int64_t id, int32_t* errCode)
832     {
833         auto changeRequest = FFIData::GetData<MediaAlbumChangeRequestImpl>(id);
834         if (!changeRequest) {
835             *errCode = OHOS_INVALID_PARAM_CODE;
836             return 0;
837         }
838         return changeRequest->CJGetAlbum(errCode);
839     }
840 
FfiMediaAlbumChangeRequestImplSetAlbumName(int64_t id,char * albumName)841     int32_t FfiMediaAlbumChangeRequestImplSetAlbumName(int64_t id, char* albumName)
842     {
843         auto changeRequest = FFIData::GetData<MediaAlbumChangeRequestImpl>(id);
844         if (!changeRequest) {
845             return OHOS_INVALID_PARAM_CODE;
846         }
847         return changeRequest->CJSetAlbumName(albumName);
848     }
849 
FfiMediaAlbumChangeRequestImplAddAssets(int64_t id,CArrI64 assets)850     int32_t FfiMediaAlbumChangeRequestImplAddAssets(int64_t id, CArrI64 assets)
851     {
852         auto changeRequest = FFIData::GetData<MediaAlbumChangeRequestImpl>(id);
853         if (!changeRequest) {
854             return OHOS_INVALID_PARAM_CODE;
855         }
856         vector<string> assetUriArray;
857         if (ParseAssetArray(assets, assetUriArray) != 0) {
858             return OHOS_INVALID_PARAM_CODE;
859         }
860         return changeRequest->CJAddAssets(assetUriArray);
861     }
862 
FfiMediaAlbumChangeRequestImplRemoveAssets(int64_t id,CArrI64 assets)863     int32_t FfiMediaAlbumChangeRequestImplRemoveAssets(int64_t id, CArrI64 assets)
864     {
865         auto changeRequest = FFIData::GetData<MediaAlbumChangeRequestImpl>(id);
866         if (!changeRequest) {
867             return OHOS_INVALID_PARAM_CODE;
868         }
869         vector<string> assetUriArray;
870         if (ParseAssetArray(assets, assetUriArray) != 0) {
871             return OHOS_INVALID_PARAM_CODE;
872         }
873         return changeRequest->CJRemoveAssets(assetUriArray);
874     }
875 
FfiMediaAlbumChangeRequestImplApplyChanges(int64_t id)876     int32_t FfiMediaAlbumChangeRequestImplApplyChanges(int64_t id)
877     {
878         auto changeRequest = FFIData::GetData<MediaAlbumChangeRequestImpl>(id);
879         if (!changeRequest) {
880             return OHOS_INVALID_PARAM_CODE;
881         }
882         return changeRequest->ApplyChanges();
883     }
884 }
885 
886 enum class CJCameraShotType : int32_t {
887     IMAGE = 0,
888     VIDEO,
889     MOVING_PHOTO,
890     BURST,
891 };
892 
CreatePhotoAssetImpl(const std::string & uri,int32_t cameraShotType,const std::string & burstKey)893 int64_t CreatePhotoAssetImpl(const std::string &uri, int32_t cameraShotType, const std::string &burstKey)
894 {
895     if (uri.empty()) {
896         return 0;
897     }
898     std::shared_ptr<FileAsset> fileAsset = std::make_shared<FileAsset>();
899     fileAsset->SetUri(uri);
900     std::string fileId = MediaFileUtils::GetIdFromUri(uri);
901     if (MediaFileUtils::IsValidInteger(fileId)) {
902         fileAsset->SetId(stoi(fileId));
903     }
904 
905     fileAsset->SetDisplayName(MediaFileUtils::GetFileName(uri));
906     if (cameraShotType == static_cast<int32_t>(CJCameraShotType::IMAGE)) {
907         fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::CAMERA));
908         fileAsset->SetMediaType(MediaType::MEDIA_TYPE_IMAGE);
909     } else if (cameraShotType == static_cast<int32_t>(CJCameraShotType::MOVING_PHOTO)) {
910         fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::MOVING_PHOTO));
911         fileAsset->SetMediaType(MediaType::MEDIA_TYPE_IMAGE);
912     } else if (cameraShotType == static_cast<int32_t>(CJCameraShotType::BURST)) {
913         fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::BURST));
914         fileAsset->SetMediaType(MediaType::MEDIA_TYPE_IMAGE);
915         fileAsset->SetBurstKey(burstKey);
916     } else if (cameraShotType == static_cast<int32_t>(CJCameraShotType::VIDEO)) {
917         fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::CAMERA));
918         fileAsset->SetMediaType(MediaType::MEDIA_TYPE_VIDEO);
919     }
920     fileAsset->SetResultNapiType(ResultNapiType::TYPE_PHOTOACCESS_HELPER);
921     auto photoAssetImpl = FFIData::Create<PhotoAssetImpl>(fileAsset);
922     return photoAssetImpl->GetID();
923 }
924 }
925 }