-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
67 lines (61 loc) · 2.24 KB
/
mod.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
61
62
63
64
65
66
67
import { serve, ServerRequest } from "https://deno.land/std@0.81.0/http/server.ts";
let NUM_CHUNKS = 8;
const s = serve({ port: 8000 });
console.log(`listening at port 8000`);
const videopath = `test_dashinit.mp4`;
const StaticFile = async (req:ServerRequest,filename:string) => {
const file = await Deno.readFile(`public/${filename}`);
await req.respond({ body: file });
}
for await (const req of s) {
//console.log(req.url);
switch(req.url) {
case "/" : {
await StaticFile(req,'index.html');
break;
}
// case "/favicon.ico" : await StaticFile(req,'favicon.ico'); break;
case "/video.js": await StaticFile(req,'video.js'); break;
case "/video" : {
const filestat = await Deno.stat(videopath);
if(req.method === 'HEAD') {
await req.respond( {
status:200,
headers: new Headers({ 'clen':String(filestat.size) })
});
break;
}
const hasRange = req.headers.get("start") || req.headers.get("end");
const start = Number(req.headers.get("start"))
const end = req.headers.get("end") ? Number(req.headers.get("end")) : filestat.size-1;
console.log({start,end});
if (hasRange) {
//console.log(part);
const file = Deno.openSync(videopath);
const chunk = new Uint8Array(end - start + 1);
file.seekSync(start,Deno.SeekMode.Start);
file.readSync(chunk);
const head = new Headers(
{
'c-start':`${start}`,
'c-end':`${end}`,
'Content-Length': `${chunk.byteLength}`,
'Content-Type': 'video/mp4',
}
)
await req.respond({
headers: head,
status: 200,
body:chunk
})
}
else {
console.log("403 forbidden");
await req.respond( { status:403 })
}
break;
}
default:
await req.respond({status:404});
}
}