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 "lcd_thumbnail_helper.h"
18 #include "medialibrary_errno.h"
19 #include "media_log.h"
20 #include "thumbnail_const.h"
21 #include "thumbnail_utils.h"
22
23 using namespace std;
24 using namespace OHOS::DistributedKv;
25 using namespace OHOS::NativeRdb;
26
27 namespace OHOS {
28 namespace Media {
CreateThumbnail(ThumbRdbOpt & opts,bool isSync)29 int32_t LcdThumbnailHelper::CreateThumbnail(ThumbRdbOpt &opts, bool isSync)
30 {
31 int err = E_ERR;
32 ThumbnailData thumbnailData;
33 shared_ptr<AbsSharedResultSet> rdbSet = QueryThumbnailInfo(opts, thumbnailData, err);
34 if (rdbSet == nullptr) {
35 MEDIA_ERR_LOG("QueryThumbnailInfo Faild [ %{public}d ]", err);
36 return err;
37 }
38
39 if (!thumbnailData.lcdKey.empty()) {
40 ThumbnailData tmpData = thumbnailData;
41 ThumbnailUtils::GenLcdKey(tmpData);
42 if (tmpData.lcdKey == thumbnailData.lcdKey) {
43 MEDIA_DEBUG_LOG("CreateLcd key is same, no need generate");
44 return E_OK;
45 }
46 }
47
48 if (isSync) {
49 DoCreateLcd(opts, thumbnailData, true);
50 } else {
51 IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateLcd, opts, thumbnailData, true);
52 }
53 return E_OK;
54 }
55
GetThumbnailPixelMap(ThumbRdbOpt & opts,shared_ptr<DataShare::ResultSetBridge> & outResultSet)56 int32_t LcdThumbnailHelper::GetThumbnailPixelMap(ThumbRdbOpt &opts,
57 shared_ptr<DataShare::ResultSetBridge> &outResultSet)
58 {
59 int err;
60 ThumbnailWait thumbnailWait(false);
61 thumbnailWait.CheckAndWait(opts.row, true);
62 ThumbnailData thumbnailData;
63 shared_ptr<AbsSharedResultSet> rdbSet = QueryThumbnailInfo(opts, thumbnailData, err);
64 if (rdbSet == nullptr) {
65 MEDIA_ERR_LOG("QueryThumbnailInfo Faild [ %{public}d ]", err);
66 return E_ERR;
67 }
68
69 if (thumbnailData.lcdKey.empty()) {
70 if (!opts.networkId.empty()) {
71 auto remoteQuery = ThumbnailUtils::QueryRemoteThumbnail(opts, thumbnailData, err);
72 if ((!remoteQuery || thumbnailData.lcdKey.empty()) &&
73 !IThumbnailHelper::DoThumbnailSync(opts, thumbnailData)) {
74 return E_THUMBNAIL_REMOTE_CREATE_FAIL;
75 }
76 } else if (!DoCreateLcd(opts, thumbnailData)) {
77 MEDIA_ERR_LOG("DoCreateLcd Faild");
78 return E_THUMBNAIL_LOCAL_CREATE_FAIL;
79 }
80 }
81
82 if (!ThumbnailUtils::IsImageExist(thumbnailData.lcdKey, opts.networkId, opts.kvStore)) {
83 MEDIA_ERR_LOG("image not exist in kvStore, key [%{public}s]", thumbnailData.lcdKey.c_str());
84 if (!DoCreateLcd(opts, thumbnailData, true)) {
85 return E_ERR;
86 }
87 }
88
89 if (!ThumbnailUtils::GetKvResultSet(opts.kvStore, thumbnailData.lcdKey, opts.networkId, outResultSet)) {
90 MEDIA_ERR_LOG("GetKvResultSet Faild");
91 return E_ERR;
92 }
93
94 ThumbnailUtils::UpdateVisitTime(opts, thumbnailData, err);
95 return E_OK;
96 }
97 } // namespace Media
98 } // namespace OHOS
99