You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.1 KiB

7 years ago
const path = require('path');
const AdmZip = require('adm-zip');
const remoteFileSize = require('remote-file-size');
const fs = require('fs-extra');
const request = require('request');
module.exports = (shepherd) => {
/*
* DL app patch
* type: GET
* params: patchList
*/
shepherd.get('/update/patch', (req, res, next) => {
7 years ago
if (shepherd.checkToken(req.query.token)) {
const successObj = {
msg: 'success',
result: 'dl started',
7 years ago
};
res.end(JSON.stringify(successObj));
shepherd.updateAgama();
} else {
const errorObj = {
msg: 'error',
result: 'unauthorized access',
};
res.end(JSON.stringify(errorObj));
}
});
shepherd.updateAgama = () => {
7 years ago
const rootLocation = path.join(__dirname, '../../');
shepherd.downloadFile({
remoteFile: 'https://github.com/pbca26/dl-test/raw/master/patch.zip',
localFile: `${rootLocation}patch.zip`,
onProgress: (received, total) => {
const percentage = (received * 100) / total;
if (percentage.toString().indexOf('.10') > -1) {
shepherd.io.emit('patch', {
msg: {
status: 'progress',
type: 'ui',
progress: percentage,
bytesTotal: total,
bytesReceived: received,
},
});
//shepherd.log(`patch ${percentage}% | ${received} bytes out of ${total} bytes.`);
}
}
})
.then(() => {
shepherd.remoteFileSize('https://github.com/pbca26/dl-test/raw/master/patch.zip', (err, remotePatchSize) => {
// verify that remote file is matching to DL'ed file
7 years ago
const localPatchSize = fs.statSync(`${rootLocation}patch.zip`).size;
shepherd.log('compare dl file size');
if (localPatchSize === remotePatchSize) {
7 years ago
const zip = new AdmZip(`${rootLocation}patch.zip`);
shepherd.log('patch succesfully downloaded');
shepherd.log('extracting contents');
if (shepherd.appConfig.dev) {
7 years ago
if (!fs.existsSync(`${rootLocation}/patch`)) {
fs.mkdirSync(`${rootLocation}/patch`);
}
}
zip.extractAllTo(/*target path*/rootLocation + (shepherd.appConfig.dev ? '/patch' : ''), /*overwrite*/true);
// TODO: extract files in chunks
shepherd.io.emit('patch', {
msg: {
type: 'ui',
status: 'done',
},
});
7 years ago
fs.unlinkSync(`${rootLocation}patch.zip`);
} else {
shepherd.io.emit('patch', {
msg: {
type: 'ui',
status: 'error',
message: 'size mismatch',
},
});
shepherd.log('patch file size doesnt match remote!');
}
});
});
}
/*
* check latest version
* type:
* params:
*/
shepherd.get('/update/patch/check', (req, res, next) => {
7 years ago
if (shepherd.checkToken(req.query.token)) {
7 years ago
const rootLocation = path.join(__dirname, '../../');
7 years ago
const options = {
url: 'https://github.com/pbca26/dl-test/raw/master/version',
method: 'GET',
};
7 years ago
request(options, (error, response, body) => {
7 years ago
if (response &&
response.statusCode &&
response.statusCode === 200) {
const remoteVersion = body.split('\n');
7 years ago
const localVersionFile = fs.readFileSync(`${rootLocation}version`, 'utf8');
7 years ago
let localVersion;
if (localVersionFile.indexOf('\r\n') > -1) {
localVersion = localVersionFile.split('\r\n');
} else {
localVersion = localVersionFile.split('\n');
}
7 years ago
if (remoteVersion[0] === localVersion[0]) {
const successObj = {
msg: 'success',
result: 'latest',
};
res.end(JSON.stringify(successObj));
} else {
const successObj = {
msg: 'success',
result: 'update',
version: {
local: localVersion[0],
remote: remoteVersion[0],
},
};
res.end(JSON.stringify(successObj));
}
} else {
7 years ago
res.end({
err: 'error getting update',
});
}
7 years ago
});
} else {
const errorObj = {
msg: 'error',
result: 'unauthorized access',
};
res.end(JSON.stringify(errorObj));
}
});
/*
* unpack zip
* type:
* params:
*/
shepherd.get('/unpack', (req, res, next) => {
7 years ago
if (shepherd.checkToken(req.query.token)) {
7 years ago
const dlLocation = path.join(__dirname, '../../');
const zip = new AdmZip(`${dlLocation}patch.zip`);
7 years ago
zip.extractAllTo(/*target path*/ `${dlLocation}/patch/unpack`, /*overwrite*/true);
const successObj = {
msg: 'success',
result: 'unpack started',
};
res.end(JSON.stringify(successObj));
} else {
const errorObj = {
msg: 'error',
result: 'unauthorized access',
};
res.end(JSON.stringify(errorObj));
}
});
return shepherd;
};