1 /*
2 * Copyright (c) 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
16 #include "preview_manager.h"
17 #include<iostream>
18 #include "factory_resource_compiler.h"
19 #include "file_entry.h"
20 #include "key_parser.h"
21 #include "resource_module.h"
22 #include "resource_util.h"
23 #include "sqlite_database.h"
24
25 namespace OHOS {
26 namespace Global {
27 namespace Restool {
28 using namespace std;
~PreviewManager()29 PreviewManager::~PreviewManager()
30 {
31 SqliteDatabase &database = SqliteDatabase::GetInstance();
32 database.CloseDatabase();
33 }
34
ScanModules(const vector<string> & modulePaths,const string & output)35 uint32_t PreviewManager::ScanModules(const vector<string> &modulePaths, const string &output)
36 {
37 SqliteDatabase &database = SqliteDatabase::GetInstance();
38 string dbPath = FileEntry::FilePath(output).Append("resources.db").GetPath();
39 database.Init(dbPath);
40 if (!database.OpenDatabase()) {
41 return RESTOOL_ERROR;
42 }
43
44 int32_t priority = 0;
45 if (priority_ >= 0) {
46 priority = priority_;
47 }
48
49 for (const auto &iter : modulePaths) {
50 if (FileEntry::IsDirectory(iter)) {
51 ResourceModule resourceMoudle(iter, output, "");
52 resourceMoudle.SetPreviewMode(true);
53 database.SetPriority(priority);
54 if (resourceMoudle.ScanResource() != RESTOOL_SUCCESS) {
55 return RESTOOL_ERROR;
56 }
57 } else if (!ScanFile(iter, priority)) {
58 return RESTOOL_ERROR;
59 }
60 if (priority_ >= 0) {
61 continue;
62 }
63 priority++;
64 }
65 return RESTOOL_SUCCESS;
66 }
67
ScanFile(const string & filePath,int32_t priority)68 bool PreviewManager::ScanFile(const string &filePath, int32_t priority)
69 {
70 if (!ResourceUtil::FileExist(filePath)) {
71 cerr << "Error: " << filePath << " non't exist." << endl;
72 return false;
73 }
74 FileInfo fileInfo;
75 fileInfo.filePath = filePath;
76 fileInfo.filename = FileEntry::FilePath(filePath).GetFilename();
77 fileInfo.dirPath = FileEntry::FilePath(filePath).GetParent().GetPath();
78 fileInfo.fileCluster = FileEntry::FilePath(fileInfo.dirPath).GetFilename();
79 fileInfo.limitKey = FileEntry::FilePath(fileInfo.dirPath).GetParent().GetFilename();
80
81 fileInfo.dirType = ResourceUtil::GetResTypeByDir(fileInfo.fileCluster);
82 if (fileInfo.dirType == ResType::INVALID_RES_TYPE) {
83 return false;
84 }
85
86 if (!KeyParser::Parse(fileInfo.limitKey, fileInfo.keyParams)) {
87 return false;
88 }
89
90 unique_ptr<IResourceCompiler> resourceCompiler =
91 FactoryResourceCompiler::CreateCompiler(fileInfo.dirType, "");
92 resourceCompiler->SetPreviewMode(true);
93 resourceCompiler->SetModuleName("");
94 if (resourceCompiler->Compile(fileInfo) != RESTOOL_SUCCESS) {
95 return false;
96 }
97 return true;
98 }
99 }
100 }
101 }
102