Kartenimport, Metadaten und Versand absichern

This commit is contained in:
Leon Tonhäuser committed 2026-11-05 09:23:44 +01:00
1 parent be6bd49da5
commit 427b6fa7b0
18 files changed
+643 -193

No files matched your search

+56 -30
View File
@@ -70,6 +70,8 @@ struct PendingUpload: Identifiable {
let id = UUID()
let target: String
let targetName: String
var variant: UploadVariant
let skipAlreadySent: Bool
var items: [PhotoItem]
/// item id → issues
var issues: [String: [PreflightIssue]]
@@ -203,7 +205,8 @@ final class AppState: ObservableObject {
@Published private(set) var sentLog = SentLog()
private var sentLogSaveTask: Task<Void, Never>?
/// Agentur-Check / double-send question waiting for the user (window "check")
@Published var pendingUpload: PendingUpload?
@Published private var pendingUploads: [PendingUpload] = []
var pendingUpload: PendingUpload? { pendingUploads.first }
/// Undo / redo of ratings, labels, tags and IPTC changes
@Published private(set) var undoStack: [UndoEntry] = []
@Published private(set) var redoStack: [UndoEntry] = []
@@ -1081,35 +1084,47 @@ final class AppState: ObservableObject {
}
statusText = "Lightroom: \(result.imported) importiert, \(result.exported.count) JPEG(s) exportiert"
guard let job, !result.exported.isEmpty else { continue }
Task { await handleExported(result.exported, job: job, sources: jobSources) }
Task { await handleExported(result, job: job, sources: jobSources) }
}
}
/// After LR export: re-apply the original IPTC (PM-identical structure) and upload.
private func handleExported(_ files: [URL], job: LightroomJob, sources jobSources: [PhotoItem]) async {
// Map exported JPEG → source item by base name (LR keeps the file name, may append "-2").
// Current state if the photo is still open (IPTC edited meanwhile), else the copy from the job.
let sources = jobSources.map { item($0.id) ?? $0 }
if job.rewriteIPTCAfterExport, let w = writer {
for file in files {
let base = file.deletingPathExtension().lastPathComponent
guard let src = sources.first(where: { base == $0.baseName || base.hasPrefix($0.baseName + "-") }) else { continue }
let jpegItem = PhotoItem(primary: file)
do { try await w.write(values: src.iptc, fields: IPTCField.all, to: jpegItem) } catch { report(error) }
private func handleExported(_ result: LightroomJobResult, job: LightroomJob, sources jobSources: [PhotoItem]) async {
let sources = Dictionary(jobSources.map { original in
let path = (original.rawFile ?? original.primary).standardizedFileURL.path
return (path, item(original.id) ?? original)
}, uniquingKeysWith: { first, _ in first })
let writer = job.rewriteIPTCAfterExport ? self.writer : nil
var exportedItems: [PhotoItem] = []
var missingSources = 0
for file in result.exported {
guard let path = result.exportSources[file]?.standardizedFileURL.path, let source = sources[path] else {
missingSources += 1
continue
}
var exported = PhotoItem(primary: file)
if job.rewriteIPTCAfterExport {
guard let writer else { continue }
do {
try await writer.write(values: source.iptc, fields: IPTCField.all, to: exported)
} catch {
report(error)
continue
}
}
exported.iptc = source.iptc
exported.exif = source.exif
exported.captureDate = source.captureDate
exportedItems.append(exported)
}
// Exported JPEGs carry the IPTC of their RAW (needed for {event} album names, rename patterns …)
let exportedItems: [PhotoItem] = files.compactMap { file in
let base = file.deletingPathExtension().lastPathComponent
guard let src = sources.first(where: { base == $0.baseName || base.hasPrefix($0.baseName + "-") }) else { return nil }
var it = PhotoItem(primary: file)
it.iptc = src.iptc; it.exif = src.exif; it.captureDate = src.captureDate
return it
if missingSources > 0 && (job.rewriteIPTCAfterExport || !job.uploadTargets.isEmpty) {
alert = AppAlert(title: "Lightroom-Export ohne Originalzuordnung",
message: "Für \(missingSources) JPEG(s) wurden IPTC-Nachbearbeitung und automatischer Versand ausgesetzt. Bitte die Lichtkasten Bridge aktualisieren und Lightroom neu starten.")
}
for target in job.uploadTargets {
requestUpload(exportedItems, to: target, skipAlreadySent: true)
}
if !job.uploadTargets.isEmpty { openWindow("tasks") }
if !job.uploadTargets.isEmpty && pendingUpload == nil { openWindow("tasks") }
// SENT workflow: show the exported JPEGs for captioning.
if settings.lightroom.openSentAfterExport, let dir = job.exportFolder {
if folder != dir { lastRawFolder = folder; open(folder: dir) } else { refreshFolder() }
@@ -1175,7 +1190,7 @@ final class AppState: ObservableObject {
var issues: [String: [PreflightIssue]] = [:]
if runCheck {
for t in withFiles {
let i = preflightIssues(for: t)
let i = preflightIssues(for: t, variant: variant)
if !i.isEmpty { issues[t.id] = i }
}
}
@@ -1186,41 +1201,52 @@ final class AppState: ObservableObject {
enqueueUpload(send, target: target, variant: variant)
return
}
pendingUpload = PendingUpload(target: target, targetName: name, items: withFiles, issues: issues, alreadySent: sentBefore)
pendingUploads.append(PendingUpload(target: target, targetName: name, variant: variant, skipAlreadySent: skipAlreadySent ?? true,
items: withFiles, issues: issues, alreadySent: sentBefore))
openWindow("check")
}
/// Agentur-Check of one photo (current metadata).
func preflightIssues(for item: PhotoItem) -> [PreflightIssue] {
func preflightIssues(for item: PhotoItem, variant: UploadVariant) -> [PreflightIssue] {
let cur = self.item(item.id) ?? item
let open = openSuggestions(for: cur.id).map { s -> String in
let name = s.replacement.map { JerseyLogic.personName(replacement: $0, columns: []) } ?? ""
return [s.code ?? "#\(s.number)", name].filter { !$0.isEmpty }.joined(separator: " ")
}
return Preflight.check(iptc: cur.iptc, exif: cur.exif, captureDate: cur.captureDate, codes: codes,
options: settings.preflight, openSuggestions: open)
options: settings.preflight, openSuggestions: open,
uploadFiles: uploadFiles(for: cur, variant: variant))
}
/// Check window: run the check again (after fixing captions in the IPTC window).
func recheckPendingUpload() {
guard var p = pendingUpload else { return }
guard var p = pendingUpload, let variant = uploadVariant(p.target) else { return }
p.variant = variant
p.items = p.items.map { item($0.id) ?? $0 }
var issues: [String: [PreflightIssue]] = [:]
let destination = settings.ftpDestinations.first { $0.id.uuidString == p.target }
if p.target != "immich" && (destination?.preflight ?? true) {
for t in p.items { let i = preflightIssues(for: t); if !i.isEmpty { issues[t.id] = i } }
for t in p.items { let i = preflightIssues(for: t, variant: variant); if !i.isEmpty { issues[t.id] = i } }
}
p.issues = issues
p.alreadySent = Set(p.items.filter { sentLog.wasSent($0, to: p.target) }.map(\.id))
pendingUpload = p
pendingUploads[0] = p
}
/// Check window: send these photos.
func confirmPendingUpload(_ send: [PhotoItem]) {
guard let p = pendingUpload, let variant = uploadVariant(p.target) else { pendingUpload = nil; return }
pendingUpload = nil
guard let p = pendingUpload else { return }
guard let variant = uploadVariant(p.target) else { discardPendingUpload(); return }
guard p.variant == variant else { recheckPendingUpload(); return }
discardPendingUpload()
guard !send.isEmpty else { return }
enqueueUpload(send.map { item($0.id) ?? $0 }, target: p.target, variant: variant)
enqueueUpload(send, target: p.target, variant: variant)
}
func discardPendingUpload() {
guard !pendingUploads.isEmpty else { return }
pendingUploads.removeFirst()
recheckPendingUpload()
}
private func enqueueUpload(_ items: [PhotoItem], target: String, variant: UploadVariant) {
+10 -1
View File
@@ -111,18 +111,25 @@ final class UploadQueue: ObservableObject {
guard let state, !files.isEmpty else { return }
let name: String
var pattern = ""
var usedNames = Set<String>()
if target == "immich" {
name = "Immich"
} else if let d = state.settings.ftpDestinations.first(where: { $0.id.uuidString == target }) {
name = d.name
pattern = d.renamePattern
let uploader = FTPUploader()
let directory = uploader.baseURL(d).lowercased()
let matchingProfiles = Set(state.settings.ftpDestinations.filter {
uploader.baseURL($0).lowercased() == directory
}.map { $0.id.uuidString })
usedNames = Set(jobs.filter { !$0.isFinished && matchingProfiles.contains($0.target) }
.map { $0.remoteName.lowercased() })
} else { return }
var batchRef: UUID?
let batch = state.tasks.start(.upload, title: "Upload → \(name)", total: files.count) { [weak self] in
if let b = batchRef { self?.cancelBatch(b) }
}
batchRef = batch
var usedNames = Set<String>()
for f in files {
let src = sourceItems.first { $0.primary == f || $0.companions.contains(f) }
var remote = f.lastPathComponent
@@ -228,6 +235,8 @@ final class UploadQueue: ObservableObject {
remoteName: job.remoteName, size: job.fileSize)
setStatus(job.id, .done)
}
} catch UploadError.remoteNameConflict(let name) {
setStatus(job.id, .failed(UploadError.remoteNameConflict(name).localizedDescription))
} catch {
if let i = jobs.firstIndex(where: { $0.id == job.id }), jobs[i].attempts < 3 {
jobs[i].status = .waiting