Electron 446: Disable setting custom downloads directory (#349)

- remove feature to set custom downloads directory
- fix formatting
- fix formatting
This commit is contained in:
Vishwas Shashidhar 2018-05-01 13:35:57 +05:30 committed by GitHub
parent 4d15f03492
commit c1e4016e93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 116 deletions

View File

@ -56,15 +56,13 @@ function showInFinder(id) {
function createDOM(arg) {
if (arg && arg._id) {
let fileDisplayName = arg.fileName;
let fileDisplayName = getFileDisplayName(arg.fileName);
let downloadItemKey = arg._id;
local.downloadItems.push(arg);
let ul = document.getElementById('download-main');
if (ul) {
let li = document.createElement('li');
li.id = downloadItemKey;
li.classList.add('download-element');
@ -197,4 +195,33 @@ 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;
}

View File

@ -83,22 +83,6 @@ const template = [{
focusedWindow.reload();
}
}
},
{
label: 'Set Downloads Directory',
click() {
electron.dialog.showOpenDialog({
title: 'Select Downloads Directory',
buttonLabel: 'Select',
properties: ['openDirectory', 'createDirectory']
}, (filePaths) => {
if (!filePaths || !Array.isArray(filePaths) || filePaths.length < 1) {
return;
}
updateConfigField('downloadsDirectory', filePaths[0]);
eventEmitter.emit('setDownloadsDirectory', filePaths[0]);
});
}
},
{ type: 'separator' },
buildMenuItem('resetzoom'),

View File

@ -43,10 +43,6 @@ let display;
let sandboxed = false;
let isAutoReload = false;
// By default, we set the user's default download directory
let defaultDownloadsDirectory = app.getPath("downloads");
let downloadsDirectory = defaultDownloadsDirectory;
// Application menu
let menu;
@ -288,48 +284,20 @@ function doCreateMainWindow(initialUrl, initialBounds, isCustomTitleBar) {
}
mainWindow.on('closed', destroyAllWindows);
// if an user has set a custom downloads directory,
// we get that data from the user config file
getConfigField('downloadsDirectory')
.then((value) => {
downloadsDirectory = value;
})
.catch((error) => {
log.send(logLevels.ERROR, 'Could not find the downloads directory config -> ' + error);
});
// Manage File Downloads
mainWindow.webContents.session.on('will-download', (event, item, webContents) => {
// When download is in progress, send necessary data to indicate the same
webContents.send('downloadProgress');
// An extra check to see if the user created downloads directory has been deleted
// This scenario can occur when user doesn't quit electron and continues using it
// across days and then deletes the folder.
if (downloadsDirectory !== defaultDownloadsDirectory && !fs.existsSync(downloadsDirectory)) {
downloadsDirectory = defaultDownloadsDirectory;
updateConfigField("downloadsDirectory", downloadsDirectory);
}
// 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());
if (isMac) {
item.setSavePath(downloadsDirectory + "/" + newFileName);
} else {
item.setSavePath(downloadsDirectory + "\\" + newFileName);
}
// Send file path to construct the DOM in the UI when the download is complete
// Send file path when download is complete
item.once('done', (e, state) => {
if (state === 'completed') {
let data = {
_id: getGuid(),
savedPath: item.getSavePath() ? item.getSavePath() : '',
total: filesize(item.getTotalBytes() ? item.getTotalBytes() : 0),
fileName: newFileName
fileName: item.getFilename() ? item.getFilename() : 'No name'
};
webContents.send('downloadCompleted', data);
}
@ -803,11 +771,6 @@ eventEmitter.on('isAlwaysOnTop', (boolean) => {
isAlwaysOnTop(boolean);
});
// set downloads directory
eventEmitter.on('setDownloadsDirectory', (newDirectory) => {
downloadsDirectory = newDirectory;
});
// node event emitter for notification settings
eventEmitter.on('notificationSettings', (notificationSettings) => {
position = notificationSettings.position;
@ -925,53 +888,6 @@ 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 = {
createMainWindow: createMainWindow,
getMainWindow: getMainWindow,

View File

@ -138,9 +138,9 @@ describe('Tests for Always on top', () => {
robot.setMouseDelay(200);
robot.moveMouse(190, 0);
robot.mouseClick();
// Key tap 8 times as "Always on Top" is in the
// 8th position under view menu item
for (let i = 0; i < 8; i++) {
// Key tap 7 times as "Always on Top" is in the
// 7th position under view menu item
for (let i = 0; i < 7; i++) {
robot.keyTap('down');
}
robot.keyTap('enter');

View File

@ -105,9 +105,9 @@ describe('Tests for Full screen', () => {
robot.mouseClick();
robot.setKeyboardDelay(100);
// Key tap 6 times as "Enter Full Screen" is in the
// 6th position under view menu item
for (let i = 0; i < 6; i++) {
// Key tap 5 times as "Enter Full Screen" is in the
// 5th position under view menu item
for (let i = 0; i < 5; i++) {
robot.keyTap('down');
}
robot.keyTap('enter');

View File

@ -119,9 +119,9 @@ describe('Tests for Minimize on Close', () => {
robot.mouseClick();
robot.setKeyboardDelay(100);
// Key tap 9 times as "Minimize on Close" is in the
// 9th position under view menu item
for (let i = 0; i < 9; i++) {
// Key tap 8 times as "Minimize on Close" is in the
// 8th position under view menu item
for (let i = 0; i < 8; i++) {
robot.keyTap('down');
}
robot.keyTap('enter');