1 /* 2 * Copyright (c) 2024 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 #ifndef OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H 17 #define OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H 18 19 #include "abs_shared_result_set.h" 20 #include "medialibrary_async_worker.h" 21 #include "metadata.h" 22 #include "timer.h" 23 #include "userfile_manager_types.h" 24 #include "values_bucket.h" 25 26 namespace OHOS { 27 namespace Media { 28 #define EXPORT __attribute__ ((visibility ("default"))) 29 constexpr int32_t PROCESS_INTERVAL = 5 * 60 * 1000; // 5 minute 30 constexpr int32_t DOWNLOAD_DURATION = 10 * 1000; // 10 seconds 31 32 class BackgroundCloudFileProcessor { 33 public: 34 EXPORT static void StartTimer(); 35 EXPORT static void StopTimer(); 36 37 private: 38 typedef struct { 39 std::vector<std::string> paths; 40 MediaType mediaType; 41 } DownloadFiles; 42 43 typedef struct { 44 int32_t fileId; 45 std::string path; 46 int64_t size; 47 int32_t width; 48 int32_t height; 49 std::string mimeType; 50 int32_t duration; 51 } AbnormalData; 52 53 typedef struct { 54 std::vector<AbnormalData> abnormalData; 55 MediaType mediaType; 56 } UpdateData; 57 58 class DownloadCloudFilesData : public AsyncTaskData { 59 public: DownloadCloudFilesData(DownloadFiles downloadFiles)60 DownloadCloudFilesData(DownloadFiles downloadFiles) : downloadFiles_(downloadFiles){}; 61 ~DownloadCloudFilesData() override = default; 62 63 DownloadFiles downloadFiles_; 64 }; 65 66 class UpdateAbnormalData : public AsyncTaskData { 67 public: UpdateAbnormalData(UpdateData updateData)68 UpdateAbnormalData(UpdateData updateData) : updateData_(updateData){}; 69 ~UpdateAbnormalData() override = default; 70 71 UpdateData updateData_; 72 }; 73 74 static void DownloadCloudFiles(); 75 static bool IsStorageInsufficient(); 76 static std::shared_ptr<NativeRdb::ResultSet> QueryCloudFiles(); 77 static void ParseDownloadFiles(std::shared_ptr<NativeRdb::ResultSet> &resultSet, DownloadFiles &downloadFiles); 78 static int32_t AddDownloadTask(const DownloadFiles &downloadFiles); 79 static void DownloadCloudFilesExecutor(AsyncTaskData *data); 80 static void StopDownloadFiles(); 81 static void ProcessCloudData(); 82 static void UpdateCloudData(); 83 static std::shared_ptr<NativeRdb::ResultSet> QueryUpdateData(); 84 static void ParseUpdateData(std::shared_ptr<NativeRdb::ResultSet> &resultSet, UpdateData &updateData); 85 static int32_t AddUpdateDataTask(const UpdateData &updateData); 86 static void UpdateCloudDataExecutor(AsyncTaskData *data); 87 static void UpdateAbnormaldata(std::unique_ptr<Metadata> &metadata, const std::string &tableName); 88 static void GetSizeAndMimeType(std::unique_ptr<Metadata> &metadata); 89 static int32_t GetExtractMetadata(std::unique_ptr<Metadata> &metadata); 90 static void StopUpdateData(); 91 static void UpdateCurrentOffset(); 92 93 static int32_t processInterval_; 94 static int32_t downloadDuration_; 95 static std::recursive_mutex mutex_; 96 static Utils::Timer timer_; 97 static uint32_t startTimerId_; 98 static uint32_t stopTimerId_; 99 static std::vector<std::string> curDownloadPaths_; 100 static bool isUpdating_; 101 static int32_t currentUpdateOffset_; 102 static int32_t currentRetryCount_; 103 static bool isDownload_; 104 }; 105 } // namespace Media 106 } // namespace OHOS 107 #endif // OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H