ew/asset_server/index.js

57 lines
2 KiB
JavaScript
Raw Normal View History

2024-02-22 23:17:32 +00:00
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) {
2024-04-25 00:05:37 +00:00
const expectedPath = __dirname + "/resources"+req.url.split("?")[0];
2024-02-22 23:17:32 +00:00
createDirFromFile(expectedPath);
2024-04-25 00:05:37 +00:00
2024-02-22 23:17:32 +00:00
let downloading = [];
if (fs.existsSync(expectedPath)) {
res.sendFile(expectedPath)
2024-02-22 23:17:32 +00:00
} else {
let url;
if (req.url.split("/")[2].length !== 2) {
url = (req.url.toLowerCase().startsWith("/android") || req.url.toLowerCase().startsWith("/ios") ? "https://lovelive-schoolidolfestival2-assets.akamaized.net" : "https://lovelive-schoolidolfestival2-album.akamaized.net") + req.url;
} else {
url = (req.url.toLowerCase().startsWith("/android") || req.url.toLowerCase().startsWith("/ios") ? "https://img-sif2.lovelive-sif2.com" : "https://album-sif2.lovelive-sif2.com") + req.url;
}
2024-02-22 23:17:32 +00:00
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)
})