• 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 "PhotoSourcePathOperation"
16 
17 #include "photo_source_path_operation.h"
18 
19 #include "media_log.h"
20 #include "medialibrary_errno.h"
21 #include "userfile_manager_types.h"
22 #include "media_file_utils.h"
23 #include "media_column.h"
24 #include "result_set_utils.h"
25 
26 namespace OHOS::Media {
ResetPhotoSourcePath(std::shared_ptr<MediaLibraryRdbStore> mediaRdbStorePtr)27 void PhotoSourcePathOperation::ResetPhotoSourcePath(std::shared_ptr<MediaLibraryRdbStore> mediaRdbStorePtr)
28 {
29     std::vector<PhotoSourcePathOperation::PhotoAssetInfo> photoAssetInfos =
30         this->GetPhotoOfMissingSourcePath(mediaRdbStorePtr);
31     int32_t count = 0;
32     for (const auto &info : photoAssetInfos) {
33         bool isSkip = info.hidden == 0 && info.dateTrashed == 0;
34         isSkip = isSkip || info.lPath.empty();
35         isSkip = isSkip || !info.sourcePath.empty();
36         if (isSkip) {
37             MEDIA_ERR_LOG("Media_Operation: obj is invalid, info: %{public}s.", info.ToString().c_str());
38             continue;
39         }
40         std::string sql = this->SQL_PHOTO_SOURCE_PATH_FIX_UPDATE;
41         std::string sourcePath = this->SOURCE_PATH_PREFIX + info.lPath + "/" + info.displayName;
42         std::vector<NativeRdb::ValueObject> params = {sourcePath, info.fileId};
43         int32_t err = mediaRdbStorePtr->ExecuteSql(sql, params);
44         if (err != NativeRdb::E_OK) {
45             MEDIA_ERR_LOG("Media_Operation: update photo source_path failed, err: %{public}d, info: %{public}s.",
46                 err,
47                 info.ToString().c_str());
48             continue;
49         }
50         count++;
51     }
52     MEDIA_INFO_LOG("Media_Operation: reset photo source_path success. count: %{public}d.", count);
53 }
54 
GetPhotoOfMissingSourcePath(std::shared_ptr<MediaLibraryRdbStore> mediaRdbStorePtr,const int32_t offset,const int32_t limit)55 std::vector<PhotoSourcePathOperation::PhotoAssetInfo> PhotoSourcePathOperation::GetPhotoOfMissingSourcePath(
56     std::shared_ptr<MediaLibraryRdbStore> mediaRdbStorePtr, const int32_t offset, const int32_t limit)
57 {
58     std::vector<PhotoSourcePathOperation::PhotoAssetInfo> photoAssetInfos;
59     CHECK_AND_RETURN_RET_LOG(mediaRdbStorePtr != nullptr, photoAssetInfos,
60         "Media_Operation: mediaRdbStorePtr is null.");
61 
62     const std::vector<NativeRdb::ValueObject> params = {offset, limit};
63     std::string querySql = this->SQL_PHOTO_SOURCE_PATH_MISSING_QUERY;
64     auto resultSet = mediaRdbStorePtr->QuerySql(querySql, params);
65     CHECK_AND_RETURN_RET(resultSet != nullptr, photoAssetInfos);
66 
67     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
68         PhotoSourcePathOperation::PhotoAssetInfo info;
69         info.albumId = GetInt64Val("album_id", resultSet);
70         info.albumName = GetStringVal("album_name", resultSet);
71         info.lPath = GetStringVal("lpath", resultSet);
72         info.fileId = GetInt64Val("file_id", resultSet);
73         info.displayName = GetStringVal("display_name", resultSet);
74         info.hidden = GetInt32Val("hidden", resultSet);
75         info.dateTrashed = GetInt64Val("date_trashed", resultSet);
76         info.sourcePath = GetStringVal("source_path", resultSet);
77         photoAssetInfos.emplace_back(info);
78     }
79     return photoAssetInfos;
80 }
81 }  // namespace OHOS::Media