Skip to content

Instantly share code, notes, and snippets.

@zonble
Created October 2, 2020 17:41
Show Gist options
  • Save zonble/423a75afcfdae9da75113e568f29baf6 to your computer and use it in GitHub Desktop.
Save zonble/423a75afcfdae9da75113e568f29baf6 to your computer and use it in GitHub Desktop.
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