Push asset server module
This commit is contained in:
52
asset_server/index.js
Normal file
52
asset_server/index.js
Normal file
@ -0,0 +1,52 @@
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
const fs = require('fs');
|
||||
var http = require('http');
|
||||
|
||||
const ip = '0.0.0.0';
|
||||
const port = 8887;
|
||||
//https://lovelive-schoolidolfestival2-album.akamaized.net
|
||||
app.use(function (req, res, next) {
|
||||
console.log(req.method, ":", req.url);
|
||||
next();
|
||||
})
|
||||
function createDirFromFile(path) {
|
||||
fs.mkdirSync(require('path').dirname(path), { recursive: true });
|
||||
}
|
||||
|
||||
app.get('/*', function (req, res) {
|
||||
const expectedPath = __dirname + "/resources"+req.url;
|
||||
createDirFromFile(expectedPath);
|
||||
let downloading = [];
|
||||
if (fs.existsSync(expectedPath)) {
|
||||
const fileStream = fs.createReadStream(expectedPath);
|
||||
res.writeHead(200);
|
||||
fileStream.pipe(res);
|
||||
} else {
|
||||
let url = (!req.url.toLowerCase().startsWith("android") ? "https://img-sif2.lovelive-sif2.com" : "https://album-sif2.lovelive-sif2.com") + req.url;
|
||||
const request = require('https').get(url, function(response) {
|
||||
response.pipe(res);
|
||||
});
|
||||
if (downloading.includes(req.url)) return;
|
||||
require('https').get(url, function(response) {
|
||||
console.log("Downloading " + req.url);
|
||||
downloading.push(req.url);
|
||||
const file = fs.createWriteStream(expectedPath);
|
||||
response.pipe(file);
|
||||
|
||||
// after download completed close filestream
|
||||
file.on("finish", () => {
|
||||
file.close();
|
||||
console.log("Download Completed " + req.url);
|
||||
});
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
var httpsServer = http.createServer(app);
|
||||
|
||||
|
||||
httpsServer.listen(port, ip, () => {
|
||||
let url = 'http://' + ip + ':' + port
|
||||
console.log('Server is listening at', url)
|
||||
})
|
Reference in New Issue
Block a user