• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "FileAsset"
16 
17 #include "file_asset.h"
18 
19 #include <fcntl.h>
20 #include <fstream>
21 #include <unistd.h>
22 
23 #include "directory_ex.h"
24 #include "media_file_utils.h"
25 #include "media_log.h"
26 #include "medialibrary_db_const.h"
27 #include "medialibrary_errno.h"
28 #include "medialibrary_type_const.h"
29 
30 using namespace std;
31 
32 namespace OHOS {
33 namespace Media {
FileAsset()34 FileAsset::FileAsset()
35     : albumUri_(DEFAULT_MEDIA_ALBUM_URI),
36     typeMask_(DEFAULT_TYPE_MASK),
37     resultNapiType_(ResultNapiType::TYPE_NAPI_MAX)
38 {}
39 
GetId() const40 int32_t FileAsset::GetId() const
41 {
42     return GetInt32Member(MEDIA_DATA_DB_ID);
43 }
44 
SetId(int32_t id)45 void FileAsset::SetId(int32_t id)
46 {
47     member_[MEDIA_DATA_DB_ID] = id;
48 }
49 
GetCount() const50 int32_t FileAsset::GetCount() const
51 {
52     return count_;
53 }
54 
SetCount(int32_t count)55 void FileAsset::SetCount(int32_t count)
56 {
57     count_ = count;
58 }
59 
GetUri() const60 const string &FileAsset::GetUri() const
61 {
62     return GetStrMember(MEDIA_DATA_DB_URI);
63 }
64 
SetUri(const string & uri)65 void FileAsset::SetUri(const string &uri)
66 {
67     member_[MEDIA_DATA_DB_URI] = uri;
68 }
69 
GetPath() const70 const string &FileAsset::GetPath() const
71 {
72     return GetStrMember(MEDIA_DATA_DB_FILE_PATH);
73 }
74 
SetPath(const string & path)75 void FileAsset::SetPath(const string &path)
76 {
77     member_[MEDIA_DATA_DB_FILE_PATH] = path;
78 }
79 
GetRelativePath() const80 const string &FileAsset::GetRelativePath() const
81 {
82     return GetStrMember(MEDIA_DATA_DB_RELATIVE_PATH);
83 }
84 
SetRelativePath(const std::string & relativePath)85 void FileAsset::SetRelativePath(const std::string &relativePath)
86 {
87     member_[MEDIA_DATA_DB_RELATIVE_PATH] = relativePath;
88 }
89 
GetMimeType() const90 const string &FileAsset::GetMimeType() const
91 {
92     return GetStrMember(MEDIA_DATA_DB_MIME_TYPE);
93 }
94 
SetMimeType(const string & mimeType)95 void FileAsset::SetMimeType(const string &mimeType)
96 {
97     member_[MEDIA_DATA_DB_MIME_TYPE] = mimeType;
98 }
99 
GetMediaType() const100 MediaType FileAsset::GetMediaType() const
101 {
102     return static_cast<Media::MediaType>(GetInt32Member(MEDIA_DATA_DB_MEDIA_TYPE));
103 }
104 
SetMediaType(MediaType mediaType)105 void FileAsset::SetMediaType(MediaType mediaType)
106 {
107     member_[MEDIA_DATA_DB_MEDIA_TYPE] = mediaType;
108 }
109 
GetDisplayName() const110 const string &FileAsset::GetDisplayName() const
111 {
112     return GetStrMember(MEDIA_DATA_DB_NAME);
113 }
114 
SetDisplayName(const string & displayName)115 void FileAsset::SetDisplayName(const string &displayName)
116 {
117     member_[MEDIA_DATA_DB_NAME] = displayName;
118 }
119 
GetSize() const120 int64_t FileAsset::GetSize() const
121 {
122     return GetInt64Member(MEDIA_DATA_DB_SIZE);
123 }
124 
SetSize(int64_t size)125 void FileAsset::SetSize(int64_t size)
126 {
127     member_[MEDIA_DATA_DB_SIZE] = size;
128 }
129 
GetDateAdded() const130 int64_t FileAsset::GetDateAdded() const
131 {
132     return GetInt64Member(MEDIA_DATA_DB_DATE_ADDED);
133 }
134 
SetDateAdded(int64_t dateAdded)135 void FileAsset::SetDateAdded(int64_t dateAdded)
136 {
137     member_[MEDIA_DATA_DB_DATE_ADDED] = dateAdded;
138 }
139 
GetDateModified() const140 int64_t FileAsset::GetDateModified() const
141 {
142     return GetInt64Member(MEDIA_DATA_DB_DATE_MODIFIED);
143 }
144 
SetDateModified(int64_t dateModified)145 void FileAsset::SetDateModified(int64_t dateModified)
146 {
147     member_[MEDIA_DATA_DB_DATE_MODIFIED] = dateModified;
148 }
149 
GetTitle() const150 const string &FileAsset::GetTitle() const
151 {
152     return GetStrMember(MEDIA_DATA_DB_TITLE);
153 }
154 
SetTitle(const string & title)155 void FileAsset::SetTitle(const string &title)
156 {
157     member_[MEDIA_DATA_DB_TITLE] = title;
158 }
159 
GetArtist() const160 const string &FileAsset::GetArtist() const
161 {
162     return GetStrMember(MEDIA_DATA_DB_ARTIST);
163 }
164 
SetArtist(const string & artist)165 void FileAsset::SetArtist(const string &artist)
166 {
167     member_[MEDIA_DATA_DB_ARTIST] = artist;
168 }
169 
GetAlbum() const170 const string &FileAsset::GetAlbum() const
171 {
172     return GetStrMember(MEDIA_DATA_DB_ALBUM);
173 }
174 
SetAlbum(const string & album)175 void FileAsset::SetAlbum(const string &album)
176 {
177     member_[MEDIA_DATA_DB_ALBUM] = album;
178 }
179 
GetWidth() const180 int32_t FileAsset::GetWidth() const
181 {
182     return GetInt32Member(MEDIA_DATA_DB_WIDTH);
183 }
184 
SetWidth(int32_t width)185 void FileAsset::SetWidth(int32_t width)
186 {
187     member_[MEDIA_DATA_DB_WIDTH] = width;
188 }
189 
GetHeight() const190 int32_t FileAsset::GetHeight() const
191 {
192     return GetInt32Member(MEDIA_DATA_DB_HEIGHT);
193 }
194 
SetHeight(int32_t height)195 void FileAsset::SetHeight(int32_t height)
196 {
197     member_[MEDIA_DATA_DB_HEIGHT] = height;
198 }
199 
GetDuration() const200 int32_t FileAsset::GetDuration() const
201 {
202     return GetInt32Member(MEDIA_DATA_DB_DURATION);
203 }
204 
SetDuration(int32_t duration)205 void FileAsset::SetDuration(int32_t duration)
206 {
207     member_[MEDIA_DATA_DB_DURATION] = duration;
208 }
209 
GetOrientation() const210 int32_t FileAsset::GetOrientation() const
211 {
212     return GetInt32Member(MEDIA_DATA_DB_ORIENTATION);
213 }
214 
SetOrientation(int32_t orientation)215 void FileAsset::SetOrientation(int32_t orientation)
216 {
217     member_[MEDIA_DATA_DB_ORIENTATION] = orientation;
218 }
219 
GetAlbumId() const220 int32_t FileAsset::GetAlbumId() const
221 {
222     return GetInt32Member(MEDIA_DATA_DB_BUCKET_ID);
223 }
224 
SetAlbumId(int32_t albumId)225 void FileAsset::SetAlbumId(int32_t albumId)
226 {
227     member_[MEDIA_DATA_DB_BUCKET_ID] = albumId;
228 }
229 
GetAlbumName() const230 const string &FileAsset::GetAlbumName() const
231 {
232     return GetStrMember(MEDIA_DATA_DB_BUCKET_NAME);
233 }
234 
SetAlbumName(const string & albumName)235 void FileAsset::SetAlbumName(const string &albumName)
236 {
237     member_[MEDIA_DATA_DB_BUCKET_NAME] = albumName;
238 }
239 
GetParent() const240 int32_t FileAsset::GetParent() const
241 {
242     return GetInt32Member(MEDIA_DATA_DB_PARENT_ID);
243 }
244 
SetParent(int32_t parent)245 void FileAsset::SetParent(int32_t parent)
246 {
247     member_[MEDIA_DATA_DB_PARENT_ID] = parent;
248 }
249 
GetAlbumUri() const250 const string &FileAsset::GetAlbumUri() const
251 {
252     return albumUri_;
253 }
254 
SetAlbumUri(const string & albumUri)255 void FileAsset::SetAlbumUri(const string &albumUri)
256 {
257     albumUri_ = albumUri;
258 }
259 
GetTypeMask() const260 const string &FileAsset::GetTypeMask() const
261 {
262     return typeMask_;
263 }
264 
SetTypeMask(const string & typeMask)265 void FileAsset::SetTypeMask(const string &typeMask)
266 {
267     typeMask_ = typeMask;
268 }
269 
GetDateTaken() const270 int64_t FileAsset::GetDateTaken() const
271 {
272     return GetInt64Member(MEDIA_DATA_DB_DATE_TAKEN);
273 }
274 
SetDateTaken(int64_t dateTaken)275 void FileAsset::SetDateTaken(int64_t dateTaken)
276 {
277     member_[MEDIA_DATA_DB_DATE_TAKEN] = dateTaken;
278 }
279 
IsPending() const280 bool FileAsset::IsPending() const
281 {
282     return GetInt32Member(MEDIA_DATA_DB_IS_PENDING);
283 }
284 
SetPending(bool dateTaken)285 void FileAsset::SetPending(bool dateTaken)
286 {
287     member_[MEDIA_DATA_DB_IS_PENDING] = dateTaken;
288 }
289 
GetTimePending() const290 int64_t FileAsset::GetTimePending() const
291 {
292     return GetInt64Member(MEDIA_DATA_DB_TIME_PENDING);
293 }
294 
SetTimePending(int64_t timePending)295 void FileAsset::SetTimePending(int64_t timePending)
296 {
297     member_[MEDIA_DATA_DB_TIME_PENDING] = timePending;
298 }
299 
IsFavorite() const300 bool FileAsset::IsFavorite() const
301 {
302     return GetInt32Member(MEDIA_DATA_DB_IS_FAV);
303 }
304 
SetFavorite(bool isFavorite)305 void FileAsset::SetFavorite(bool isFavorite)
306 {
307     member_[MEDIA_DATA_DB_IS_FAV] = isFavorite;
308 }
309 
GetDateTrashed() const310 int64_t FileAsset::GetDateTrashed() const
311 {
312     return GetInt64Member(MEDIA_DATA_DB_DATE_TRASHED);
313 }
314 
SetDateTrashed(int64_t dateTrashed)315 void FileAsset::SetDateTrashed(int64_t dateTrashed)
316 {
317     member_[MEDIA_DATA_DB_DATE_TRASHED] = dateTrashed;
318 }
319 
GetSelfId() const320 const string &FileAsset::GetSelfId() const
321 {
322     return GetStrMember(MEDIA_DATA_DB_SELF_ID);
323 }
324 
SetSelfId(const string & selfId)325 void FileAsset::SetSelfId(const string &selfId)
326 {
327     member_[MEDIA_DATA_DB_SELF_ID] = selfId;
328 }
329 
GetIsTrash() const330 int32_t FileAsset::GetIsTrash() const
331 {
332     return GetInt32Member(MEDIA_DATA_DB_IS_TRASH);
333 }
334 
SetIsTrash(int32_t isTrash)335 void FileAsset::SetIsTrash(int32_t isTrash)
336 {
337     member_[MEDIA_DATA_DB_IS_TRASH] = isTrash;
338 }
339 
GetRecyclePath() const340 const string &FileAsset::GetRecyclePath() const
341 {
342     return GetStrMember(MEDIA_DATA_DB_RECYCLE_PATH);
343 }
344 
SetRecyclePath(const string & recyclePath)345 void FileAsset::SetRecyclePath(const string &recyclePath)
346 {
347     member_[MEDIA_DATA_DB_RECYCLE_PATH] = recyclePath;
348 }
349 
GetResultNapiType() const350 ResultNapiType FileAsset::GetResultNapiType() const
351 {
352     return resultNapiType_;
353 }
354 
SetResultNapiType(const ResultNapiType type)355 void FileAsset::SetResultNapiType(const ResultNapiType type)
356 {
357     resultNapiType_ = type;
358 }
359 
CreateAsset(const string & filePath)360 int32_t FileAsset::CreateAsset(const string &filePath)
361 {
362     MEDIA_ERR_LOG("CreateAsset in");
363     int32_t errCode = E_ERR;
364 
365     if (filePath.empty()) {
366         MEDIA_ERR_LOG("Filepath is empty");
367         return E_VIOLATION_PARAMETERS;
368     }
369 
370     if (MediaFileUtils::IsFileExists(filePath)) {
371         MEDIA_ERR_LOG("the file exists path: %{private}s", filePath.c_str());
372         return E_FILE_EXIST;
373     }
374 
375     size_t slashIndex = filePath.rfind('/');
376     if (slashIndex != string::npos) {
377         string fileName = filePath.substr(slashIndex + 1);
378         if (!fileName.empty() && fileName.at(0) != '.') {
379             size_t dotIndex = filePath.rfind('.');
380             if ((dotIndex == string::npos) && (GetMediaType() != MEDIA_TYPE_FILE)) {
381                 return errCode;
382             }
383         }
384     }
385 
386     ofstream file(filePath);
387     if (!file) {
388         MEDIA_ERR_LOG("Output file path could not be created errno %{public}d", errno);
389         return errCode;
390     }
391 
392     file.close();
393 
394     return E_SUCCESS;
395 }
396 
ModifyAsset(const string & oldPath,const string & newPath)397 int32_t FileAsset::ModifyAsset(const string &oldPath, const string &newPath)
398 {
399     int32_t err = E_MODIFY_DATA_FAIL;
400 
401     if (oldPath.empty() || newPath.empty()) {
402         MEDIA_ERR_LOG("Failed to modify asset, oldPath: %{private}s or newPath: %{private}s is empty!",
403             oldPath.c_str(), newPath.c_str());
404         return err;
405     }
406     if (!MediaFileUtils::IsFileExists(oldPath)) {
407         MEDIA_ERR_LOG("Failed to modify asset, oldPath: %{private}s does not exist!", oldPath.c_str());
408         return E_NO_SUCH_FILE;
409     }
410     if (MediaFileUtils::IsFileExists(newPath)) {
411         MEDIA_ERR_LOG("Failed to modify asset, newPath: %{private}s is already exist!", newPath.c_str());
412         return E_FILE_EXIST;
413     }
414     err = rename(oldPath.c_str(), newPath.c_str());
415     if (err < 0) {
416         MEDIA_ERR_LOG("Failed ModifyAsset errno %{public}d", errno);
417         return E_FILE_OPER_FAIL;
418     }
419 
420     return E_SUCCESS;
421 }
422 
IsFileExists(const string & filePath)423 bool FileAsset::IsFileExists(const string &filePath)
424 {
425     return MediaFileUtils::IsFileExists(filePath);
426 }
427 
DeleteAsset(const string & filePath)428 int32_t FileAsset::DeleteAsset(const string &filePath)
429 {
430     int32_t errCode = E_ERR;
431     if (!MediaFileUtils::IsDirectory(filePath)) {
432         errCode = remove(filePath.c_str());
433     } else {
434         errCode = MediaFileUtils::RemoveDirectory(filePath);
435     }
436     if (errCode != E_SUCCESS) {
437         MEDIA_ERR_LOG("DeleteAsset failed, filePath: %{private}s, errno: %{public}d, errmsg: %{public}s",
438             filePath.c_str(), errno, strerror(errno));
439     }
440     return errCode;
441 }
442 
OpenAsset(const string & filePath,const string & mode)443 int32_t FileAsset::OpenAsset(const string &filePath, const string &mode)
444 {
445     return MediaFileUtils::OpenFile(filePath, mode);
446 }
447 
GetMemberMap()448 std::unordered_map<std::string, std::variant<int32_t, int64_t, std::string>> &FileAsset::GetMemberMap()
449 {
450     return member_;
451 }
452 
GetMemberValue(const std::string & name)453 std::variant<int32_t, int64_t, std::string> &FileAsset::GetMemberValue(const std::string &name)
454 {
455     return member_[name];
456 }
457 
GetStrMember(const std::string & name) const458 const string &FileAsset::GetStrMember(const std::string &name) const
459 {
460     return (member_.count(name) > 0) ? get<string>(member_.at(name)) : DEFAULT_STR;
461 }
462 
GetInt32Member(const std::string & name) const463 int32_t FileAsset::GetInt32Member(const std::string &name) const
464 {
465     return (member_.count(name) > 0) ? get<int32_t>(member_.at(name)) : DEFAULT_INT32;
466 }
467 
GetInt64Member(const std::string & name) const468 int64_t FileAsset::GetInt64Member(const std::string &name) const
469 {
470     return (member_.count(name) > 0) ? get<int64_t>(member_.at(name)) : DEFAULT_INT64;
471 }
472 }  // namespace Media
473 }  // namespace OHOS
474