Commit c3f4ecc2 authored by nano's avatar nano

update package

parent c1ba2a38
This diff is collapsed.
import * as path from 'path'
export default {
upload_path: path.join(__dirname, "./test/upload")
upload_path: path.join(__dirname, "./test/upload"),
download_path: path.join(__dirname, "./test/release/downloads")
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ import * as path from 'path'
import * as fs from 'fs-extra-promise'
import {mongodb} from '../src/models/iridium'
import {crawlPath, caculateSHA256, archive, archiveSingle, untar} from "./utils";
import {Archive, File} from "../src/models/Package";
const upload_path = path.join(__dirname, '../test/upload')
const release_path = path.join(__dirname, '../test/release')
......@@ -31,45 +32,51 @@ export async function bundle(...args) {
// untar upload package
await untar(uploadFile_path, package_path)
let hashes = new Map<string, object>();
// let hashes = {}
let files = new Map<string, File>();
let archives = new Map<string, Archive>();
// let files = {}
await crawlPath(package_path, {
onFile: async (file) => {
let hash = await caculateSHA256(file)
let file_hash = await caculateSHA256(file)
hashes.set(file, {
file: file,
hash: hash,
files.set(file, {
path: file,
hash: file_hash,
size: (await fs.statAsync(file)).size.toString()
})
// hashes[file] = {
// file: file,
// hash: hash,
// size: (await fs.statAsync(file)).size.toString()
// }
let sand_file = path.join(sand_path, `${hash}.tar.gz`)
let sand_file = path.join(sand_path, `${file_hash}.tar.gz`)
archives.set(sand_file, {
path: sand_file,
hash: await caculateSHA256(sand_file),
size: (await fs.statAsync(sand_file)).size.toString()
})
await archiveSingle(sand_file, [file], package_path)
},
onDir: async (files, _path, depth) => {
},
})
// TODO: 上传checksum: hashes
// TODO: 上传checksum: files
const full_file = path.join(full_path, `${package_id}.tar.gz`)
const fullFile = path.join(full_path, `${package_id}.tar.gz`)
await fs.removeAsync(full_file)
await archive(full_file, await fs.readdirAsync(package_path), package_path)
await fs.removeAsync(fullFile)
await archive(fullFile, await fs.readdirAsync(package_path), package_path)
// TODO: 上传meta
const full_hash = await caculateSHA256(full_file)
const full_size = (await fs.statAsync(full_file)).size.toString()
const fullHash = await caculateSHA256(fullFile)
const fullSize = (await fs.statAsync(fullFile)).size.toString()
// TODO: 增量包
return {
files: Array.from(hashes.values())
files: Array.from(files.values()),
archives: Array.from(archives.values()),
fullFile,
fullSize,
fullHash
}
}
......@@ -9,6 +9,7 @@ import { mongodb } from './src/models/iridium'
// import index from './routes/index';
import upload from './src/routes/upload';
// import users from './src/routes/users';
import package from './src/routes/package'
import apps from './src/routes/app';
// import packages from './routes/packages';
......@@ -64,6 +65,7 @@ app.use(bodyParser());
// app.use(users.routes());
app.use(apps.routes());
app.use(upload.routes());
app.use(package.routes());
// app.use(packages.routes());
mongodb.connect().then(() => {
......
......@@ -16,16 +16,26 @@ export interface File {
hash: string;
}
export interface Archive {
path: string;
size: number;
hash: string;
}
export interface Package {
id: string;
name: string;
appId: string;
fullSize: number;
fullHash: string;
fullPath: string;
version: string;
status: string;
type: string;
locales: Locale[];
platforms: Platform[];
files?: File[];
archives?: Archive[];
}
......@@ -38,6 +48,12 @@ export class PackageSchema extends Instance<Package, PackageSchema> implements P
name: string;
@Property(String, false)
appId: string;
@Property(Number, false)
fullSize: number;
@Property(String, false)
fullHash: string;
@Property(String, false)
fullPath: string;
@Property(String, true)
type: string;
@Property(String, true)
......@@ -50,6 +66,8 @@ export class PackageSchema extends Instance<Package, PackageSchema> implements P
platforms: Platform[];
@Property(Array, false)
files: File[];
@Property(Array, false)
archives: Archive[];
static onCreating(pack: Package){
pack.status = pack.status || 'init'
......
......@@ -23,11 +23,11 @@ const router = new Router();
// })
// });
router.get('/apps', async (ctx: Context, next) => {
router.get('/v1/apps', async (ctx: Context, next) => {
ctx.body = await mongodb.Apps.find({}).toArray()
})
router.post('/apps/:id', async (ctx: Context, next) => {
router.post('/v1/app/:id', async (ctx: Context, next) => {
if (!ctx.request.body.id || ctx.params.id !== ctx.request.body.id) {
ctx.throw(400, "App is not same")
}
......@@ -43,7 +43,7 @@ router.post('/apps/:id', async (ctx: Context, next) => {
}
})
router.patch('/apps/:id', async (ctx: Context, next) => {
router.patch('/v1/app/:id', async (ctx: Context, next) => {
let _app: App = ctx.request.body
let app: AppSchema | null = await mongodb.Apps.findOne({id: ctx.params.id});
if (!app) {
......@@ -53,141 +53,9 @@ router.patch('/apps/:id', async (ctx: Context, next) => {
ctx.throw(400, `Can not change AppID`)
}
// if(Array.isArray(data.packages)) {
// data.packages = await Promise.all(data.packages.map(async _p => {
// if(_p._id) {
// const p = await mongodb.Packages.findOne({_id: toObjectID(_p._id)})
//
// if(!_p.platforms) {
// ctx.throw(400, `请填写支持的平台:${_p.id}`)
// }
// else if(!_p.locales) {
// ctx.throw(400, `请填写支持的语言:${_p.id}`)
// }
// else if(!_p.version) {
// ctx.throw(400, `请填写版本号:${_p.id}`)
// }
// else if(vercomp(_p.version, p.version) == -1){
// ctx.throw(400, `版本号有误 :${_p.id}`)
// }
//
// if(p.status == 'init') {
// p.handleUpdate(_p)
// await p.save()
// return p._id
// }
// else {
// return p._id
// }
//
// } else {
// let pack: Package = {
// id: _p.id,
// name: _p.name,
// version: _p.version,
// appId: ctx.params.id,
// locales: _p.locales,
// platforms: _p.platforms,
// status: 'init'
// }
// const newP = await mongodb.Packages.insert(pack)
// return newP._id
// }
// }))
// }
app!.handleUpdate(_app)
ctx.body = await app!.save()
})
router.get('/packages/ready', async (ctx: Context, next) => {
if (!ctx.request.query.appId) {
ctx.throw(400, "appId must be required!")
}
let packs = await mongodb.Packages.find({appId: ctx.request.query.appId, status: 'uploaded'})
ctx.body = {
[ctx.request.query.appId]: packs
}
})
router.get('/packages/manage', async (ctx: Context, next) => {
if (!ctx.request.query.appId) {
ctx.throw(400, "appId must be required!")
}
let packs = await mongodb.Packages.find({appId: ctx.request.query.appId, type: 'editing'}).toArray()
ctx.body = {
[ctx.request.query.appId]: packs
}
})
router.post('/packages', async (ctx: Context, next) => {
const _p: Package = ctx.request.body
if (!_p.id) {
ctx.throw(400, `id 参数缺失:${_p.id}`)
}
if (!_p.platforms || _p.platforms.length == 0) {
ctx.throw(400, `请填写支持的平台:${_p.id}`)
}
else if (!_p.locales || _p.locales.length == 0) {
ctx.throw(400, `请填写支持的语言:${_p.id}`)
}
else if (!_p.version) {
ctx.throw(400, `请填写版本号:${_p.id}`)
}
await mongodb.Packages.update({id: _p.id}, {$set: { type: 'edited' }}, {multi: true})
let _pack: Package = {
id: _p.id,
name: _p.name,
version: _p.version,
appId: _p.appId,
locales: _p.locales,
platforms: _p.platforms,
status: 'init',
type: 'editing'
}
ctx.body = await mongodb.Packages.insert(_pack)
})
router.patch('/packages', async (ctx: Context, next) => {
const _p: Package = ctx.request.body
const p = await mongodb.Packages.findOne({_id: toObjectID(_p._id)})
if (!_p.id) {
ctx.throw(400, `id 参数缺失:${_p.id}`)
}
if (!_p.platforms || _p.platforms.length == 0) {
ctx.throw(400, `请填写支持的平台:${_p.id}`)
}
else if (!_p.locales || _p.locales.length == 0) {
ctx.throw(400, `请填写支持的语言:${_p.id}`)
}
else if (!_p.version) {
ctx.throw(400, `请填写版本号:${_p.id}`)
}
if (p.status == 'init') {
p.handleUpdate(_p)
ctx.body = await p.save()
} else {
ctx.throw(400, `非法操作:${_p.id}`)
}
})
router.delete('/packages', async(ctx: Context, next) => {
const _p: Package = ctx.request.body
const p = await mongodb.Packages.findOne({_id: toObjectID(_p._id)})
p.type = 'edited'
p.status = 'delete'
await p.save()
ctx.body = {
message: 'delete successful'
}
})
export default router
\ No newline at end of file
import Router = require('koa-router');
import {toObjectID} from 'iridium'
import {mongodb} from '../models/iridium'
import {Context} from "koa";
import config from '../../config'
import {Archive, Package} from "../models/Package";
const router = new Router();
router.get('/v2/packages', async (ctx: Context, next) => {
if (!ctx.request.query.appId) {
ctx.throw(400, "appId must be required!")
}
let packs = await mongodb.Packages.find({appId: ctx.request.query.appId, status: 'uploaded'})
ctx.body = {
[ctx.request.query.appId]: packs
}
})
router.post('/v2/package/:id/update', async (ctx: Context, next) => {
const package_id = ctx.params.id
const download_path = config.download_path
const request_overhead = 1024 * 1024
let sandSize = ctx.request.body.length * request_overhead
let pack = await mongodb.Packages.findOne({id: package_id, status: 'ready'})
let {fullSize, fullPath} = pack!
let files
let fullFiles = new Map<string, Archive>()
pack!.archives.map((f) => {
fullFiles.set(f.path, f)
})
if(fullSize > sandSize) {
files = ctx.request.body.map((_file) => {
const file: Archive|undefined = fullFiles.get(_file)
if(!file) {
//
}
sandSize += file.size
return {
path: file.path,
size: file.size,
hash: file.hash
}
})
}
if( sandSize <= fullSize ) {
files = [{
path: pack.fullPath,
size: pack.fullSize,
hash: pack.fullHash
}]
}
ctx.body = files
})
router.get('/v1/packages', async (ctx: Context, next) => {
if (!ctx.request.query.appId) {
ctx.throw(400, "appId must be required!")
}
let packs = await mongodb.Packages.find({appId: ctx.request.query.appId, type: 'editing'}).toArray()
ctx.body = {
[ctx.request.query.appId]: packs
}
})
router.post('/v1/package', async (ctx: Context, next) => {
const _p: Package = ctx.request.body
if (!_p.id) {
ctx.throw(400, `id 参数缺失:${_p.id}`)
}
if (!_p.platforms || _p.platforms.length == 0) {
ctx.throw(400, `请填写支持的平台:${_p.id}`)
}
else if (!_p.locales || _p.locales.length == 0) {
ctx.throw(400, `请填写支持的语言:${_p.id}`)
}
else if (!_p.version) {
ctx.throw(400, `请填写版本号:${_p.id}`)
}
await mongodb.Packages.update({id: _p.id}, {$set: { type: 'edited' }}, {multi: true})
let _pack: Package = {
id: _p.id,
name: _p.name,
version: _p.version,
appId: _p.appId,
locales: _p.locales,
platforms: _p.platforms,
status: 'init',
type: 'editing'
}
ctx.body = await mongodb.Packages.insert(_pack)
})
router.patch('/v1/package', async (ctx: Context, next) => {
const _p: Package = ctx.request.body
const p = await mongodb.Packages.findOne({_id: toObjectID(_p._id)})
if (!_p.id) {
ctx.throw(400, `id 参数缺失:${_p.id}`)
}
if (!_p.platforms || _p.platforms.length == 0) {
ctx.throw(400, `请填写支持的平台:${_p.id}`)
}
else if (!_p.locales || _p.locales.length == 0) {
ctx.throw(400, `请填写支持的语言:${_p.id}`)
}
else if (!_p.version) {
ctx.throw(400, `请填写版本号:${_p.id}`)
}
if (p.status == 'init') {
p.handleUpdate(_p)
ctx.body = await p.save()
} else {
ctx.throw(400, `非法操作:${_p.id}`)
}
})
router.delete('/v1/package', async(ctx: Context, next) => {
const _p: Package = ctx.request.body
const p = await mongodb.Packages.findOne({_id: toObjectID(_p._id)})
p.type = 'edited'
p.status = 'delete'
await p.save()
ctx.body = {
message: 'delete successful'
}
})
export default router
\ No newline at end of file
......@@ -86,23 +86,23 @@ export const UploadPackage = async (ctx: Context) => {
file.on('close', async() => {
pack.status = 'uploading'
await pack.save()
pack!.status = 'uploading'
await pack!.save()
resolve(pack)
resolve(pack!)
// 上传完, 打包
const bundled = await bundle(filename)
pack.files = bundled.files
pack.status = 'uploaded'
await pack.save()
Object.assign(pack, bundled)
pack!.status = 'uploaded'
await pack!.save()
// 打包完,上传阿里云
})
file.on('error', async (error) => {
pack.status = 'failed'
await pack.save()
pack!.status = 'failed'
await pack!.save()
reject(error)
})
......@@ -167,11 +167,11 @@ const uploadPackageUrl = async (ctx: Context) => {
}
router.post('/upload/image', UploadImage)
router.post('/v1/upload/image', UploadImage)
router.post('/upload/package/:id', UploadPackage)
router.post('/v1/upload/package/:id', UploadPackage)
router.post('/upload/packageUrl', uploadPackageUrl)
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