• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "Thumbnail"
16 
17 #include <fcntl.h>
18 
19 #include "default_thumbnail_helper.h"
20 #include "medialibrary_errno.h"
21 #include "media_log.h"
22 #include "thumbnail_const.h"
23 #include "thumbnail_utils.h"
24 
25 using namespace std;
26 using namespace OHOS::DistributedKv;
27 using namespace OHOS::NativeRdb;
28 
29 namespace OHOS {
30 namespace Media {
CreateThumbnail(ThumbRdbOpt & opts,bool isSync)31 int32_t DefaultThumbnailHelper::CreateThumbnail(ThumbRdbOpt &opts, bool isSync)
32 {
33     ThumbnailData thumbnailData;
34     GetThumbnailInfo(opts, thumbnailData);
35 
36     string fileName = GetThumbnailPath(thumbnailData.path, THUMBNAIL_THUMB_SUFFIX);
37     if (access(fileName.c_str(), F_OK) == 0) {
38         MEDIA_DEBUG_LOG("CreateThumbnail key is same, no need generate");
39         return E_OK;
40     }
41 
42     if (isSync) {
43         DoCreateThumbnail(opts, thumbnailData);
44     } else {
45         IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateThumbnail, opts, thumbnailData, true);
46     }
47     return E_OK;
48 }
49 
GetThumbnailPixelMap(ThumbRdbOpt & opts)50 int32_t DefaultThumbnailHelper::GetThumbnailPixelMap(ThumbRdbOpt &opts)
51 {
52     ThumbnailWait thumbnailWait(false);
53     thumbnailWait.CheckAndWait(opts.row, false);
54     ThumbnailData thumbnailData;
55     GetThumbnailInfo(opts, thumbnailData);
56 
57     string fileName = GetThumbnailPath(thumbnailData.path, THUMBNAIL_THUMB_SUFFIX);
58     if (access(fileName.c_str(), F_OK) != 0) {
59         if (!DoCreateThumbnail(opts, thumbnailData)) {
60             return E_THUMBNAIL_LOCAL_CREATE_FAIL;
61         }
62         if (!opts.path.empty()) {
63             fileName = GetThumbnailPath(thumbnailData.path, THUMBNAIL_THUMB_SUFFIX);
64         }
65     }
66     auto fd = open(fileName.c_str(), O_RDONLY);
67     if (fd >= 0) {
68         return fd;
69     }
70     return -errno;
71 }
72 } // namespace Media
73 } // namespace OHOS
74