From 47dcbf65085e25328a527f2ca97e209e0ef4c72c Mon Sep 17 00:00:00 2001 From: Freddie Wang Date: Tue, 8 Dec 2020 20:21:11 +0800 Subject: [PATCH] v1.19rc --- build-env/assets/changelog.md | 3 +- build-env/assets/static/js/exposed.js | 2 +- build-env/assets/static/js/main.js | 30 ++++++ build-env/assets/views/_settings.pug | 8 +- package-lock.json | 149 +++++++++++++++++++++++++- package.json | 4 +- src/eamuse/EamusePlugin.ts | 7 +- src/utils/EamuseIO.ts | 17 ++- src/utils/Logger.ts | 2 +- src/webui/emit.ts | 71 +++++++++++- src/webui/index.ts | 2 +- 11 files changed, 275 insertions(+), 20 deletions(-) diff --git a/build-env/assets/changelog.md b/build-env/assets/changelog.md index 687d0ba..a9d7015 100644 --- a/build-env/assets/changelog.md +++ b/build-env/assets/changelog.md @@ -2,6 +2,7 @@ - **WebUI**: Query shell is now avaliable for all installed plugins even if there are no DB data of them - **WebUI**: Fixed a issue where the WebUI of plugins with uppercase letter cannot be accessed +- **WebUI**: Custom WebUI pages now works without `//DATA//` section. - **API**: Fixed a typechecking issue where `{ exists: true }` query is only allowed on number/string fields - **API**: Provided API `R.DataFile` to allow users to upload their data to the plugins folder - **API**: `DB.Upsert` now rejects null as refid @@ -28,7 +29,7 @@ ## v1.15 beta -- **PluginLoader**: Lodash (`\_`) is now exposed as API. +- **PluginLoader**: Lodash (`_`) is now exposed as API. - **WebUI**: Added a query shell for plugins. You need to enable developer mode to use it - **WebUI**: Added a dark mode which follows system's color preference diff --git a/build-env/assets/static/js/exposed.js b/build-env/assets/static/js/exposed.js index 2565f23..07b455b 100644 --- a/build-env/assets/static/js/exposed.js +++ b/build-env/assets/static/js/exposed.js @@ -1,3 +1,3 @@ function emit(event, data) { - return axios.post(`/emit/${event}`, data); + return axios.post(`/emit/event/${event}`, data); } diff --git a/build-env/assets/static/js/main.js b/build-env/assets/static/js/main.js index 5317b3f..85876f6 100644 --- a/build-env/assets/static/js/main.js +++ b/build-env/assets/static/js/main.js @@ -88,4 +88,34 @@ if (str == '') str = undefined; $(this).css({ background: GeoPattern.generate(str).toDataUrl() }); }); + + $('.data-upload').on('change', e => { + const data = new FormData(); + data.append('upload', e.currentTarget.files[0], e.currentTarget.files[0].name); + axios({ + method: 'post', + url: `/emit/upload/${encodeURIComponent(e.currentTarget.getAttribute('name'))}`, + data: data, + headers: { + 'Content-Type': `multipart/form-data; boundary=${data._boundary}`, + }, + }) + .then(response => { + window.location.reload(true); + }) + .catch(error => { + window.location.reload(true); + }); + }); + + $('.data-delete').on('click', e => { + axios + .delete(`/emit/upload/${encodeURIComponent(e.currentTarget.getAttribute('deleting'))}`) + .then(response => { + window.location.reload(true); + }) + .catch(error => { + window.location.reload(true); + }); + }); })(); diff --git a/build-env/assets/views/_settings.pug b/build-env/assets/views/_settings.pug index 6fff0e7..a488504 100644 --- a/build-env/assets/views/_settings.pug +++ b/build-env/assets/views/_settings.pug @@ -75,7 +75,7 @@ mixin file_control(option) if option.uploaded .file.is-primary.has-name label.file-label - input.file-input(type='file' name='test' accept='.xml') + input.data-upload.file-input(type='file' name=option.path accept=option.accept) span.file-cta span.file-icon i.mdi.mdi-check @@ -86,7 +86,7 @@ mixin file_control(option) else if option.required .file.is-warning.has-name label.file-label - input.file-input(type='file' name='test' accept='.xml') + input.data-upload.file-input(type='file' name=option.path accept=option.accept) span.file-cta span.file-icon i.mdi.mdi-alert @@ -97,7 +97,7 @@ mixin file_control(option) else .file.is-link.has-name label.file-label - input.file-input(type='file' name='test' accept='.xml') + input.data-upload.file-input(type='file' name=option.path accept=option.accept) span.file-cta span.file-icon i.mdi.mdi-file @@ -107,7 +107,7 @@ mixin file_control(option) i.mdi.mdi-upload if option.uploaded p.control - button.button.is-danger + button.data-delete.button.is-danger(deleting=option.path) span.icon i.mdi.mdi-trash-can span Clear diff --git a/package-lock.json b/package-lock.json index ce0f0ff..9e380a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -163,6 +163,15 @@ "integrity": "sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==", "dev": true }, + "@types/multer": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/@types/multer/-/multer-1.4.4.tgz", + "integrity": "sha512-wdfkiKBBEMTODNbuF3J+qDDSqJxt50yB9pgDiTcFew7f97Gcc7/sM4HR66ofGgpJPOALWOqKAch4gPyqEXSkeQ==", + "dev": true, + "requires": { + "@types/express": "*" + } + }, "@types/nedb": { "version": "1.8.9", "resolved": "https://registry.npmjs.org/@types/nedb/-/nedb-1.8.9.tgz", @@ -296,6 +305,11 @@ "color-convert": "^2.0.1" } }, + "append-field": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz", + "integrity": "sha1-HjRA6RXwsSA9I3SOeO3XubW0PlY=" + }, "arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", @@ -462,6 +476,38 @@ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" }, + "busboy": { + "version": "0.2.14", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz", + "integrity": "sha1-bCpiLvz0fFe7vh4qnDetNseSVFM=", + "requires": { + "dicer": "0.2.5", + "readable-stream": "1.1.x" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + } + } + }, "byline": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/byline/-/byline-5.0.0.tgz", @@ -611,6 +657,41 @@ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, + "concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "requires": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" + }, + "dependencies": { + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + } + } + }, "connect-flash": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/connect-flash/-/connect-flash-0.1.1.tgz", @@ -727,6 +808,38 @@ "kuler": "1.0.x" } }, + "dicer": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/dicer/-/dicer-0.2.5.tgz", + "integrity": "sha1-WZbAhrszIYyBLAkL3cCc0S+stw8=", + "requires": { + "readable-stream": "1.1.x", + "streamsearch": "0.1.2" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + } + } + }, "diff": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", @@ -1554,9 +1667,9 @@ } }, "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + "version": "4.17.20", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", + "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" }, "logform": { "version": "2.1.2", @@ -1700,6 +1813,21 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "multer": { + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/multer/-/multer-1.4.2.tgz", + "integrity": "sha512-xY8pX7V+ybyUpbYMxtjM9KAiD9ixtg5/JkeKUTD6xilfDv0vzzOFcCp4Ljb1UU3tSOM3VTZtKo63OmzOrGi3Cg==", + "requires": { + "append-field": "^1.0.0", + "busboy": "^0.2.11", + "concat-stream": "^1.5.2", + "mkdirp": "^0.5.1", + "object-assign": "^4.1.1", + "on-finished": "^2.3.0", + "type-is": "^1.6.4", + "xtend": "^4.0.0" + } + }, "multistream": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/multistream/-/multistream-2.1.1.tgz", @@ -2476,6 +2604,11 @@ } } }, + "streamsearch": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz", + "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=" + }, "string-width": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", @@ -2606,6 +2739,11 @@ "mime-types": "~2.1.24" } }, + "typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" + }, "typedoc": { "version": "0.17.6", "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.17.6.tgz", @@ -2874,6 +3012,11 @@ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + }, "y18n": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", diff --git a/package.json b/package.json index 208b330..284bf64 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@types/pug": "^2.0.4", "@types/showdown": "^1.9.3", "@types/sizeof": "^1.0.1", + "@types/multer": "^1.4.4", "prettier": "^2.0.5", "typedoc": "^0.17.6", "typescript": "^3.9.3" @@ -38,8 +39,9 @@ "hashids": "^2.2.1", "iconv-lite": "^0.5.1", "ini": "^1.3.5", - "lodash": "^4.17.15", + "lodash": "^4.17.20", "memorystore": "^1.6.2", + "multer": "^1.4.2", "nedb": "^1.8.0", "node-stream-zip": "^1.11.1", "open": "^7.0.4", diff --git a/src/eamuse/EamusePlugin.ts b/src/eamuse/EamusePlugin.ts index 03e41de..6a833e0 100644 --- a/src/eamuse/EamusePlugin.ts +++ b/src/eamuse/EamusePlugin.ts @@ -170,9 +170,10 @@ export class EamusePlugin { try { const template = readFileSync(filePath, { encoding: 'utf8' }); - const dataBlocks = template.match( - /^\/\/DATA\/\/\s*$[\n|\r|\n\r]((?:^\s+[_a-z]\w*:\s*.+$[\n|\r|\n\r]?)+)/gm - ); + const dataBlocks = + template.match( + /^\/\/DATA\/\/\s*$[\n|\r|\n\r]((?:^\s+[_a-z]\w*:\s*.+$[\n|\r|\n\r]?)+)/gm + ) || []; const fn = compile(template); const props: { [field: string]: string } = {}; diff --git a/src/utils/EamuseIO.ts b/src/utils/EamuseIO.ts index e4c9173..40320dc 100644 --- a/src/utils/EamuseIO.ts +++ b/src/utils/EamuseIO.ts @@ -1,4 +1,4 @@ -import { existsSync, mkdirSync, readFileSync, writeFile, readFile, readdir } from 'fs'; +import { existsSync, mkdirSync, readFileSync, writeFile, readFile, readdir, unlink } from 'fs'; import { Logger } from './Logger'; import path from 'path'; @@ -131,7 +131,7 @@ export async function ReadDir(plugin: PluginDetect, file: string) { export async function WriteFile( plugin: PluginDetect, file: string, - data: string, + data: string | Buffer, options: { encoding?: string | null; mode?: number | string; flag?: string } | string | null ) { const target = path.resolve(PLUGIN_PATH, plugin.name, file); @@ -157,6 +157,19 @@ export async function WriteFile( }); } +export async function DeleteFile(plugin: PluginDetect, file: string) { + const target = path.resolve(PLUGIN_PATH, plugin.name, file); + + return new Promise(resolve => { + unlink(target, err => { + if (err) { + Logger.error(`file writing failed: ${err}`, { plugin }); + } + resolve(); + }); + }); +} + export async function ReadFile( plugin: PluginDetect, file: string, diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts index fa48c43..7cf732d 100644 --- a/src/utils/Logger.ts +++ b/src/utils/Logger.ts @@ -11,7 +11,7 @@ export const Logger = winston.createLogger({ let stack = ''; if (info.stack) { stack += `\n${info.stack}`; - } else if ((info.message as any).stack) { + } else if (info.message && (info.message as any).stack) { stack += `\n${(info.message as any).stack}`; } diff --git a/src/webui/emit.ts b/src/webui/emit.ts index d142e22..c7d3eb2 100644 --- a/src/webui/emit.ts +++ b/src/webui/emit.ts @@ -2,16 +2,19 @@ import { Router } from 'express'; import { urlencoded, json } from 'body-parser'; import { ROOT_CONTAINER } from '../eamuse/index'; import { Logger } from '../utils/Logger'; +import multer from 'multer'; +import { WriteFile, DeleteFile } from '../utils/EamuseIO'; +import { DATAFILE_MAP } from '../utils/ArgConfig'; -export const emit = Router(); +export const ajax = Router(); -emit.post('/:event', urlencoded({ extended: true }), json(), async (req, res) => { +ajax.post('/event/:event', urlencoded({ extended: true }), json(), async (req, res) => { if (!req.headers.referer) { res.sendStatus(400); return; } - const match = req.headers.referer.match(/\/plugin\/([^\/]*)\//); + const match = req.headers.referer.match(/\/plugin\/([^\/]*)(?:\/.*)*$/); if (!match) { res.sendStatus(400); return; @@ -36,3 +39,65 @@ emit.post('/:event', urlencoded({ extended: true }), json(), async (req, res) => res.redirect(req.headers.referer); }); + +var storage = multer.memoryStorage(); +var upload = multer({ storage: storage }); +// File upload +ajax.post('/upload/:path', upload.single('upload'), async (req, res) => { + if (!req.headers.referer) { + res.sendStatus(400); + return; + } + + const match = req.headers.referer.match(/\/plugin\/([^\/]*)(?:\/.*)*$/); + if (!match) { + res.sendStatus(400); + return; + } + + const plugin = ROOT_CONTAINER.getPluginByID(match[1]); + if (!plugin) { + res.sendStatus(404); + return; + } + + const path = req.params.path; + if (DATAFILE_MAP[plugin.Identifier] && DATAFILE_MAP[plugin.Identifier].has(path)) { + WriteFile({ name: plugin.Identifier, core: true }, path, req.file.buffer, { encoding: null }); + + res.sendStatus(200); + } else { + res.sendStatus(400); + } +}); + +ajax.delete('/upload/:path', upload.single('upload'), async (req, res) => { + if (!req.headers.referer) { + res.sendStatus(400); + return; + } + + const match = req.headers.referer.match(/\/plugin\/([^\/]*)(?:\/.*)*$/); + if (!match) { + res.sendStatus(400); + return; + } + + const plugin = ROOT_CONTAINER.getPluginByID(match[1]); + if (!plugin) { + res.sendStatus(404); + return; + } + + const path = req.params.path; + if (DATAFILE_MAP[plugin.Identifier] && DATAFILE_MAP[plugin.Identifier].has(path)) { + try { + await DeleteFile({ name: plugin.Identifier, core: true }, path); + res.sendStatus(200); + } catch { + res.sendStatus(500); + } + } else { + res.sendStatus(400); + } +}); diff --git a/src/webui/index.ts b/src/webui/index.ts index 5ea751b..ff958f5 100644 --- a/src/webui/index.ts +++ b/src/webui/index.ts @@ -47,7 +47,7 @@ import { fun } from './fun'; import { card2nfc, nfc2card, cardType } from '../utils/CardCipher'; import { groupBy, startCase, lowerCase, upperFirst } from 'lodash'; import { sizeof } from 'sizeof'; -import { emit } from './emit'; +import { ajax as emit } from './emit'; import { Logger } from '../utils/Logger'; const memorystore = createMemoryStore(session);