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