Commit ad75b565 authored by nano's avatar nano

error handing

parent c3f4ecc2
This diff is collapsed.
......@@ -43,18 +43,18 @@ export async function bundle(...args) {
files.set(file, {
path: file,
hash: file_hash,
size: (await fs.statAsync(file)).size.toString()
size: (await fs.statAsync(file)).size
})
let sand_file = path.join(sand_path, `${file_hash}.tar.gz`)
await archiveSingle(sand_file, [file], package_path)
archives.set(sand_file, {
path: sand_file,
hash: await caculateSHA256(sand_file),
size: (await fs.statAsync(sand_file)).size.toString()
size: (await fs.statAsync(sand_file)).size
})
await archiveSingle(sand_file, [file], package_path)
},
onDir: async (files, _path, depth) => {
},
......@@ -68,7 +68,7 @@ export async function bundle(...args) {
// TODO: 上传meta
const fullHash = await caculateSHA256(fullFile)
const fullSize = (await fs.statAsync(fullFile)).size.toString()
const fullSize = (await fs.statAsync(fullFile)).size
// TODO: 增量包
......
......@@ -23,7 +23,7 @@ router.post('/v2/package/:id/update', async (ctx: Context, next) => {
let sandSize = ctx.request.body.length * request_overhead
let pack = await mongodb.Packages.findOne({id: package_id, status: 'ready'})
let pack = await mongodb.Packages.findOne({id: package_id, status: 'uploaded'})
let {fullSize, fullPath} = pack!
......@@ -61,8 +61,6 @@ router.post('/v2/package/:id/update', async (ctx: Context, next) => {
})
router.get('/v1/packages', async (ctx: Context, next) => {
if (!ctx.request.query.appId) {
ctx.throw(400, "appId must be required!")
......
......@@ -20,6 +20,12 @@ import config from '../../config'
// path: path.join(__dirname, '../../test/upload')
// }
const checkExtension = async (file) => {
const ext = mime.extension(file.mime);
if (['zip', 'gz', 'rar', '7z'].indexOf(ext) === -1) {
throw new Error('Unsupported file type');
}
}
import Router = require('koa-router');
const ossStream = Client(new OSS({
......@@ -36,10 +42,7 @@ const UploadImage = async (ctx: Context) => {
const {files} = await busboy(ctx.req);
ctx.body = await Promise.all(files.map(async file => {
const ext = mime.extension(file.mime);
if (['png', 'jpg', 'jpeg', 'gif', 'webp'].indexOf(ext) === -1) {
throw new Error('Unsupported image type');
}
await checkExtension(file)
const filename = `test/${uuid.v1()}`;
......@@ -86,23 +89,33 @@ export const UploadPackage = async (ctx: Context) => {
file.on('close', async() => {
pack!.status = 'uploading'
await pack!.save()
resolve(pack!)
// 上传完, 打包
const bundled = await bundle(filename)
Object.assign(pack, bundled)
pack!.status = 'uploaded'
await pack!.save()
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()
}
})
file.on('error', async (error) => {
pack!.status = 'failed'
await pack!.save()
pack.status = 'failed'
await pack.save()
reject(error)
})
......@@ -136,13 +149,21 @@ const uploadPackageUrl = async (ctx: Context) => {
const { files } = await downloader.send('tellStatus', m.gid)
const [file] = files
// 打包
const bundled = await bundle(path.basename(file.path))
try {
await checkExtension(file)
// 打包完, 上传阿里云
pack.files = bundled.files
pack.status = 'uploaded'
await pack.save()
// 打包
const bundled = await bundle(path.basename(file.path))
// 打包完, 上传阿里云
pack.files = bundled.files
pack.status = 'uploaded'
await pack.save()
} catch (e) {
pack.status = 'failed'
await pack.save()
}
}
downloader.onDownloadError = async(err) => {
......
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