Created
October 2, 2020 17:41
-
-
Save zonble/423a75afcfdae9da75113e568f29baf6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
webServer?.addHandler(forMethod: "GET", pathRegex: "(.*?).mp4", request: GCDWebServerRequest.self, asyncProcessBlock: { request, completion in | |
let filename = request.url.lastPathComponent | |
let clipFileSize = getClipFileSize() | |
let response = GCDWebServerStreamedResponse(contentType: "video/mp4", asyncStreamBlock: { block in | |
/// Fetch data | |
}) | |
let disposition = "attachment; filename=\"\(filename)\"" | |
response.setValue(disposition, forAdditionalHeader: "Content-Disposition") | |
response.setValue("video/mp4", forAdditionalHeader: "Content-Type") | |
response.setValue("bytes", forAdditionalHeader: "Accept-Ranges") | |
response.setValue("keep-alive", forAdditionalHeader: "Connection") | |
if request.hasByteRange() && request.byteRange.length > 0 { | |
response.statusCode = 206 | |
response.setValue("bytes \(byteRangeString)/\(clipFileSize)", forAdditionalHeader: "Content-Range") | |
let length = UInt(request.byteRange.length) | |
response.contentLength = length | |
} else { | |
response.statusCode = 200 | |
response.contentLength = clipFileSize | |
} | |
completion(response) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment