Commit bfde585f authored by nano's avatar nano

docker

parent 761af839
This diff is collapsed.
FROM node:alpine FROM node
RUN apt-get update
RUN apt-get install aria2 -y
RUN npm install yarn -g
RUN mkdir -p /usr/src/app RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app WORKDIR /usr/src/app
COPY package.json /usr/src/app COPY package.json /usr/src/app
RUN npm install RUN yarn install
COPY . /usr/src/app COPY . /usr/src/app
EXPOSE 8080 EXPOSE 8080
CMD ["npm","start"] CMD ["./entrypoint.sh"]
#允许rpc
enable-rpc=true
#允许非外部访问
rpc-listen-all=true
#RPC端口, 仅当默认端口被占用时修改
rpc-listen-port=6800
#最大同时下载数(任务数), 路由建议值: 3
max-concurrent-downloads=10
#断点续传
continue=true
#同服务器连接数
max-connection-per-server=10
#最小文件分片大小, 下载线程数上限取决于能分出多少片, 对于小文件重要
min-split-size=10M
#单文件最大线程数, 路由建议值: 5
split=10
#下载速度限制
max-overall-download-limit=0
#单文件速度限制
max-download-limit=0
#上传速度限制
max-overall-upload-limit=0
#单文件速度限制
max-upload-limit=0
#文件保存路径, 默认为当前启动位置
dir=/Users/vai/Downloads
#!/usr/bin/sh
aria2c --conf-path=/usr/src/app/aria2.conf -D
npm start
...@@ -13,21 +13,16 @@ ...@@ -13,21 +13,16 @@
"aria2": "^3.0.0", "aria2": "^3.0.0",
"async-busboy": "^0.4.0", "async-busboy": "^0.4.0",
"bluebird": "^3.5.0", "bluebird": "^3.5.0",
"dot-env": "^0.0.1",
"dotenv": "^4.0.0", "dotenv": "^4.0.0",
"fs-extra-promise": "^1.0.1", "fs-extra-promise": "^1.0.1",
"inversify": "^3.3.0", "inversify": "^3.3.0",
"iridium": "^7.1.6", "iridium": "^7.1.6",
"is-zip": "^1.0.0",
"koa": "^2.0.0", "koa": "^2.0.0",
"koa-bodyparser": "^4.2.0", "koa-bodyparser": "^4.2.0",
"koa-hbs": "next", "koa-hbs": "next",
"koa-log4": "^2.1.0", "koa-log4": "^2.1.0",
"koa-router": "^7.0.1", "koa-router": "^7.0.1",
"mime": "^1.3.4", "mime": "^1.3.4",
"mongoose": "^4.9.4",
"mongoose-timestamp": "^0.6.0",
"mongoose-unique-validator": "^1.0.4",
"tmp": "0.0.31", "tmp": "0.0.31",
"uuid": "^3.0.1", "uuid": "^3.0.1",
"vercomp": "^1.0.2" "vercomp": "^1.0.2"
...@@ -40,14 +35,10 @@ ...@@ -40,14 +35,10 @@
"@types/koa-bodyparser": "^3.0.22", "@types/koa-bodyparser": "^3.0.22",
"@types/koa-router": "^7.0.22", "@types/koa-router": "^7.0.22",
"@types/log4js": "0.0.32", "@types/log4js": "0.0.32",
"@types/mongodb": "^2.1.36",
"@types/mongoose": "^4.7.8",
"@types/node": "^7.0.13", "@types/node": "^7.0.13",
"@types/pluralize": "0.0.27", "@types/pluralize": "0.0.27",
"@types/tmp": "0.0.32", "@types/tmp": "0.0.32",
"@types/uuid": "^2.0.29", "@types/uuid": "^2.0.29",
"concurrently": "^3.1.0",
"lite-server": "^2.2.2",
"nodemon": "^1.11.0", "nodemon": "^1.11.0",
"tslint": "^5.1.0", "tslint": "^5.1.0",
"typescript": "^2.1.4" "typescript": "^2.1.4"
......
import {Collection, Index, Instance, Property} from 'Iridium'; import {Collection, Index, Instance, Property} from 'iridium';
import {handleImg} from '../utils'; import {handleImg} from '../utils';
interface I18n<T> { interface I18n<T> {
...@@ -113,7 +113,7 @@ export class AppSchema extends Instance<App, AppSchema> implements App { ...@@ -113,7 +113,7 @@ export class AppSchema extends Instance<App, AppSchema> implements App {
toJSON() { toJSON() {
this.Convert(); this.Convert();
return JSON.parse(this); return this;
} }
Convert() { Convert() {
......
...@@ -44,18 +44,18 @@ router.post('/v1/app/:id', async (ctx: Context, next) => { ...@@ -44,18 +44,18 @@ router.post('/v1/app/:id', async (ctx: Context, next) => {
}); });
router.patch('/v1/app/:id', async (ctx: Context, next) => { router.patch('/v1/app/:id', async (ctx: Context, next) => {
let _app: App = ctx.request.body; let _app = ctx.request.body;
let app: AppSchema | null = await mongodb.Apps.findOne({id: ctx.params.id}); let app = await mongodb.Apps.findOne({id: ctx.params.id});
if (!app) { if (!app) {
ctx.throw(400, `App ${ctx.params.id} Not Found `); return ctx.throw(400, `App ${ctx.params.id} Not Found `);
} }
if (!ctx.request.body.id || ctx.request.body.id !== app!.id) { if (ctx.request.body.id || ctx.request.body.id !== app!.id) {
ctx.throw(400, `Can not change AppID`); ctx.throw(400, `Can not change AppID`);
} }
app!.handleUpdate(_app); app.handleUpdate(_app);
ctx.body = await app!.save(); ctx.body = await app.save();
}); });
export default router; export default router;
import {Context} from 'koa';
import {OSS} from 'aliyun-sdk';
import * as busboy from 'async-busboy';
import * as mime from 'mime';
import * as uuid from 'uuid';
import * as Client from 'aliyun-oss-upload-stream';
import * as fs from 'fs-extra-promise';
import * as path from 'path';
import * as Aria2 from 'aria2';
import {bundle} from '../../package/main';
import {mongodb} from '../models/Iridium';
import {toObjectID} from 'iridium';
import config from '../../config';
// const Aria2Options = {
// host: 'localhost',
// port: 6800,
// secure: false,
// secret: '',
// path: path.join(__dirname, '../../test/upload')
// }
const checkPackage = async (file) => {
if (['application/zip', 'application/gz', 'application/rar', 'application/7z', 'application/x-gzip'].indexOf(file.mime) === -1) {
console.log(file.mime)
throw new Error(`Unsupported file type: ${file.mime}`);
}
};
const checkImage = async (file) => {
const ext = mime.extension(file.mime);
if (['png', 'jpg', 'jpeg', 'gif', 'webp'].indexOf(ext) === -1) {
throw new Error('Unsupported file type');
}
};
import Router = require('koa-router');
const ossStream = Client(new OSS({
accessKeyId: process.env['OSS_ACCESS_ID'],
secretAccessKey: process.env['OSS_ACCESS_KEY'],
endpoint: process.env['OSS_ENDPOINT'],
apiVersion: '2013-10-15'
}));
const router = new Router();
const UploadImage = async (ctx: Context) => {
try {
const {files} = await busboy(ctx.req);
ctx.body = await Promise.all(files.map(async file => {
await checkImage(file);
const filename = `test/${uuid.v1()}`;
const upload = ossStream.upload({
Bucket: process.env['OSS_BUCKET'],
Key: filename,
ContentType: file.mimeType
});
file.pipe(upload);
return await new Promise((resolve, reject) => {
upload.on('error', reject);
upload.on('uploaded', resolve);
});
}));
} catch (err) {
ctx.throw(403, err);
}
};
export const UploadPackage = async (ctx: Context) => {
try {
const {files} = await busboy(ctx.req);
ctx.body = await Promise.all(files.map(async file => {
await checkPackage(file);
const filename = uuid.v1();
const archive_path = path.join(__dirname, '../../test/upload');
await fs.ensureDirAsync(archive_path);
const archive = fs.createWriteStream(path.join(archive_path, filename));
let pack = await mongodb.Packages.findOne({_id: toObjectID(ctx.params.id)});
if (!pack) {
return ctx.throw(400, 'pack not exists');
}
return await new Promise((resolve, reject) => {
file.pipe(archive);
file.on('close', async () => {
try {
pack!.status = 'uploading';
await pack!.save();
resolve(pack!);
// 上传完, 打包
const bundled = await bundle(filename);
Object.assign(pack, bundled);
pack!.status = 'uploaded';
await mongodb.Packages.update({id: pack!.id}, {$set: {status: 'deprecated'}}, {multi: true});
await pack!.save();
// 打包完,上传阿里云
} catch (e) {
pack!.status = 'failed';
await pack!.save();
console.log(e);
}
});
file.on('error', async (error) => {
pack!.status = 'failed';
await pack!.save();
reject(error);
});
});
}));
} catch (err) {
ctx.throw(403, err);
}
};
const uploadPackageUrl = async (ctx: Context) => {
if (!ctx.request.body.url) {
ctx.throw(400, 'params error');
}
// testUrl: https://r.my-card.in/release/dist/0c16a3ecb115fd7cf575ccdd64f62a8f3edc635b087950e4ed4f3f781972bbfd.tar.gz
const downloader = new Aria2;
let pack = await mongodb.Packages.findOne({_id: toObjectID(ctx.request.body._id)});
if (!pack) {
return ctx.throw(400, 'pack not exists');
}
await downloader.open();
downloader.onDownloadStart = async () => {
pack!.status = 'uploading';
await pack!.save();
};
downloader.onDownloadComplete = async (m) => {
const {files} = await downloader.send('tellStatus', m.gid);
const [file] = files;
try {
await checkPackage(file);
// 打包
const bundled = await bundle(path.basename(file.path));
// 打包完, 上传阿里云
Object.assign(pack, bundled);
pack!.status = 'uploaded';
await mongodb.Packages.update({id: pack!.id}, {$set: {status: 'deprecated'}}, {multi: true});
await pack!.save();
} catch (e) {
pack!.status = 'failed';
await pack!.save();
}
};
downloader.onDownloadError = async (err) => {
// console.log(await downloader.send('tellStatus', err.gid))
pack!.status = 'failed';
await pack!.save();
console.log(err);
};
ctx.body = await new Promise((resolve, reject) => {
downloader.onmessage = m => {
if (m['error']) {
reject(m['error']);
} else {
resolve(m);
}
};
downloader.send('addUri', [ctx.request.body.url], {dir: config.upload_path});
});
};
router.post('/v1/upload/image', UploadImage);
router.post('/v1/upload/package/:id', UploadPackage);
router.post('/v1/upload/packageUrl', uploadPackageUrl);
export default router;
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