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 #define MLOG_TAG "PhotoAssetCopyOperation"
16 
17 #include "photo_asset_copy_operation.h"
18 
19 #include "photo_displayname_operation.h"
20 #include "photo_burst_operation.h"
21 #include "media_log.h"
22 #include "media_column.h"
23 #include "result_set_utils.h"
24 #include "media_file_utils.h"
25 #include "photo_asset_info.h"
26 
27 namespace OHOS::Media {
SetTargetPhotoInfo(const std::shared_ptr<NativeRdb::ResultSet> & resultSet)28 PhotoAssetCopyOperation &PhotoAssetCopyOperation::SetTargetPhotoInfo(
29     const std::shared_ptr<NativeRdb::ResultSet> &resultSet)
30 {
31     if (resultSet == nullptr) {
32         MEDIA_ERR_LOG("Media_Operation: resultSet is null.");
33         return *this;
34     }
35     this->photoAssetInfo_.displayName = GetStringVal(MediaColumn::MEDIA_NAME, resultSet);
36     this->photoAssetInfo_.subtype = GetInt32Val(PhotoColumn::PHOTO_SUBTYPE, resultSet);
37     return *this;
38 }
39 
40 /**
41  * @brief Set the target album id.
42  *
43  * @param targetAlbumId The target album id. Only positive integers are accepted.
44  */
SetTargetAlbumId(const int32_t targetAlbumId)45 PhotoAssetCopyOperation &PhotoAssetCopyOperation::SetTargetAlbumId(const int32_t targetAlbumId)
46 {
47     if (targetAlbumId <= 0) {
48         return *this;
49     }
50     this->photoAssetInfo_.ownerAlbumId = targetAlbumId;
51     return *this;
52 }
53 
54 /**
55  * @brief Set the display name.
56  *
57  * @param displayName The display name. Only non-empty strings are accepted.
58  */
SetDisplayName(const std::string & displayName)59 PhotoAssetCopyOperation &PhotoAssetCopyOperation::SetDisplayName(const std::string &displayName)
60 {
61     if (displayName.empty()) {
62         return *this;
63     }
64     this->photoAssetInfo_.displayName = displayName;
65     return *this;
66 }
67 
68 /**
69  * @brief Set the PhotoAssetInfo of the target photo to ValuesBucket
70  *
71  * @param rdbStore The rdb store.
72  * @param values The values bucket.
73  */
CopyPhotoAsset(const std::shared_ptr<MediaLibraryRdbStore> & rdbStore,NativeRdb::ValuesBucket & values)74 void PhotoAssetCopyOperation::CopyPhotoAsset(
75     const std::shared_ptr<MediaLibraryRdbStore> &rdbStore, NativeRdb::ValuesBucket &values)
76 {
77     PhotoAssetInfo photoAssetInfo(this->photoAssetInfo_);
78     // Find the unique display name for the photo.
79     std::string displayName = PhotoDisplayNameOperation().SetTargetPhotoInfo(photoAssetInfo).FindDisplayName(rdbStore);
80     if (!displayName.empty()) {
81         values.PutString(MediaColumn::MEDIA_NAME, displayName);
82         values.PutString(MediaColumn::MEDIA_TITLE, MediaFileUtils::GetTitleFromDisplayName(displayName));
83     } else {
84         MEDIA_ERR_LOG("Failed to get display name. Object: %{public}s", photoAssetInfo.ToString().c_str());
85         return;
86     }
87     // Find the burst key by the unique display name for the burst photos.
88     photoAssetInfo.displayName = displayName;
89     std::string burstKey = PhotoBurstOperation().SetTargetPhotoInfo(photoAssetInfo).FindBurstKey(rdbStore);
90     if (!burstKey.empty()) {
91         values.PutString(PhotoColumn::PHOTO_BURST_KEY, burstKey);
92     }
93 }
94 }  // namespace OHOS::Media