• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2023 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 <nlohmann/json.hpp>
20 
21 #include "datashare_business_error.h"
22 #include "datashare_predicates.h"
23 #include "datashare_result_set.h"
24 #include "directory_ex.h"
25 #include "media_column.h"
26 #include "media_exif.h"
27 #include "media_file_utils.h"
28 #include "media_log.h"
29 #include "medialibrary_db_const.h"
30 #include "medialibrary_helper_container.h"
31 #include "medialibrary_errno.h"
32 #include "medialibrary_type_const.h"
33 #include "sandbox_helper.h"
34 #include "uri.h"
35 #include "values_bucket.h"
36 
37 using namespace std;
38 
39 namespace OHOS {
40 namespace Media {
41 static constexpr int MAP_INT_MAX = 50;
42 using json = nlohmann::json;
FileAsset()43 FileAsset::FileAsset()
44     : albumUri_(DEFAULT_MEDIA_ALBUM_URI), resultNapiType_(ResultNapiType::TYPE_NAPI_MAX)
45 {
46     member_.reserve(MAP_INT_MAX);
47 }
48 
GetId() const49 int32_t FileAsset::GetId() const
50 {
51     return GetInt32Member(MEDIA_DATA_DB_ID);
52 }
53 
SetId(int32_t id)54 void FileAsset::SetId(int32_t id)
55 {
56     member_[MEDIA_DATA_DB_ID] = id;
57 }
58 
GetCount() const59 int32_t FileAsset::GetCount() const
60 {
61     return GetInt32Member(MEDIA_DATA_DB_COUNT);
62 }
63 
SetCount(int32_t count)64 void FileAsset::SetCount(int32_t count)
65 {
66     member_[MEDIA_DATA_DB_COUNT] = count;
67 }
68 
GetUri() const69 const string &FileAsset::GetUri() const
70 {
71     return GetStrMember(MEDIA_DATA_DB_URI);
72 }
73 
SetUri(const string & uri)74 void FileAsset::SetUri(const string &uri)
75 {
76     member_[MEDIA_DATA_DB_URI] = uri;
77 }
78 
GetPath() const79 const string &FileAsset::GetPath() const
80 {
81     return GetStrMember(MEDIA_DATA_DB_FILE_PATH);
82 }
83 
SetPath(const string & path)84 void FileAsset::SetPath(const string &path)
85 {
86     member_[MEDIA_DATA_DB_FILE_PATH] = path;
87 }
88 
GetRelativePath() const89 const string &FileAsset::GetRelativePath() const
90 {
91     return GetStrMember(MEDIA_DATA_DB_RELATIVE_PATH);
92 }
93 
SetRelativePath(const string & relativePath)94 void FileAsset::SetRelativePath(const string &relativePath)
95 {
96     member_[MEDIA_DATA_DB_RELATIVE_PATH] = relativePath;
97 }
98 
GetMimeType() const99 const string &FileAsset::GetMimeType() const
100 {
101     return GetStrMember(MEDIA_DATA_DB_MIME_TYPE);
102 }
103 
SetMimeType(const string & mimeType)104 void FileAsset::SetMimeType(const string &mimeType)
105 {
106     member_[MEDIA_DATA_DB_MIME_TYPE] = mimeType;
107 }
108 
GetMediaType() const109 MediaType FileAsset::GetMediaType() const
110 {
111     return static_cast<Media::MediaType>(GetInt32Member(MEDIA_DATA_DB_MEDIA_TYPE));
112 }
113 
SetMediaType(MediaType mediaType)114 void FileAsset::SetMediaType(MediaType mediaType)
115 {
116     member_[MEDIA_DATA_DB_MEDIA_TYPE] = mediaType;
117 }
118 
GetDisplayName() const119 const string &FileAsset::GetDisplayName() const
120 {
121     return GetStrMember(MEDIA_DATA_DB_NAME);
122 }
123 
SetDisplayName(const string & displayName)124 void FileAsset::SetDisplayName(const string &displayName)
125 {
126     member_[MEDIA_DATA_DB_NAME] = displayName;
127 }
128 
GetSize() const129 int64_t FileAsset::GetSize() const
130 {
131     return GetInt64Member(MEDIA_DATA_DB_SIZE);
132 }
133 
SetSize(int64_t size)134 void FileAsset::SetSize(int64_t size)
135 {
136     member_[MEDIA_DATA_DB_SIZE] = size;
137 }
138 
GetDateAdded() const139 int64_t FileAsset::GetDateAdded() const
140 {
141     return GetInt64Member(MEDIA_DATA_DB_DATE_ADDED);
142 }
143 
SetDateAdded(int64_t dateAdded)144 void FileAsset::SetDateAdded(int64_t dateAdded)
145 {
146     member_[MEDIA_DATA_DB_DATE_ADDED] = dateAdded;
147 }
148 
GetDateModified() const149 int64_t FileAsset::GetDateModified() const
150 {
151     return GetInt64Member(MEDIA_DATA_DB_DATE_MODIFIED);
152 }
153 
SetDateModified(int64_t dateModified)154 void FileAsset::SetDateModified(int64_t dateModified)
155 {
156     member_[MEDIA_DATA_DB_DATE_MODIFIED] = dateModified;
157 }
158 
GetTitle() const159 const string &FileAsset::GetTitle() const
160 {
161     return GetStrMember(MEDIA_DATA_DB_TITLE);
162 }
163 
SetTitle(const string & title)164 void FileAsset::SetTitle(const string &title)
165 {
166     member_[MEDIA_DATA_DB_TITLE] = title;
167 }
168 
GetArtist() const169 const string &FileAsset::GetArtist() const
170 {
171     return GetStrMember(MEDIA_DATA_DB_ARTIST);
172 }
173 
SetArtist(const string & artist)174 void FileAsset::SetArtist(const string &artist)
175 {
176     member_[MEDIA_DATA_DB_ARTIST] = artist;
177 }
178 
GetAlbum() const179 const string &FileAsset::GetAlbum() const
180 {
181     return GetStrMember(MEDIA_DATA_DB_ALBUM);
182 }
183 
SetAlbum(const string & album)184 void FileAsset::SetAlbum(const string &album)
185 {
186     member_[MEDIA_DATA_DB_ALBUM] = album;
187 }
188 
GetPosition() const189 int32_t FileAsset::GetPosition() const
190 {
191     return GetInt32Member(MEDIA_DATA_DB_POSITION);
192 }
193 
SetPosition(int32_t position)194 void FileAsset::SetPosition(int32_t position)
195 {
196     member_[MEDIA_DATA_DB_POSITION] = position;
197 }
198 
GetWidth() const199 int32_t FileAsset::GetWidth() const
200 {
201     return GetInt32Member(MEDIA_DATA_DB_WIDTH);
202 }
203 
SetWidth(int32_t width)204 void FileAsset::SetWidth(int32_t width)
205 {
206     member_[MEDIA_DATA_DB_WIDTH] = width;
207 }
208 
GetHeight() const209 int32_t FileAsset::GetHeight() const
210 {
211     return GetInt32Member(MEDIA_DATA_DB_HEIGHT);
212 }
213 
SetHeight(int32_t height)214 void FileAsset::SetHeight(int32_t height)
215 {
216     member_[MEDIA_DATA_DB_HEIGHT] = height;
217 }
218 
GetDuration() const219 int32_t FileAsset::GetDuration() const
220 {
221     return GetInt32Member(MEDIA_DATA_DB_DURATION);
222 }
223 
SetDuration(int32_t duration)224 void FileAsset::SetDuration(int32_t duration)
225 {
226     member_[MEDIA_DATA_DB_DURATION] = duration;
227 }
228 
GetOrientation() const229 int32_t FileAsset::GetOrientation() const
230 {
231     return GetInt32Member(MEDIA_DATA_DB_ORIENTATION);
232 }
233 
SetOrientation(int32_t orientation)234 void FileAsset::SetOrientation(int32_t orientation)
235 {
236     member_[MEDIA_DATA_DB_ORIENTATION] = orientation;
237 }
238 
GetAlbumId() const239 int32_t FileAsset::GetAlbumId() const
240 {
241     return GetInt32Member(MEDIA_DATA_DB_BUCKET_ID);
242 }
243 
SetAlbumId(int32_t albumId)244 void FileAsset::SetAlbumId(int32_t albumId)
245 {
246     member_[MEDIA_DATA_DB_BUCKET_ID] = albumId;
247 }
248 
GetAlbumName() const249 const string &FileAsset::GetAlbumName() const
250 {
251     return GetStrMember(MEDIA_DATA_DB_BUCKET_NAME);
252 }
253 
SetAlbumName(const string & albumName)254 void FileAsset::SetAlbumName(const string &albumName)
255 {
256     member_[MEDIA_DATA_DB_BUCKET_NAME] = albumName;
257 }
258 
GetParent() const259 int32_t FileAsset::GetParent() const
260 {
261     return GetInt32Member(MEDIA_DATA_DB_PARENT_ID);
262 }
263 
SetParent(int32_t parent)264 void FileAsset::SetParent(int32_t parent)
265 {
266     member_[MEDIA_DATA_DB_PARENT_ID] = parent;
267 }
268 
GetAlbumUri() const269 const string &FileAsset::GetAlbumUri() const
270 {
271     return albumUri_;
272 }
273 
SetAlbumUri(const string & albumUri)274 void FileAsset::SetAlbumUri(const string &albumUri)
275 {
276     albumUri_ = albumUri;
277 }
278 
GetDateTaken() const279 int64_t FileAsset::GetDateTaken() const
280 {
281     return GetInt64Member(MEDIA_DATA_DB_DATE_TAKEN);
282 }
283 
SetDateTaken(int64_t dateTaken)284 void FileAsset::SetDateTaken(int64_t dateTaken)
285 {
286     member_[MEDIA_DATA_DB_DATE_TAKEN] = dateTaken;
287 }
288 
GetTimePending() const289 int64_t FileAsset::GetTimePending() const
290 {
291     return GetInt64Member(MEDIA_DATA_DB_TIME_PENDING);
292 }
293 
SetTimePending(int64_t timePending)294 void FileAsset::SetTimePending(int64_t timePending)
295 {
296     member_[MEDIA_DATA_DB_TIME_PENDING] = timePending;
297 }
298 
IsFavorite() const299 bool FileAsset::IsFavorite() const
300 {
301     return GetInt32Member(MEDIA_DATA_DB_IS_FAV);
302 }
303 
SetFavorite(bool isFavorite)304 void FileAsset::SetFavorite(bool isFavorite)
305 {
306     member_[MEDIA_DATA_DB_IS_FAV] = isFavorite;
307 }
308 
GetLatitude()309 int64_t FileAsset::GetLatitude()
310 {
311     return GetInt64Member(MEDIA_DATA_DB_LATITUDE);
312 }
313 
SetLatitude(int64_t latitude)314 void FileAsset::SetLatitude(int64_t latitude)
315 {
316     member_[MEDIA_DATA_DB_LATITUDE] = latitude;
317 }
318 
GetLongitude()319 int64_t FileAsset::GetLongitude()
320 {
321     return GetInt64Member(MEDIA_DATA_DB_LONGITUDE);
322 }
323 
SetLongitude(int64_t longitude)324 void FileAsset::SetLongitude(int64_t longitude)
325 {
326     member_[MEDIA_DATA_DB_LONGITUDE] = longitude;
327 }
328 
SetPhotoIdAndQuality(const string & photoId,int photoQuality)329 void FileAsset::SetPhotoIdAndQuality(const string &photoId, int photoQuality)
330 {
331     member_[MEDIA_DATA_DB_PHOTO_ID] = photoId;
332     member_[MEDIA_DATA_DB_PHOTO_QUALITY] = photoQuality;
333 }
334 
GetPhotoIdAndQuality() const335 pair<string, int> FileAsset::GetPhotoIdAndQuality() const
336 {
337     return make_pair(GetStrMember(MEDIA_DATA_DB_PHOTO_ID), GetInt32Member(MEDIA_DATA_DB_PHOTO_QUALITY));
338 }
339 
GetDateTrashed() const340 int64_t FileAsset::GetDateTrashed() const
341 {
342     return GetInt64Member(MEDIA_DATA_DB_DATE_TRASHED);
343 }
344 
SetDateTrashed(int64_t dateTrashed)345 void FileAsset::SetDateTrashed(int64_t dateTrashed)
346 {
347     member_[MEDIA_DATA_DB_DATE_TRASHED] = dateTrashed;
348 }
349 
GetSelfId() const350 const string &FileAsset::GetSelfId() const
351 {
352     return GetStrMember(MEDIA_DATA_DB_SELF_ID);
353 }
354 
SetSelfId(const string & selfId)355 void FileAsset::SetSelfId(const string &selfId)
356 {
357     member_[MEDIA_DATA_DB_SELF_ID] = selfId;
358 }
359 
GetIsTrash() const360 int32_t FileAsset::GetIsTrash() const
361 {
362     if (resultNapiType_ == ResultNapiType::TYPE_USERFILE_MGR ||
363         resultNapiType_ == ResultNapiType::TYPE_PHOTOACCESS_HELPER) {
364         return static_cast<int32_t>(GetInt64Member(MediaColumn::MEDIA_DATE_TRASHED));
365     }
366 
367     return GetInt32Member(MEDIA_DATA_DB_IS_TRASH);
368 }
369 
SetIsTrash(int32_t isTrash)370 void FileAsset::SetIsTrash(int32_t isTrash)
371 {
372     member_[MEDIA_DATA_DB_IS_TRASH] = isTrash;
373 }
374 
GetRecyclePath() const375 const string &FileAsset::GetRecyclePath() const
376 {
377     return GetStrMember(MEDIA_DATA_DB_RECYCLE_PATH);
378 }
379 
SetRecyclePath(const string & recyclePath)380 void FileAsset::SetRecyclePath(const string &recyclePath)
381 {
382     member_[MEDIA_DATA_DB_RECYCLE_PATH] = recyclePath;
383 }
384 
GetOwnerPackage() const385 const string FileAsset::GetOwnerPackage() const
386 {
387     return GetStrMember(MEDIA_DATA_DB_OWNER_PACKAGE);
388 }
389 
SetOwnerPackage(const string & ownerPackage)390 void FileAsset::SetOwnerPackage(const string &ownerPackage)
391 {
392     member_[MEDIA_DATA_DB_OWNER_PACKAGE] = ownerPackage;
393 }
394 
GetResultNapiType() const395 ResultNapiType FileAsset::GetResultNapiType() const
396 {
397     return resultNapiType_;
398 }
399 
GetPackageName() const400 const string FileAsset::GetPackageName() const
401 {
402     return GetStrMember(MediaColumn::MEDIA_PACKAGE_NAME);
403 }
404 
SetPackageName(const string & packageName)405 void FileAsset::SetPackageName(const string &packageName)
406 {
407     member_[MediaColumn::MEDIA_PACKAGE_NAME] = packageName;
408 }
409 
SetResultNapiType(const ResultNapiType type)410 void FileAsset::SetResultNapiType(const ResultNapiType type)
411 {
412     resultNapiType_ = type;
413 }
414 
GetPhotoSubType() const415 int32_t FileAsset::GetPhotoSubType() const
416 {
417     return GetInt32Member(PhotoColumn::PHOTO_SUBTYPE);
418 }
419 
SetPhotoSubType(int32_t photoSubType)420 void FileAsset::SetPhotoSubType(int32_t photoSubType)
421 {
422     member_[PhotoColumn::PHOTO_SUBTYPE] = photoSubType;
423 }
424 
GetCameraShotKey() const425 const std::string &FileAsset::GetCameraShotKey() const
426 {
427     return GetStrMember(PhotoColumn::CAMERA_SHOT_KEY);
428 }
429 
SetCameraShotKey(const std::string & cameraShotKey)430 void FileAsset::SetCameraShotKey(const std::string &cameraShotKey)
431 {
432     member_[PhotoColumn::CAMERA_SHOT_KEY] = cameraShotKey;
433 }
434 
IsHidden() const435 bool FileAsset::IsHidden() const
436 {
437     return GetInt32Member(MediaColumn::MEDIA_HIDDEN);
438 }
439 
SetHidden(bool isHidden)440 void FileAsset::SetHidden(bool isHidden)
441 {
442     member_[MediaColumn::MEDIA_HIDDEN] = isHidden;
443 }
444 
GetAllExif() const445 const std::string &FileAsset::GetAllExif() const
446 {
447     return GetStrMember(PhotoColumn::PHOTO_ALL_EXIF);
448 }
449 
SetAllExif(const string & allExif)450 void FileAsset::SetAllExif(const string &allExif)
451 {
452     member_[PhotoColumn::PHOTO_ALL_EXIF] = allExif;
453 }
454 
GetUserComment() const455 const std::string &FileAsset::GetUserComment() const
456 {
457     return GetStrMember(PhotoColumn::PHOTO_USER_COMMENT);
458 }
459 
SetUserComment(const string & userComment)460 void FileAsset::SetUserComment(const string &userComment)
461 {
462     member_[PhotoColumn::PHOTO_USER_COMMENT] = userComment;
463 }
464 
GetFilePath() const465 const std::string &FileAsset::GetFilePath() const
466 {
467     return GetStrMember(MediaColumn::MEDIA_FILE_PATH);
468 }
469 
SetFilePath(const std::string & filePath)470 void FileAsset::SetFilePath(const std::string &filePath)
471 {
472     member_[MediaColumn::MEDIA_FILE_PATH] = filePath;
473 }
474 
GetPhotoEditTime() const475 int64_t FileAsset::GetPhotoEditTime() const
476 {
477     return GetInt64Member(PhotoColumn::PHOTO_EDIT_TIME);
478 }
479 
SetPhotoEditTime(int64_t photoEditTime)480 void FileAsset::SetPhotoEditTime(int64_t photoEditTime)
481 {
482     member_[PhotoColumn::PHOTO_EDIT_TIME] = photoEditTime;
483 }
484 
SetOpenStatus(int32_t fd,int32_t openStatus)485 void FileAsset::SetOpenStatus(int32_t fd, int32_t openStatus)
486 {
487     lock_guard<mutex> lock(openStatusMapMutex_);
488     if (openStatusMap_ == nullptr) {
489         openStatusMap_ = make_shared<unordered_map<int32_t, int32_t>>();
490     }
491     openStatusMap_->insert({fd, openStatus});
492 }
493 
RemoveOpenStatus(int32_t fd)494 void FileAsset::RemoveOpenStatus(int32_t fd)
495 {
496     lock_guard<mutex> lock(openStatusMapMutex_);
497     if (openStatusMap_ == nullptr) {
498         return;
499     }
500     openStatusMap_->erase(fd);
501 }
502 
GetOpenStatus(int32_t fd)503 int32_t FileAsset::GetOpenStatus(int32_t fd)
504 {
505     lock_guard<mutex> lock(openStatusMapMutex_);
506     if (openStatusMap_ == nullptr) {
507         return E_INVALID_VALUES;
508     }
509     if (openStatusMap_->find(fd) != openStatusMap_->end()) {
510         return openStatusMap_->at(fd);
511     } else {
512         MEDIA_ERR_LOG("can not find this fd: [%{public}d]", fd);
513         return E_INVALID_VALUES;
514     }
515 }
516 
GetMemberMap()517 unordered_map<string, variant<int32_t, int64_t, string, double>> &FileAsset::GetMemberMap()
518 {
519     return member_;
520 }
521 
GetMemberValue(const string & name)522 variant<int32_t, int64_t, string, double> &FileAsset::GetMemberValue(const string &name)
523 {
524     return member_[name];
525 }
526 
GetStrMember(const string & name) const527 const string &FileAsset::GetStrMember(const string &name) const
528 {
529     return (member_.count(name) > 0) ? get<string>(member_.at(name)) : DEFAULT_STR;
530 }
531 
GetInt32Member(const string & name) const532 int32_t FileAsset::GetInt32Member(const string &name) const
533 {
534     return (member_.count(name) > 0) ? get<int32_t>(member_.at(name)) : DEFAULT_INT32;
535 }
536 
GetInt64Member(const string & name) const537 int64_t FileAsset::GetInt64Member(const string &name) const
538 {
539     return (member_.count(name) > 0) ? get<int64_t>(member_.at(name)) : DEFAULT_INT64;
540 }
541 
GetPhotoIndex() const542 int32_t FileAsset::GetPhotoIndex() const
543 {
544     return GetInt32Member(PHOTO_INDEX);
545 }
546 
SetResultTypeMap(const string & colName,ResultSetDataType type)547 void FileAsset::SetResultTypeMap(const string &colName, ResultSetDataType type)
548 {
549     if (resultTypeMap_.count(colName) != 0) {
550         return;
551     }
552     resultTypeMap_.insert(make_pair(colName, type));
553 }
GetAssetJson()554 string FileAsset::GetAssetJson()
555 {
556     json jsonObject;
557     for (auto &[colName, _]  : member_) {
558         if (resultTypeMap_.count(colName) == 0) {
559             continue;
560         }
561         switch (resultTypeMap_.at(colName)) {
562             case TYPE_STRING:
563                 jsonObject[colName] = GetStrMember(colName);
564                 break;
565             case TYPE_INT32:
566                 jsonObject[colName] = GetInt32Member(colName);
567                 break;
568             case TYPE_INT64:
569                 jsonObject[colName] = GetInt64Member(colName);
570                 break;
571             default:
572                 break;
573         }
574     }
575     jsonObject[PHOTO_DATA_IMAGE_IMAGE_DESCRIPTION] =
576         AppFileService::SandboxHelper::Decode(jsonObject[PHOTO_DATA_IMAGE_IMAGE_DESCRIPTION]);
577     return jsonObject.dump();
578 }
579 }  // namespace Media
580 }  // namespace OHOS
581