Skip to content

Commit

Permalink
Added filename generation for plugin if no value is specified for s…
Browse files Browse the repository at this point in the history
…ome reason
  • Loading branch information
modos189 committed Sep 19, 2024
1 parent 9dc7c01 commit 3acc1c4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/backup.js
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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(`${plugin_uid}.user.js`);
}
external_plugins[channel][plugin_filename] = all_storage[key][plugin_uid]['code'];
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -310,6 +310,7 @@ export class Manager extends Worker {
plugins_user[plugin_uid] = Object.assign(meta, {
uid: plugin_uid,
status: 'on',
filename: script['filename'] ? script['filename'] : sanitizeFileName(`${plugin_uid}.user.js`),
code: code,
});

Expand Down
3 changes: 3 additions & 0 deletions test/manager.2.external.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe('manage.js external plugins integration tests', function () {
category: 'Controls',
status: 'on',
user: true,
filename: 'Bookmarks for maps and portals+httpsgithub.comIITC-CEingress-intel-total-conversion.user.js',
code: external_code,
};
const run = await manager.addUserScripts(scripts);
Expand Down Expand Up @@ -145,6 +146,7 @@ describe('manage.js external plugins integration tests', function () {
category: 'Misc',
status: 'on',
user: true,
filename: 'Bookmarks2 for maps and portals+httpsgithub.comIITC-CEingress-intel-total-conversion.user.js',
code: external_code,
};
const run = await manager.addUserScripts(scripts);
Expand Down Expand Up @@ -356,6 +358,7 @@ describe('manage.js external plugins integration tests', function () {
category: 'Controls',
status: 'on',
user: true,
filename: 'Bookmarks for maps and portals+httpsgithub.comIITC-CEingress-intel-total-conversion.user.js',
code: external_code,
};
const run = await manager.addUserScripts(scripts);
Expand Down
5 changes: 2 additions & 3 deletions test/manager.9.backup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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+httpsgithub.comIITC-CEingress-intel-total-conversion.user.js': external_code,
},
},
data: {
Expand Down Expand Up @@ -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,
},
Expand All @@ -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 () {
Expand Down

0 comments on commit 3acc1c4

Please sign in to comment.