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 #define MLOG_TAG "MediaLibraryCommNapi"
17 
18 #include "media_library_comm_napi.h"
19 
20 #include "file_asset_napi.h"
21 #include "media_file_utils.h"
22 #include "media_photo_asset_proxy.h"
23 
24 using namespace std;
25 
26 namespace OHOS {
27 namespace Media {
MediaLibraryCommNapi()28 MediaLibraryCommNapi::MediaLibraryCommNapi() {}
29 
~MediaLibraryCommNapi()30 MediaLibraryCommNapi::~MediaLibraryCommNapi() {}
31 
32 // The current function is only provided to the camera framework.
CreatePhotoAssetNapi(napi_env env,const string & uri,int32_t cameraShotType,const string & burstKey)33 napi_value MediaLibraryCommNapi::CreatePhotoAssetNapi(
34     napi_env env, const string &uri, int32_t cameraShotType, const string &burstKey)
35 {
36     if (uri.empty()) {
37         NAPI_ERR_LOG("uri is empty");
38         return nullptr;
39     }
40     shared_ptr<FileAsset> fileAsset = make_shared<FileAsset>();
41     fileAsset->SetUri(uri);
42     string fileId = MediaFileUtils::GetIdFromUri(uri);
43     if (!fileId.empty()) {
44         fileAsset->SetId(stoi(fileId));
45     }
46 
47     fileAsset->SetDisplayName(MediaFileUtils::GetFileName(uri));
48     if (cameraShotType == static_cast<int32_t>(CameraShotType::IMAGE)) {
49         fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::CAMERA));
50         fileAsset->SetMediaType(MediaType::MEDIA_TYPE_IMAGE);
51     } else if (cameraShotType == static_cast<int32_t>(CameraShotType::MOVING_PHOTO)) {
52         fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::MOVING_PHOTO));
53         fileAsset->SetMediaType(MediaType::MEDIA_TYPE_IMAGE);
54     } else if (cameraShotType == static_cast<int32_t>(CameraShotType::BURST)) {
55         fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::BURST));
56         fileAsset->SetMediaType(MediaType::MEDIA_TYPE_IMAGE);
57         fileAsset->SetBurstKey(burstKey);
58     } else if (cameraShotType == static_cast<int32_t>(CameraShotType::VIDEO)) {
59         fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::CAMERA));
60         fileAsset->SetMediaType(MediaType::MEDIA_TYPE_VIDEO);
61     } else {
62         NAPI_ERR_LOG("invalid cameraShotKey: %{public}d", cameraShotType);
63     }
64     fileAsset->SetResultNapiType(ResultNapiType::TYPE_PHOTOACCESS_HELPER);
65     return FileAssetNapi::CreatePhotoAsset(env, fileAsset);
66 }
67 } // namespace Media
68 } // namespace OHOS