Commit 1cf95efe authored by nano's avatar nano

update

parent c94d3285
...@@ -3,29 +3,70 @@ import * as Mongorito from 'mongorito'; ...@@ -3,29 +3,70 @@ import * as Mongorito from 'mongorito';
import Model = Mongorito.Model; import Model = Mongorito.Model;
import {field} from '../db/decorators'; import {field} from '../db/decorators';
import {ModelExistsError} from './errors'; import {ModelExistsError} from './errors';
import {File} from './package'
/** /**
* Created by weijian on 2016/12/28. * Created by weijian on 2016/12/28.
*/ */
interface I18n<T> { interface I18n<T> {
[locale: string]: 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 { export class App extends Model {
@field @field
id: string; id: string;
@field @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 @field
parent?: string; parent?: string;
@field @field
locales: string[]; tag?: string[];
@field
dependencies?: Platform<string[]>;
@field @field
news: I18n<{title: string, url: string, image: string}[]>; references?: Platform<string[]>;
@field @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 @field
conference?: string; conference?: string;
@field @field
data: any; data?: any;
@field
icon?: string;
@field
cover?: string;
@field
background?: string;
@field
packages?: Package[];
async checkExists() { async checkExists() {
let app = await App.findOne({id: this.id}); let app = await App.findOne({id: this.id});
......
...@@ -14,18 +14,28 @@ export interface Action { ...@@ -14,18 +14,28 @@ export interface Action {
open?: string; open?: string;
} }
export interface File {
path: string,
size: number,
hash: string,
}
export class Package extends Model { export class Package extends Model {
@field
id: string;
@field @field
appId: string; appId: string;
@field @field
version: string; version: string;
@field @field
locale: Locale[]; locales: Locale[];
@field @field
platform: Platform[]; platforms: Platform[];
@field @field
actions: {[key: string]: Action}; files: File[];
static async findAllByApp(appId: string) { static async findAllByApp(appId: string) {
return await Package.find({appId: appId}); return await Package.find({appId: appId});
......
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
"migrate": "tsc && mongo-migrate --runMongoMigrate --config mongodb_config.json --dbPropName dbSettings" "migrate": "tsc && mongo-migrate --runMongoMigrate --config mongodb_config.json --dbPropName dbSettings"
}, },
"dependencies": { "dependencies": {
"@types/bluebird": "^3.5.0",
"@types/uuid": "^2.0.29",
"async-busboy": "^0.3.4",
"bluebird": "^3.5.0",
"clone": "^2.1.0", "clone": "^2.1.0",
"core-js": "^2.4.1", "core-js": "^2.4.1",
"hammerjs": "^2.0.8", "hammerjs": "^2.0.8",
...@@ -19,13 +23,18 @@ ...@@ -19,13 +23,18 @@
"koa-bodyparser": "^3.2.0", "koa-bodyparser": "^3.2.0",
"koa-log4": "^2.1.0", "koa-log4": "^2.1.0",
"koa-router": "^7.0.1", "koa-router": "^7.0.1",
"mime": "^1.3.4",
"mongodb": "^2.2.16", "mongodb": "^2.2.16",
"mongodb-migrate": "^2.0.1", "mongodb-migrate": "^2.0.1",
"mongorito": "^2.2.0", "mongorito": "^2.2.0",
"nodemon": "^1.11.0",
"random": "^1.0.0-beta-1",
"react": "^15.4.2", "react": "^15.4.2",
"react-intl": "^2.2.3", "react-intl": "^2.2.3",
"reflect-metadata": "^0.1.9", "reflect-metadata": "^0.1.9",
"tmp": "0.0.31", "tmp": "0.0.31",
"typed-promisify": "^0.4.0",
"uuid": "^3.0.1",
"zone.js": "^0.7.4" "zone.js": "^0.7.4"
}, },
"devDependencies": { "devDependencies": {
......
...@@ -21,7 +21,6 @@ router.get('/apps/:id', async(ctx, next) => { ...@@ -21,7 +21,6 @@ router.get('/apps/:id', async(ctx, next) => {
}); });
router.post('/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) { if (!ctx.request.body.id || ctx.params.id !== ctx.request.body.id) {
throw new ModelInvalidError('App id not same'); throw new ModelInvalidError('App id not same');
} }
...@@ -45,6 +44,7 @@ router.patch('/apps/:id', async(ctx, next) => { ...@@ -45,6 +44,7 @@ router.patch('/apps/:id', async(ctx, next) => {
if (!ctx.request.body.id || ctx.request.body.id !== app.id) { if (!ctx.request.body.id || ctx.request.body.id !== app.id) {
throw new ModelInvalidError('Can not change AppID'); throw new ModelInvalidError('Can not change AppID');
} }
Object.assign(app, ctx.request.body); Object.assign(app, ctx.request.body);
ctx.body = await app.save(); 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 Koa = require('koa');
import index from './routes/index'; import index from './routes/index';
import upload from './routes/upload';
import users from './routes/users'; import users from './routes/users';
import apps from './routes/apps'; import apps from './routes/apps';
import packages from './routes/packages'; import packages from './routes/packages';
...@@ -46,7 +47,7 @@ app.use(async(ctx, next) => { ...@@ -46,7 +47,7 @@ app.use(async(ctx, next) => {
app.use(async(ctx, next) => { app.use(async(ctx, next) => {
ctx.set('Access-Control-Allow-Origin', '*'); ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH'); 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') { if (ctx.method === 'OPTIONS') {
ctx.status = 204; ctx.status = 204;
} else { } else {
...@@ -59,6 +60,7 @@ app.use(bodyParser()); ...@@ -59,6 +60,7 @@ app.use(bodyParser());
app.use(index.routes()); app.use(index.routes());
app.use(users.routes()); app.use(users.routes());
app.use(apps.routes()); app.use(apps.routes());
app.use(upload.routes());
app.use(packages.routes()); app.use(packages.routes());
Mongorito.connect(url).then(() => { Mongorito.connect(url).then(() => {
app.listen(8001, () => { app.listen(8001, () => {
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
"es2017", "es2017",
"dom" "dom"
], ],
"noImplicitAny": true, "noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true, "suppressImplicitAnyIndexErrors": true,
"skipLibCheck": 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