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 #ifndef OHOS_MTP_MEDIA_LIBRARY_H
16 #define OHOS_MTP_MEDIA_LIBRARY_H
17 
18 #include <memory>
19 #include <mutex>
20 #include <unordered_map>
21 #include <string>
22 #include <vector>
23 #include <filesystem>
24 #include "file_asset.h"
25 #include "mtp_operation_context.h"
26 #include "object_info.h"
27 #include "property.h"
28 #include "pixel_map.h"
29 
30 namespace OHOS {
31 namespace Media {
32 #define EXPORT __attribute__ ((visibility ("default")))
33 namespace sf = std::filesystem;
34 class MtpMediaLibrary {
35 public:
36     EXPORT MtpMediaLibrary() = default;
37     EXPORT virtual ~MtpMediaLibrary() = default;
38     EXPORT static std::shared_ptr<MtpMediaLibrary> GetInstance();
39 
40     EXPORT void Init();
41     EXPORT void Clear();
42     EXPORT int32_t GetHandles(int32_t parentId, std::vector<int> &outHandles,
43         MediaType mediaType = MEDIA_TYPE_DEFAULT);
44     EXPORT int32_t GetHandles(const std::shared_ptr<MtpOperationContext> &context,
45         std::shared_ptr<UInt32List> &outHandles);
46     EXPORT int32_t GetObjectInfo(const std::shared_ptr<MtpOperationContext> &context,
47         std::shared_ptr<ObjectInfo> &outObjectInfo);
48     EXPORT int32_t GetFd(const std::shared_ptr<MtpOperationContext> &context, int32_t &outFd);
49     EXPORT int32_t GetThumb(const std::shared_ptr<MtpOperationContext> &context,
50         std::shared_ptr<UInt8List> &outThumb);
51     EXPORT int32_t SendObjectInfo(const std::shared_ptr<MtpOperationContext> &context,
52         uint32_t &outStorageID, uint32_t &outParent, uint32_t &outHandle);
53     EXPORT int32_t GetPathById(const int32_t id, std::string &outPath);
54     EXPORT int32_t GetPathByContextParent(const std::shared_ptr<MtpOperationContext> &context, std::string &path);
55     EXPORT int32_t GetIdByPath(const std::string &path, uint32_t &outId);
56     EXPORT int32_t MoveObject(const std::shared_ptr<MtpOperationContext> &context, uint32_t &repeatHandle);
57     EXPORT int32_t CopyObject(const std::shared_ptr<MtpOperationContext> &context,
58         uint32_t &outObjectHandle, uint32_t &oldHandle);
59     EXPORT int32_t DeleteObject(const std::shared_ptr<MtpOperationContext> &context);
60     EXPORT int32_t SetObjectPropValue(const std::shared_ptr<MtpOperationContext> &context);
61     EXPORT int32_t CloseFd(const std::shared_ptr<MtpOperationContext> &context, int32_t fd);
62     EXPORT int32_t GetObjectPropList(const std::shared_ptr<MtpOperationContext> &context,
63         std::shared_ptr<std::vector<Property>> &outProps);
64     EXPORT int32_t GetObjectPropValue(const std::shared_ptr<MtpOperationContext> &context,
65         uint64_t &outIntVal, uint128_t &outLongVal, std::string &outStrVal);
66     EXPORT int32_t GetRealPath(const std::string &path, std::string &outPath);
67     EXPORT bool TryAddExternalStorage(const std::string &fsUuid, uint32_t &storageId);
68     EXPORT bool TryRemoveExternalStorage(const std::string &fsUuid, uint32_t &storageId);
69     EXPORT int GetStorageIds();
70     EXPORT void DeleteHandlePathMap(const std::string &path, const uint32_t id);
71     EXPORT uint32_t ObserverAddPathToMap(const std::string &path);
72     EXPORT void ObserverDeletePathToMap(const std::string &path);
73 
74 private:
75     void AddToHandlePathMap(const std::string &path, const uint32_t id);
76     void ModifyHandlePathMap(const std::string &from, const std::string &to);
77     void ModifyPathHandleMap(const std::string &path, const uint32_t id);
78     bool StartsWith(const std::string& str, const std::string& prefix);
79     void MoveHandlePathMap(const std::string &path, const std::string &to);
80     void MoveRepeatDirHandlePathMap(const std::string &path, const std::string &to);
81     uint32_t MoveObjectSub(const sf::path &fromPath, const sf::path &toPath, const bool &isDir,
82         uint32_t &repeatHandle);
83     uint32_t GetId();
84     uint32_t GetParentId(const std::string &path);
85     int32_t ScanDirNoDepth(const std::string &root, std::shared_ptr<UInt32List> &out);
86     uint32_t ScanDirWithType(const std::string &root, std::shared_ptr<std::unordered_map<uint32_t, std::string>> &out);
87     uint32_t ScanDirTraverseWithType(const std::string &root,
88         std::shared_ptr<std::unordered_map<uint32_t, std::string>> &out);
89     uint32_t GetSizeFromOfft(const off_t &size);
90     uint32_t AddPathToMap(const std::string &path);
91     std::shared_ptr<std::unordered_map<uint32_t, std::string>> GetHandlesMap(
92         const std::shared_ptr<MtpOperationContext> &context);
93     void GetExternalStorages();
94     bool AddExternalStorage(const std::string &fsUuid, uint32_t &storageId);
95     const std::string GetExternalPathByUuid(const std::string &fsUuid);
96     void GetHandles(const uint32_t handle, const std::string &root,
97         std::shared_ptr<std::unordered_map<uint32_t, std::string>> &out);
98     void ErasePathInfo(const uint32_t handle, const std::string &path);
99     void ErasePathInfoSub(const std::string &path);
100     bool CompressImage(PixelMap &pixelMap, std::vector<uint8_t> &data);
101     int32_t GetVideoThumb(const std::shared_ptr<MtpOperationContext> &context,
102         std::shared_ptr<UInt8List> &outThumb);
103     int32_t GetPictureThumb(const std::shared_ptr<MtpOperationContext> &context,
104         std::shared_ptr<UInt8List> &outThumb);
105     void CondCloseFd(const bool condition, const int fd);
106     void CorrectStorageId(const std::shared_ptr<MtpOperationContext> &context);
107 
108     static std::shared_ptr<MtpMediaLibrary> instance_;
109     static std::atomic<uint32_t> id_;
110 };
111 } // namespace Media
112 } // namespace OHOS
113 #endif // OHOS_MTP_MEDIA_LIBRARY_H
114