• 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 
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 
HasEditData(int64_t editTime)78 bool PhotoFileUtils::HasEditData(int64_t editTime)
79 {
80     return editTime > 0;
81 }
82 
HasSource(bool hasEditDataCamera,int64_t editTime,int32_t effectMode)83 bool PhotoFileUtils::HasSource(bool hasEditDataCamera, int64_t editTime, int32_t effectMode)
84 {
85     return hasEditDataCamera || editTime > 0 ||
86            (effectMode > static_cast<int32_t>(MovingPhotoEffectMode::DEFAULT) &&
87                effectMode != static_cast<int32_t>(MovingPhotoEffectMode::IMAGE_ONLY));
88 }
89 
GetMetaPathFromOrignalPath(const std::string & srcPath,std::string & metaPath)90 int32_t PhotoFileUtils::GetMetaPathFromOrignalPath(const std::string &srcPath, std::string &metaPath)
91 {
92     if (srcPath.empty()) {
93         MEDIA_ERR_LOG("getMetaPathFromOrignalPath: source file invalid!");
94         return E_INVALID_PATH;
95     }
96 
97     size_t pos = srcPath.find(META_RECOVERY_PHOTO_RELATIVE_PATH);
98     if (pos == string::npos) {
99     MEDIA_ERR_LOG("getMetaPathFromOrignalPath: source path is not a photo path");
100     return E_INVALID_PATH;
101     }
102 
103     metaPath = srcPath;
104     metaPath.replace(pos, META_RECOVERY_PHOTO_RELATIVE_PATH.length(), META_RECOVERY_META_RELATIVE_PATH);
105     metaPath += META_RECOVERY_META_FILE_SUFFIX;
106 
107     return E_OK;
108 }
109 
GetMetaDataRealPath(const string & photoPath,int32_t userId)110 string PhotoFileUtils::GetMetaDataRealPath(const string &photoPath, int32_t userId)
111 {
112     string metaPath;
113     int ret = GetMetaPathFromOrignalPath(photoPath, metaPath);
114     if (ret != E_OK) {
115         return "";
116     }
117     return AppendUserId(ROOT_MEDIA_DIR, userId) + metaPath.substr(ROOT_MEDIA_DIR.length());
118 }
119 
GetThumbDir(const string & photoPath,int32_t userId)120 string PhotoFileUtils::GetThumbDir(const string &photoPath, int32_t userId)
121 {
122     if (!CheckPhotoPath(photoPath)) {
123         return "";
124     }
125     return AppendUserId(ROOT_MEDIA_DIR, userId) + ".thumbs/" + photoPath.substr(ROOT_MEDIA_DIR.length());
126 }
127 
GetLCDPath(const string & photoPath,int32_t userId)128 string PhotoFileUtils::GetLCDPath(const string &photoPath, int32_t userId)
129 {
130     string thumbDir = GetThumbDir(photoPath, userId);
131     if (thumbDir.empty()) {
132         return "";
133     }
134     return thumbDir + "/LCD.jpg";
135 }
136 
GetTHMPath(const string & photoPath,int32_t userId)137 string PhotoFileUtils::GetTHMPath(const string &photoPath, int32_t userId)
138 {
139     string thumbDir = GetThumbDir(photoPath, userId);
140     if (thumbDir.empty()) {
141         return "";
142     }
143     return thumbDir + "/THM.jpg";
144 }
145 
IsLaterThan(const string & currentPath,const string & targetPath)146 static bool IsLaterThan(const string &currentPath, const string &targetPath)
147 {
148     int64_t targetDateModified = 0;
149     if (!MediaFileUtils::GetDateModified(targetPath, targetDateModified)) {
150         return false;
151     }
152     int64_t currentDateModified = 0;
153     if (!MediaFileUtils::GetDateModified(currentPath, currentDateModified)) {
154         return false;
155     }
156     return currentDateModified > targetDateModified;
157 }
158 
IsThumbnailExists(const string & photoPath)159 bool PhotoFileUtils::IsThumbnailExists(const string &photoPath)
160 {
161     if (photoPath.empty()) {
162         return false;
163     }
164 
165     string lcdPath = GetLCDPath(photoPath);
166     string thmPath = GetTHMPath(photoPath);
167     return MediaFileUtils::IsFileExists(lcdPath) || MediaFileUtils::IsFileExists(thmPath);
168 }
169 
IsThumbnailLatest(const string & photoPath)170 bool PhotoFileUtils::IsThumbnailLatest(const string &photoPath)
171 {
172     if (photoPath.empty()) {
173         return false;
174     }
175 
176     string lcdPath = GetLCDPath(photoPath);
177     if (!lcdPath.empty() && IsLaterThan(lcdPath, photoPath)) {
178         MEDIA_DEBUG_LOG("lcd %{private}s is latest", lcdPath.c_str());
179         return true;
180     }
181 
182     string thmPath = GetTHMPath(photoPath);
183     if (!thmPath.empty() && IsLaterThan(thmPath, photoPath)) {
184         MEDIA_DEBUG_LOG("thm %{private}s is latest", thmPath.c_str());
185         return true;
186     }
187     return false;
188 }
189 
ExtractYearMonthDay(const std::string & detailTime)190 std::tuple<std::string, std::string, std::string> PhotoFileUtils::ExtractYearMonthDay(const std::string &detailTime)
191 {
192     const size_t detailTimeLength = 19;
193     if (detailTime.length() != detailTimeLength) {
194         MEDIA_ERR_LOG("invalid length, detailTime: %{public}s", detailTime.c_str());
195         return std::make_tuple("", "", "");
196     }
197 
198     const size_t yearDelimiter = 4;
199     const size_t monthDelimiter = 7;
200     const size_t dateDelimiter = 10;
201     if (detailTime[yearDelimiter] != ':' || detailTime[monthDelimiter] != ':' || detailTime[dateDelimiter] != ' ') {
202         MEDIA_ERR_LOG("invalid delimiter, detailTime: %{public}s", detailTime.c_str());
203         return std::make_tuple("", "", "");
204     }
205     const size_t yearIndex = 0;
206     const size_t yearCount = 4;
207     std::string year = detailTime.substr(yearIndex, yearCount);
208     const size_t monthIndex = 5;
209     const size_t monthCount = 2;
210     std::string month = detailTime.substr(monthIndex, monthCount);
211     const size_t dayIndex = 8;
212     const size_t dayCount = 2;
213     std::string day = detailTime.substr(dayIndex, dayCount);
214     if (!all_of(year.begin(), year.end(), ::isdigit) || !all_of(month.begin(), month.end(), ::isdigit) ||
215         !all_of(day.begin(), day.end(), ::isdigit)) {
216         MEDIA_ERR_LOG("invalid year month day, detailTime: %{public}s", detailTime.c_str());
217         return std::make_tuple("", "", "");
218     }
219 
220     return std::make_tuple(year, year + month, year + month + day);
221 }
222 } // namespace OHOS::Media