Skip to content

Commit 66075a5

Browse files
G4brymclaude
andauthored
Fix share link download counter incrementing before file existence check (#146)
The download counter was incremented before verifying the shared file still exists in the bucket. If the file was deleted, accessing the share link would increment the counter and eventually exhaust the download limit without any actual downloads. Move file retrieval before counter increment so the count only increases on successful downloads. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0109d8c commit 66075a5

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"r2-explorer": patch
3+
---
4+
5+
Fix share link download counter incrementing before verifying file exists. Previously, if the shared file was deleted from the bucket, accessing the share link would still increment the download counter before returning a 404 error. This could exhaust the download limit without any actual downloads occurring. The counter now only increments after confirming the file exists.

packages/worker/src/modules/buckets/getShareLink.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,16 @@ export class GetShareLink extends OpenAPIRoute {
111111
}
112112
}
113113

114-
// Increment download counter
114+
// Get the actual file before incrementing counter
115+
const file = await bucket.get(shareMetadata.key);
116+
117+
if (!file) {
118+
throw new HTTPException(404, {
119+
message: "Shared file not found",
120+
});
121+
}
122+
123+
// Increment download counter only after confirming file exists
115124
shareMetadata.currentDownloads++;
116125
await bucket.put(
117126
`.r2-explorer/sharable-links/${shareId}.json`,
@@ -125,15 +134,6 @@ export class GetShareLink extends OpenAPIRoute {
125134
},
126135
);
127136

128-
// Get the actual file
129-
const file = await bucket.get(shareMetadata.key);
130-
131-
if (!file) {
132-
throw new HTTPException(404, {
133-
message: "Shared file not found",
134-
});
135-
}
136-
137137
// Return the file with proper headers
138138
const headers = new Headers();
139139
file.writeHttpMetadata(headers);

0 commit comments

Comments
 (0)