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 #define MLOG_TAG "PhotoFileUtils"
17 
18 #include "photo_file_utils.h"
19 
20 #include "media_file_utils.h"
21 #include "media_log.h"
22 #include "medialibrary_errno.h"
23 #include "medialibrary_type_const.h"
24 
25 using namespace std;
26 
27 namespace OHOS::Media {
AppendUserId(const string & path,int32_t userId)28 string PhotoFileUtils::AppendUserId(const string& path, int32_t userId)
29 {
30     if (userId < 0 || !MediaFileUtils::StartsWith(path, ROOT_MEDIA_DIR)) {
31         return path;
32     }
33 
34     return "/storage/cloud/" + to_string(userId) + "/files/" + path.substr(ROOT_MEDIA_DIR.length());
35 }
36 
CheckPhotoPath(const string & photoPath)37 static bool CheckPhotoPath(const string& photoPath)
38 {
39     return photoPath.length() >= ROOT_MEDIA_DIR.length() && MediaFileUtils::StartsWith(photoPath, ROOT_MEDIA_DIR);
40 }
41 
GetEditDataDir(const string & photoPath,int32_t userId)42 string PhotoFileUtils::GetEditDataDir(const string& photoPath, int32_t userId)
43 {
44     if (!CheckPhotoPath(photoPath)) {
45         return "";
46     }
47 
48     return AppendUserId(MEDIA_EDIT_DATA_DIR, userId) + photoPath.substr(ROOT_MEDIA_DIR.length());
49 }
50 
GetEditDataPath(const string & photoPath,int32_t userId)51 string PhotoFileUtils::GetEditDataPath(const string& photoPath, int32_t userId)
52 {
53     string parentPath = GetEditDataDir(photoPath, userId);
54     if (parentPath.empty()) {
55         return "";
56     }
57     return parentPath + "/editdata";
58 }
59 
GetEditDataCameraPath(const string & photoPath,int32_t userId)60 string PhotoFileUtils::GetEditDataCameraPath(const string& photoPath, int32_t userId)
61 {
62     string parentPath = GetEditDataDir(photoPath, userId);
63     if (parentPath.empty()) {
64         return "";
65     }
66     return parentPath + "/editdata_camera";
67 }
68 
GetEditDataSourcePath(const string & photoPath,int32_t userId)69 string PhotoFileUtils::GetEditDataSourcePath(const string& photoPath, int32_t userId)
70 {
71     string parentPath = GetEditDataDir(photoPath, userId);
72     if (parentPath.empty()) {
73         return "";
74     }
75     return parentPath + "/source." + MediaFileUtils::GetExtensionFromPath(photoPath);
76 }
77 } // namespace OHOS::Media