Merge pull request #429 from aurimasv/rename-attachment
When renaming attachment files, keep file extension if not provided
This commit is contained in:
commit
8e276b30d4
|
@ -388,22 +388,30 @@
|
||||||
|
|
||||||
// Rename associated file
|
// Rename associated file
|
||||||
if (checkState.value) {
|
if (checkState.value) {
|
||||||
var renamed = item.renameAttachmentFile(newTitle.value);
|
var newFilename = newTitle.value.trim();
|
||||||
|
if (newFilename.search(/\.\w{1,10}$/) == -1) {
|
||||||
|
// User did not specify extension. Use current
|
||||||
|
var oldExt = item.getFilename().match(/\.\w{1,10}$/);
|
||||||
|
if (oldExt) newFilename += oldExt[0];
|
||||||
|
}
|
||||||
|
var renamed = item.renameAttachmentFile(newFilename);
|
||||||
if (renamed == -1) {
|
if (renamed == -1) {
|
||||||
var confirmed = nsIPS.confirm(
|
var confirmed = nsIPS.confirm(
|
||||||
window,
|
window,
|
||||||
'',
|
'',
|
||||||
newTitle.value + ' exists. Overwrite existing file?'
|
newFilename + ' exists. Overwrite existing file?'
|
||||||
);
|
);
|
||||||
if (confirmed) {
|
if (!confirmed) {
|
||||||
item.renameAttachmentFile(newTitle.value, true);
|
// If they said not to overwrite existing file,
|
||||||
break;
|
// start again
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
// If they said not to overwrite existing file,
|
|
||||||
// start again
|
// Force overwrite, but make sure we check that this doesn't fail
|
||||||
continue;
|
renamed = item.renameAttachmentFile(newFilename, true);
|
||||||
}
|
}
|
||||||
else if (renamed == -2) {
|
|
||||||
|
if (renamed == -2) {
|
||||||
nsIPS.alert(
|
nsIPS.alert(
|
||||||
window,
|
window,
|
||||||
Zotero.getString('general.error'),
|
Zotero.getString('general.error'),
|
||||||
|
|
|
@ -3009,6 +3009,7 @@ Zotero.Item.prototype.renameAttachmentFile = function(newName, overwrite) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var origModDate = file.lastModifiedTime;
|
||||||
try {
|
try {
|
||||||
newName = Zotero.File.getValidFileName(newName);
|
newName = Zotero.File.getValidFileName(newName);
|
||||||
|
|
||||||
|
@ -3027,18 +3028,17 @@ Zotero.Item.prototype.renameAttachmentFile = function(newName, overwrite) {
|
||||||
// files, since dest.exists() will just show true on a case-insensitive
|
// files, since dest.exists() will just show true on a case-insensitive
|
||||||
// filesystem anyway.
|
// filesystem anyway.
|
||||||
if (file.leafName.toLowerCase() != dest.leafName.toLowerCase()) {
|
if (file.leafName.toLowerCase() != dest.leafName.toLowerCase()) {
|
||||||
if (overwrite) {
|
if (!overwrite && dest.exists()) {
|
||||||
dest.remove(false);
|
|
||||||
}
|
|
||||||
else if (dest.exists()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file.moveTo(null, newName);
|
|
||||||
// Update mod time and clear hash so the file syncs
|
// Update mod time and clear hash so the file syncs
|
||||||
// TODO: use an integer counter instead of mod time for change detection
|
// TODO: use an integer counter instead of mod time for change detection
|
||||||
dest.lastModifiedTime = new Date();
|
// Update mod time first, because it may fail for read-only files on Windows
|
||||||
|
file.lastModifiedTime = new Date();
|
||||||
|
file.moveTo(null, newName);
|
||||||
|
|
||||||
this.relinkAttachmentFile(dest);
|
this.relinkAttachmentFile(dest);
|
||||||
|
|
||||||
Zotero.DB.beginTransaction();
|
Zotero.DB.beginTransaction();
|
||||||
|
@ -3051,6 +3051,8 @@ Zotero.Item.prototype.renameAttachmentFile = function(newName, overwrite) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
// Restore original modification date in case we managed to change it
|
||||||
|
try { file.lastModifiedTime = origModDate } catch (e) {}
|
||||||
Zotero.debug(e);
|
Zotero.debug(e);
|
||||||
Components.utils.reportError(e);
|
Components.utils.reportError(e);
|
||||||
return -2;
|
return -2;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user