2020-11-07 17:59:37 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
const { dirname } = require('@joplin/lib/path-utils');
|
2021-01-22 19:41:11 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2019-12-29 19:58:40 +02:00
|
|
|
const pluginAssets = require('./pluginAssets/index');
|
2021-01-23 17:51:19 +02:00
|
|
|
import KvStore from '@joplin/lib/services/KvStore';
|
2024-08-02 15:51:49 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
import FsDriverWeb from './utils/fs-driver/fs-driver-rn.web';
|
|
|
|
|
|
|
|
const logger = Logger.create('PluginAssetsLoader');
|
2019-12-29 19:58:40 +02:00
|
|
|
|
|
|
|
export default class PluginAssetsLoader {
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
private static instance_: PluginAssetsLoader = null;
|
2019-12-29 19:58:40 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static instance() {
|
2019-12-29 19:58:40 +02:00
|
|
|
if (PluginAssetsLoader.instance_) return PluginAssetsLoader.instance_;
|
|
|
|
PluginAssetsLoader.instance_ = new PluginAssetsLoader();
|
|
|
|
return PluginAssetsLoader.instance_;
|
|
|
|
}
|
|
|
|
|
2024-08-02 15:51:49 +02:00
|
|
|
private destDir_() {
|
|
|
|
return `${Setting.value('resourceDir')}/pluginAssets`;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async importAssetsMobile_() {
|
|
|
|
const destDir = this.destDir_();
|
|
|
|
|
|
|
|
for (const name in pluginAssets.files) {
|
|
|
|
const dataBase64 = pluginAssets.files[name].data;
|
|
|
|
const destPath = `${destDir}/${name}`;
|
|
|
|
await shim.fsDriver().mkdir(dirname(destPath));
|
|
|
|
await shim.fsDriver().unlink(destPath);
|
|
|
|
|
|
|
|
logger.info(`PluginAssetsLoader: Copying: ${name} => ${destPath}`);
|
|
|
|
await shim.fsDriver().writeFile(destPath, dataBase64);
|
|
|
|
}
|
2019-12-29 19:58:40 +02:00
|
|
|
}
|
|
|
|
|
2024-08-02 15:51:49 +02:00
|
|
|
private async importAssetsWeb_() {
|
|
|
|
const destDir = this.destDir_();
|
|
|
|
const fsDriver = shim.fsDriver() as FsDriverWeb;
|
|
|
|
|
|
|
|
await Promise.all(pluginAssets.files.map(async (name: string) => {
|
|
|
|
const destPath = `${destDir}/${name}`;
|
|
|
|
const response = await fetch(`pluginAssets/${name}`);
|
|
|
|
await shim.fsDriver().mkdir(dirname(destPath));
|
|
|
|
|
|
|
|
await shim.fsDriver().unlink(destPath);
|
|
|
|
await fsDriver.writeFile(destPath, await response.arrayBuffer(), 'Buffer');
|
|
|
|
}));
|
2019-12-29 19:58:40 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async importAssets() {
|
2024-08-02 15:51:49 +02:00
|
|
|
const destDir = this.destDir_();
|
2019-12-29 19:58:40 +02:00
|
|
|
await shim.fsDriver().mkdir(destDir);
|
|
|
|
|
|
|
|
const hash = pluginAssets.hash;
|
|
|
|
if (hash === await KvStore.instance().value('PluginAssetsLoader.lastHash')) {
|
2024-08-02 15:51:49 +02:00
|
|
|
logger.info(`PluginAssetsLoader: Assets are up to date. Hash: ${hash}`);
|
2019-12-29 19:58:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-02 15:51:49 +02:00
|
|
|
logger.info(`PluginAssetsLoader: Importing assets to ${destDir}`);
|
2019-12-29 19:58:40 +02:00
|
|
|
|
|
|
|
try {
|
2024-08-02 15:51:49 +02:00
|
|
|
if (shim.mobilePlatform() === 'web') {
|
|
|
|
await this.importAssetsWeb_();
|
|
|
|
} else {
|
|
|
|
await this.importAssetsMobile_();
|
2019-12-29 19:58:40 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-08-02 15:51:49 +02:00
|
|
|
logger.error(error);
|
2019-12-29 19:58:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
await KvStore.instance().setValue('PluginAssetsLoader.lastHash', hash);
|
2019-12-29 19:58:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|