Commit a747926e authored by 神楽坂玲奈's avatar 神楽坂玲奈

first commit

parents
/.idea/
/node_modules/
FROM node
RUN apt update
RUN apt install -y aria2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ARG NODE_ENV
ENV NODE_ENV $NODE_ENV
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
EXPOSE 80
CMD [ "npm", "start" ]
"use strict";
const fs = require('fs');
const path = require("path");
const crypto = require('crypto');
const download_path = '/downloads';
Array.prototype.max = function () {
return this.sort()[this.length - 1];
}
exports.refresh = async function (platform, version) {
let files = await new Promise((resolve, reject)=> {
fs.readdir(download_path, (error, files)=> {
if (error) {
reject(error);
} else {
resolve(files);
}
})
});
switch (platform) {
case 'darwin':
this.darwin = files.filter(file=>path.extname(file) == '.dmg').max();
this.darwin_update = files.filter(file=>path.extname(file) == '.zip').max();
if (!this.darwin_update) {
return;
}
this.darwin_version = this.darwin_update.match(/MyCard-(.+)-mac\.zip/)[1];
break;
case 'linux':
this.linux = files.filter(file=>path.extname(file) == '.AppImage').max();
break;
case 'win32':
this.win32 = files.filter(file=>path.extname(file) == '.exe').max();
if (!this.win32) {
return;
}
return new Promise((resolve, reject)=> {
let hash = crypto.createHash('sha256');
hash.setEncoding('hex');
fs.createReadStream(path.join(download_path, this.win32)).pipe(hash);
hash.on('finish', ()=> {
let obj = {
version: version,
path: this.win32,
sha2: hash.read()
};
let data = Object.entries(obj).map(([key, value])=>`${key}: ${value}`).join("\n");
fs.writeFile(path.join(download_path, 'latest.yml'), data, (error)=> {
if (error) {
reject(error)
} else {
resolve();
}
});
});
hash.on('error', reject);
});
break;
default:
return Promise.all([this.refresh('darwin'), this.refresh('linux'), this.refresh('win32')]);
}
};
{
"name": "mycard-releases",
"version": "0.0.1",
"scripts": {
"start": "aria2c --enable-rpc --rpc-allow-origin-all --continue --split=10 --min-split-size=1M --max-connection-per-server=10 --auto-file-renaming=false --daemon && node --harmony server.js"
},
"dependencies": {
"koa": "next",
"koa-router": "next",
"aria2": "latest"
}
}
"use strict";
const router = require('koa-router')();
const latest = require('../models/latest');
const path = require("path");
const download_url = 'https://wudizhanche.mycard.moe/downloads';
router.get('/download', async(ctx) => {
let file = latest[ctx.query.platform];
if (!file) {
throw 'no file';
}
ctx.redirect(path.join(download_url, file));
});
router.get('/update', async(ctx)=> {
if (ctx.query.version < latest.darwin_version) {
ctx.body = {url: path.join(download_url, latest.darwin_update)}
} else {
ctx.response.status = 204
}
});
module.exports = router;
\ No newline at end of file
"use strict";
const router = require('koa-router')();
const EventEmitter = require('events');
const Aria2 = require('aria2');
const latest = require('../models/latest');
const download_path = '/downloads';
const aria2 = new Aria2();
aria2.open();
const eventemitter = new EventEmitter();
aria2.onDownloadComplete = (event) => {
eventemitter.emit(event.gid);
};
aria2.onDownloadError = (event) => {
eventemitter.emit(event.gid, 'error');
};
router.post('/publish', async(ctx)=> {
let version = ctx.query.version;
let platform = ctx.query.platform;
let files = [];
switch (platform) {
case 'osx':
platform = 'darwin';
files.push([`mycard-${version}.dmg`, `MyCard-${version}.dmg`]);
files.push([`mycard-${version}-mac.zip`, `MyCard-${version}-mac.zip`]);
break;
case 'linux':
files.push([`mycard-${version}-x86_64.AppImage`, `mycard-${version}-x86_64.AppImage`]);
break;
case 'win32':
files.push([`mycard-Setup-${version}.exe`, `MyCard Setup ${version}.exe`]);
break;
default:
let error = new Error(`unknown platform: ${platform}`);
error.expose = true;
error.status = 400;
throw error;
}
await Promise.all(files.map(async([artifact, file])=> {
let gid = await aria2.addUri([`https://github.com/mycard/mycard/releases/download/${version}/${artifact}`], {
dir: download_path,
out: file
});
console.log('start download', file);
return new Promise((resolve, reject)=> {
eventemitter.once(gid, (error)=> {
if (error) {
let error = new Error(`can't download artifact: ${artifact}`);
error.expose = true;
error.status = 400;
reject(error);
} else {
resolve()
}
})
})
}));
console.log('download finish');
await latest.refresh(platform, version);
ctx.response.status = 204;
});
module.exports = router;
\ No newline at end of file
'use strict';
const Koa = require('koa');
const app = new Koa();
app.use(require('./routes/download').routes());
app.use(require('./routes/publish').routes());
const latest = require('./models/latest');
latest.refresh().then(()=> {
console.log(latest);
app.listen(80);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment