Commit 24bbadbe authored by nano's avatar nano

fix checksum

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