1 /*
2 * Copyright (C) 2021 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 #include "file_asset.h"
17 #include <cerrno>
18
19 using namespace std;
20
21 namespace OHOS {
22 namespace Media {
FileAsset()23 FileAsset::FileAsset()
24 : id_(DEFAULT_MEDIA_ID),
25 uri_(DEFAULT_MEDIA_URI),
26 path_(DEFAULT_MEDIA_PATH),
27 relativePath_(DEFAULT_MEDIA_RELATIVE_PATH),
28 mimeType_(DEFAULT_MEDIA_MIMETYPE),
29 mediaType_(DEFAULT_MEDIA_TYPE),
30 displayName_(DEFAULT_MEDIA_NAME),
31 size_(DEFAULT_MEDIA_SIZE),
32 dateAdded_(DEFAULT_MEDIA_DATE_ADDED),
33 dateModified_(DEFAULT_MEDIA_DATE_MODIFIED),
34 title_(DEFAULT_MEDIA_TITLE),
35 artist_(DEFAULT_MEDIA_ARTIST),
36 album_(DEFAULT_MEDIA_ALBUM),
37 width_(DEFAULT_MEDIA_WIDTH),
38 height_(DEFAULT_MEDIA_HEIGHT),
39 duration_(DEFAULT_MEDIA_DURATION),
40 orientation_(DEFAULT_MEDIA_ORIENTATION),
41 albumId_(DEFAULT_ALBUM_ID),
42 albumName_(DEFAULT_ALBUM_NAME),
43 parent_(DEFAULT_MEDIA_PARENT),
44 albumUri_(DEFAULT_MEDIA_ALBUM_URI),
45 dateTaken_(DEFAULT_MEDIA_DATE_TAKEN)
46 {}
47
GetId() const48 int32_t FileAsset::GetId() const
49 {
50 return id_;
51 }
52
SetId(int32_t id)53 void FileAsset::SetId(int32_t id)
54 {
55 id_ = id;
56 }
57
GetUri() const58 const string &FileAsset::GetUri() const
59 {
60 return uri_;
61 }
62
SetUri(const string & uri)63 void FileAsset::SetUri(const string &uri)
64 {
65 uri_ = uri;
66 }
67
GetPath() const68 const string &FileAsset::GetPath() const
69 {
70 return path_;
71 }
72
SetPath(const string & path)73 void FileAsset::SetPath(const string &path)
74 {
75 path_ = path;
76 }
77
GetRelativePath() const78 const string &FileAsset::GetRelativePath() const
79 {
80 return relativePath_;
81 }
82
SetRelativePath(const std::string & relativePath)83 void FileAsset::SetRelativePath(const std::string &relativePath)
84 {
85 relativePath_ = relativePath;
86 }
87
GetMimeType() const88 const string &FileAsset::GetMimeType() const
89 {
90 return mimeType_;
91 }
92
SetMimeType(const string & mimeType)93 void FileAsset::SetMimeType(const string &mimeType)
94 {
95 mimeType_ = mimeType;
96 }
97
GetMediaType() const98 MediaType FileAsset::GetMediaType() const
99 {
100 return mediaType_;
101 }
102
SetMediaType(MediaType mediaType)103 void FileAsset::SetMediaType(MediaType mediaType)
104 {
105 mediaType_ = mediaType;
106 }
107
GetDisplayName() const108 const string &FileAsset::GetDisplayName() const
109 {
110 return displayName_;
111 }
112
SetDisplayName(const string & displayName)113 void FileAsset::SetDisplayName(const string &displayName)
114 {
115 displayName_ = displayName;
116 }
117
GetSize() const118 int64_t FileAsset::GetSize() const
119 {
120 return size_;
121 }
122
SetSize(int64_t size)123 void FileAsset::SetSize(int64_t size)
124 {
125 size_ = size;
126 }
127
GetDateAdded() const128 int64_t FileAsset::GetDateAdded() const
129 {
130 return dateAdded_;
131 }
132
SetDateAdded(int64_t dateAdded)133 void FileAsset::SetDateAdded(int64_t dateAdded)
134 {
135 dateAdded_ = dateAdded;
136 }
137
GetDateModified() const138 int64_t FileAsset::GetDateModified() const
139 {
140 return dateModified_;
141 }
142
SetDateModified(int64_t dateModified)143 void FileAsset::SetDateModified(int64_t dateModified)
144 {
145 dateModified_ = dateModified;
146 }
147
GetTitle() const148 const string &FileAsset::GetTitle() const
149 {
150 return title_;
151 }
152
SetTitle(const string & title)153 void FileAsset::SetTitle(const string &title)
154 {
155 title_ = title;
156 }
157
GetArtist() const158 const string &FileAsset::GetArtist() const
159 {
160 return artist_;
161 }
162
SetArtist(const string & artist)163 void FileAsset::SetArtist(const string &artist)
164 {
165 artist_ = artist;
166 }
167
GetAlbum() const168 const string &FileAsset::GetAlbum() const
169 {
170 return album_;
171 }
172
SetAlbum(const string & album)173 void FileAsset::SetAlbum(const string &album)
174 {
175 album_ = album;
176 }
177
GetWidth() const178 int32_t FileAsset::GetWidth() const
179 {
180 return width_;
181 }
182
SetWidth(int32_t width)183 void FileAsset::SetWidth(int32_t width)
184 {
185 width_ = width;
186 }
187
GetHeight() const188 int32_t FileAsset::GetHeight() const
189 {
190 return height_;
191 }
192
SetHeight(int32_t height)193 void FileAsset::SetHeight(int32_t height)
194 {
195 height_ = height;
196 }
197
GetDuration() const198 int32_t FileAsset::GetDuration() const
199 {
200 return duration_;
201 }
202
SetDuration(int32_t duration)203 void FileAsset::SetDuration(int32_t duration)
204 {
205 duration_ = duration;
206 }
207
GetOrientation() const208 int32_t FileAsset::GetOrientation() const
209 {
210 return orientation_;
211 }
212
SetOrientation(int32_t orientation)213 void FileAsset::SetOrientation(int32_t orientation)
214 {
215 orientation_ = orientation;
216 }
217
GetAlbumId() const218 int32_t FileAsset::GetAlbumId() const
219 {
220 return albumId_;
221 }
222
SetAlbumId(int32_t albumId)223 void FileAsset::SetAlbumId(int32_t albumId)
224 {
225 albumId_ = albumId;
226 }
227
GetAlbumName() const228 const string &FileAsset::GetAlbumName() const
229 {
230 return albumName_;
231 }
232
SetAlbumName(const string & albumName)233 void FileAsset::SetAlbumName(const string &albumName)
234 {
235 albumName_ = albumName;
236 }
237
GetParent() const238 int32_t FileAsset::GetParent() const
239 {
240 return parent_;
241 }
SetParent(int32_t parent)242 void FileAsset::SetParent(int32_t parent)
243 {
244 parent_ = parent;
245 }
GetAlbumUri() const246 const string &FileAsset::GetAlbumUri() const
247 {
248 return albumUri_;
249 }
SetAlbumUri(const string & albumUri)250 void FileAsset::SetAlbumUri(const string &albumUri)
251 {
252 albumUri_ = albumUri;
253 }
GetDateTaken() const254 int64_t FileAsset::GetDateTaken() const
255 {
256 return dateTaken_;
257 }
SetDateTaken(int64_t dateTaken)258 void FileAsset::SetDateTaken(int64_t dateTaken)
259 {
260 dateTaken_ = dateTaken;
261 }
262
IsPending() const263 bool FileAsset::IsPending() const
264 {
265 return isPending_;
266 }
SetPending(bool dateTaken)267 void FileAsset::SetPending(bool dateTaken)
268 {
269 isPending_ = dateTaken;
270 }
271
GetTimePending() const272 int64_t FileAsset::GetTimePending() const
273 {
274 return timePending_;
275 }
276
SetTimePending(int64_t timePending)277 void FileAsset::SetTimePending(int64_t timePending)
278 {
279 timePending_ = timePending;
280 }
281
IsFavorite() const282 bool FileAsset::IsFavorite() const
283 {
284 return isFavorite_;
285 }
286
SetFavorite(bool isFavorite)287 void FileAsset::SetFavorite(bool isFavorite)
288 {
289 isFavorite_ = isFavorite;
290 }
291
GetDateTrashed() const292 int64_t FileAsset::GetDateTrashed() const
293 {
294 return dateTrashed_;
295 }
296
SetDateTrashed(int64_t dateTrashed)297 void FileAsset::SetDateTrashed(int64_t dateTrashed)
298 {
299 dateTrashed_ = dateTrashed;
300 }
301
GetSelfId() const302 const string &FileAsset::GetSelfId() const
303 {
304 return selfId_;
305 }
306
SetSelfId(const string & selfId)307 void FileAsset::SetSelfId(const string &selfId)
308 {
309 selfId_ = selfId;
310 }
311
CreateAsset(const string & filePath)312 int32_t FileAsset::CreateAsset(const string &filePath)
313 {
314 MEDIA_ERR_LOG("CreateAsset in");
315 int32_t errCode = FAIL;
316
317 if (filePath.empty()) {
318 MEDIA_ERR_LOG("Filepath is empty");
319 return DATA_ABILITY_VIOLATION_PARAMETERS;
320 }
321
322 if (MediaFileUtils::IsFileExists(filePath)) {
323 MEDIA_ERR_LOG("the file exists path: %{private}s", filePath.c_str());
324 return DATA_ABILITY_DUPLICATE_CREATE;
325 }
326
327 size_t slashIndex = filePath.rfind('/');
328 if (slashIndex != string::npos) {
329 string fileName = filePath.substr(slashIndex + 1);
330 if (!fileName.empty() && fileName.at(0) != '.') {
331 size_t dotIndex = filePath.rfind('.');
332 if (dotIndex == string::npos && mediaType_ != MEDIA_TYPE_FILE) {
333 return errCode;
334 }
335 }
336 }
337
338 ofstream file(filePath);
339 if (!file) {
340 MEDIA_ERR_LOG("Output file path could not be created errno %{public}d", errno);
341 return errCode;
342 }
343
344 file.close();
345
346 return DATA_ABILITY_SUCCESS;
347 }
348
ModifyAsset(const string & oldPath,const string & newPath)349 int32_t FileAsset::ModifyAsset(const string &oldPath, const string &newPath)
350 {
351 int32_t errRet = DATA_ABILITY_MODIFY_DATA_FAIL;
352
353 if (!oldPath.empty() && !newPath.empty() &&
354 MediaFileUtils::IsFileExists(oldPath) &&
355 !MediaFileUtils::IsFileExists(newPath)) {
356 errRet = rename(oldPath.c_str(), newPath.c_str());
357 }
358
359 return errRet;
360 }
361
IsFileExists(const string & filePath)362 bool FileAsset::IsFileExists(const string &filePath)
363 {
364 return MediaFileUtils::IsFileExists(filePath);
365 }
366
DeleteAsset(const string & filePath)367 int32_t FileAsset::DeleteAsset(const string &filePath)
368 {
369 return remove(filePath.c_str());
370 }
371
OpenAsset(const string & filePath,const string & mode)372 int32_t FileAsset::OpenAsset(const string &filePath, const string &mode)
373 {
374 int32_t errCode = FAIL;
375
376 if (filePath.empty() || mode.empty()) {
377 return errCode;
378 }
379
380 int32_t flags = O_RDWR;
381 if (mode == MEDIA_FILEMODE_READONLY) {
382 flags = O_RDONLY;
383 } else if (mode == MEDIA_FILEMODE_WRITEONLY) {
384 flags = O_WRONLY;
385 } else if (mode == MEDIA_FILEMODE_WRITETRUNCATE) {
386 flags = O_WRONLY | O_TRUNC;
387 } else if (mode == MEDIA_FILEMODE_WRITEAPPEND) {
388 flags = O_WRONLY | O_APPEND;
389 } else if (mode == MEDIA_FILEMODE_READWRITETRUNCATE) {
390 flags = O_RDWR | O_TRUNC;
391 }
392
393 if (filePath.size() >= PATH_MAX) {
394 MEDIA_ERR_LOG("File path too long %{public}d", (int)filePath.size());
395 return errCode;
396 }
397 MEDIA_INFO_LOG("File path is %{public}s", filePath.c_str());
398 char actualPath[PATH_MAX];
399 memset_s(actualPath, PATH_MAX, '\0', PATH_MAX);
400 auto absFilePath = realpath(filePath.c_str(), actualPath);
401 if (absFilePath == nullptr) {
402 MEDIA_ERR_LOG("Failed to obtain the canonical path for source path %{public}s %{public}d",
403 filePath.c_str(), errno);
404 return errCode;
405 }
406
407 MEDIA_INFO_LOG("File absFilePath is %{public}s", absFilePath);
408 return open(absFilePath, flags);
409 }
410
CloseAsset(int32_t fd)411 int32_t FileAsset::CloseAsset(int32_t fd)
412 {
413 return close(fd);
414 }
415 } // namespace Media
416 } // namespace OHOS
417