Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): classify shared file URLs from original content

Chris3e1ffa0c0dfb
diff --git a/apps/mobile/docs/incoming-sharing.md b/apps/mobile/docs/incoming-sharing.md
index f8449de65c..bf64be7b53 100644
--- a/apps/mobile/docs/incoming-sharing.md
+++ b/apps/mobile/docs/incoming-sharing.md
@@ -41,6 +41,9 @@ new sending privileges: normal conversation hydration and authorization still ap
media and exported documents take precedence over their text/link previews. Add a
semantic adapter only when a new representation requires decoding; generic data
formats need no extension allowlist.
+ File-URL classification uses the original file's UTI or image header, never a
+ provider-wide thumbnail. Unknown extensionless documents remain original files;
+ actual extensionless raster images retain image processing.
- `plugins/with-incoming-share-formats.js`: generated Expo controller integration.
Checked patch anchors fail prebuild if the upstream template changes.
- `fingerprint.config.cjs`: explicitly hashes the share extension's Swift, strings
diff --git a/apps/mobile/plugins/incoming-share-formats.swift b/apps/mobile/plugins/incoming-share-formats.swift
index 76f57931fa..829f67d38a 100644
--- a/apps/mobile/plugins/incoming-share-formats.swift
+++ b/apps/mobile/plugins/incoming-share-formats.swift
@@ -89,9 +89,7 @@
provider.loadItem(forTypeIdentifier: identifier, options: nil) { value, _ in
// URL lifetime belongs to this callback, including loadItem fallbacks.
if let url = value as? URL {
- let image = UTType(filenameExtension: url.pathExtension)?.conforms(to: .image)
- ?? provider.hasItemConformingToTypeIdentifier(UTType.image.identifier)
- continuation.resume(returning: self.copyAndProcessFile(url: url, type: kind ?? (image ? .image : .file)))
+ continuation.resume(returning: self.copyAndProcessFile(url: url, type: kind ?? self.fileShareType(url)))
return
}
guard let kind, let data = (value as? Data) ?? (value as? UIImage)?.pngData() else {
@@ -109,3 +107,19 @@
}
}
}
+
+ private func fileShareType(_ url: URL) -> ShareType {
+ guard url.isFileURL else { return .file }
+ let access = url.startAccessingSecurityScopedResource()
+ defer { if access { url.stopAccessingSecurityScopedResource() } }
+ let type = (try? url.resourceValues(forKeys: [.contentTypeKey]))?.contentType
+ ?? UTType(filenameExtension: url.pathExtension)
+ if type?.conforms(to: .image) == true { return .image }
+ // Inspect only the original's image header, without decoding its pixels.
+ // Other representations on the provider may be unrelated thumbnails.
+ if url.pathExtension.isEmpty,
+ let source = CGImageSourceCreateWithURL(url as CFURL, [kCGImageSourceShouldCache: false] as CFDictionary),
+ let identifier = CGImageSourceGetType(source),
+ UTType(identifier as String)?.conforms(to: .image) == true { return .image }
+ return .file
+ }
diff --git a/apps/mobile/plugins/with-incoming-share-formats.js b/apps/mobile/plugins/with-incoming-share-formats.js
index 323af43c2c..611feb47ab 100644
--- a/apps/mobile/plugins/with-incoming-share-formats.js
+++ b/apps/mobile/plugins/with-incoming-share-formats.js
@@ -7,6 +7,7 @@ function patchIncomingShareFormats(source) {
if (source.split(from).length !== 2) throw new Error('expo-sharing input contract changed');
return source.replace(from, to);
};
+ source = replace('import UniformTypeIdentifiers', 'import UniformTypeIdentifiers\nimport ImageIO');
const start = source.indexOf(' private func parseProvider(');
const end = source.indexOf(' // MARK: - File Management', start);
if (start < 0 || end < 0) throw new Error('expo-sharing provider contract changed');
diff --git a/apps/mobile/scripts/test-incoming-share-formats.mjs b/apps/mobile/scripts/test-incoming-share-formats.mjs
index b4894b6fa7..fb51c65bbf 100644
--- a/apps/mobile/scripts/test-incoming-share-formats.mjs
+++ b/apps/mobile/scripts/test-incoming-share-formats.mjs
@@ -14,6 +14,7 @@ const source = readFileSync(join(dirname(fileURLToPath(import.meta.url)), '../pl
const harness = `
import Foundation
import UniformTypeIdentifiers
+import ImageIO
let testRoot = URL(fileURLWithPath: CommandLine.arguments[1])
let NSExtensionJavaScriptPreprocessingResultsKey = "NSExtensionJavaScriptPreprocessingResultsKey"
class UIImage: NSObject { func pngData() -> Data? { nil } }
@@ -51,6 +52,27 @@ final class Adapter {
try! FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: root) }
let adapter = Adapter()
+ // The file URL owns the bytes; a separate image representation may be only
+ // its thumbnail. Extensionless originals must not inherit that preview type.
+ let png = Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=")!
+ for (name, bytes, kind) in [("extensionless-document", Data("original document bytes".utf8), ShareType.file),
+ ("extensionless-image", png, ShareType.image)] {
+ let url = root.appendingPathComponent(name)
+ try! bytes.write(to: url)
+ let provider = EphemeralProvider()
+ provider.url = url
+ provider.registerItem(forTypeIdentifier: UTType.fileURL.identifier, loadHandler: { _, _, _ in })
+ provider.registerDataRepresentation(forTypeIdentifier: UTType.image.identifier, visibility: .all) { done in
+ done(png, nil); return nil
+ }
+ adapter.requireProviderCallback = true
+ guard let payload = await adapter.parse(provider), payload.type == kind,
+ let result = URL(string: payload.value) else { fatalError("Thumbnail retyped extensionless original: " + name) }
+ precondition(result.lastPathComponent == name && (try! Data(contentsOf: result)) == bytes)
+ precondition(!FileManager.default.fileExists(atPath: url.path))
+ adapter.requireProviderCallback = false
+ }
+ print("PASS: extensionless file URLs use their original content, not thumbnails")
for ext in ["md", "swift"] {
let type = UTType(filenameExtension: ext)!
precondition(type.conforms(to: .plainText))