• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2024 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "command/query_command_v10.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include "media_log.h"
23 #include "userfile_client_ex.h"
24 #include "media_file_utils.h"
25 #include "utils/database_utils.h"
26 #include "utils/mediatool_command_utils.h"
27 #include "medialibrary_errno.h"
28 #include "datashare_predicates.h"
29 #include "userfile_client.h"
30 #include "constant.h"
31 #include "result_set_utils.h"
32 #include "parameter.h"
33 #include "parameters.h"
34 
35 namespace OHOS {
36 namespace Media {
37 namespace MediaTool {
38 const string PATH_HEAD = "/storage/cloud/";
QueryMediaFiles(const QueryParam & queryParam)39 static int32_t QueryMediaFiles(const QueryParam &queryParam)
40 {
41     std::vector<std::string> queryfilePaths;
42     std::string fileTableName = MediaFileUtils::GetTableNameByDisplayName(queryParam.displayName);
43     if (fileTableName != AudioColumn::AUDIOS_TABLE && fileTableName != PhotoColumn::PHOTOS_TABLE) {
44         printf("find 0 result\n");
45         printf("The displayName format is not correct!\n");
46         return Media::E_ERR;
47     }
48 
49     auto resultSet = UserFileClientEx::GetResultsetByDisplayName(fileTableName, queryParam.displayName);
50     if (resultSet == nullptr || resultSet->GoToFirstRow() != 0) {
51         printf("find 0 result\n");
52         return Media::E_ERR;
53     }
54     int count = 0;
55     if (resultSet->GetRowCount(count) != 0) {
56         return Media::E_ERR;
57     }
58     printf("find %d result\n", count);
59     if (queryParam.pathFlag) {
60         string activeUserId = "100";
61         if (MediatoolCommandUtils::QueryActiveUserId(activeUserId) != E_OK) {
62             MEDIA_ERR_LOG("Query active user id failed. Use 100 for safety.");
63         }
64         printf("path\n");
65         do {
66             auto path = GetStringVal(MediaColumn::MEDIA_FILE_PATH, resultSet);
67             auto pos = path.find("/storage/cloud/file", 0);
68             if (pos != string::npos) {
69                 path.insert(pos + PATH_HEAD.length(), activeUserId + "/");
70             }
71             printf("%s\n", path.c_str());
72         } while (!resultSet->GoToNextRow());
73         resultSet->Close();
74     } else if (queryParam.uriFlag) {
75         std::shared_ptr<FetchResult<FileAsset>> fetchResult = std::make_shared<FetchResult<FileAsset>>(resultSet);
76         fetchResult->SetResultNapiType(ResultNapiType::TYPE_USERFILE_MGR);
77         DumpOpt dumpOpt;
78         dumpOpt.count = fetchResult->GetCount();
79         MEDIA_DEBUG_LOG("count:%{public}d", dumpOpt.count);
80         dumpOpt.columns = {
81             MEDIA_DATA_DB_URI
82         };
83         DatabaseUtils::Dump(dumpOpt, fetchResult);
84     }
85     return Media::E_OK;
86 }
87 
Start(const ExecEnv & env)88 int32_t QueryCommandV10::Start(const ExecEnv &env)
89 {
90     return QueryMediaFiles(env.queryParam);
91 }
92 }
93 }
94 }
95