-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathcreateShareLink.ts
More file actions
141 lines (126 loc) · 3.38 KB
/
createShareLink.ts
File metadata and controls
141 lines (126 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { OpenAPIRoute } from "chanfana";
import { HTTPException } from "hono/http-exception";
import { z } from "zod";
import type { AppContext, ShareMetadata } from "../../types";
export class CreateShareLink extends OpenAPIRoute {
schema = {
operationId: "post-bucket-create-share-link",
tags: ["Buckets"],
summary: "Create shareable link for file",
request: {
params: z.object({
bucket: z.string(),
key: z.string(),
}),
body: {
content: {
"application/json": {
schema: z.object({
expiresIn: z
.number()
.optional()
.describe("Expiration time in seconds"),
password: z.string().optional().describe("Optional password"),
maxDownloads: z.number().optional().describe("Maximum downloads"),
}),
},
},
},
},
responses: {
"200": {
description: "Share link created successfully",
content: {
"application/json": {
schema: z.object({
shareId: z.string(),
shareUrl: z.string(),
expiresAt: z.number().optional(),
}),
},
},
},
},
};
async handle(c: AppContext) {
const data = await this.getValidatedData<typeof this.schema>();
const bucketName = data.params.bucket;
const bucket = c.env[bucketName] as R2Bucket | undefined;
if (!bucket) {
throw new HTTPException(500, {
message: `Bucket binding not found: ${bucketName}`,
});
}
const key = decodeURIComponent(escape(atob(data.params.key)));
// Verify the file exists
const fileExists = await bucket.head(key);
if (!fileExists) {
throw new HTTPException(404, {
message: `File not found: ${key}`,
});
}
// Generate unique share ID
let shareId = "";
let attempts = 0;
const maxAttempts = 5;
while (attempts < maxAttempts) {
shareId = crypto.randomUUID().replace(/-/g, "").substring(0, 10);
const existingShare = await bucket.head(
`.r2-explorer/sharable-links/${shareId}.json`,
);
if (!existingShare) {
break;
}
attempts++;
}
if (attempts === maxAttempts) {
throw new HTTPException(500, {
message: "Failed to generate unique share ID",
});
}
// Hash password if provided
let passwordHash: string | undefined;
if (data.body.password) {
const encoder = new TextEncoder();
const passwordData = encoder.encode(data.body.password);
const hashBuffer = await crypto.subtle.digest("SHA-256", passwordData);
passwordHash = Array.from(new Uint8Array(hashBuffer))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
// Calculate expiration timestamp
const expiresAt = data.body.expiresIn
? Date.now() + data.body.expiresIn * 1000
: undefined;
// Create share metadata
const shareMetadata: ShareMetadata = {
bucket: bucketName,
key: key,
expiresAt: expiresAt,
passwordHash: passwordHash,
maxDownloads: data.body.maxDownloads,
currentDownloads: 0,
createdBy: c.get("authentication_username") || "anonymous",
createdAt: Date.now(),
};
// Store share metadata in R2
await bucket.put(
`.r2-explorer/sharable-links/${shareId}.json`,
JSON.stringify(shareMetadata),
{
httpMetadata: { contentType: "application/json" },
customMetadata: {
targetBucket: bucketName,
targetKey: key,
},
},
);
// Construct share URL
const shareUrl = `${new URL(c.req.url).origin}/share/${shareId}`;
return c.json({
shareId,
shareUrl,
expiresAt,
});
}
}