Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jacobsapps/a91e64d2d73edc5e766bc1aa94bbde16 to your computer and use it in GitHub Desktop.

Select an option

Save jacobsapps/a91e64d2d73edc5e766bc1aa94bbde16 to your computer and use it in GitHub Desktop.
func uploadChunks(videoURL: URL, sessionId: String, fileSize: Int64) async throws {
var offset: Int64 = 0
let chunkSize: Int64 = 512 * 1024
while offset < fileSize {
let handle = try FileHandle(forReadingFrom: videoURL)
defer { try? handle.close() }
try handle.seek(toOffset: UInt64(offset))
let chunkData = try handle.read(upToCount: Int(min(chunkSize, fileSize - offset))) ?? Data()
let chunkFile = FileManager.default.temporaryDirectory
.appendingPathComponent("chunk-\(UUID().uuidString).bin")
try chunkData.write(to: chunkFile)
let request = makeChunkRequest(sessionId: sessionId, offset: offset, totalBytes: fileSize)
let task = session.uploadTask(with: request, fromFile: chunkFile)
task.resume()
offset += Int64(chunkData.count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment