Commit 7d487b3b authored by wudizhanche1000's avatar wudizhanche1000

解压

parent ad895998
......@@ -8,6 +8,7 @@
node-debug.log
!/node_modules/@types/
!/node_modules/@types/mongorito/
!/node_modules/@types/is-zip/
!systemjs.config.js
/mongodb_config.json
......
......@@ -16,6 +16,8 @@ export interface Action {
export class Package extends Model {
@field
appId: string;
@field
version: string;
@field
......@@ -24,4 +26,8 @@ export class Package extends Model {
platform: Platform[];
@field
actions: {[key: string]: Action};
static async findAllByApp(appId: string) {
return await Package.find({appId: appId});
}
}
/**
* Created by weijian on 2017/1/6.
*/
export function isZip()
interface isZip {
(f: Buffer|Uint8Array): boolean
}
declare module "is-zip" {
export = is_zip;
}
declare var is_zip: isZip;
......@@ -24,6 +24,7 @@
"core-js": "^2.4.1",
"font-awesome": "^4.7.0",
"hammerjs": "^2.0.8",
"is-zip": "^1.0.0",
"koa": "^2.0.0",
"koa-bodyparser": "^3.2.0",
"koa-log4": "^2.1.0",
......@@ -35,6 +36,7 @@
"reflect-metadata": "^0.1.9",
"rxjs": "5.0.1",
"systemjs": "0.19.40",
"tmp": "0.0.31",
"zone.js": "^0.7.4"
},
"devDependencies": {
......@@ -47,6 +49,7 @@
"@types/mongodb": "^2.1.36",
"@types/node": "^6.0.55",
"@types/pluralize": "0.0.27",
"@types/tmp": "0.0.32",
"concurrently": "^3.1.0",
"lite-server": "^2.2.2",
"tslint": "^3.15.1",
......
......@@ -5,10 +5,69 @@
import Router = require('koa-router');
import {NotFound} from '../koa/errors';
import {Package} from '../models/package';
import * as tmp from 'tmp';
import {ChildProcess} from 'child_process';
import fs = require('fs');
import http = require('http');
import isZip = require('is-zip');
import path = require('path');
import child_process = require('child_process');
const router = new Router();
router.get('/packages/all/:appId', async(ctx, next) => {
interface Option {
dir?: string;
}
class Archive {
constructor(public tarPath = 'tar', public unzipPath = 'unzip') {
}
async extract(options?: Option) {
}
private async isZip(file: string): Promise<boolean > {
return new Promise<boolean>((resolve, reject) => {
fs.readFile(file, (err, data) => {
if (err) {
reject(err);
} else {
resolve(isZip(data));
}
});
});
}
async decompress(file: string, option?: Option) {
let dir = '.';
if (option) {
if (option.dir) {
dir = option.dir;
}
}
await new Promise(async(resolve, reject) => {
let process: ChildProcess;
if (await this.isZip(file)) {
// TODO zip解压
process = child_process.spawn(this.unzipPath);
} else {
process = child_process.spawn(this.tarPath, ['xvf', file, '-c', dir]);
}
process.on('exit', (code, signal) => {
if (code !== 0) {
reject(`tar exited by accident, code ${code}`);
} else {
resolve();
}
});
});
}
}
router.get('/packages/all/:appId', async(ctx, next) => {
let appId = ctx.params.appId;
ctx.body = await Package.findAllByApp(appId);
});
router.get('/packages/:id', async(ctx, next) => {
let p: Package|null = await Package.findOne({id: ctx.params.id});
......@@ -18,7 +77,25 @@ router.get('/packages/:id', async(ctx, next) => {
ctx.body = p;
});
router.post('/packages/:id', async(ctx, next) => {
let p = new Package(ctx.request.body);
console.log(p, p.version);
new Promise<string|Buffer>((resolve, reject) => {
let downloadUrl = ctx.request.body.downloadUrl;
tmp.tmpName((e, file) => {
if (e) {
reject(e);
} else {
let writeStream = fs.createWriteStream(file);
http.get(downloadUrl, (response) => {
response.pipe(writeStream);
writeStream.on('finish', () => {
resolve(writeStream.path);
});
}).on('error', (err) => {
reject(err);
});
}
});
});
// TODO 打包
});
export default router;
......@@ -26,7 +26,7 @@ app.use(async(ctx, next) => {
await next();
} catch (err) {
// will only respond with JSON
console.log(err.status);
console.log(err);
ctx.status = err.status || 500;
ctx.body = {
message: err.message,
......
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