1 /*
2  * Copyright (c) 2023 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_FILEMGMT_FILE_TRANSFER_MANAGER_H
17 #define OHOS_FILEMGMT_FILE_TRANSFER_MANAGER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <mutex>
22 #include <string>
23 #include <sys/stat.h>
24 #include <vector>
25 
26 #include "message_handler.h"
27 #include "session_manager.h"
28 namespace OHOS::FileManagement::CloudSync {
29 class FileTransferManager : public RecieveDataHandler, public std::enable_shared_from_this<FileTransferManager> {
30 public:
31     explicit FileTransferManager(std::shared_ptr<SessionManager> sessionManager);
~FileTransferManager()32     ~FileTransferManager(){};
33 
34     void Init();
35 
36     void DownloadFileFromRemoteDevice(const std::string &networkId,
37                                       const int32_t userId,
38                                       const uint64_t taskId,
39                                       const std::string &uri);
40     void OnMessageHandle(const std::string &senderNetworkId,
41                          int receiverSessionId,
42                          const void *data,
43                          unsigned int dataLen) override;
44     void OnFileRecvHandle(const std::string &senderNetworkId, const char *filePath, int result) override;
45     void OnSessionClosed() override;
46     void HandleDownloadFileRequest(MessageHandler &msgHandler,
47                                   const std::string &senderNetworkId,
48                                   int receiverSessionId);
49     void HandleDownloadFileResponse(MessageHandler &msgHandler);
50     void HandleRecvFileFinished();
51 
52 private:
53     struct TaskInfo {
54         std::string uri;
55         std::string relativePath;
56         uint64_t taskId;
57     };
58     bool IsFileExists(const std::string &filePath);
59     std::tuple<std::string, std::string>
60         UriToPath(const std::string &uri, const int32_t userId, bool isCheckFileExists = true);
61     void AddTransTask(const std::string &uri, const int32_t userId, uint64_t taskId);
62     void FinishTransTask(const std::string &relativePath, int result);
63     void RemoveTransTask(uint64_t taskId);
64     void IncTransTaskCount();
65     void DecTransTaskCount();
66     std::mutex taskMutex_;
67     std::vector<TaskInfo> taskInfos_;
68     std::atomic<int> taskCount_{0};
69 
70     std::shared_ptr<SessionManager> sessionManager_;
71 };
72 } // namespace OHOS::FileManagement::CloudSync
73 
74 #endif // OHOS_FILEMGMT_FILE_TRANSFER_MANAGER_H