Skip to content

Commit

Permalink
Fix stuck file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali committed Apr 28, 2023
1 parent 7234024 commit e66f240
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions submodules/Postbox/Sources/MediaBoxFileContextV2Impl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,17 @@ final class MediaBoxFileContextV2Impl: MediaBoxFileContext {
self.fullPath = fullPath
self.metaPath = metaPath

do {
self.fileMap = try MediaBoxFileMap.read(manager: self.manager, path: self.metaPath)
} catch {
if !FileManager.default.fileExists(atPath: self.partialPath) {
let _ = try? FileManager.default.removeItem(atPath: self.metaPath)
self.fileMap = MediaBoxFileMap()
self.fileMap.serialize(manager: self.manager, to: self.metaPath)
} else {
do {
self.fileMap = try MediaBoxFileMap.read(manager: self.manager, path: self.metaPath)
} catch {
let _ = try? FileManager.default.removeItem(atPath: self.metaPath)
self.fileMap = MediaBoxFileMap()
}
}

self.destinationFile = self.manager.open(path: self.partialPath, mode: .readwrite)
Expand Down Expand Up @@ -172,6 +178,17 @@ final class MediaBoxFileContextV2Impl: MediaBoxFileContext {
completed: completed
)
if self.updateRangeRequest(request: request) {
if !self.isComplete, let truncationSize = self.fileMap.truncationSize, truncationSize == self.fileMap.sum {
self.isComplete = true

let linkResult = link(self.partialPath, self.fullPath)
if linkResult != 0 {
postboxLog("MediaBoxFileContextV2Impl: error while linking \(self.partialPath): \(linkResult)")
}
}

self.updateRequests()

return EmptyDisposable
} else {
let index = self.rangeRequests.add(request)
Expand Down

0 comments on commit e66f240

Please sign in to comment.