Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Resumed Uploads start at the beginning of the file #7

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions Sources/MuxUploadSDK/Upload/ChunkedFileUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ChunkedFileUploader {
private var _currentState: InternalUploadState = .ready
private var overallProgress: Progress = Progress()
private var lastSeenUpdate: InternalUploadState? = nil
private var lastByte: UInt64 = 0
private var lastReadCount: UInt64 = 0

func addDelegate(withToken token: Int, _ delegate: ChunkedFileUploaderDelegate) {
delegates.updateValue(delegate, forKey: token)
Expand Down Expand Up @@ -87,8 +87,6 @@ class ChunkedFileUploader {
let task = Task.detached { [self] in
do {
// It's fine if it's already open, that's handled by ignoring the call
try file.openFile(fileURL: uploadInfo.videoFile)
try file.seekTo(byte: lastByte)
let result = try await makeWorker().performUpload()
file.close()

Expand All @@ -103,14 +101,14 @@ class ChunkedFileUploader {
if let lastUpdate = lastSeenUpdate {
switch lastUpdate {
case .uploading(let update): do {
if lastByte > 0 {
lastByte = UInt64(update.progress.completedUnitCount)
if lastReadCount > 0 {
lastReadCount = UInt64(update.progress.completedUnitCount)
}
}
default: break
}
}
let uploadError = InternalUploaderError(reason: error, lastByte: lastByte)
let uploadError = InternalUploaderError(reason: error, lastByte: lastReadCount)
notifyStateFromWorker(.failure(uploadError))
}

Expand All @@ -122,7 +120,8 @@ class ChunkedFileUploader {
return Worker(
uploadInfo: uploadInfo,
chunkedFile: file,
progress: overallProgress
progress: overallProgress,
startByte: lastReadCount
) { progress, startTime, eventTime in
let update = Update(
progress: progress,
Expand All @@ -149,7 +148,7 @@ class ChunkedFileUploader {

if case .uploading(let update) = state {
let count = update.progress.completedUnitCount
lastByte = UInt64(count)
lastReadCount = UInt64(count)
}

for delegate in delegates.values {
Expand All @@ -175,7 +174,7 @@ class ChunkedFileUploader {
init(uploadInfo: UploadInfo, file: ChunkedFile, startingByte: UInt64 = 0) {
self.uploadInfo = uploadInfo
self.file = file
self.lastByte = startingByte
self.lastReadCount = startingByte
}

enum InternalUploadState {
Expand Down Expand Up @@ -214,14 +213,19 @@ fileprivate actor Worker {
private let chunkedFile: ChunkedFile
private let overallProgress: Progress
private let progressHandler: ProgressHandler
private let startingReadCount: UInt64

func performUpload() async throws -> ChunkedFileUploader.Update {
try chunkedFile.openFile(fileURL: uploadInfo.videoFile)
try chunkedFile.seekTo(byte: startingReadCount)

let startTime = Date().timeIntervalSince1970

let fileSize = chunkedFile.fileSize
let wideFileSize = Int64(fileSize)
overallProgress.totalUnitCount = wideFileSize
overallProgress.isCancellable = false
overallProgress.completedUnitCount = Int64(startingReadCount)

var readBytes: Int
repeat {
Expand Down Expand Up @@ -272,11 +276,13 @@ fileprivate actor Worker {
uploadInfo: UploadInfo,
chunkedFile: ChunkedFile,
progress: Progress,
startByte: UInt64,
_ progressHandler: @escaping ProgressHandler
) {
self.uploadInfo = uploadInfo
self.chunkedFile = chunkedFile
self.progressHandler = progressHandler
self.overallProgress = progress
self.startingReadCount = startByte
}
}
6 changes: 3 additions & 3 deletions apps/Test App/Test App/Screens/UploadScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class UploadScreenViewModel: ObservableObject {

}

let PUT_URL = "https://storage.googleapis.com/video-storage-gcp-us-east4-vop1-uploads/KmgByhv1MfRPismF8kPK3G?Expires=1678317874&GoogleAccessId=uploads-gcp-us-east1-vop1%40mux-video-production.iam.gserviceaccount.com&Signature=L%2BrmPZ2LW%2FOHrvXops0V%2Bp8AYuGRV3CkLywUl5lNdhWTQe3Iz85WXnDCLmAWnTNmiGVx3RWtAf5zRJ0Ahgcaz7hkq7kPpvUgx2NRzLukSeRix9CHowcshgqI8eQEtSx7HKxD8E2%2Boh0ur7tldNDCjBoTofg7yEzSu%2F2pqPp3qySf3nnjdbI86miKmLEK7d1YO431L3Ai5N6axWA9pR78cgrq7X48%2FqhDHmITtqRwx%2Baossr2Jar9FRY2PLFIHFawnyKbYGfmGHwDIR%2FCaqOFTUlnSKpbFJ5BZPGCu2HQVZbhuBSrWthg34hczlFT0K5410MMksyLXn8j0LC%2B757cQw%3D%3D&upload_id=ADPycdv6m_6zr0XH6-0w2EdY2wZzspZ1ofLu-2l9e64ni24k-FX66g31mkyHcawdRv5ITVksYpwJUU6VKblDkq143jKR"
let PUT_URL = "https://storage.googleapis.com/video-storage-gcp-us-east4-vop1-uploads/4OAV5fGb8RMf2ElvbeLz8I?Expires=1680807117&GoogleAccessId=uploads-gcp-us-east1-vop1%40mux-video-production.iam.gserviceaccount.com&Signature=dOFesy7mXnbKnytmdDsFKCVVZ6lW12JvPsTPOxSn0egin4WD6hVOQpTAmc3XycR%2F7OkoYKnTV6wauec9mrjLkOF4fdGuHUC76YZTMhezUFxNZYkEkf7rniRRUuiuJp%2B5IZvCdkYX0VfNMUDHG1pzi8XvEVN1evLI6CoA%2F6OPUJOtMC%2BX%2FTLlHUvFVzwPk06CzzjYer7ZE3O72UMtXFEAs4QAsf0L3eTfWJav4si%2F6F1Y3BgO4ypG7zKmStxsvQNa%2F71DlIwxo%2BO0n2iJFfh48te9kvI%2B7cqsgb7aaQcLIotJ040Jps5mRDlC5Q35yRIWhxRNmCfSLRnLUB1104SNwQ%3D%3D&upload_id=ADPycdu7bDavvtbJw3M9ukiH30zs06Ql-Twu7EHWfSKb48kvXU6Xd2Zn4EgBteTLwE3A0KrQEGg2H7p9rRKUX9D_Kt7OU_DHcGl4"

@Published
var uploadScreenState: AppUploadState = .not_started
Expand Down Expand Up @@ -128,12 +128,12 @@ class UploadScreenViewModel: ObservableObject {
session.exportAsynchronously {
DispatchQueue.main.async {
NSLog("Yay, Media exported & ready for upload!")
self.beginMuxUpload(videoFile: outFile)
self.beginUploadToMux(videoFile: outFile)
}
}
}

private func beginMuxUpload(videoFile: URL) {
private func beginUploadToMux(videoFile: URL) {
let upload = MuxUpload(
uploadURL: URL(string: PUT_URL)!,
videoFileURL: videoFile,
Expand Down