Skip to content

Commit

Permalink
No need for pending property as we can compute it from the listing.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Feb 9, 2024
1 parent 3d9d0e6 commit 051d080
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ The total quota for each project is simply calculated by `(CURRENT_YEAR - year)

Each project's current usage is tracked in `{project}/..usage`, which contains a JSON object with the following properties:
- `total`: the total number of bytes allocated to user-supplied files (i.e., not including `..`-prefixed internal files).
- `~pending_on_complete_only` (optional): internal use only, this should be ignored by readers.

## Interacting with the API

Expand Down
15 changes: 5 additions & 10 deletions src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ function checkBadName(name, type) {
}
}

const pending_name = "~pending_on_complete_only";

export async function initializeUploadHandler(request, env, nonblockers) {
let project = decodeURIComponent(request.params.project);
let asset = decodeURIComponent(request.params.asset);
Expand Down Expand Up @@ -275,9 +273,7 @@ export async function initializeUploadHandler(request, env, nonblockers) {
}
let link_details = await checkLinks(split.link, project, asset, version, env, manifest_cache);

// Checking that the quota isn't exceeded. Note that 'pending_name'
// should only EVER be used by completeUploadHandler, so even if it's
// non-zero here, we just ignore it.
// Checking that the quota isn't exceeded.
let current_usage = 0;
for (const s of split.simple) {
current_usage += s.size;
Expand All @@ -289,9 +285,6 @@ export async function initializeUploadHandler(request, env, nonblockers) {
throw new http.HttpError("upload exceeds the storage quota for this project", 400);
}

usage[pending_name] = current_usage;
bucket_writes.push(s3.quickUploadJson(upath, usage, env));

// Build a manifest for inspection.
let manifest = {};
for (const s of split.simple) {
Expand Down Expand Up @@ -400,7 +393,9 @@ export async function completeUploadHandler(request, env, nonblockers) {
// We scan the manifest to check that all files were uploaded. We also
// collect links for some more work later.
let manifest = await assets.manifest;
let added_usage = 0;
let linkable = {};

for (const [k, v] of Object.entries(manifest)) {
let s = assets.listing.get(k);
let found = (typeof s != "undefined");
Expand All @@ -427,6 +422,7 @@ export async function completeUploadHandler(request, env, nonblockers) {
} else if (s != v.size) {
throw new http.HttpError("actual size of '" + k + "' does not match its reported size in the manifest", 400);
}
added_usage += s;
}
}

Expand All @@ -449,8 +445,7 @@ export async function completeUploadHandler(request, env, nonblockers) {
// Updating the usage file.
let upath = pkeys.usage(project);
let usage = await s3.quickFetchJson(upath, env);
usage.total += usage[pending_name];
delete usage[pending_name]
usage.total += added_usage;
bucket_writes.push(s3.quickUploadJson(upath, usage, env));

if (is_official) {
Expand Down
11 changes: 5 additions & 6 deletions tests/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,6 @@ test("initializeUploadHandler works correctly for simple uploads", async () => {
expect(typeof lckbody.session_hash).toEqual("string");
expect(lckbody.version).toEqual("v0");

// Check that the usage file was updated.
let usinfo = await env.BOUND_BUCKET.get("test-upload/..usage");
let usbody = await usinfo.json();
expect(usbody["~pending_on_complete_only"]).toEqual(123);

// Check that a version summary file was posted to the bucket.
let sinfo = await env.BOUND_BUCKET.get("test-upload/blob/v0/..summary");
let sbody = await sinfo.json();
Expand Down Expand Up @@ -747,7 +742,11 @@ test("completeUploadHandler works correctly", async () => {
// Checking that the usage has been updated.
let usinfo = await env.BOUND_BUCKET.get("test-upload/..usage");
let usbody = await usinfo.json();
expect(usbody.total).toBeGreaterThan(original_size);
let new_size = original_size;
for (const x of [ "makoto", "akane", "chito" ]) { //i.e., everything set as 'simple'.
new_size += payload[x].length;
}
expect(usbody.total).toEqual(new_size);

// Check that a log was added.
let found_logs = await setup.fetchLogs(env);
Expand Down

0 comments on commit 051d080

Please sign in to comment.