Skip to content

Commit 3ca9f73

Browse files
fix. plugin icons
1 parent 5ff9470 commit 3ca9f73

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

src/fileSystem/internalFs.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ const internalFs = {
113113
*/
114114

115115
delete(filename) {
116-
return new Promise((resolve, reject) => {
116+
return new Promise(async (resolve, reject) => {
117117
console.log("Deleting " + filename);
118118

119-
if (!this.exists(filename)) {
120-
console.log(`File ${filename} doesnt exists`);
119+
if (!(await this.exists(filename))) {
120+
console.warn(`File ${filename} doesnt exists`);
121121
resolve();
122122
} else {
123123
Filesystem.stat({ path: filename })
@@ -146,6 +146,7 @@ const internalFs = {
146146
* @param {string} encoding
147147
* @returns {Promise}
148148
*/
149+
149150
readFile(filename) {
150151
return new Promise((resolve, reject) => {
151152
reject = setMessage(reject);
@@ -166,6 +167,30 @@ const internalFs = {
166167
});
167168
},
168169

170+
readFileRaw(filename) {
171+
return new Promise((resolve, reject) => {
172+
reject = setMessage(reject);
173+
Filesystem.readFile({ path: filename, encoding: Encoding.BASE64 })
174+
.then((readFileResult) => {
175+
const base64 = readFileResult.data;
176+
const binary = atob(base64);
177+
const bytes = new Uint8Array(binary.length);
178+
for (let i = 0; i < binary.length; i++) {
179+
bytes[i] = binary.charCodeAt(i);
180+
}
181+
182+
resolve({ data: bytes.buffer });
183+
})
184+
.catch((error) => {
185+
console.error(
186+
`Failed to read raw file "${filename}", error: `,
187+
error,
188+
);
189+
reject(error);
190+
});
191+
});
192+
},
193+
169194
readStringFile(filename) {
170195
return new Promise((resolve, reject) => {
171196
reject = setMessage(reject);

src/lib/checkPluginsUpdate.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ajax from "@deadlyjack/ajax";
2+
import internalFs from "fileSystem/internalFs";
23
import fsOperation from "../fileSystem";
34
import Url from "../utils/Url";
45

@@ -10,16 +11,20 @@ export default async function checkPluginsUpdate() {
1011
plugins.forEach((pluginDir) => {
1112
promises.push(
1213
(async () => {
13-
const plugin = await fsOperation(
14-
Url.join(pluginDir.url, "plugin.json"),
15-
).readFile("json");
14+
try {
15+
const plugin = await fsOperation(
16+
Url.join(pluginDir.url, "plugin.json"),
17+
).readFile("json");
1618

17-
const res = await ajax({
18-
url: `https://acode.app/api/plugin/check-update/${plugin.id}/${plugin.version}`,
19-
});
19+
const res = await ajax({
20+
url: `https://acode.app/api/plugin/check-update/${plugin.id}/${plugin.version}`,
21+
});
2022

21-
if (res.update) {
22-
updates.push(plugin.id);
23+
if (res && res.update === true) {
24+
updates.push(plugin.id);
25+
}
26+
} catch (e) {
27+
console.warn(e);
2328
}
2429
})(),
2530
);

src/lib/loadPlugin.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ export default async function loadPlugin(pluginId, justInstalled = false) {
4040

4141
const data = result.data;
4242

43-
console.log(`data ${data}`);
44-
4543
const blob = new Blob([data], { type: "text/javascript" });
4644
const url = URL.createObjectURL(blob);
4745

src/sidebarApps/extensions/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import purchaseListener from "handlers/purchase";
1111
import constants from "lib/constants";
1212
import InstallState from "lib/installState";
1313
import settings from "lib/settings";
14+
import mimeType from "mime-types";
1415
import FileBrowser from "pages/fileBrowser";
1516
import plugin from "pages/plugin";
1617
import Url from "utils/Url";
@@ -301,6 +302,8 @@ async function loadExplore() {
301302
}
302303
}
303304

305+
const iconUrls = new Map();
306+
304307
async function listInstalledPlugins() {
305308
let dirItems;
306309

@@ -330,7 +333,23 @@ async function listInstalledPlugins() {
330333

331334
try {
332335
const iconUrl = getLocalRes(id, plugin.icon);
333-
plugin.icon = iconUrl;
336+
if (iconUrls.has(iconUrl)) {
337+
URL.revokeObjectURL(iconUrls.get(iconUrl));
338+
iconUrls.delete(iconUrl);
339+
}
340+
341+
if (await internalFs.exists(iconUrl)) {
342+
const fileContent = await internalFs.readFileRaw(iconUrl);
343+
const ext = Url.extname(iconUrl);
344+
const mime = mimeType.lookup(ext);
345+
const blob = new Blob([fileContent.data], { type: mime });
346+
const url = URL.createObjectURL(blob);
347+
plugin.icon = url;
348+
iconUrls.set(iconUrl, url);
349+
} else {
350+
console.error(`File path ${iconUrl} doesnt exists`);
351+
plugin.icon = null;
352+
}
334353
} catch (err) {
335354
plugin.icon = null;
336355
}

0 commit comments

Comments
 (0)