Skip to content

Commit 717bce2

Browse files
committed
Remove .enc check
1 parent 6049738 commit 717bce2

File tree

1 file changed

+27
-60
lines changed

1 file changed

+27
-60
lines changed

routes/[filename].tsx

+27-60
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import { checkQuota, updateQuotas } from '../utils/quotas.ts'
44
import { recordDeletion, recordDownload, recordUpload } from '../utils/stats.ts'
55

66
const MAX_FILE_SIZE = parseInt(Deno.env.get('MAX_FILE_SIZE') || '10485760') // 10MB default
7-
const FILENAME = 'only_you_know'
8-
9-
// Initialize KV store
107
const kv = await Deno.openKv()
118

129
interface FileData {
@@ -16,37 +13,29 @@ interface FileData {
1613
deletionKey: string
1714
}
1815

19-
async function storeFileData(hash: string, data: FileData) {
20-
await kv.set(['files', hash], data)
16+
async function storeFileData(key: string, data: FileData) {
17+
await kv.set(['files', key], data)
2118
}
2219

23-
async function getFileData(hash: string): Promise<FileData | null> {
24-
const res = await kv.get<FileData>(['files', hash])
20+
async function getFileData(key: string): Promise<FileData | null> {
21+
const res = await kv.get<FileData>(['files', key])
2522
return res.value
2623
}
2724

28-
async function deleteFileData(hash: string) {
29-
await kv.delete(['files', hash])
25+
async function deleteFileData(key: string) {
26+
await kv.delete(['files', key])
3027
}
3128

3229
export const handler: Handlers = {
3330
async PUT(req, ctx) {
3431
try {
35-
const filename = ctx.params.filename
36-
37-
if (!filename.endsWith('.enc')) {
38-
return new Response('Only encrypted files (.enc) are accepted', {
39-
status: 400,
40-
})
41-
}
32+
const key = ctx.params.filename // This is our storage key
4233

4334
const size = req.headers.get('content-length')
4435
if (!size || parseInt(size) > MAX_FILE_SIZE) {
4536
return new Response(
4637
`File too large. Maximum size is ${MAX_FILE_SIZE / 1024 / 1024}MB`,
47-
{
48-
status: 413,
49-
},
38+
{ status: 413 }
5039
)
5140
}
5241

@@ -55,31 +44,24 @@ export const handler: Handlers = {
5544
if (!quotaOk) {
5645
return new Response(
5746
'Service quota exceeded (storage or transfer limit reached)',
58-
{
59-
status: 507,
60-
},
47+
{ status: 507 }
6148
)
6249
}
6350

6451
// Read the request body
6552
const arrayBuffer = await req.arrayBuffer()
6653
const fileData = new Uint8Array(arrayBuffer)
6754

68-
// Calculate SHA-256 hash of the file content
69-
const hash = await crypto.subtle.digest('SHA-256', fileData)
70-
const hashHex = Array.from(new Uint8Array(hash))
71-
.map((b) => b.toString(16).padStart(2, '0'))
72-
.join('')
73-
74-
// Extract key and IV from the filename
75-
const authKey = filename.slice(-64 - 32 - 4, -4) // 64 for key, 32 for iv, 4 for .enc
55+
// Extract IV from the request - it's added to the key for deletion auth
56+
const iv = key.slice(64, 96)
57+
const deletionKey = key + iv // Full key+iv for deletion auth
7658

7759
// Store file data and metadata in KV
78-
await storeFileData(hashHex, {
60+
await storeFileData(key, {
7961
content: fileData,
8062
size: fileSize,
8163
created: new Date().toISOString(),
82-
deletionKey: authKey,
64+
deletionKey
8365
})
8466

8567
await updateQuotas(fileSize, true)
@@ -88,10 +70,10 @@ export const handler: Handlers = {
8870
// Auto-deletion after 24h
8971
setTimeout(async () => {
9072
try {
91-
const fileInfo = await getFileData(hashHex)
73+
const fileInfo = await getFileData(key)
9274
if (fileInfo) {
9375
await recordDeletion(fileInfo.size)
94-
await deleteFileData(hashHex)
76+
await deleteFileData(key)
9577
}
9678
} catch {
9779
// Ignore deletion errors
@@ -107,18 +89,9 @@ export const handler: Handlers = {
10789

10890
async GET(_req, ctx) {
10991
try {
110-
const filename = ctx.params.filename
111-
112-
if (!filename.endsWith('.enc')) {
113-
return new Response('Only encrypted files (.enc) are allowed', {
114-
status: 400,
115-
})
116-
}
117-
118-
// Get file hash from the start of the filename
119-
const fileHash = filename.slice(0, 64) // SHA-256 hash is 64 hex chars
120-
121-
const fileData = await getFileData(fileHash)
92+
const key = ctx.params.filename
93+
const fileData = await getFileData(key)
94+
12295
if (!fileData) {
12396
return new Response('File not found', { status: 404 })
12497
}
@@ -130,16 +103,15 @@ export const handler: Handlers = {
130103
start(controller) {
131104
controller.enqueue(fileData.content)
132105
controller.close()
133-
},
106+
}
134107
})
135108

136109
return new Response(stream, {
137110
headers: {
138111
'content-type': 'application/octet-stream',
139112
'cache-control': 'no-store',
140-
'content-disposition': `attachment; filename="${FILENAME}"`,
141-
'content-length': fileData.size.toString(),
142-
},
113+
'content-length': fileData.size.toString()
114+
}
143115
})
144116
} catch (err) {
145117
console.error('Download error:', err)
@@ -149,20 +121,15 @@ export const handler: Handlers = {
149121

150122
async DELETE(req, ctx) {
151123
try {
152-
const filename = ctx.params.filename
153-
if (!filename.endsWith('.enc')) {
154-
return new Response('Invalid file type', { status: 400 })
155-
}
156-
157-
const fileHash = filename.slice(0, 64)
124+
const key = ctx.params.filename
158125

159126
const authHeader = req.headers.get('Authorization')
160127
if (!authHeader?.startsWith('Bearer ')) {
161128
return new Response('Authorization required', { status: 401 })
162129
}
163130
const deletionKey = authHeader.slice(7)
164131

165-
const fileData = await getFileData(fileHash)
132+
const fileData = await getFileData(key)
166133
if (!fileData) {
167134
return new Response('File not found', { status: 404 })
168135
}
@@ -172,12 +139,12 @@ export const handler: Handlers = {
172139
}
173140

174141
await recordDeletion(fileData.size)
175-
await deleteFileData(fileHash)
142+
await deleteFileData(key)
176143

177144
return new Response('File deleted', { status: 200 })
178145
} catch (err) {
179146
console.error('Delete error:', err)
180147
return new Response('Delete failed', { status: 500 })
181148
}
182-
},
183-
}
149+
}
150+
}

0 commit comments

Comments
 (0)