Commit 1cf95efe authored by nano's avatar nano

update

parent c94d3285
......@@ -3,29 +3,70 @@ import * as Mongorito from 'mongorito';
import Model = Mongorito.Model;
import {field} from '../db/decorators';
import {ModelExistsError} from './errors';
import {File} from './package'
/**
* Created by weijian on 2016/12/28.
*/
interface I18n<T> {
[locale: string]: T;
}
interface Platform<T> {
[platform: string]: T;
}
interface Package{
id: string;
platforms: Platform<string[]>;
locales: I18n<string[]>;
files: File[];
}
export class App extends Model {
@field
id: string;
@field
name: I18n<string>;
name?: I18n<string>;
@field
description?: I18n<string>
@field
developers?: I18n<[{ name: string, url: string }]>
@field
publishers?: I18n<[{ name: string, url: string }]>
@field
released_at?: string;
@field
category?: string;
@field
parent?: string;
@field
locales: string[];
tag?: string[];
@field
dependencies?: Platform<string[]>;
@field
news: I18n<{title: string, url: string, image: string}[]>;
references?: Platform<string[]>;
@field
changelog: I18n<string>;
homepage?: string;
@field
locales?: string[];
@field
actions?: Platform<{[key: string]: {execuate: string, args: string[], env: {[key: string]: string}}}>;
@field
files?: {[key: string]: { sync: boolean}}
@field
version?: Platform<string>;
@field
news?: I18n<{title: string, url: string, image: string}[]>;
@field
conference?: string;
@field
data: any;
data?: any;
@field
icon?: string;
@field
cover?: string;
@field
background?: string;
@field
packages?: Package[];
async checkExists() {
let app = await App.findOne({id: this.id});
......
......@@ -14,18 +14,28 @@ export interface Action {
open?: string;
}
export interface File {
path: string,
size: number,
hash: string,
}
export class Package extends Model {
@field
id: string;
@field
appId: string;
@field
version: string;
@field
locale: Locale[];
locales: Locale[];
@field
platform: Platform[];
platforms: Platform[];
@field
actions: {[key: string]: Action};
files: File[];
static async findAllByApp(appId: string) {
return await Package.find({appId: appId});
......
......@@ -11,6 +11,10 @@
"migrate": "tsc && mongo-migrate --runMongoMigrate --config mongodb_config.json --dbPropName dbSettings"
},
"dependencies": {
"@types/bluebird": "^3.5.0",
"@types/uuid": "^2.0.29",
"async-busboy": "^0.3.4",
"bluebird": "^3.5.0",
"clone": "^2.1.0",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
......@@ -19,13 +23,18 @@
"koa-bodyparser": "^3.2.0",
"koa-log4": "^2.1.0",
"koa-router": "^7.0.1",
"mime": "^1.3.4",
"mongodb": "^2.2.16",
"mongodb-migrate": "^2.0.1",
"mongorito": "^2.2.0",
"nodemon": "^1.11.0",
"random": "^1.0.0-beta-1",
"react": "^15.4.2",
"react-intl": "^2.2.3",
"reflect-metadata": "^0.1.9",
"tmp": "0.0.31",
"typed-promisify": "^0.4.0",
"uuid": "^3.0.1",
"zone.js": "^0.7.4"
},
"devDependencies": {
......
......@@ -21,7 +21,6 @@ router.get('/apps/:id', async(ctx, next) => {
});
router.post('/apps/:id', async(ctx, next) => {
console.log(ctx.request.body)
if (!ctx.request.body.id || ctx.params.id !== ctx.request.body.id) {
throw new ModelInvalidError('App id not same');
}
......@@ -45,6 +44,7 @@ router.patch('/apps/:id', async(ctx, next) => {
if (!ctx.request.body.id || ctx.request.body.id !== app.id) {
throw new ModelInvalidError('Can not change AppID');
}
Object.assign(app, ctx.request.body);
ctx.body = await app.save();
});
......
import * as Router from 'koa-router'
import * as path from 'path'
import * as uuid from 'uuid'
import * as _fs from 'fs'
import { promisifyAll} from 'bluebird'
const fs:any = promisifyAll(_fs)
const busboy = require('async-busboy')
const mime = require('mime')
const router = new Router();
function uploadImageStream(file){
return new Promise(async (resolve, reject) => {
const ext = mime.extension(file.mime)
if(['png','jpg','jpeg','gif','webp'].indexOf(ext) === -1) {
return reject(new Error("Unsupported image type"))
}
const hash = uuid.v1()
const uploadDir = 'upload'
const fileName = `${hash}.${ext}`
try {
let access = await fs.accessAsync(uploadDir)
} catch (error) {
await fs.mkdirAsync(uploadDir)
}
let writeStream = fs.createWriteStream(path.join(uploadDir, fileName))
file.pipe(writeStream)
resolve({fileName})
})
}
router.post('/upload/image', async(ctx, next) => {
try {
const {files} = await busboy(ctx.req)
const res = await Promise.all(files.map(file => {
return uploadImageStream(file)
}))
ctx.body = res
} catch (err) {
ctx.throw(403, err)
}
})
export default router;
import Koa = require('koa');
import index from './routes/index';
import upload from './routes/upload';
import users from './routes/users';
import apps from './routes/apps';
import packages from './routes/packages';
......@@ -46,7 +47,7 @@ app.use(async(ctx, next) => {
app.use(async(ctx, next) => {
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH');
ctx.set('Access-Control-Allow-Headers', 'Content-Type');
ctx.set('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With',);
if (ctx.method === 'OPTIONS') {
ctx.status = 204;
} else {
......@@ -59,6 +60,7 @@ app.use(bodyParser());
app.use(index.routes());
app.use(users.routes());
app.use(apps.routes());
app.use(upload.routes());
app.use(packages.routes());
Mongorito.connect(url).then(() => {
app.listen(8001, () => {
......
......@@ -11,7 +11,7 @@
"es2017",
"dom"
],
"noImplicitAny": true,
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"skipLibCheck": true
}
......
This diff is collapsed.
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