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