forked from Tomas/webdl
Compare commits
3 Commits
4eb5be59eb
...
3b846bed39
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b846bed39 | |||
| fc664848f7 | |||
| da18669c57 |
45
src/index.ts
45
src/index.ts
@ -80,9 +80,48 @@ app.get("/api/data/:id", async (req, res)=>{
|
|||||||
const head = Buffer.concat(head_chunks);
|
const head = Buffer.concat(head_chunks);
|
||||||
detectBufferMime(head).then((mime)=>{
|
detectBufferMime(head).then((mime)=>{
|
||||||
res.contentType(mime);
|
res.contentType(mime);
|
||||||
res.write(head);
|
res.header("Accept-Ranges", "bytes");
|
||||||
stream.pipe(res);
|
const range = req.range(dl.received);
|
||||||
stream.resume();
|
if (range)
|
||||||
|
{
|
||||||
|
if (typeof(range) == "number")
|
||||||
|
return res.status(400).send({error:"Invalid range"});
|
||||||
|
|
||||||
|
const r = range[0]!;
|
||||||
|
r.end += 1;
|
||||||
|
|
||||||
|
res.status(206);
|
||||||
|
res.header("Content-Range", `bytes ${r.start}-${r.end-1}/${dl.received}`);
|
||||||
|
res.header("Content-Length", String(r.end - r.start));
|
||||||
|
if (r.start < head.length)
|
||||||
|
res.write(head.slice(r.start, r.end));
|
||||||
|
|
||||||
|
if ((r.end-r.start) - head.length <= 0)
|
||||||
|
return res.end();
|
||||||
|
|
||||||
|
let start = r.start-head.length;
|
||||||
|
let end = r.end-head.length;
|
||||||
|
stream.on("data", (chunk)=>{
|
||||||
|
if (start < chunk.length)
|
||||||
|
res.write(chunk.slice(Math.max(0, start), end));
|
||||||
|
start -= chunk.length;
|
||||||
|
end -= chunk.length;
|
||||||
|
if (end <= 0)
|
||||||
|
{
|
||||||
|
res.end();
|
||||||
|
stream.pause();
|
||||||
|
stream.destroy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
stream.resume();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res.header("Content-Length", String(dl.received));
|
||||||
|
res.write(head);
|
||||||
|
stream.pipe(res);
|
||||||
|
stream.resume();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,7 +27,7 @@
|
|||||||
<a x-bind:href="'/api/data/'+id" x-text="dl.filename || dl.url"></a>
|
<a x-bind:href="'/api/data/'+id" x-text="dl.filename || dl.url"></a>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<template x-if="dl.received != dl.total">
|
<template x-if="!dl.finished">
|
||||||
<div>
|
<div>
|
||||||
<span style="flex:1" x-text="dl.filename || dl.url"></span>
|
<span style="flex:1" x-text="dl.filename || dl.url"></span>
|
||||||
<progress style="flex:1" x-bind:max="dl.total" x-bind:value="dl.received"></progress>
|
<progress style="flex:1" x-bind:max="dl.total" x-bind:value="dl.received"></progress>
|
||||||
|
|||||||
Reference in New Issue
Block a user