• 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 "default_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 DefaultThumbnailHelper::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.thumbnailKey.empty()) {
40         ThumbnailData tmpData = thumbnailData;
41         ThumbnailUtils::GenThumbnailKey(tmpData);
42         if (tmpData.thumbnailKey == thumbnailData.thumbnailKey) {
43             MEDIA_DEBUG_LOG("CreateThumbnail key is same, no need generate");
44             return E_OK;
45         }
46     }
47     if (isSync) {
48         DoCreateThumbnail(opts, thumbnailData, true);
49     } else {
50         IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateThumbnail, opts, thumbnailData, true);
51     }
52     return E_OK;
53 }
54 
GetThumbnailPixelMap(ThumbRdbOpt & opts,shared_ptr<DataShare::ResultSetBridge> & outResultSet)55 int32_t DefaultThumbnailHelper::GetThumbnailPixelMap(ThumbRdbOpt &opts,
56     shared_ptr<DataShare::ResultSetBridge> &outResultSet)
57 {
58     int err = E_ERR;
59     ThumbnailWait thumbnailWait(false);
60     thumbnailWait.CheckAndWait(opts.row, false);
61     ThumbnailData thumbnailData;
62     shared_ptr<AbsSharedResultSet> rdbSet = QueryThumbnailInfo(opts, thumbnailData, err);
63     if (rdbSet == nullptr) {
64         MEDIA_ERR_LOG("QueryThumbnailInfo Faild [ %{public}d ]", err);
65         return err;
66     }
67 
68     if (thumbnailData.thumbnailKey.empty()) {
69         if (!opts.networkId.empty()) {
70             auto remoteQuery = ThumbnailUtils::QueryRemoteThumbnail(opts, thumbnailData, err);
71             if ((!remoteQuery || thumbnailData.thumbnailKey.empty()) &&
72                 !IThumbnailHelper::DoThumbnailSync(opts, thumbnailData)) {
73                 return E_THUMBNAIL_REMOTE_CREATE_FAIL;
74             }
75         } else if (!DoCreateThumbnail(opts, thumbnailData)) {
76             MEDIA_ERR_LOG("DoCreateThumbnail Faild");
77             return E_THUMBNAIL_LOCAL_CREATE_FAIL;
78         }
79     }
80 
81     if (!ThumbnailUtils::IsImageExist(thumbnailData.thumbnailKey, opts.networkId, opts.kvStore)) {
82         MEDIA_ERR_LOG("image not exist in kvStore, key [%{public}s]", thumbnailData.thumbnailKey.c_str());
83         if (!DoCreateThumbnail(opts, thumbnailData, true)) {
84             return E_ERR;
85         }
86     }
87 
88     if (!ThumbnailUtils::GetKvResultSet(opts.kvStore, thumbnailData.thumbnailKey, opts.networkId, outResultSet)) {
89         MEDIA_ERR_LOG("GetKvResultSet Faild");
90         return E_ERR;
91     }
92 
93     thumbnailData.lcdKey.clear();
94     ThumbnailUtils::DoUpdateRemoteThumbnail(opts, thumbnailData, err);
95     return E_OK;
96 }
97 } // namespace Media
98 } // namespace OHOS
99