From 5f2f85ec9f377c5dfcb5c8d1d37a96dcd218186e Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Thu, 19 Sep 2024 19:26:56 +0500 Subject: [PATCH] Added `filename` generation for plugin if no value is specified for some reason --- src/backup.js | 7 +++++-- src/helpers.js | 19 +++++++++++++++++++ src/manager.js | 3 ++- test/manager.2.external.spec.js | 3 +++ test/manager.9.backup.spec.js | 5 ++--- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/backup.js b/src/backup.js index e4c21b1..c4ae47a 100644 --- a/src/backup.js +++ b/src/backup.js @@ -1,6 +1,6 @@ // @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3 -import { isSet, parseMeta } from './helpers.js'; +import { isSet, parseMeta, sanitizeFileName } from './helpers.js'; import deepmerge from '@bundled-es-modules/deepmerge'; /** @@ -123,7 +123,10 @@ export const exportExternalPlugins = (all_storage) => { // Loop through each plugin UID in the current key's storage data for (const plugin_uid in all_storage[key]) { // Get the plugin's filename and code from the storage data and add to the external_plugins object - const plugin_filename = all_storage[key][plugin_uid]['filename']; + let plugin_filename = all_storage[key][plugin_uid]['filename']; + if (!plugin_filename) { + plugin_filename = sanitizeFileName(`${all_storage[key][plugin_uid]['name']}.user.js`); + } external_plugins[channel][plugin_filename] = all_storage[key][plugin_uid]['code']; } } diff --git a/src/helpers.js b/src/helpers.js index dc633df..0807a27 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -212,3 +212,22 @@ export function clearWait() { export function isSet(value) { return typeof value !== 'undefined' && value !== null; } + +/** + * Processes a string by removing invalid characters for the file system and limiting its length. + * + * @param {string} input - The original string to be converted into a file name. + * @param {number} maxLength - The maximum length of the file name (default is 255 characters). + * @returns {string} - The processed string. + */ +export function sanitizeFileName(input, maxLength = 255) { + const invalidChars = /[/\\:*?"<>|]/g; + let sanitized = input.replace(invalidChars, ''); + + // Truncate the length to maxLength characters + if (sanitized.length > maxLength) { + sanitized = sanitized.slice(0, maxLength); + } + + return sanitized; +} diff --git a/src/manager.js b/src/manager.js index de6e742..ea96e08 100644 --- a/src/manager.js +++ b/src/manager.js @@ -2,7 +2,7 @@ import { Worker } from './worker.js'; import * as migrations from './migrations.js'; -import { getUID, isSet } from './helpers.js'; +import { getUID, isSet, sanitizeFileName } from './helpers.js'; import * as backup from './backup.js'; /** @@ -310,6 +310,7 @@ export class Manager extends Worker { plugins_user[plugin_uid] = Object.assign(meta, { uid: plugin_uid, status: 'on', + filename: meta['filename'] ? meta['filename'] : sanitizeFileName(`${meta['name']}.user.js`), code: code, }); diff --git a/test/manager.2.external.spec.js b/test/manager.2.external.spec.js index a89fb34..a5763fb 100644 --- a/test/manager.2.external.spec.js +++ b/test/manager.2.external.spec.js @@ -97,6 +97,7 @@ describe('manage.js external plugins integration tests', function () { category: 'Controls', status: 'on', user: true, + filename: 'Bookmarks for maps and portals.user.js', code: external_code, }; const run = await manager.addUserScripts(scripts); @@ -145,6 +146,7 @@ describe('manage.js external plugins integration tests', function () { category: 'Misc', status: 'on', user: true, + filename: 'Bookmarks2 for maps and portals.user.js', code: external_code, }; const run = await manager.addUserScripts(scripts); @@ -356,6 +358,7 @@ describe('manage.js external plugins integration tests', function () { category: 'Controls', status: 'on', user: true, + filename: 'Bookmarks for maps and portals.user.js', code: external_code, }; const run = await manager.addUserScripts(scripts); diff --git a/test/manager.9.backup.spec.js b/test/manager.9.backup.spec.js index 915c38b..b74e807 100644 --- a/test/manager.9.backup.spec.js +++ b/test/manager.9.backup.spec.js @@ -21,7 +21,7 @@ describe('getBackupData and setBackupData', function () { custom: {}, release: { 'total-conversion-build.user.js': external_iitc_code, - 'bookmarks1.user.js': external_code, + 'Bookmarks for maps and portals.user.js': external_code, }, }, data: { @@ -131,7 +131,6 @@ describe('getBackupData and setBackupData', function () { namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion', name: 'Bookmarks for maps and portals', category: 'Controls', - filename: 'bookmarks1.user.js', }, code: external_code, }, @@ -146,10 +145,10 @@ describe('getBackupData and setBackupData', function () { status: 'on', user: true, code: external_code, - filename: 'bookmarks1.user.js', }, }; const run = await manager.addUserScripts(scripts); + delete run['Bookmarks for maps and portals+https://github.com/IITC-CE/ingress-intel-total-conversion']['filename']; expect(run).to.deep.equal(installed); }); it('Add plugin settings data', async function () {