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 "uri_convert_handler.h"
17 
18 #include "media_column.h"
19 #include "photo_album_column.h"
20 
21 using namespace std;
22 
23 namespace OHOS {
24 namespace Media {
25 
26 using ChangeType = DataShare::DataShareObserver::ChangeType;
27 unordered_map<ChangeType, NotifyType> NotifyTypeChangeMap = {
28     {ChangeType::INSERT, NotifyType::NOTIFY_ADD},
29     {ChangeType::UPDATE, NotifyType::NOTIFY_UPDATE},
30     {ChangeType::DELETE, NotifyType::NOTIFY_REMOVE},
31 };
32 
AddNewNotify(CloudSyncHandleData & newHandleData,const list<Uri> & sendUris,const ChangeType & changeType)33 static void AddNewNotify(CloudSyncHandleData &newHandleData,
34     const list<Uri> &sendUris, const ChangeType &changeType)
35 {
36     if (sendUris.size() <= 0) {
37         return;
38     }
39     if (changeType == ChangeType::INSERT) {
40         return;
41     }
42     ChangeType sendType = static_cast<ChangeType>(NotifyTypeChangeMap[changeType]);
43     if (newHandleData.notifyInfo.find(sendType) == newHandleData.notifyInfo.end()) {
44         newHandleData.notifyInfo[sendType] = sendUris;
45     } else {
46         newHandleData.notifyInfo[sendType].insert(
47             newHandleData.notifyInfo[sendType].end(), sendUris.begin(), sendUris.end());
48     }
49     return;
50 }
51 
Handle(const CloudSyncHandleData & handleData)52 void UriConvertHandler::Handle(const CloudSyncHandleData &handleData)
53 {
54     const string org_uri_prefix = "file://cloudsync/";
55     const string new_uri_prefix = "file://media/";
56     CloudSyncNotifyInfo newNotifyInfo;
57     CloudSyncHandleData newHandleData = handleData;
58 
59     if (handleData.orgInfo.type == ChangeType::OTHER) {
60         AddNewNotify(newHandleData, { Uri(PhotoColumn::PHOTO_URI_PREFIX) }, ChangeType::DELETE);
61         AddNewNotify(newHandleData, { Uri(PhotoAlbumColumns::ALBUM_URI_PREFIX) }, ChangeType::DELETE);
62     } else {
63         newNotifyInfo.type = handleData.orgInfo.type;
64         for (auto &uri : handleData.orgInfo.uris) {
65             string uriString = uri.ToString();
66             size_t pos = uriString.find(org_uri_prefix);
67             Uri newUri = Uri(uriString.replace(pos, org_uri_prefix.length(), new_uri_prefix));
68             newNotifyInfo.uris.push_back(newUri);
69         }
70         AddNewNotify(newHandleData, newNotifyInfo.uris, newNotifyInfo.type);
71     }
72 
73     if (nextHandler_ != nullptr) {
74         nextHandler_->Handle(newHandleData);
75     }
76     return ;
77 }
78 } //namespace Media
79 } //namespace OHOS
80