PM-.snap-Import (FieldsToApply, Merge, Aufnahmedatum), Aufnahmeort/Copyright-Status/DataMining, FTP-Profile Beautiful Sports
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
0b41f09206
commit
42ab9c9361
13 files changed
+472
-39
No files matched your search
@@ -23,36 +23,90 @@ public enum XMPTemplateIO {
|
||||
|
||||
public static func importTemplate(from url: URL) throws -> IPTCTemplate {
|
||||
let data = try Data(contentsOf: url)
|
||||
var t = try parse(data: data)
|
||||
t.name = url.deletingPathExtension().lastPathComponent
|
||||
var t = try parseAny(data: data)
|
||||
// PM saves "<id>-<Name>.snap" – drop the numeric prefix for display.
|
||||
var name = url.deletingPathExtension().lastPathComponent
|
||||
if let dash = name.firstIndex(of: "-"), name[..<dash].allSatisfy(\.isNumber), name.distance(from: name.startIndex, to: dash) >= 4 {
|
||||
name = String(name[name.index(after: dash)...])
|
||||
}
|
||||
t.name = name.replacingOccurrences(of: "_", with: " ")
|
||||
return t
|
||||
}
|
||||
|
||||
/// Accepts both a Photo Mechanic `.snap` (IPTCSettings wrapper) and plain XMP.
|
||||
public static func parseAny(data: Data) throws -> IPTCTemplate {
|
||||
if let snap = try? PMSnapFile.parse(data: data), let xmp = snap.xmpRecord {
|
||||
var t = try parse(data: Data(xmp.utf8))
|
||||
applySnapSettings(snap, to: &t)
|
||||
return t
|
||||
}
|
||||
return try parse(data: data)
|
||||
}
|
||||
|
||||
/// PM bookkeeping properties inside the XMP record (not image metadata).
|
||||
static let pmControlProperties: Set<String> = [
|
||||
"FieldsToApply", "FieldsToMerge", "ColorClass", "Tagged", "PMVersion", "ColorClassEval", "ColorClassApply",
|
||||
"RatingEval", "RatingApply", "TagEval", "TagApply", "CaptionMergeStyle", "ApplyDateType", "Prefs",
|
||||
]
|
||||
|
||||
public static func parse(data: Data) throws -> IPTCTemplate {
|
||||
let props = try XMPPropertyCollector.collect(data: data)
|
||||
var values = IPTCValues()
|
||||
var extras: [String: String] = [:]
|
||||
var pm: [String: [String]] = [:]
|
||||
var found = 0
|
||||
for p in props {
|
||||
if p.namespace == XMPNamespace.photomechanic && pmControlProperties.contains(p.localName) {
|
||||
pm[p.localName] = p.values
|
||||
continue
|
||||
}
|
||||
if let field = IPTCField.forXMP(namespace: p.namespace, localName: p.localName) {
|
||||
found += 1
|
||||
if field.kind == .list {
|
||||
var list = values.lists[field.id] ?? []
|
||||
list.append(contentsOf: p.values.filter { !$0.isEmpty })
|
||||
list.append(contentsOf: p.values.filter { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty })
|
||||
values.lists[field.id] = list.isEmpty ? nil : list
|
||||
} else {
|
||||
let v = p.values.joined(separator: field.xmpContainer == .seq ? "; " : "\n")
|
||||
if !v.isEmpty { values.text[field.id] = v }
|
||||
if !v.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { values.text[field.id] = v }
|
||||
}
|
||||
} else if p.namespace != XMPNamespace.rdf, !p.values.isEmpty,
|
||||
} else if p.namespace != XMPNamespace.rdf,
|
||||
p.values.contains(where: { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }),
|
||||
!(p.namespace == XMPNamespace.iptcCore && p.localName == "CreatorContactInfo") {
|
||||
extras["\(p.namespace)|\(p.localName)"] = p.values.joined(separator: "\u{1F}")
|
||||
}
|
||||
}
|
||||
// PM stores IIM date as yyyy-mm-dd in photoshop:DateCreated – keep only the date part.
|
||||
// Keep only the date part of a fixed Date Created ("2026-02-15T00:00:00+01:00").
|
||||
if let d = values.text["dateCreated"], d.count > 10 { values.text["dateCreated"] = String(d.prefix(10)) }
|
||||
guard found > 0 || !extras.isEmpty else { throw TemplateImportError.noFieldsFound }
|
||||
return IPTCTemplate(name: "Importiert", values: values, extras: extras)
|
||||
|
||||
var t = IPTCTemplate(name: "Importiert", values: values, extras: extras)
|
||||
// PM checkboxes: FieldsToApply holds 0x200 + IIM dataset (0x8000+ = PM extended fields).
|
||||
if let apply = pm["FieldsToApply"] {
|
||||
let codes = Set(apply.compactMap { Int($0.replacingOccurrences(of: "0x", with: ""), radix: 16) })
|
||||
var applied = Set<String>()
|
||||
for (fieldID, ds) in IPTCField.iimDataset where codes.contains(0x200 + ds) { applied.insert(fieldID) }
|
||||
// Extended (XMP-only) fields use PM-internal codes ≥ 0x8000.
|
||||
for c in codes where c >= 0x8000 {
|
||||
if let id = pmExtendedCodes[c] { applied.insert(id) } else { t.unmappedPMCodes.append("0x" + String(c, radix: 16)) }
|
||||
}
|
||||
t.appliedFields = applied
|
||||
}
|
||||
// FieldsToMerge: keywords (0x219) listed → append, else replace (PM default for a template).
|
||||
if let merge = pm["FieldsToMerge"] {
|
||||
t.listMergeMode = merge.contains(where: { $0.lowercased() == "0x219" }) ? .append : .replace
|
||||
}
|
||||
if let dt = pm["ApplyDateType"]?.first { t.dateFromCapture = (dt == "2") }
|
||||
return t
|
||||
}
|
||||
|
||||
/// Known PM extended field codes (derived from real .snap files; extend when new ones show up).
|
||||
static let pmExtendedCodes: [Int: String] = [
|
||||
0x8013: "event",
|
||||
]
|
||||
|
||||
static func applySnapSettings(_ snap: PMSnapFile, to t: inout IPTCTemplate) {
|
||||
if let style = snap.applyDateStyle { t.dateFromCapture = (style == "2") }
|
||||
}
|
||||
|
||||
// MARK: Export
|
||||
@@ -61,6 +115,7 @@ public enum XMPTemplateIO {
|
||||
var simpleAttrs: [String] = []
|
||||
var elements: [String] = []
|
||||
var contact: [String] = []
|
||||
var locationCreated: [String] = []
|
||||
|
||||
func esc(_ s: String) -> String {
|
||||
s.replacingOccurrences(of: "&", with: "&").replacingOccurrences(of: "<", with: "<")
|
||||
@@ -77,6 +132,10 @@ public enum XMPTemplateIO {
|
||||
contact.append(" <\(q)>\(esc(vals[0]))</\(q)>")
|
||||
continue
|
||||
}
|
||||
if field.section == .locationCreated {
|
||||
locationCreated.append(" <\(q)>\(esc(vals[0]))</\(q)>")
|
||||
continue
|
||||
}
|
||||
switch field.xmpContainer {
|
||||
case .simple:
|
||||
simpleAttrs.append(" \(q)=\"\(esc(vals[0]))\"")
|
||||
@@ -88,6 +147,9 @@ public enum XMPTemplateIO {
|
||||
elements.append(" <\(q)>\n <\(tag)>\n\(lis)\n </\(tag)>\n </\(q)>")
|
||||
}
|
||||
}
|
||||
if !locationCreated.isEmpty {
|
||||
elements.append(" <Iptc4xmpExt:LocationCreated>\n <rdf:Bag>\n <rdf:li rdf:parseType=\"Resource\">\n\(locationCreated.joined(separator: "\n"))\n </rdf:li>\n </rdf:Bag>\n </Iptc4xmpExt:LocationCreated>")
|
||||
}
|
||||
if !contact.isEmpty {
|
||||
elements.append(" <Iptc4xmpCore:CreatorContactInfo rdf:parseType=\"Resource\">\n\(contact.joined(separator: "\n"))\n </Iptc4xmpCore:CreatorContactInfo>")
|
||||
}
|
||||
@@ -184,7 +246,8 @@ final class XMPPropertyCollector: NSObject, XMLParserDelegate {
|
||||
|
||||
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
|
||||
guard let frame = stack.popLast() else { return }
|
||||
let text = frame.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
// Keep text exactly (captions may start with blank lines), but treat pure formatting whitespace as empty.
|
||||
let text = frame.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? "" : frame.text
|
||||
if frame.ns == XMPNamespace.rdf && frame.local == "li" {
|
||||
// Hand the li value up to the nearest real property.
|
||||
if let idx = stack.lastIndex(where: { !isContainer($0.ns, $0.local) }) {
|
||||
@@ -193,6 +256,7 @@ final class XMPPropertyCollector: NSObject, XMLParserDelegate {
|
||||
return
|
||||
}
|
||||
if isContainer(frame.ns, frame.local) { return }
|
||||
if frame.ns == XMPNamespace.photomechanic && frame.liValues.isEmpty && frame.hasChildElements { return }
|
||||
if !frame.liValues.isEmpty {
|
||||
props.append(Property(namespace: frame.ns, localName: frame.local, values: frame.liValues))
|
||||
} else if !frame.hasChildElements {
|
||||
@@ -200,3 +264,42 @@ final class XMPPropertyCollector: NSObject, XMLParserDelegate {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Photo Mechanic `.snap` file: `<IPTCSettings><XMPRecord>…escaped XMP…</XMPRecord><ApplyFlags/>…`
|
||||
struct PMSnapFile {
|
||||
var xmpRecord: String?
|
||||
var applyFlags: String?
|
||||
var mergeFlags: String?
|
||||
var applyDateStyle: String?
|
||||
|
||||
static func parse(data: Data) throws -> PMSnapFile {
|
||||
final class Delegate: NSObject, XMLParserDelegate {
|
||||
var root: String?
|
||||
var current: String?
|
||||
var buffer = ""
|
||||
var result = PMSnapFile()
|
||||
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?,
|
||||
qualifiedName qName: String?, attributes attributeDict: [String: String] = [:]) {
|
||||
if root == nil { root = elementName }
|
||||
current = elementName
|
||||
buffer = ""
|
||||
}
|
||||
func parser(_ parser: XMLParser, foundCharacters string: String) { buffer += string }
|
||||
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
|
||||
switch elementName {
|
||||
case "XMPRecord": result.xmpRecord = buffer
|
||||
case "ApplyFlags": result.applyFlags = buffer
|
||||
case "MergeFlags": result.mergeFlags = buffer
|
||||
case "ApplyDateStyle": result.applyDateStyle = buffer.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
default: break
|
||||
}
|
||||
buffer = ""
|
||||
}
|
||||
}
|
||||
let d = Delegate()
|
||||
let p = XMLParser(data: data)
|
||||
p.delegate = d
|
||||
guard p.parse(), d.root == "IPTCSettings" else { throw TemplateImportError.unreadable("keine .snap-Datei") }
|
||||
return d.result
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user