Live-Upload-Rate (MB/s, Mbit/s, Restzeit) für FTP (curl-Fortschritt) und Immich, Fortschritt pro Datei
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
This commit is contained in:
1 parent
2bf0d851aa
commit
5505fa1f37
5 files changed
+198
-12
No files matched your search
@@ -94,15 +94,26 @@ public final class ImmichClient: @unchecked Sendable {
|
||||
|
||||
/// Uploads one file as multipart/form-data (streamed from a temp file, RAWs can be large).
|
||||
/// Newer Immich (3.x) dropped deviceAssetId/deviceId; older servers require them → automatic retry.
|
||||
public func upload(file: URL, captureDate: Date?) async throws -> UploadResult {
|
||||
public func upload(file: URL, captureDate: Date?, progress: (@Sendable (Int64) -> Void)? = nil) async throws -> UploadResult {
|
||||
do {
|
||||
return try await upload(file: file, captureDate: captureDate, legacyFields: false)
|
||||
return try await upload(file: file, captureDate: captureDate, legacyFields: false, progress: progress)
|
||||
} catch ImmichError.http(400, let body) where body.contains("deviceAssetId") || body.contains("deviceId") {
|
||||
return try await upload(file: file, captureDate: captureDate, legacyFields: true)
|
||||
return try await upload(file: file, captureDate: captureDate, legacyFields: true, progress: progress)
|
||||
}
|
||||
}
|
||||
|
||||
private func upload(file: URL, captureDate: Date?, legacyFields: Bool) async throws -> UploadResult {
|
||||
/// Reports bytes sent for the live upload rate.
|
||||
final class ProgressDelegate: NSObject, URLSessionTaskDelegate, @unchecked Sendable {
|
||||
let onSent: (@Sendable (Int64) -> Void)?
|
||||
init(_ f: (@Sendable (Int64) -> Void)?) { onSent = f }
|
||||
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64,
|
||||
totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
|
||||
onSent?(totalBytesSent)
|
||||
}
|
||||
}
|
||||
|
||||
private func upload(file: URL, captureDate: Date?, legacyFields: Bool,
|
||||
progress: (@Sendable (Int64) -> Void)?) async throws -> UploadResult {
|
||||
let attrs = try FileManager.default.attributesOfItem(atPath: file.path)
|
||||
let modified = (attrs[.modificationDate] as? Date) ?? Date()
|
||||
let size = (attrs[.size] as? NSNumber)?.int64Value ?? 0
|
||||
@@ -139,7 +150,7 @@ public final class ImmichClient: @unchecked Sendable {
|
||||
|
||||
var r = try request("assets", method: "POST")
|
||||
r.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
|
||||
let (data, resp) = try await session.upload(for: r, fromFile: body)
|
||||
let (data, resp) = try await session.upload(for: r, fromFile: body, delegate: ProgressDelegate(progress))
|
||||
let code = (resp as? HTTPURLResponse)?.statusCode ?? 0
|
||||
guard (200..<300).contains(code) else { throw ImmichError.http(code, String(decoding: data, as: UTF8.self)) }
|
||||
guard let obj = try JSONSerialization.jsonObject(with: data) as? [String: Any], let id = obj["id"] as? String else {
|
||||
|
||||
Reference in new issue
Block a user