Skip to content

Commit

Permalink
Fix bug where directory name cannot be nonalphanumeric
Browse files Browse the repository at this point in the history
  • Loading branch information
Wiley Yu committed Dec 13, 2023
1 parent b6fcb30 commit 55f2c5e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RELEASE:=$(shell grep version manifest.json | sed '2q;d' | sed -e 's/^ *"version

zotmoov.xpi: FORCE
rm -rf $@
zip -FSr $@ bootstrap.js locale manifest.json prefs.js prefs.xhtml zotmoov.js zotmoov_prefs.js zotmoov_menus.js -x \*.DS_Store
zip -FSr $@ bootstrap.js locale manifest.json prefs.js prefs.xhtml zotmoov.js zotmoov_prefs.js zotmoov_menus.js lib -x \*.DS_Store

zotmoov-%-fx.xpi: zotmoov.xpi
mv $< $@
Expand Down
2 changes: 2 additions & 0 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ async function install()
async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })
{
Services.scriptloader.loadSubScript(rootURI + 'zotmoov.js');
Services.scriptloader.loadSubScript(rootURI + 'lib/sanitize-filename.js');

Services.scriptloader.loadSubScript(rootURI + 'zotmoov_menus.js');

Zotero.PreferencePanes.register(
Expand Down
56 changes: 56 additions & 0 deletions lib/sanitize-filename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Modified from https://github.com/parshap/node-sanitize-filename
* Replaces characters in strings that are illegal/unsafe for filenames.
* Unsafe characters are either removed or replaced by a substitute set
* in the optional `options` object.
*
* Illegal Characters on Various Operating Systems
* / ? < > \ : * | "
* https://kb.acronis.com/content/39790
*
* Unicode Control codes
* C0 0x00-0x1f & C1 (0x80-0x9f)
* http://en.wikipedia.org/wiki/C0_and_C1_control_codes
*
* Reserved filenames on Unix-based systems (".", "..")
* Reserved filenames in Windows ("CON", "PRN", "AUX", "NUL", "COM1",
* "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
* "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", and
* "LPT9") case-insesitively and with or without filename extensions.
*
* Capped at 255 characters in length.
* http://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs
*/

Zotero.ZotMoov.Sanitize =
{
_truncate(str, nchars)
{
let enc = new TextEncoder();
let dec = new TextDecoder('utf-8');
let uint8 = enc.encode(str)
let section = uint8.slice(0,nchars)
let result = dec.decode(section);
return result.replace(/\uFFFD/g, '');

},

sanitize(input, replacement) {
const illegalRe = /[\/\?<>\\:\*\|"]/g;
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
const reservedRe = /^\.+$/;
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
const windowsTrailingRe = /[\. ]+$/;

if (typeof input !== 'string') {
throw new Error('Input must be string');
}
var sanitized = input
.replace(illegalRe, replacement)
.replace(controlRe, replacement)
.replace(reservedRe, replacement)
.replace(windowsReservedRe, replacement)
.replace(windowsTrailingRe, replacement);
return Zotero.ZotMoov.Sanitize._truncate(sanitized, 255);
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"manifest_version": 2,
"name": "ZotMoov",
"version": "0.0.3b6",
"version": "0.0.3b7",
"description": "Mooves attachments and links them",
"author": "Wiley Yu",
"applications": {
Expand Down
2 changes: 1 addition & 1 deletion zotmoov.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Zotero.ZotMoov =
for (let i = collection_names.length - 1; i >= 0; i--) // Iterate backwards
{
let collection_name = collection_names[i];
collection_name = collection_name.replace(/[^a-z0-9]/gi, '_'); // Convert to file safe string
collection_name = Zotero.ZotMoov.Sanitize.sanitize(collection_name, '_'); // Convert to file safe string
local_dst_path = PathUtils.join(local_dst_path, collection_name);
}
}
Expand Down

0 comments on commit 55f2c5e

Please sign in to comment.