1 /*
2  * Copyright (C) 2021-2022 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 #define MLOG_TAG "DirOperation"
16 
17 #include "medialibrary_dir_operations.h"
18 
19 #include <algorithm>
20 
21 #include "abs_rdb_predicates.h"
22 #include "datashare_predicates.h"
23 #include "datashare_result_set.h"
24 #include "media_log.h"
25 #include "medialibrary_file_operations.h"
26 #include "media_file_utils.h"
27 #include "media_smart_map_column.h"
28 #include "medialibrary_data_manager.h"
29 #include "medialibrary_errno.h"
30 #include "medialibrary_smartalbum_map_operations.h"
31 #include "medialibrary_object_utils.h"
32 #include "rdb_utils.h"
33 #include "scanner_utils.h"
34 
35 using namespace std;
36 using namespace OHOS::NativeRdb;
37 using namespace OHOS::RdbDataShareAdapter;
38 
39 namespace OHOS {
40 namespace Media {
TrashDirOperation(MediaLibraryCommand & cmd)41 int32_t MediaLibraryDirOperations::TrashDirOperation(MediaLibraryCommand &cmd)
42 {
43     ValueObject valueObject;
44     if (!cmd.GetValueBucket().GetObject(MEDIA_DATA_DB_ID, valueObject)) {
45         return E_HAS_DB_ERROR;
46     }
47     int32_t dirId = DEFAULT_ASSETID;
48     valueObject.GetInt(dirId);
49     if (dirId <= 0) {
50         return E_GET_VALUEBUCKET_FAIL;
51     }
52     ValuesBucket valuesBucket;
53     valuesBucket.PutInt(SMARTALBUMMAP_DB_CHILD_ASSET_ID, dirId);
54     valuesBucket.PutInt(SMARTALBUMMAP_DB_ALBUM_ID, TRASH_ALBUM_ID_VALUES);
55     MediaLibraryCommand smartMapCmd(OperationObject::SMART_ALBUM_MAP, OperationType::CREATE, valuesBucket);
56     return MediaLibrarySmartAlbumMapOperations::HandleSmartAlbumMapOperation(smartMapCmd);
57 }
58 
CreateDirOperation(MediaLibraryCommand & cmd)59 int32_t MediaLibraryDirOperations::CreateDirOperation(MediaLibraryCommand &cmd)
60 {
61     ValueObject valueObject;
62     if (!cmd.GetValueBucket().GetObject(MEDIA_DATA_DB_RELATIVE_PATH, valueObject)) {
63         return E_HAS_DB_ERROR;
64     }
65     string relativePath;
66     valueObject.GetString(relativePath);
67     if (relativePath.empty()) {
68         return E_GET_VALUEBUCKET_FAIL;
69     }
70     ValuesBucket values;
71     values.PutString(MEDIA_DATA_DB_NAME, MEDIA_NO_FILE);
72     values.PutString(MEDIA_DATA_DB_RELATIVE_PATH, relativePath);
73     values.PutInt(MEDIA_DATA_DB_MEDIA_TYPE, MEDIA_TYPE_NOFILE);
74     cmd.SetValueBucket(values);
75     return MediaLibraryObjectUtils::CreateFileObj(cmd);
76 }
77 
HandleDirOperation(MediaLibraryCommand & cmd)78 int32_t MediaLibraryDirOperations::HandleDirOperation(MediaLibraryCommand &cmd)
79 {
80     int32_t errCode = E_FAIL;
81     switch (cmd.GetOprnType()) {
82         case OperationType::CREATE:
83             errCode = CreateDirOperation(cmd);
84             break;
85         case OperationType::TRASH:
86             errCode = TrashDirOperation(cmd);
87             break;
88         default:
89             MEDIA_WARN_LOG("Unknown operation type %{private}d", cmd.GetOprnType());
90             break;
91         }
92     if (errCode < 0) {
93         MEDIA_ERR_LOG("HandleDirOperations erroCode = %{public}d", errCode);
94     }
95     return errCode;
96 }
97 } // namespace Media
98 } // namespace OHOS
99