mirror of
https://github.com/finos/SymphonyElectron.git
synced 2024-12-28 09:51:06 -06:00
Merge pull request #256 from VishwasShashidhar/electron-205
electron-205: fixed file overwrite issues when a custom download folder is set by the user
This commit is contained in:
commit
6c15d14fa8
@ -56,7 +56,8 @@ function showInFinder(id) {
|
|||||||
function createDOM(arg) {
|
function createDOM(arg) {
|
||||||
|
|
||||||
if (arg && arg._id) {
|
if (arg && arg._id) {
|
||||||
let fileDisplayName = getFileDisplayName(arg.fileName);
|
|
||||||
|
let fileDisplayName = arg.fileName;
|
||||||
let downloadItemKey = arg._id;
|
let downloadItemKey = arg._id;
|
||||||
|
|
||||||
local.downloadItems.push(arg);
|
local.downloadItems.push(arg);
|
||||||
@ -196,33 +197,4 @@ function initiate() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a file display name for the download item
|
|
||||||
*/
|
|
||||||
function getFileDisplayName(fileName) {
|
|
||||||
let fileList = local.downloadItems;
|
|
||||||
let fileNameCount = 0;
|
|
||||||
let fileDisplayName = fileName;
|
|
||||||
|
|
||||||
/* Check if a file with the same name exists
|
|
||||||
* (akin to the user downloading a file with the same name again)
|
|
||||||
* in the download bar
|
|
||||||
*/
|
|
||||||
for (let i = 0; i < fileList.length; i++) {
|
|
||||||
if (fileName === fileList[i].fileName) {
|
|
||||||
fileNameCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If it exists, add a count to the name like how Chrome does */
|
|
||||||
if (fileNameCount) {
|
|
||||||
let extLastIndex = fileDisplayName.lastIndexOf('.');
|
|
||||||
let fileCount = ' (' + fileNameCount + ')';
|
|
||||||
|
|
||||||
fileDisplayName = fileDisplayName.slice(0, extLastIndex) + fileCount + fileDisplayName.slice(extLastIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
return fileDisplayName;
|
|
||||||
}
|
}
|
@ -38,7 +38,10 @@ let alwaysOnTop = false;
|
|||||||
let position = 'lower-right';
|
let position = 'lower-right';
|
||||||
let display;
|
let display;
|
||||||
let sandboxed = false;
|
let sandboxed = false;
|
||||||
let downloadsDirectory;
|
|
||||||
|
// By default, we set the user's default download directory
|
||||||
|
let defaultDownloadsDirectory = app.getPath("downloads");
|
||||||
|
let downloadsDirectory = defaultDownloadsDirectory;
|
||||||
|
|
||||||
// note: this file is built using browserify in prebuild step.
|
// note: this file is built using browserify in prebuild step.
|
||||||
const preloadMainScript = path.join(__dirname, 'preload/_preloadMain.js');
|
const preloadMainScript = path.join(__dirname, 'preload/_preloadMain.js');
|
||||||
@ -245,14 +248,6 @@ function doCreateMainWindow(initialUrl, initialBounds) {
|
|||||||
getConfigField('downloadsDirectory')
|
getConfigField('downloadsDirectory')
|
||||||
.then((value) => {
|
.then((value) => {
|
||||||
downloadsDirectory = value;
|
downloadsDirectory = value;
|
||||||
// if the directory has been deleted, try creating it.
|
|
||||||
if (!fs.existsSync(downloadsDirectory)) {
|
|
||||||
const directoryCreated = fs.mkdirSync(downloadsDirectory);
|
|
||||||
// If the directory creation failed, we use the default downloads directory
|
|
||||||
if (!directoryCreated) {
|
|
||||||
downloadsDirectory = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
log.send(logLevels.ERROR, 'Could not find the downloads directory config -> ' + error);
|
log.send(logLevels.ERROR, 'Could not find the downloads directory config -> ' + error);
|
||||||
@ -263,21 +258,28 @@ function doCreateMainWindow(initialUrl, initialBounds) {
|
|||||||
|
|
||||||
// When download is in progress, send necessary data to indicate the same
|
// When download is in progress, send necessary data to indicate the same
|
||||||
webContents.send('downloadProgress');
|
webContents.send('downloadProgress');
|
||||||
|
|
||||||
// if the user has set a custom downloads directory, save file to that directory
|
// An extra check to see if the user created downloads directory has been deleted
|
||||||
// if otherwise, we save it to the operating system's default downloads directory
|
// This scenario can occur when user doesn't quit electron and continues using it
|
||||||
if (downloadsDirectory) {
|
// across days and then deletes the folder.
|
||||||
item.setSavePath(downloadsDirectory + "/" + item.getFilename());
|
if (downloadsDirectory !== defaultDownloadsDirectory && !fs.existsSync(downloadsDirectory)) {
|
||||||
|
downloadsDirectory = defaultDownloadsDirectory;
|
||||||
|
updateConfigField("downloadsDirectory", downloadsDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send file path when download is complete
|
// We check the downloads directory to see if a file with the similar name
|
||||||
|
// already exists and get a unique filename if that's the case
|
||||||
|
let newFileName = getUniqueFileName(item.getFilename());
|
||||||
|
item.setSavePath(downloadsDirectory + "/" + newFileName);
|
||||||
|
|
||||||
|
// Send file path to construct the DOM in the UI when the download is complete
|
||||||
item.once('done', (e, state) => {
|
item.once('done', (e, state) => {
|
||||||
if (state === 'completed') {
|
if (state === 'completed') {
|
||||||
let data = {
|
let data = {
|
||||||
_id: getGuid(),
|
_id: getGuid(),
|
||||||
savedPath: item.getSavePath() ? item.getSavePath() : '',
|
savedPath: item.getSavePath() ? item.getSavePath() : '',
|
||||||
total: filesize(item.getTotalBytes() ? item.getTotalBytes() : 0),
|
total: filesize(item.getTotalBytes() ? item.getTotalBytes() : 0),
|
||||||
fileName: item.getFilename() ? item.getFilename() : 'No name'
|
fileName: newFileName
|
||||||
};
|
};
|
||||||
webContents.send('downloadCompleted', data);
|
webContents.send('downloadCompleted', data);
|
||||||
}
|
}
|
||||||
@ -771,6 +773,53 @@ function repositionMainWindow() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a unique filename like Chrome
|
||||||
|
* from a user's download directory
|
||||||
|
* @param filename filename passed by the remote server
|
||||||
|
* @returns {String} the new filename
|
||||||
|
*/
|
||||||
|
function getUniqueFileName(filename) {
|
||||||
|
|
||||||
|
// By default, we assume that the file exists
|
||||||
|
const fileExists = true;
|
||||||
|
|
||||||
|
// We break the file from it's extension to get the name
|
||||||
|
const actualFilename = filename.substr(0, filename.lastIndexOf('.')) || filename;
|
||||||
|
const fileType = filename.split('.').pop();
|
||||||
|
|
||||||
|
// We use this to set the new file name with an increment on the previous existing file
|
||||||
|
let fileNumber = 0;
|
||||||
|
let newPath;
|
||||||
|
|
||||||
|
while (fileExists) {
|
||||||
|
|
||||||
|
let fileNameString = fileNumber.toString();
|
||||||
|
|
||||||
|
// By default, we know if the file doesn't exist,
|
||||||
|
// we can use the filename sent by the remote server
|
||||||
|
let current = filename;
|
||||||
|
|
||||||
|
// If the file already exists, we know that the
|
||||||
|
// file number variable is increased, so,
|
||||||
|
// we construct a new file name with the file number
|
||||||
|
if (fileNumber > 0) {
|
||||||
|
current = actualFilename + " (" + fileNameString + ")." + fileType;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the file exists, increment the file number and repeat the loop
|
||||||
|
if (fs.existsSync(downloadsDirectory + "/" + current)) {
|
||||||
|
fileNumber++;
|
||||||
|
} else {
|
||||||
|
newPath = current;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPath;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createMainWindow: createMainWindow,
|
createMainWindow: createMainWindow,
|
||||||
getMainWindow: getMainWindow,
|
getMainWindow: getMainWindow,
|
||||||
|
@ -16,11 +16,11 @@ describe('download manager', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should inject multiple download items during multiple downloads', function() {
|
it('should inject multiple download items during multiple downloads', function() {
|
||||||
electron.ipcRenderer.send('downloadCompleted', { _id: '12345', fileName: 'test.png', total: 100 });
|
electron.ipcRenderer.send('downloadCompleted', { _id: '12345', fileName: 'test (1).png', total: 100 });
|
||||||
electron.ipcRenderer.send('downloadCompleted', { _id: '67890', fileName: 'test.png', total: 200 });
|
electron.ipcRenderer.send('downloadCompleted', { _id: '67890', fileName: 'test (2).png', total: 200 });
|
||||||
|
|
||||||
let fileNames = document.getElementsByClassName('text-cutoff');
|
let fileNames = document.getElementsByClassName('text-cutoff');
|
||||||
let fNames = [];
|
let fNames = [];
|
||||||
|
|
||||||
for (var i = 0; i < fileNames.length; i++) {
|
for (var i = 0; i < fileNames.length; i++) {
|
||||||
fNames.push(fileNames[i].innerHTML);
|
fNames.push(fileNames[i].innerHTML);
|
||||||
|
Loading…
Reference in New Issue
Block a user