Skip to content

Commit

Permalink
Merge pull request #10825 from greenido/fixing-issue-9931
Browse files Browse the repository at this point in the history
Copy a file to the same directory
  • Loading branch information
MorrisJobke authored Nov 22, 2018
2 parents 510f720 + 802e4a7 commit 68ad2ae
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion apps/files/js/filelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2313,7 +2313,55 @@
// not overwrite it
targetPath = targetPath + '/';
}
self.filesClient.copy(dir + fileName, targetPath + fileName)
var targetPathAndName = targetPath + fileName;
if ((dir + fileName) === targetPathAndName) {
var dotIndex = targetPathAndName.indexOf(".");
if ( dotIndex > 1) {
var leftPartOfName = targetPathAndName.substr(0, dotIndex);
var fileNumber = leftPartOfName.match(/\d+/);
// TRANSLATORS name that is appended to copied files with the same name, will be put in parenthesis and appened with a number if it is the second+ copy
var copyNameLocalized = t('files', 'copy');
if (isNaN(fileNumber) ) {
fileNumber++;
targetPathAndName = targetPathAndName.replace(/(?=\.[^.]+$)/g, " (" + copyNameLocalized + " " + fileNumber + ")");
}
else {
// Check if we have other files with 'copy X' and the same name
var maxNum = 1;
if (self.files !== null) {
leftPartOfName = leftPartOfName.replace("/", "");
leftPartOfName = leftPartOfName.replace(new RegExp("\\(" + copyNameLocalized + "( \\d+)?\\)"),"");
// find the last file with the number extension and add one to the new name
for (var j = 0; j < self.files.length; j++) {
var cName = self.files[j].name;
if (cName.indexOf(leftPartOfName) > -1) {
if (cName.indexOf("(" + copyNameLocalized + ")") > 0) {
targetPathAndName = targetPathAndName.replace(new RegExp(" \\(" + copyNameLocalized + "\\)"),"");
if (maxNum == 1) {
maxNum = 2;
}
}
else {
var cFileNumber = cName.match(new RegExp("\\(" + copyNameLocalized + " (\\d+)\\)"));
if (cFileNumber && parseInt(cFileNumber[1]) >= maxNum) {
maxNum = parseInt(cFileNumber[1]) + 1;
}
}
}
}
targetPathAndName = targetPathAndName.replace(new RegExp(" \\(" + copyNameLocalized + " \\d+\\)"),"");
}
// Create the new file name with _x at the end
// Start from 2 per a special request of the 'standard'
var extensionName = " (" + copyNameLocalized + " " + maxNum +")";
if (maxNum == 1) {
extensionName = " (" + copyNameLocalized + ")";
}
targetPathAndName = targetPathAndName.replace(/(?=\.[^.]+$)/g, extensionName);
}
}
}
self.filesClient.copy(dir + fileName, targetPathAndName)
.done(function () {
filesToNotify.push(fileName);

Expand All @@ -2327,6 +2375,7 @@
oldFile.data('size', newSize);
oldFile.find('td.filesize').text(OC.Util.humanFileSize(newSize));
}
self.reload();
})
.fail(function(status) {
if (status === 412) {
Expand Down

0 comments on commit 68ad2ae

Please sign in to comment.