Skip to content

Commit

Permalink
Merge pull request #89 from textury/bg/chunks-tx
Browse files Browse the repository at this point in the history
  • Loading branch information
cedriking authored Mar 24, 2022
2 parents f2b8df9 + 32d116a commit 0b560e2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/routes/chunk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChunkDB } from '../db/chunks';
import { Chunk } from 'faces/chunk';
import Router from 'koa-router';
import { b64UrlToBuffer } from '../utils/encoding';

let chunkDB: ChunkDB;
let oldDbPath: string;
Expand All @@ -13,6 +14,12 @@ export async function postChunkRoute(ctx: Router.RouterContext) {
}

const chunk = ctx.request.body as unknown as Chunk;
const lastOffset = await chunkDB.getLastChunkOffset();
if (!lastOffset) chunk.offset = b64UrlToBuffer(chunk.chunk).length;
else {
const lastChunk = await chunkDB.getByOffset(lastOffset);
chunk.offset = lastOffset + b64UrlToBuffer(lastChunk.chunk).length;
}

await chunkDB.create(chunk);

Expand All @@ -29,11 +36,13 @@ export async function getChunkOffsetRoute(ctx: Router.RouterContext) {
}
const offset = +ctx.params.offset;

let chunk = await chunkDB.getByOffset(offset);
const chunk = await chunkDB.getByOffset(offset);
if (!chunk) {
chunk = await chunkDB.getLowerOffset(offset);
ctx.status = 204;
return;
}
ctx.body = chunk;
return;
} catch (error) {
console.error({ error });
}
Expand Down

0 comments on commit 0b560e2

Please sign in to comment.