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 INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_ASSET_CHANGE_REQUEST_IMPL_H
17 #define INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_ASSET_CHANGE_REQUEST_IMPL_H
18 
19 #include <fstream>
20 #include <vector>
21 #include <mutex>
22 
23 #include "media_asset_base_capi.h"
24 #include "nocopyable.h"
25 #include "media_asset_types.h"
26 #include "unique_fd.h"
27 #include "media_asset_edit_data.h"
28 #include "datashare_helper.h"
29 #include "media_asset.h"
30 #include "media_asset_change_request.h"
31 
32 constexpr int32_t YES = 1;
33 constexpr int32_t NO = 0;
34 
35 namespace OHOS {
36 namespace Media {
37 
38 class MediaAssetChangeRequestImpl : public MediaAssetChangeRequest, public NoCopyable {
39 public:
40     MediaAssetChangeRequestImpl(std::shared_ptr<MediaAsset> mediaAsset);
41     ~MediaAssetChangeRequestImpl();
42 
43     MediaLibrary_ErrorCode GetWriteCacheHandler(int32_t* fd) override;
44     MediaLibrary_ErrorCode AddResourceWithUri(MediaLibrary_ResourceType resourceType, char* fileUri) override;
45     MediaLibrary_ErrorCode AddResourceWithBuffer(MediaLibrary_ResourceType resourceType, uint8_t* buffer,
46         uint32_t length) override;
47     MediaLibrary_ErrorCode SaveCameraPhoto(MediaLibrary_ImageFileType imageFileType) override;
48     MediaLibrary_ErrorCode DiscardCameraPhoto() override;
49     MediaLibrary_ErrorCode ApplyChanges() override;
50 
51     void RecordChangeOperation(AssetChangeOperation changeOperation) override;
52 
53 private:
54     bool IsMovingPhoto();
55     bool CheckWriteOperation(MediaLibrary_ResourceType resourceType);
56     bool CheckMovingPhotoResource(MediaLibrary_ResourceType resourceType);
57     bool ContainsResource(MediaLibrary_ResourceType resourceType);
58     bool Contains(AssetChangeOperation changeOperation);
59     int32_t OpenWriteCacheHandler(bool isMovingPhotoVideo = false);
60     uint32_t FetchAddCacheFileId();
61     bool ChangeOperationExecute(AssetChangeOperation option);
62     bool CheckChangeOperations();
63     bool CheckMovingPhotoWriteOperation();
64     bool SubmitCacheExecute();
65     bool AddResourceExecute();
66     bool SaveCameraPhotoExecute();
67     bool DiscardCameraPhotoExecute();
68     bool HasWritePermission();
69     bool WriteBySecurityComponent();
70     bool IsCreation();
71     int32_t CopyToMediaLibrary(bool isCreation, AddResourceMode mode);
72     int32_t CreateAssetBySecurityComponent(std::string& assetUri);
73     int32_t CopyMovingPhotoVideo(const std::string& assetUri);
74     int32_t CopyFileToMediaLibrary(const OHOS::UniqueFd& destFd, bool isMovingPhotoVideo = false);
75     int32_t CopyDataBufferToMediaLibrary(const OHOS::UniqueFd& destFd, bool isMovingPhotoVideo = false);
76     void SetNewFileAsset(int32_t id, const std::string& uri);
77     bool SendToCacheFile(const OHOS::UniqueFd& destFd, bool isMovingPhotoVideo = false);
78     bool IsSetEffectMode();
79     int32_t SubmitCache(bool isCreation, bool isSetEffectMode);
80     int32_t SendFile(const OHOS::UniqueFd& srcFd, const OHOS::UniqueFd& destFd);
81     int32_t PutMediaAssetEditData(OHOS::DataShare::DataShareValuesBucket& valuesBucket);
82     bool HasAddResource(MediaLibrary_ResourceType resourceType);
83     bool AddMovingPhotoVideoExecute();
84     bool AddResourceByMode(const OHOS::UniqueFd& uniqueFd, AddResourceMode mode,
85         bool isMovingPhotoVideo = false);
86     bool WriteCacheByArrayBuffer(const OHOS::UniqueFd& destFd, bool isMovingPhotoVideo = false);
87     void DiscardHighQualityPhoto();
88 
89 private:
90     std::shared_ptr<MediaAsset> mediaAsset_ = nullptr;
91     std::vector<MediaLibrary_ResourceType> addResourceTypes_;
92     std::vector<AssetChangeOperation> assetChangeOperations_;
93     std::string cacheMovingPhotoVideoName_;
94     std::string cacheFileName_;
95     static std::atomic<uint32_t> cacheFileId_;
96     std::mutex mutex_;
97     uint8_t* dataBuffer_ = nullptr;
98     uint8_t* movingPhotoVideoDataBuffer_ = nullptr;
99     OHOS::DataShare::DataShareValuesBucket creationValuesBucket_;
100     AddResourceMode movingPhotoVideoResourceMode_;
101     std::string realPath_;
102     std::string movingPhotoVideoRealPath_;
103     uint32_t movingPhotoVideoBufferSize_;
104     uint32_t dataBufferSize_;
105     AddResourceMode addResourceMode_;
106     std::shared_ptr<MediaAssetEditData> editData_ = nullptr;
107 };
108 
109 }
110 }
111 
112 #endif // INTERFACES_INNERKITS_NATIVE_INCLUDE_MEDIA_ASSET_CHANGE_REQUEST_IMPL_H
113