Many more fixes...
This commit is contained in:
+19
-4
@@ -6,6 +6,7 @@ import { getEnvCfg } from './env-config';
|
||||
const path = require('path');
|
||||
const { autoUpdater } = require('electron-updater');
|
||||
const download = require('./download');
|
||||
const upload = require('./upload');
|
||||
const extract = require('extract-zip');
|
||||
|
||||
const Readable = require('stream').Readable;
|
||||
@@ -160,9 +161,11 @@ class ADB {
|
||||
rejectUnauthorized: process.env.NODE_ENV !== 'dev',
|
||||
json: { token: token },
|
||||
};
|
||||
console.log("IFT:", options)
|
||||
request(options.url, options, (error, response, body) => {
|
||||
if (!error && body.data && body.data.apps && body.data.apps.length) {
|
||||
let tasks = [];
|
||||
console.log("IFT Body", body);
|
||||
for (let i = 0; i < body.data.apps.length; i++) {
|
||||
let app = body.data.apps[i];
|
||||
if (Number(app.app_categories_id) === 1) {
|
||||
@@ -861,13 +864,23 @@ function setupApp() {
|
||||
|
||||
const adb = new ADB();
|
||||
|
||||
ipcMain.on('download-url', async (event, { url, token, directory, filename }) => {
|
||||
download(url, path.join(directory, filename), stats => {
|
||||
ipcMain.on('download-url', async (event, { url, token, directory, filename, options }) => {
|
||||
download(url, directory ? path.join(directory, filename) : filename, stats => {
|
||||
return event.sender.send('download-progress', { stats, token });
|
||||
})
|
||||
.then(() => event.sender.send('download-url', { token }))
|
||||
}, options)
|
||||
.then((fileName) => event.sender.send('download-url', { token, fileName }))
|
||||
.catch(e => event.sender.send('download-url-fail', { e, token }));
|
||||
});
|
||||
|
||||
ipcMain.on('upload-url', async (event, { url, token, filename, options }) => {
|
||||
upload(url, filename, stats => {
|
||||
return event.sender.send('download-progress', { stats, token });
|
||||
}, options)
|
||||
.then((fileName) => event.sender.send('download-url', { token, fileName }))
|
||||
.catch(e => event.sender.send('download-url-fail', { e, token }));
|
||||
|
||||
});
|
||||
|
||||
ipcMain.on('extract-file', async (event, { token, directory, filename }) => {
|
||||
extract(
|
||||
filename,
|
||||
@@ -890,6 +903,7 @@ function setupApp() {
|
||||
}, 5000);
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on('adb-command', (event, arg) => {
|
||||
const success = d => {
|
||||
//console.log(" ", arg.command, "Success", d)
|
||||
@@ -912,6 +926,7 @@ function setupApp() {
|
||||
try {
|
||||
switch (arg.command) {
|
||||
case 'installFromToken':
|
||||
console.log("E-IFT", arg.settings.token)
|
||||
adb.installFromToken(arg.settings.token, success, reject);
|
||||
break;
|
||||
case 'setupAdb':
|
||||
|
||||
+39
-14
@@ -1,33 +1,57 @@
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
const _fs = require('fs');
|
||||
const { basename } = require('path');
|
||||
const https = require('node:https');
|
||||
const http = require('node:http');
|
||||
const nodeFS = require('node:fs');
|
||||
const nodePath = require('node:path');
|
||||
const Url = require('url');
|
||||
const electron = require('electron');
|
||||
|
||||
const TIMEOUT = 60000;
|
||||
|
||||
module.exports = function(url, path, progress) {
|
||||
module.exports = function(url, filePath, progress, options) {
|
||||
const uri = Url.parse(url);
|
||||
if (!path) {
|
||||
path = basename(uri.path);
|
||||
if (!filePath) {
|
||||
filePath = nodePath.basename(uri.path);
|
||||
}
|
||||
const file = _fs.createWriteStream(path);
|
||||
|
||||
let file, fileName = filePath;
|
||||
function get(url, callback) {
|
||||
let request = (uri.protocol === 'https:' ? https : http).get(url, function(response) {
|
||||
if (response.headers.location) {
|
||||
get(response.headers.location, callback);
|
||||
} else {
|
||||
if (options && options.useContentDispositionFilename) {
|
||||
const contentDisposition = response.headers['content-disposition'];
|
||||
if (contentDisposition) {
|
||||
const appData = nodePath.join(electron.app.getPath('appData'), 'SideQuest');
|
||||
const match = contentDisposition.match(/filename="(.+)"/);
|
||||
if (match && match.length > 1) {
|
||||
fileName = nodePath.join(appData, match[1]);
|
||||
file = nodeFS.createWriteStream(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!file) {
|
||||
file = nodeFS.createWriteStream(filePath);
|
||||
}
|
||||
callback(null, response);
|
||||
}
|
||||
});
|
||||
request.on('error', function(err) {
|
||||
callback(err);
|
||||
});
|
||||
request.setTimeout(TIMEOUT, function() {
|
||||
request.abort();
|
||||
callback(new Error(`request timeout after ${TIMEOUT / 1000.0}s`));
|
||||
});
|
||||
if (options && options.timeout !== undefined) {
|
||||
if (options.timeout > 0) {
|
||||
request.setTimeout(TIMEOUT, function() {
|
||||
request.abort();
|
||||
callback(new Error(`request timeout after ${options.timeout / 1000.0}s`));
|
||||
});
|
||||
}
|
||||
} else {
|
||||
request.setTimeout(TIMEOUT, function() {
|
||||
request.abort();
|
||||
callback(new Error(`request timeout after ${TIMEOUT / 1000.0}s`));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
@@ -35,7 +59,8 @@ module.exports = function(url, path, progress) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
const len = parseInt(res.headers['content-length'], 10);
|
||||
let len = parseInt(res.headers['content-length'], 10);
|
||||
if (isNaN(len) || len <= 0) { len = 1; }
|
||||
let downloaded = 0;
|
||||
let percent = 0;
|
||||
res.on('data', function(chunk) {
|
||||
@@ -46,7 +71,7 @@ module.exports = function(url, path, progress) {
|
||||
})
|
||||
.on('end', function() {
|
||||
file.end();
|
||||
resolve(true);
|
||||
resolve(fileName);
|
||||
})
|
||||
.on('error', function(err) {
|
||||
reject(err);
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
const _fs = require('fs');
|
||||
const _path = require('path');
|
||||
const request = require('request');
|
||||
const progress = require('request-progress');
|
||||
|
||||
|
||||
module.exports = function(url, filePath, progressCallback, options?) {
|
||||
|
||||
let contentType = 'application/zip';
|
||||
if (options && options.contentType) {
|
||||
contentType = options.contentType;
|
||||
}
|
||||
let fileName = _path.basename(filePath);
|
||||
if (options && options.fileName) {
|
||||
fileName = options.fileName;
|
||||
}
|
||||
|
||||
const formData = {
|
||||
file: {
|
||||
value: _fs.createReadStream(filePath),
|
||||
options: {
|
||||
filename: fileName,
|
||||
contentType: contentType,
|
||||
},
|
||||
},
|
||||
};
|
||||
const requestOptions = {
|
||||
url: url,
|
||||
method: 'POST',
|
||||
formData: formData,
|
||||
};
|
||||
return new Promise(function(resolve, reject) {
|
||||
progress(request(url, requestOptions), { throttle: 1000 })
|
||||
.on('error', error => {
|
||||
reject(error);
|
||||
})
|
||||
.on('progress', state => {
|
||||
progressCallback(state.percent);
|
||||
})
|
||||
.on('end', () => {
|
||||
resolve(true);
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user