• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
16 #define MLOG_TAG "Media_Cloud_Service"
17 
18 #include "cloud_media_scan_service.h"
19 
20 
21 #include "media_file_utils.h"
22 #include "medialibrary_rdb_utils.h"
23 #include "medialibrary_unistore_manager.h"
24 #include "medialibrary_notify.h"
25 #include "metadata.h"
26 #include "metadata_extractor.h"
27 #include "mimetype_utils.h"
28 #include "photo_album_column.h"
29 #include "scanner_utils.h"
30 #include "shooting_mode_column.h"
31 
32 namespace OHOS::Media::CloudSync {
33 
GetFileMetadata(std::unique_ptr<Metadata> & data)34 static int32_t GetFileMetadata(std::unique_ptr<Metadata> &data)
35 {
36     if (data == nullptr) {
37         MEDIA_ERR_LOG("data is nullptr");
38         return E_FAIL;
39     }
40     struct stat statInfo {};
41     if (stat(data->GetFilePath().c_str(), &statInfo) != 0) {
42         MEDIA_ERR_LOG("stat syscall err %{public}d", errno);
43         return E_FAIL;
44     }
45     data->SetFileSize(statInfo.st_size);
46     auto dateModified = static_cast<int64_t>(MediaFileUtils::Timespec2Millisecond(statInfo.st_mtim));
47     if (dateModified == 0) {
48         dateModified = MediaFileUtils::UTCTimeMilliSeconds();
49         MEDIA_WARN_LOG("Invalid dateModified from st_mtim, use current time instead: %{public}lld",
50             static_cast<long long>(dateModified));
51     }
52     if (dateModified != 0 && data->GetFileDateModified() == 0) {
53         data->SetFileDateModified(dateModified);
54     }
55     string extension = ScannerUtils::GetFileExtension(data->GetFileName());
56     string mimeType = MimeTypeUtils::GetMimeTypeFromExtension(extension);
57     data->SetFileExtension(extension);
58     data->SetFileMimeType(mimeType);
59     return E_OK;
60 }
61 
FillMetadata(std::unique_ptr<Metadata> & data)62 int32_t CloudMediaScanService::FillMetadata(std::unique_ptr<Metadata>& data)
63 {
64     if (data == nullptr) {
65         MEDIA_ERR_LOG("data is nullptr");
66         return E_FAIL;
67     }
68     int32_t err = GetFileMetadata(data);
69     if (err != E_OK) {
70         MEDIA_ERR_LOG("failed to get file metadata");
71         return err;
72     }
73     if (data->GetFileMediaType() == MEDIA_TYPE_IMAGE) {
74         err = MetadataExtractor::ExtractImageMetadata(data);
75     } else {
76         err = MetadataExtractor::ExtractAVMetadata(data, Scene::AV_META_SCENE_CLONE);
77     }
78     CHECK_AND_RETURN_RET_LOG(err == E_OK, err, "failed to extension data");
79     return E_OK;
80 }
81 
ScanMetaData(const string & path,std::unique_ptr<Metadata> & data)82 int32_t CloudMediaScanService::ScanMetaData(const string& path, std::unique_ptr<Metadata>& data)
83 {
84     if (data == nullptr) {
85         MEDIA_ERR_LOG("data is nullptr");
86         return E_FAIL;
87     }
88     const string fileName = MediaFileUtils::GetFileName(path);
89     data->SetFilePath(path);
90     data->SetFileName(fileName);
91     data->SetFileMediaType(MediaFileUtils::GetMediaType(fileName));
92     return FillMetadata(data);
93 }
94 
ScanShootingMode(const string & path,CloudMediaScanService::ScanResult & result)95 int32_t CloudMediaScanService::ScanShootingMode(const string& path, CloudMediaScanService::ScanResult& result)
96 {
97     std::unique_ptr<Metadata> data = make_unique<Metadata>();
98     if (data == nullptr) {
99         MEDIA_ERR_LOG("data is nullptr");
100         return E_FAIL;
101     }
102     if (ScanMetaData(path, data) != E_OK) {
103         MEDIA_ERR_LOG("failed to scan metadata");
104         return E_FAIL;
105     }
106     result.shootingMode = data->GetShootingMode();
107     result.shootingModeTag = data->GetShootingModeTag();
108     result.frontCamera = data->GetFrontCamera();
109     result.scanSuccess = true;
110     return E_OK;
111 }
112 
NotifyAnalysisAlbum(const vector<string> & changedAlbumIds)113 static void NotifyAnalysisAlbum(const vector<string>& changedAlbumIds)
114 {
115     if (changedAlbumIds.size() <= 0) {
116         return;
117     }
118     auto watch = MediaLibraryNotify::GetInstance();
119     CHECK_AND_RETURN_LOG(watch != nullptr, "Can not get MediaLibraryNotify Instance");
120     for (const string& albumId : changedAlbumIds) {
121         watch->Notify(MediaFileUtils::GetUriByExtrConditions(
122             PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX, albumId), NotifyType::NOTIFY_UPDATE);
123     }
124 }
125 
UpdateAndNotifyShootingModeAlbumIfNeeded(const CloudMediaScanService::ScanResult & scanResult)126 void CloudMediaScanService::UpdateAndNotifyShootingModeAlbumIfNeeded(
127     const CloudMediaScanService::ScanResult& scanResult)
128 {
129     vector<ShootingModeAlbumType> albumTypes = ShootingModeAlbum::GetShootingModeAlbumOfAsset(
130         -1, "", -1, scanResult.frontCamera, scanResult.shootingMode);
131 
132     vector<string> albumIdsToUpdate;
133     for (const auto& type : albumTypes) {
134         int32_t albumId;
135         if (MediaLibraryRdbUtils::QueryShootingModeAlbumIdByType(type, albumId)) {
136             albumIdsToUpdate.push_back(to_string(albumId));
137         }
138     }
139 
140     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
141     CHECK_AND_RETURN_LOG(rdbStore != nullptr, "rdbstore is nullptr");
142 
143     if (albumIdsToUpdate.size() > 0) {
144         MediaLibraryRdbUtils::UpdateAnalysisAlbumInternal(rdbStore, albumIdsToUpdate);
145         NotifyAnalysisAlbum(albumIdsToUpdate);
146     }
147 }
148 }  // namespace OHOS::Media::CloudSync