- Automatically check for and remove invalid characters in filenames on a sync upload error

- Add newline and characters not valid in XML (mostly control characters) to filename blacklist
- Strip invalid characters when user is renaming a file manually
This commit is contained in:
Dan Stillman 2009-06-02 11:13:41 +00:00
parent 5b7923c71d
commit f681730e75
3 changed files with 40 additions and 0 deletions

View file

@ -2516,6 +2516,8 @@ Zotero.Item.prototype.renameAttachmentFile = function(newName, overwrite) {
}
try {
newName = Zotero.File.getValidFileName(newName);
if (file.leafName == newName) {
return true;
}

View file

@ -215,6 +215,10 @@ Zotero.File = new function(){
// TODO: use space instead, and figure out what's doing extra
// URL encode when saving attachments that trigger this
fileName = fileName.replace(/[\/\\\?%\*:|"<>]/g, '');
// Replace newlines (which shouldn't be in the string in the first place) with spaces
fileName = fileName.replace(/\n/g, ' ');
// Strip characters not valid in XML, since they won't sync and they're probably unwanted
fileName = fileName.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ud800-\udfff\ufffe\uffff]/g, '');
// Don't allow blank filename
if (!fileName) {
fileName = '_';

View file

@ -1467,6 +1467,40 @@ Zotero.Sync.Server = new function () {
if (firstChild.localName == 'error') {
switch (firstChild.getAttribute('code')) {
case 'INVALID_UPLOAD_DATA':
// On the off-chance that this error is due to invalid characters
// in a filename, check them all (since getting a more specific
// error from the server would be difficult)
var sql = "SELECT itemID FROM itemAttachments WHERE linkMode IN (?,?)";
var ids = Zotero.DB.columnQuery(sql, [Zotero.Attachments.LINK_MODE_IMPORTED_FILE, Zotero.Attachments.LINK_MODE_IMPORTED_URL]);
if (ids) {
var items = Zotero.Items.get(ids);
var rolledBack = false;
for each(var item in items) {
var file = item.getFile();
if (!file) {
continue;
}
try {
var fn = file.leafName;
// TODO: move stripping logic (copied from _xmlize()) to Utilities
var xmlfn = file.leafName.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ud800-\udfff\ufffe\uffff]/g, '');
if (fn != xmlfn) {
if (!rolledBack) {
Zotero.DB.rollbackAllTransactions();
}
Zotero.debug("Changing invalid filename to " + xmlfn);
item.renameAttachmentFile(xmlfn);
}
}
catch (e) {
Zotero.debug(e);
Components.utils.reportError(e);
}
}
}
break;
case 'ITEM_MISSING':
var [libraryID, key] = firstChild.getAttribute('missingItem').split('/');
if (libraryID == Zotero.libraryID) {