-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_downloaded.ts
60 lines (49 loc) · 1.79 KB
/
split_downloaded.ts
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
const url = new URL(import.meta.resolve('./cookie.png'))
/**
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
*/
// const handler = async (req: Request) => {
// // const body = `Your user-agent is:\n\n${req.headers.get('user-agent')}`
// const body = await Deno.readFile(url)
// const headers = new Headers({
// 'Content-Disposition': 'attachment; filename=text',
// 'Access-Control-Allow-Origin': '*',
// 'Accept-Ranges': 'bytes',
// })
// const range = req.headers.get('range')
// const bodyLength = body.length
// if (req.url.endsWith('/file') && range) {
// console.log(range)
// let [start, end] = range
// .replace(/bytes=/, '')
// .split('-')
// .map(Number)
// end = end || bodyLength
// if (start >= 0 && start < end && end <= bodyLength) {
// headers.set('Content-Range', `bytes ${start}-${end}/${bodyLength}`)
// const part = body.slice(Number(start), Number(end))
// return new Response(part, { headers, status: 206 })
// } else {
// return new Response('Range Not Satisfiable', { status: 416 })
// }
// }
// return new Response('Not Found', { status: 404 })
// }
const handler = async (req: Request) => {
const fileStream = (await Deno.open(url)).readable
const headers = new Headers({ 'Access-Control-Allow-Origin': '*' })
if (req.url.endsWith('/file')) {
return new Response(fileStream, { headers, status: 200 })
}
return new Response('Not Found', { status: 404 })
}
Deno.serve(
{
onListen({ port }) {
console.log(`Listening on http://localhost:${port}/`)
},
},
handler
)