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 if (mediaRdbStorePtr == nullptr) {
60 MEDIA_ERR_LOG("Media_Operation: mediaRdbStorePtr is null.");
61 return photoAssetInfos;
62 }
63 const std::vector<NativeRdb::ValueObject> params = {offset, limit};
64 std::string querySql = this->SQL_PHOTO_SOURCE_PATH_MISSING_QUERY;
65 auto resultSet = mediaRdbStorePtr->QuerySql(querySql, params);
66 if (resultSet == nullptr) {
67 return photoAssetInfos;
68 }
69 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
70 PhotoSourcePathOperation::PhotoAssetInfo info;
71 info.albumId = GetInt64Val("album_id", resultSet);
72 info.albumName = GetStringVal("album_name", resultSet);
73 info.lPath = GetStringVal("lpath", resultSet);
74 info.fileId = GetInt64Val("file_id", resultSet);
75 info.displayName = GetStringVal("display_name", resultSet);
76 info.hidden = GetInt32Val("hidden", resultSet);
77 info.dateTrashed = GetInt64Val("date_trashed", resultSet);
78 info.sourcePath = GetStringVal("source_path", resultSet);
79 photoAssetInfos.emplace_back(info);
80 }
81 return photoAssetInfos;
82 }
83 } // namespace OHOS::Media