thedesk/app/main/dl.js

127 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-05-19 17:39:30 +10:00
function dl(mainWindow, lang_path, base) {
const electron = require("electron");
const shell = electron.shell;
2019-04-03 14:59:29 +11:00
const fs = require("fs");
2019-05-19 17:39:30 +10:00
const { download } = require('electron-dl');
const BrowserWindow = electron.BrowserWindow;
const dialog = electron.dialog;
2019-04-03 14:59:29 +11:00
var updatewin = null;
2019-05-19 17:39:30 +10:00
var ipc = electron.ipcMain;
const app = electron.app;
const join = require('path').join;
ipc.on('update', function (e, x, y) {
2019-04-03 14:59:29 +11:00
2019-05-19 17:39:30 +10:00
var platform = process.platform;
var bit = process.arch;
if (platform != "others") {
updatewin = new BrowserWindow({
2019-05-19 20:24:27 +10:00
webPreferences: {
nodeIntegration:true
},
2019-05-19 17:39:30 +10:00
width: 600,
height: 400,
"transparent": false, // ウィンドウの背景を透過
"frame": false, // 枠の無いウィンドウ
"resizable": false,
"modal": true
});
var lang = fs.readFileSync(lang_path, 'utf8');
updatewin.loadURL(base + lang + '/update.html');
return "true"
} else {
return false;
}
})
//アプデDL
2019-04-03 14:59:29 +11:00
ipc.on('download-btn', (e, args) => {
2019-05-19 17:39:30 +10:00
2019-04-03 14:59:29 +11:00
var platform = process.platform;
var bit = process.arch;
dialog.showSaveDialog(null, {
title: 'Save',
defaultPath: app.getPath('home') + "/" + args[1]
}, (savedFiles) => {
console.log(savedFiles);
if (!savedFiles) {
return false;
}
if (platform == "win32") {
var m = savedFiles.match(/(.+)\\(.+)$/);
} else {
var m = savedFiles.match(/(.+)\/(.+)$/);
}
//console.log(m);
if (isExistFile(savedFiles)) {
fs.unlinkSync(savedFiles);
}
dl(args[0], args[1], m[1]);
});
});
function isExistFile(file) {
try {
fs.statSync(file);
return true
} catch (err) {
if (err.code === 'ENOENT') return false
}
}
function dl(url, file, dir) {
2019-05-19 17:39:30 +10:00
2019-04-03 14:59:29 +11:00
updatewin.webContents.send('mess', "ダウンロードを開始します。");
const opts = {
directory: dir,
openFolderWhenDone: true,
2019-05-19 17:39:30 +10:00
onProgress: function (e) {
2019-04-03 14:59:29 +11:00
updatewin.webContents.send('prog', e);
},
saveAs: false
};
2019-05-26 21:42:52 +10:00
download(updatewin,
2019-05-19 17:39:30 +10:00
url, opts)
2019-04-03 14:59:29 +11:00
.then(dl => {
updatewin.webContents.send('mess', "ダウンロードが完了しました。");
app.quit();
})
.catch(console.error);
}
ipc.on('general-dl', (e, args) => {
2019-05-19 17:39:30 +10:00
2019-04-03 14:59:29 +11:00
var name = "";
var platform = process.platform;
var bit = process.arch;
if (args[1] == "") {
if (platform == "win32") {
var dir = app.getPath('home') + "\\Pictures\\TheDesk";
} else if (platform == "linux" || platform == "darwin") {
var dir = app.getPath('home') + "/Pictures/TheDesk";
}
} else {
var dir = args[1];
}
const opts = {
directory: dir,
filename: name,
openFolderWhenDone: false,
2019-05-19 17:39:30 +10:00
onProgress: function (e) {
2019-04-03 14:59:29 +11:00
mainWindow.webContents.send('general-dl-prog', e);
},
saveAs: false
};
download(BrowserWindow.getFocusedWindow(),
2019-05-19 17:39:30 +10:00
args[0], opts)
2019-04-03 14:59:29 +11:00
.then(dl => {
mainWindow.webContents.send('general-dl-message', dir);
})
.catch(console.error);
2019-05-19 17:39:30 +10:00
});
ipc.on('open-finder', (e, folder) => {
2019-04-03 14:59:29 +11:00
shell.showItemInFolder(folder)
2019-05-19 17:39:30 +10:00
});
2019-04-03 14:59:29 +11:00
}
exports.dl = dl;