1 /*
2 * Copyright (C) 2025 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 #define MLOG_TAG "Media_Cloud_Utils"
17
18 #include "cloud_media_attachment_utils.h"
19
20 #include "directory_ex.h"
21 #include "media_log.h"
22 #include "cloud_file_data_dto.h"
23 #include "media_file_utils.h"
24 #include "cloud_media_file_utils.h"
25 #include "photo_file_utils.h"
26 #include "thumbnail_const.h"
27
28 namespace OHOS::Media::CloudSync {
29 // LCOV_EXCL_START
AddRawIntoContent(const DownloadAssetData & downloadData,PhotosDto & photosDto)30 bool CloudMediaAttachmentUtils::AddRawIntoContent(const DownloadAssetData &downloadData, PhotosDto &photosDto)
31 {
32 bool isAdded = false;
33 std::string path = downloadData.path;
34 std::string rawFilePath = PhotoFileUtils::GetEditDataSourcePath(path);
35 std::string editDataCameraPath = PhotoFileUtils::GetEditDataCameraPath(path);
36 MEDIA_INFO_LOG("download rawFilePath %{public}s", rawFilePath.c_str());
37 MEDIA_INFO_LOG("download editDataCameraPath %{public}s", editDataCameraPath.c_str());
38 bool hasEditDataCamera = (!editDataCameraPath.empty() && access(editDataCameraPath.c_str(), F_OK) == 0);
39 bool hasSource = PhotoFileUtils::HasSource(hasEditDataCamera, downloadData.editTime, downloadData.effectMode);
40 bool rawFileExist = access(rawFilePath.c_str(), F_OK) == 0;
41 bool isValid = hasSource && !rawFileExist;
42 if (!isValid) {
43 MEDIA_INFO_LOG("download not add raw %{public}d, %{public}d", hasSource, rawFileExist);
44 return isAdded;
45 }
46 MEDIA_INFO_LOG("enter add raw");
47 std::string downLoadPath = rawFilePath;
48 std::string editFilePath;
49 std::string editFileName;
50 CHECK_AND_RETURN_RET_LOG(CloudMediaFileUtils::GetParentPathAndFilename(downLoadPath, editFilePath, editFileName),
51 isAdded,
52 "failed to GetParentPathAndFilename");
53 std::string rawFileKey = "raw";
54 CloudFileDataDto rawDto;
55 rawDto.path = editFilePath;
56 rawDto.fileName = editFileName;
57 size_t editFileSize = 0;
58 CloudMediaFileUtils::GetFileSizeV2(downLoadPath, editFileSize);
59 rawDto.size = static_cast<int64_t>(editFileSize);
60 MEDIA_INFO_LOG("rawDto.path %{public}s", editFilePath.c_str());
61 MEDIA_INFO_LOG("rawDto.fileName %{public}s", editFileName.c_str());
62 photosDto.attachment.insert(std::make_pair(rawFileKey, rawDto));
63 isAdded = true;
64 MEDIA_INFO_LOG("success add raw");
65 return isAdded;
66 }
67
AddEditDataIntoContent(const DownloadAssetData & downloadData,PhotosDto & photosDto)68 bool CloudMediaAttachmentUtils::AddEditDataIntoContent(const DownloadAssetData &downloadData, PhotosDto &photosDto)
69 {
70 bool isAdded = false;
71 std::string path = downloadData.path;
72 std::string rawFilePath = PhotoFileUtils::GetEditDataSourcePath(path);
73 std::string editDataPath = PhotoFileUtils::GetEditDataPath(path);
74 std::string editDataCameraPath = PhotoFileUtils::GetEditDataCameraPath(path);
75 MEDIA_INFO_LOG("download rawFilePath %{public}s", rawFilePath.c_str());
76 MEDIA_INFO_LOG("download editDataPath %{public}s", editDataPath.c_str());
77 MEDIA_INFO_LOG("download editDataCameraPath %{public}s", editDataCameraPath.c_str());
78 bool isValid = PhotoFileUtils::HasEditData(downloadData.editTime) && access(editDataPath.c_str(), F_OK) != 0;
79 if (!isValid) {
80 return isAdded;
81 }
82 MEDIA_INFO_LOG("enter add edit");
83 std::string downLoadPath = editDataPath;
84 std::string editFilePath = MediaFileUtils::GetParentPath(downLoadPath);
85 std::string editFileName = MediaFileUtils::GetFileName(downLoadPath);
86 std::string editFileKey = "editData";
87 CloudFileDataDto editDto;
88 editDto.path = editFilePath;
89 editDto.fileName = editFileName;
90 size_t editFileSize = 0;
91 CloudMediaFileUtils::GetFileSizeV2(downLoadPath, editFileSize);
92 editDto.size = static_cast<int64_t>(editFileSize);
93 MEDIA_INFO_LOG("editDto.path %{public}s", editFilePath.c_str());
94 MEDIA_INFO_LOG("editDto.fileName %{public}s", editFileName.c_str());
95 photosDto.attachment.insert(std::make_pair(editFileKey, editDto));
96 isAdded = true;
97 MEDIA_INFO_LOG("success add edit");
98 return isAdded;
99 }
100
GetContent(const std::string & fileKey,const DownloadAssetData & downloadData,PhotosDto & photosDto)101 int32_t CloudMediaAttachmentUtils::GetContent(
102 const std::string &fileKey, const DownloadAssetData &downloadData, PhotosDto &photosDto)
103 {
104 MEDIA_INFO_LOG("enter GetContent");
105 std::string filePath;
106 std::string fileName;
107 CHECK_AND_RETURN_RET_LOG(CloudMediaFileUtils::GetParentPathAndFilename(downloadData.path, filePath, fileName),
108 E_ERR,
109 "failed to GetParentPathAndFilename");
110 MEDIA_INFO_LOG("filePath %{public}s", filePath.c_str());
111 MEDIA_INFO_LOG("fileName %{public}s", fileName.c_str());
112 CloudFileDataDto contentDto;
113 contentDto.path = filePath;
114 contentDto.fileName = fileName;
115 contentDto.size = downloadData.fileSize;
116 photosDto.attachment.insert(std::make_pair(fileKey, contentDto));
117
118 bool added = false;
119 added = CloudMediaAttachmentUtils::AddRawIntoContent(downloadData, photosDto);
120 if (PhotoFileUtils::HasEditData(downloadData.editTime)) {
121 bool editAdded = CloudMediaAttachmentUtils::AddEditDataIntoContent(downloadData, photosDto);
122 added = added || editAdded;
123 }
124 if (added) {
125 std::string parentPath;
126 std::string parentName;
127 std::string rawFilePath = PhotoFileUtils::GetEditDataSourcePath(downloadData.path);
128 CHECK_AND_RETURN_RET_LOG(CloudMediaFileUtils::GetParentPathAndFilename(rawFilePath, parentPath, parentName),
129 E_ERR,
130 "failed to GetParentPathAndFilename");
131 MEDIA_INFO_LOG("get parentPath %{public}s", parentPath.c_str());
132 ForceCreateDirectory(parentPath);
133 }
134 return E_OK;
135 }
136
GetThumbnail(const std::string & fileKey,const DownloadAssetData & downloadData,PhotosDto & photosDto)137 int32_t CloudMediaAttachmentUtils::GetThumbnail(
138 const std::string &fileKey, const DownloadAssetData &downloadData, PhotosDto &photosDto)
139 {
140 MEDIA_INFO_LOG("enter GetThumbnail");
141 bool isRotation = downloadData.orientation != ROTATE_ANGLE_0;
142 std::string thumbSuffix = isRotation ? THUMBNAIL_THUMB_EX_SUFFIX : THUMBNAIL_THUMB_SUFFIX;
143 std::string thumbLocalPath = GetThumbnailPath(downloadData.path, thumbSuffix);
144 CloudFileDataDto thmDto;
145 CHECK_AND_RETURN_RET_LOG(
146 CloudMediaFileUtils::GetParentPathAndFilename(thumbLocalPath, thmDto.path, thmDto.fileName),
147 E_ERR,
148 "failed to GetParentPathAndFilename");
149 size_t thumbFileSize = 0;
150 CloudMediaFileUtils::GetFileSizeV2(thumbLocalPath, thumbFileSize);
151 thmDto.size = static_cast<int64_t>(thumbFileSize);
152 std::string thumFileKey = "thumbnail";
153 MEDIA_INFO_LOG("thmDto.path %{public}s", thmDto.path.c_str());
154 MEDIA_INFO_LOG("thmDto.fileName %{public}s", thmDto.fileName.c_str());
155 photosDto.attachment.insert(std::make_pair(thumFileKey, thmDto));
156 return E_OK;
157 }
158
GetLcdThumbnail(const std::string & fileKey,const DownloadAssetData & downloadData,PhotosDto & photosDto)159 int32_t CloudMediaAttachmentUtils::GetLcdThumbnail(
160 const std::string &fileKey, const DownloadAssetData &downloadData, PhotosDto &photosDto)
161 {
162 MEDIA_INFO_LOG("enter GetLcdThumbnail");
163 bool isRotation = downloadData.orientation != ROTATE_ANGLE_0;
164 std::string lcdSuffix = isRotation ? THUMBNAIL_LCD_EX_SUFFIX : THUMBNAIL_LCD_SUFFIX;
165 std::string lcdLocalPath = GetThumbnailPath(downloadData.path, lcdSuffix);
166 CloudFileDataDto lcdDto;
167 CHECK_AND_RETURN_RET_LOG(CloudMediaFileUtils::GetParentPathAndFilename(lcdLocalPath, lcdDto.path, lcdDto.fileName),
168 E_ERR,
169 "failed to GetParentPathAndFilename");
170 size_t lcdFileSize = 0;
171 CloudMediaFileUtils::GetFileSizeV2(lcdLocalPath, lcdFileSize);
172 lcdDto.size = static_cast<int64_t>(lcdFileSize);
173 std::string lcdFileKey = "lcd";
174 MEDIA_INFO_LOG("lcdDto.path %{public}s", lcdDto.path.c_str());
175 MEDIA_INFO_LOG("lcdDto.fileName %{public}s", lcdDto.fileName.c_str());
176 photosDto.attachment.insert(std::make_pair(lcdFileKey, lcdDto));
177 return E_OK;
178 }
179
GetAttachment(const std::string & fileKey,const DownloadAssetData & downloadData,PhotosDto & photosDto)180 int32_t CloudMediaAttachmentUtils::GetAttachment(
181 const std::string &fileKey, const DownloadAssetData &downloadData, PhotosDto &photosDto)
182 {
183 if (fileKey == "content") {
184 return GetContent(fileKey, downloadData, photosDto);
185 } else if (fileKey == "thumbnail") {
186 return GetThumbnail(fileKey, downloadData, photosDto);
187 } else if (fileKey == "lcd") {
188 return GetLcdThumbnail(fileKey, downloadData, photosDto);
189 }
190 MEDIA_ERR_LOG("Failed to GetAttachment, fileKey: %{public}s", fileKey.c_str());
191 return E_ERR;
192 }
193 // LCOV_EXCL_STOP
194 } // namespace OHOS::Media::CloudSync