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 "lcd_thumbnail_helper.h"
20 #include "medialibrary_errno.h"
21 #include "media_log.h"
22 #include "thumbnail_const.h"
23 #include "thumbnail_utils.h"
24 #include "thumbnail_utils.h"
25
26 using namespace std;
27 using namespace OHOS::DistributedKv;
28 using namespace OHOS::NativeRdb;
29
30 namespace OHOS {
31 namespace Media {
CreateThumbnail(ThumbRdbOpt & opts,bool isSync)32 int32_t LcdThumbnailHelper::CreateThumbnail(ThumbRdbOpt &opts, bool isSync)
33 {
34 ThumbnailData thumbnailData;
35 GetThumbnailInfo(opts, thumbnailData);
36
37 string fileName = GetThumbnailPath(thumbnailData.path, THUMBNAIL_LCD_SUFFIX);
38 if (access(fileName.c_str(), F_OK) == 0) {
39 MEDIA_DEBUG_LOG("CreateThumbnail key is same, no need generate");
40 return E_OK;
41 }
42
43 if (isSync) {
44 DoCreateLcd(opts, thumbnailData);
45 } else {
46 IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateLcd, opts, thumbnailData, true);
47 }
48 return E_OK;
49 }
50
GetThumbnailPixelMap(ThumbRdbOpt & opts)51 int32_t LcdThumbnailHelper::GetThumbnailPixelMap(ThumbRdbOpt &opts)
52 {
53 int err;
54 ThumbnailWait thumbnailWait(false);
55 thumbnailWait.CheckAndWait(opts.row, true);
56 ThumbnailData thumbnailData;
57 GetThumbnailInfo(opts, thumbnailData);
58
59 string fileName = GetThumbnailPath(thumbnailData.path, THUMBNAIL_LCD_SUFFIX);
60 if (access(fileName.c_str(), F_OK) != 0) {
61 if (!DoCreateLcd(opts, thumbnailData)) {
62 return E_THUMBNAIL_LOCAL_CREATE_FAIL;
63 }
64 if (!opts.path.empty()) {
65 fileName = GetThumbnailPath(thumbnailData.path, THUMBNAIL_LCD_SUFFIX);
66 }
67 }
68 auto fd = open(fileName.c_str(), O_RDONLY);
69 if (fd >= 0) {
70 ThumbnailUtils::UpdateVisitTime(opts, thumbnailData, err);
71 return fd;
72 }
73 return -errno;
74 }
75 } // namespace Media
76 } // namespace OHOS
77