Handle overlong filenames in getClosestDirectory()
Some checks are pending
CI / Test (Windows arm64) (push) Blocked by required conditions
CI / Test (Windows x64) (push) Blocked by required conditions
CI / Build, Upload (push) Waiting to run
CI / Detect changes (push) Waiting to run
CI / Test () (push) Blocked by required conditions
CI / Test (macOS NFS) (push) Blocked by required conditions
CI / Utilities Tests (push) Waiting to run

relinkAttachment() calls getClosestDirectory() before showing the file
picker, and a too-long filename caused the OS.File.stat() in
getClosestDirectory() to throw, which prevented the file picker from
appearing after clicking Locate.

https://forums.zotero.org/discussion/133685/
This commit is contained in:
Dan Stillman 2026-09-10 07:22:19 -04:00
parent 913aff342d
commit ab34e9031a
2 changed files with 11 additions and 1 deletions

View file

@ -102,7 +102,9 @@ Zotero.File = new function () {
}
}
catch (e) {
if (e.becauseNoSuchFile) {}
// A missing file, or a filename too long for the filesystem, might still have
// an existing parent directory, so continue below
if (e.becauseNoSuchFile || e.message?.includes('NS_ERROR_FILE_NAME_TOO_LONG')) {}
// A path that can't be parsed (e.g., a POSIX-style path on Windows) doesn't
// exist either
else if (e.message?.includes('NS_ERROR_FILE_UNRECOGNIZED_PATH')) {

View file

@ -199,6 +199,14 @@ describe("Zotero.File", function () {
assert.equal(closest, tmpDir);
});
it("should return parent directory for a filename too long for the filesystem", async function () {
var tmpDir = await getTempDirectory();
var closest = await Zotero.File.getClosestDirectory(
OS.Path.join(tmpDir, 'a'.repeat(1000) + '.pdf')
);
assert.equal(closest, tmpDir);
});
it("should return false for a path that doesn't exist at all", async function () {
assert.isFalse(await Zotero.File.getClosestDirectory('/a/b/c'));
});