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 #include "cloud_thumbnail_observer.h"
17 #include "medialibrary_rdb_utils.h"
18
19 using namespace std;
20 using namespace OHOS::DistributedKv;
21 using namespace OHOS::NativeRdb;
22
23 namespace OHOS {
24 namespace Media {
25
IsFileIdValid(const std::string & fileId)26 static inline bool IsFileIdValid(const std::string& fileId)
27 {
28 if (fileId.empty()) {
29 return false;
30 }
31 for (char const& ch : fileId) {
32 if (std::isdigit(ch) == 0) {
33 return false;
34 }
35 }
36 return true;
37 }
38
ParseUriCloudDownload(const Uri & uri)39 std::string CloudThumbnailObserver::ParseUriCloudDownload(const Uri &uri)
40 {
41 string uriString = uri.ToString();
42 auto pos = uriString.find_last_of('/');
43 CHECK_AND_RETURN_RET(pos != std::string::npos, "");
44 string idString = uriString.substr(pos + 1);
45 CHECK_AND_RETURN_RET_LOG(IsFileIdValid(idString), "",
46 "cloud observer get no valid fileId and uri : %{public}s", uriString.c_str());
47 return idString;
48 }
49
CreateAstcBatchCloudDownload(const ChangeInfo & changeInfo)50 void CloudThumbnailObserver::CreateAstcBatchCloudDownload(const ChangeInfo &changeInfo)
51 {
52 for (auto &uri : changeInfo.uris_) {
53 string idString = ParseUriCloudDownload(uri);
54 if (idString == "") {
55 continue;
56 }
57 ThumbnailService::GetInstance()->CreateAstcCloudDownload(idString);
58 }
59 }
60
OnChange(const ChangeInfo & changeInfo)61 void CloudThumbnailObserver::OnChange(const ChangeInfo &changeInfo)
62 {
63 MediaLibraryRdbUtils::SetNeedRefreshAlbum(true);
64 if (changeInfo.changeType_ == ChangeType::INSERT) {
65 CreateAstcBatchCloudDownload(changeInfo);
66 } else {
67 MEDIA_DEBUG_LOG("change type is %{public}d, not insert", changeInfo.changeType_);
68 }
69 }
70 } // namespace Media
71 } // namespace OHOS