Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/file.js')
-rw-r--r--lib/file.js57
1 files changed, 1 insertions, 56 deletions
diff --git a/lib/file.js b/lib/file.js
index f38fc0d..d25dca7 100644
--- a/lib/file.js
+++ b/lib/file.js
@@ -3,9 +3,7 @@ import { PassThrough } from 'streamx'
import { BlobWriteStream } from 'fast-blob-stream'
import streamToBuffer from 'stream-with-known-length-to-buffer'
import queueMicrotask from 'queue-microtask'
-import rangeParser from 'range-parser'
import mime from 'mime'
-import eos from 'end-of-stream'
import FileStream from './file-stream.js'
export default class File extends EventEmitter {
@@ -122,7 +120,7 @@ export default class File extends EventEmitter {
if (typeof window === 'undefined') throw new Error('browser-only method')
const writeStream = new BlobWriteStream(blob => {
cb(null, blob)
- }, { mimeType: this._getMimeType() })
+ }, { mimeType: mime.getType(this.name) || 'application/octet-stream' })
this.createReadStream().pipe(writeStream)
}
@@ -132,59 +130,6 @@ export default class File extends EventEmitter {
})
}
- _serve (req) {
- const res = {
- status: 200,
- headers: {
- // Support range-requests
- 'Accept-Ranges': 'bytes',
- 'Content-Type': mime.getType(this.name),
- 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0',
- Expires: '0'
- },
- body: req.method === 'HEAD' ? '' : 'STREAM'
- }
- // force the browser to download the file if if it's opened in a new tab
- if (req.destination === 'document') {
- res.headers['Content-Type'] = 'application/octet-stream'
- res.headers['Content-Disposition'] = 'attachment'
- res.body = 'DOWNLOAD'
- }
-
- // `rangeParser` returns an array of ranges, or an error code (number) if
- // there was an error parsing the range.
- let range = rangeParser(this.length, req.headers.range || '')
-
- if (range.constructor === Array) {
- res.status = 206 // indicates that range-request was understood
-
- // no support for multi-range request, just use the first range
- range = range[0]
-
- res.headers['Content-Range'] = `bytes ${range.start}-${range.end}/${this.length}`
- res.headers['Content-Length'] = `${range.end - range.start + 1}`
- } else {
- res.headers['Content-Length'] = this.length
- }
-
- const stream = req.method === 'GET' && this.createReadStream(range)
-
- let pipe = null
- if (stream) {
- this.emit('stream', { stream, req, file: this }, piped => {
- pipe = piped
-
- // piped stream might not close the original filestream on close/error, this is agressive but necessary
- eos(piped, () => {
- if (piped) piped.destroy()
- stream.destroy()
- })
- })
- }
-
- return [res, pipe || stream, pipe && stream]
- }
-
getStreamURL () {
if (!this._server) throw new Error('No server created')
const url = `${this._server.pathname}/${this._torrent.infoHash}/${encodeURI(this.path)}`