Skip to content

Commit 30c9267

Browse files
fix(file-tools): preserve copied RTF and Unicode text
1 parent 1047205 commit 30c9267

10 files changed

Lines changed: 776 additions & 74 deletions

File tree

‎App/FileMint/FileContentClipboard.swift‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ enum FileContentClipboard {
1717
}
1818

1919
/// Keep the original RTF, not a lossy attributed-string re-export. The
20-
/// importer supplies only the companion plain-text representation.
20+
/// independent Core extraction must agree byte-for-byte with native import.
2121
static func prepareRTF(_ data: Data) throws -> FileContentClipboardPayload {
22-
try FileContentCopyPolicy.validateRTFStructure(data)
22+
let text = try FileContentCopyPolicy.validatedRTFText(data)
2323
let attributed: NSAttributedString
2424
do {
2525
attributed = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.rtf],
@@ -29,17 +29,26 @@ enum FileContentClipboard {
2929
attributed.enumerateAttribute(.attachment, in: NSRange(location: 0, length: attributed.length)) { value, _, _ in
3030
if value != nil { hasAttachment = true }
3131
}
32-
guard !hasAttachment else { throw FileContentCopyError.invalidRTF }
33-
try FileContentCopyPolicy.validateText(attributed.string)
34-
return FileContentClipboardPayload(text: attributed.string, rtf: data)
32+
guard !hasAttachment, attributed.string.utf8.elementsEqual(text.utf8) else {
33+
throw FileContentCopyError.invalidRTF
34+
}
35+
return FileContentClipboardPayload(text: text, rtf: data)
3536
}
3637

3738
@MainActor
3839
static func write(_ payload: FileContentClipboardPayload, to pasteboard: NSPasteboard,
3940
expectedChangeCount: Int) throws {
4041
// Build all representations before claiming ownership of the real board.
4142
let item = NSPasteboardItem()
42-
guard item.setString(payload.text, forType: .string),
43+
let textWasSet: Bool
44+
if payload.text.unicodeScalars.first?.value == 0xFEFF {
45+
// NSPasteboard's UTF-8 reader consumes one encoding BOM. Keep a
46+
// separate transport BOM when FEFF is the first content character.
47+
textWasSet = item.setData(Data([0xEF, 0xBB, 0xBF]) + Data(payload.text.utf8), forType: .string)
48+
} else {
49+
textWasSet = item.setString(payload.text, forType: .string)
50+
}
51+
guard textWasSet,
4352
payload.rtf.map({ item.setData($0, forType: .rtf) }) ?? true else {
4453
throw FileContentCopyError.clipboardWriteFailed
4554
}

‎App/FileMint/FileContentCopyController.swift‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ final class FileContentCopyController: NSObject, ObservableObject, NSWindowDeleg
196196
return text("This file has no text content. The clipboard was kept.", "文件没有文本内容,已保留原剪贴板。")
197197
case FileContentCopyError.invalidEncoding:
198198
return text("This is not supported text. Use UTF-8 or UTF-16 with a BOM.", "内容不是支持的文本,请使用 UTF-8 或带 BOM 的 UTF-16 编码。")
199-
case FileContentCopyError.unsupported, FileContentCopyError.invalidRTF, FileInfoError.unsupported:
200-
return text("This file cannot be copied completely. Supported RTF must contain text and styles only.",
201-
"无法完整拷贝此文件。支持的 RTF 只能包含文字和样式。")
199+
case FileContentCopyError.invalidRTF:
200+
return text("This RTF uses unsupported formatting or invalid text. Export as UTF-8 text and try again.",
201+
"RTF 含不支持的格式或无效文字。请导出为 UTF-8 文本后重试。")
202+
case FileContentCopyError.unsupported, FileInfoError.unsupported:
203+
return text("Only code/text files and RTF with basic text and styles are supported.",
204+
"仅支持代码、文本及含基础文字和样式的 RTF 文件。")
202205
case FileContentCopyError.clipboardChanged:
203206
return text("The clipboard changed while reading. Its newer content was kept; copy this file again if needed.",
204207
"读取期间剪贴板已变化,已保留较新的内容;如需拷贝此文件,请重新操作。")

‎CorePackage/Sources/FileMintCore/FileContentCopy.swift‎

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ public enum FileContentCopyPolicy {
6767
} else {
6868
bytes = data; encoding = .utf8
6969
}
70-
// The exact encoding round trip rejects repaired surrogate sequences.
71-
guard let text = String(data: bytes, encoding: encoding),
70+
// The standard UTF-8 decoder preserves a content FEFF after our BOM
71+
// removal. Exact byte round trips reject every repaired sequence, while
72+
// remaining available on the minimum supported macOS version.
73+
let decoded = encoding == .utf8 ? String(decoding: bytes, as: UTF8.self)
74+
: String(data: bytes, encoding: encoding)
75+
guard let text = decoded,
7276
text.data(using: encoding, allowLossyConversion: false) == bytes else {
7377
throw FileContentCopyError.invalidEncoding
7478
}
@@ -85,56 +89,10 @@ public enum FileContentCopyPolicy {
8589
}) else { throw FileContentCopyError.invalidEncoding }
8690
}
8791

88-
/// Validate bounded RTF framing before invoking the platform importer. This
89-
/// is not an RTF renderer; original bytes remain the rich representation.
90-
public static func validateRTFStructure(_ data: Data) throws {
91-
guard data.count <= maximumBytes else { throw FileContentCopyError.tooLarge }
92-
let bytes = Array(data)
93-
guard bytes.starts(with: Array("{\\rtf1".utf8)), bytes.count > 6,
94-
!(48...57).contains(bytes[6]) else { throw FileContentCopyError.invalidRTF }
95-
let unsupported: Set<String> = ["pict", "object", "objdata", "bin", "shp", "shpinst", "shppict",
96-
"nonshppict", "nextgraphic", "filetbl", "datastore", "datafield"]
97-
func letter(_ value: UInt8) -> Bool { (65...90).contains(value) || (97...122).contains(value) }
98-
func hex(_ value: UInt8) -> Bool {
99-
(48...57).contains(value) || (65...70).contains(value) || (97...102).contains(value)
100-
}
101-
var depth = 0, index = 0
102-
while index < bytes.count {
103-
let byte = bytes[index]
104-
guard byte != 0 else { throw FileContentCopyError.invalidRTF }
105-
if byte == 123 {
106-
depth += 1
107-
guard depth <= 128 else { throw FileContentCopyError.invalidRTF }
108-
} else if byte == 125 {
109-
depth -= 1
110-
guard depth >= 0 else { throw FileContentCopyError.invalidRTF }
111-
if depth == 0 {
112-
guard bytes[(index + 1)...].allSatisfy({ [9, 10, 13, 32].contains($0) }) else {
113-
throw FileContentCopyError.invalidRTF
114-
}
115-
return
116-
}
117-
} else if byte == 92 {
118-
index += 1
119-
guard index < bytes.count else { throw FileContentCopyError.invalidRTF }
120-
if bytes[index] == 39 {
121-
guard index + 2 < bytes.count, hex(bytes[index + 1]), hex(bytes[index + 2]) else {
122-
throw FileContentCopyError.invalidRTF
123-
}
124-
index += 2
125-
} else if letter(bytes[index]) {
126-
let start = index
127-
while index < bytes.count, letter(bytes[index]) { index += 1 }
128-
guard index - start <= 32 else { throw FileContentCopyError.invalidRTF }
129-
let word = String(decoding: bytes[start..<index], as: UTF8.self).lowercased()
130-
guard !unsupported.contains(word) else { throw FileContentCopyError.invalidRTF }
131-
// Revisit the delimiter (which may be a structural brace).
132-
continue
133-
}
134-
}
135-
index += 1
136-
}
137-
throw FileContentCopyError.invalidRTF
92+
/// Independently extract only the supported RTF subset. The app must compare
93+
/// this text with its native import before publishing the original bytes.
94+
public static func validatedRTFText(_ data: Data) throws -> String {
95+
try RTFContentValidator.text(in: data)
13896
}
13997
}
14098

‎CorePackage/Sources/FileMintCore/Localization.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public enum FileMintStrings {
359359
.moveHereMenuPosition: ("“Move Here” menu location", "「移到此处」菜单位置"),
360360
.copyNamesSettingsHint: ("Includes extensions, one item per line.", "包含文件后缀,多选时每项一行。"),
361361
.copyPathsSettingsHint: ("Copies full paths, one item per line; on empty space, copies the current folder path.", "拷贝完整路径,多选时每项一行;空白处右键则拷贝当前文件夹路径。"),
362-
.copyContentsSettingsHint: ("Copies one code, text or text-only RTF file without opening it. Preserves RTF styles. Up to 8 MiB.", "无需打开文件,完整拷贝单个代码、文本或纯文字 RTF 文件;保留 RTF 样式,上限 8 MiB。"),
362+
.copyContentsSettingsHint: ("Copies one code/text file or RTF with basic text and styles. Advanced RTF content is unsupported. Up to 8 MiB.", "无需打开文件,完整拷贝单个代码、文本或含基础文字和样式的 RTF;不支持复杂 RTF 内容,上限 8 MiB。"),
363363
.moveSettingsHint: ("Select items, then right-click their destination to move them.", "先选择项目,再到目标文件夹右键完成移动。"),
364364
.deleteSettingsHint: ("Bypasses Trash. Deletion cannot be undone.", "不经过废纸篓,删除后无法撤销。"),
365365
.showInMainMenu: ("Show in main menu", "显示在一级菜单"),

0 commit comments

Comments
 (0)