Commit 24bbadbe authored by nano's avatar nano

fix checksum

parent ca8fddda
This diff is collapsed.
FROM node:alpine
MAINTAINER nanoo
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
......
......@@ -39,11 +39,12 @@ export async function bundle(...args) {
await crawlPath(package_path, {
relative: true,
onFile: async (file) => {
let file_hash = await caculateSHA256(file)
files.set(file, {
path: file,
path: path.relative(package_path,file),
hash: file_hash,
size: (await fs.statAsync(file)).size
})
......@@ -55,14 +56,17 @@ export async function bundle(...args) {
let sand_hash = await caculateSHA256(sand_file)
archives.set(sand_file, {
path: sand_file,
path: path.relative(sand_path, sand_file),
hash: sand_hash,
size: (await fs.statAsync(sand_file)).size
})
await fs.renameAsync(sand_file, path.join(path.dirname(sand_file), `${sand_hash}.tar.gz`))
},
onDir: async (files, _path, depth) => {
onDir: async (_files, _path, depth) => {
files.set(_path, {
path: path.relative(package_path, _path),
})
},
})
......@@ -80,7 +84,6 @@ export async function bundle(...args) {
return {
files: Array.from(files.values()),
archives: Array.from(archives.values()),
fullPath,
fullSize,
fullHash
}
......
......@@ -12,8 +12,8 @@ export interface Action {
export interface File {
path: string;
size: number;
hash: string;
size?: number;
hash?: string;
}
export interface Archive {
......@@ -28,7 +28,6 @@ export interface Package {
appId: string;
fullSize: number;
fullHash: string;
fullPath: string;
version: string;
status: string;
type: string;
......@@ -52,8 +51,6 @@ export class PackageSchema extends Instance<Package, PackageSchema> implements P
fullSize: number;
@Property(String, false)
fullHash: string;
@Property(String, false)
fullPath: string;
@Property(String, true)
type: string;
@Property(String, true)
......
......@@ -4,33 +4,39 @@ import {mongodb} from '../models/iridium'
import {Context} from "koa";
import config from '../../config'
import {Archive, Package} from "../models/Package";
import {renderChecksum} from "../utils";
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.params.id, status: 'uploaded'})
let packs = await mongodb.Packages.find({appId: ctx.request.query.appId, status: 'uploaded'}).toArray()
ctx.body = {
[ctx.request.query.appId]: packs
}
})
router.get('/v2/package/:id', async(ctx: Context, next) => {
//TODO
router.get('/v2/package/:id/checksum', async(ctx: Context, next) => {
let pack = await mongodb.Packages.findOne({id: ctx.params.id, status: 'uploaded'})
if(!pack) {
return ctx.throw(400, 'pack error')
}
ctx.body = renderChecksum(pack.files)
})
router.get('/v2/package/:id/meta', async(ctx: Context, next) => {
let {fullHash, fullSize, fullPath} = await mongodb.Packages.findOne({id: ctx.params.id, status: 'uploaded'}) || {}
if(!fullHash || !fullSize || !fullPath) {
ctx.throw(400, 'pack error')
let pack = await mongodb.Packages.findOne({id: ctx.params.id, status: 'uploaded'})
if(!pack) {
return ctx.throw(400, 'pack error')
}
await ctx['render']('update', {files: {
name: fullPath,
size: fullSize,
hash: fullHash
name: pack.id,
size: pack.fullSize,
hash: pack.fullHash
}})
})
......@@ -69,7 +75,7 @@ router.post('/v2/package/:id/update', async (ctx: Context, next) => {
if( sandSize <= fullSize ) {
files = [{
path: pack.fullPath,
path: pack.id,
size: pack.fullSize,
hash: pack.fullHash
}]
......
......@@ -21,8 +21,9 @@ import config from '../../config'
// }
const checkPackage = async (file) => {
const ext = mime.extension(file.mime);
if (['zip', 'gz', 'rar', '7z'].indexOf(ext) === -1) {
// const ext = mime.extension(file.mime);
if (['zip', 'gz', 'rar', '7z', 'application/x-gzip'].indexOf(file.mime) === -1) {
throw new Error('Unsupported file type');
}
}
......@@ -77,10 +78,7 @@ export const UploadPackage = 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 (['zip', 'gz', 'rar', '7z'].indexOf(ext) === -1) {
throw new Error('Unsupported file type');
}
await checkPackage(file)
const filename = uuid.v1()
......@@ -97,8 +95,6 @@ export const UploadPackage = async (ctx: Context) => {
file.on('close', async() => {
try {
pack.status = 'uploading'
await pack.save()
......
......@@ -12,4 +12,8 @@ export const handleImg = (img) => {
} else {
return 'https://cdn01.moecube.com/accounts/default_avatar.jpg';
}
}
export function renderChecksum(files: { path: string, hash?: string }[]) {
return files.map(({ path, hash }) => `${hash || ''} ${path}`).join('\n');
}
\ No newline at end of file
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