Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2023 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "filesystemapi.h"
23 :
24 : #include "io/fileinfo.h"
25 :
26 : #include "apiutils.h"
27 :
28 : #include "log.h"
29 :
30 : using namespace muse;
31 : using namespace muse::api;
32 : using namespace muse::io;
33 :
34 0 : FileSystemApi::FileSystemApi(IApiEngine* e)
35 0 : : ApiObject(e)
36 : {
37 0 : }
38 :
39 0 : QString FileSystemApi::fileName(const QString& path) const
40 : {
41 0 : return FileInfo(path).fileName().toQString();
42 : }
43 :
44 0 : QString FileSystemApi::baseName(const QString& path) const
45 : {
46 0 : return FileInfo(path).baseName().toQString();
47 : }
48 :
49 0 : JSRet FileSystemApi::remove(const QString& path)
50 : {
51 0 : Ret ret = fsRestriction()->isPathAllowed(path);
52 0 : if (!ret) {
53 0 : return retToJs(ret);
54 : }
55 :
56 0 : ret = fileSystem()->remove(path);
57 0 : return retToJs(ret);
58 0 : }
59 :
60 0 : JSRet FileSystemApi::clear(const QString& path)
61 : {
62 0 : Ret ret = fsRestriction()->isPathAllowed(path);
63 0 : if (!ret) {
64 0 : return retToJs(ret);
65 : }
66 :
67 0 : ret = fileSystem()->clear(path);
68 0 : return retToJs(ret);
69 0 : }
70 :
71 0 : JSRet FileSystemApi::copy(const QString& src, const QString& dst, bool replace)
72 : {
73 0 : Ret ret = fsRestriction()->isPathAllowed(src);
74 0 : if (!ret) {
75 0 : return retToJs(ret);
76 : }
77 :
78 0 : ret = fsRestriction()->isPathAllowed(dst);
79 0 : if (!ret) {
80 0 : return retToJs(ret);
81 : }
82 :
83 0 : ret = fileSystem()->copy(src, dst, replace);
84 0 : return retToJs(ret);
85 0 : }
86 :
87 0 : JSRetVal FileSystemApi::scanFiles(const QString& rootDir, const QStringList& filters, const QString& mode) const
88 : {
89 0 : Ret ret = fsRestriction()->isPathAllowed(rootDir);
90 0 : if (!ret) {
91 0 : return retValToJs(RetVal<io::paths_t>::make_ret(ret));
92 : }
93 :
94 0 : auto toIoScanMode = [](const QString& m)
95 : {
96 0 : if (m == "FilesInCurrentDir") {
97 0 : return io::ScanMode::FilesInCurrentDir;
98 0 : } else if (m == "FilesAndFoldersInCurrentDir") {
99 0 : return io::ScanMode::FilesAndFoldersInCurrentDir;
100 0 : } else if (m == "FilesInCurrentDirAndSubdirs") {
101 0 : return io::ScanMode::FilesInCurrentDirAndSubdirs;
102 : } else {
103 0 : LOGE() << "unknown mode: " << m;
104 : }
105 :
106 0 : return io::ScanMode::FilesInCurrentDirAndSubdirs;
107 : };
108 :
109 0 : RetVal<io::paths_t> rv = fileSystem()->scanFiles(rootDir, toStdVector(filters), toIoScanMode(mode));
110 0 : return retValToJs(rv);
111 0 : }
112 :
113 0 : JSRet FileSystemApi::writeTextFile(const QString& filePath, const QString& str) const
114 : {
115 0 : Ret ret = fsRestriction()->isPathAllowed(filePath);
116 0 : if (!ret) {
117 0 : return retToJs(ret);
118 : }
119 :
120 0 : QByteArray data = str.toUtf8();
121 0 : ret = fileSystem()->writeFile(filePath, ByteArray::fromQByteArrayNoCopy(data));
122 0 : return retToJs(ret);
123 0 : }
124 :
125 0 : JSRetVal FileSystemApi::readTextFile(const QString& filePath) const
126 : {
127 0 : Ret ret = fsRestriction()->isPathAllowed(filePath);
128 0 : if (!ret) {
129 0 : return retValToJs(RetVal<QString>::make_ret(ret));
130 : }
131 :
132 0 : RetVal<ByteArray> data = fileSystem()->readFile(filePath);
133 0 : RetVal<QString> sr;
134 0 : sr.ret = data.ret;
135 0 : sr.val = QString::fromUtf8(data.val.toQByteArrayNoCopy());
136 0 : return retValToJs(sr);
137 0 : }
|