Merge 0a56cdb4e99949581ea9e47982c490b3b8d3ef42 into 04995f68e470a18d1fc99f86fa326c2a87bab4f2

This commit is contained in:
Taksh Kothari 2026-11-09 13:18:17 +00:00 committed by GitHub
commit e8ef81f03f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 157 additions and 7 deletions

View File

@ -13,6 +13,7 @@ extension BitchatMessage {
enum Media {
case voice(URL)
case image(URL)
case file(URL)
}
// Cache the directory lookup to avoid repeated FileManager calls during view rendering
@ -43,6 +44,9 @@ extension BitchatMessage {
guard let baseDirectory = Cache.shared.filesDir else { return nil }
func url(for category: MimeType.Category) -> URL? {
// Real transfers write `messagePrefix + lastPathComponent`. A
// spoofed text bubble can still carry `[file] ../prekeys/…`;
// ShareLink would then export whatever that path resolves to.
guard content.hasPrefix(category.messagePrefix),
let filename = String(content.dropFirst(category.messagePrefix.count)).trimmedOrNilIfEmpty,
Self.isSafeMediaFilename(filename)
@ -50,13 +54,15 @@ extension BitchatMessage {
return nil
}
// Check outgoing first for sent messages, incoming for received
let subdir = sender == nickname ? "\(category.mediaDir)/outgoing" : "\(category.mediaDir)/incoming"
// Construct URL directly without fileExists check (avoids blocking disk I/O in view body)
// Files are checked during playback/display, so missing files fail gracefully
let directory = baseDirectory.appendingPathComponent(subdir, isDirectory: true)
return directory.appendingPathComponent(filename)
let directory = baseDirectory
.appendingPathComponent(subdir, isDirectory: true)
.standardizedFileURL
let candidate = directory.appendingPathComponent(filename).standardizedFileURL
guard candidate.deletingLastPathComponent().path == directory.path else {
return nil
}
return candidate
}
if let url = url(for: .audio) {
@ -65,6 +71,9 @@ extension BitchatMessage {
if let url = url(for: .image) {
return .image(url)
}
if let url = url(for: .file) {
return .file(url)
}
return nil
}

View File

@ -0,0 +1,118 @@
import SwiftUI
/// Row for a received or sent `[file]` transfer. The payload is already on
/// disk (`files/incoming` or `files/outgoing`); the timeline used to render
/// it as ordinary chat text, so there was no way to open it.
struct FileAttachmentView: View {
@ThemedPalette private var palette
let url: URL
let isSending: Bool
let sendProgress: Double?
let onCancel: (() -> Void)?
let onDelete: (() -> Void)?
@State private var showDeleteConfirmation = false
private enum Strings {
static let delete = String(localized: "media.image.action.delete", comment: "Context menu action that deletes a received image")
static let deleteConfirmTitle = String(localized: "media.image.delete_confirm_title", comment: "Title of the confirmation dialog before deleting a received image")
static let deleteConfirmMessage = String(localized: "media.image.delete_confirm_message", comment: "Body of the confirmation dialog before deleting a received image")
static let cancelSend = String(localized: "media.accessibility.cancel_send", comment: "Accessibility label for the cancel button on an in-flight media send")
static let sending = String(localized: "media.image.accessibility.sending", comment: "Accessibility label for an image that is still sending")
}
var body: some View {
HStack(alignment: .center, spacing: 10) {
Image(systemName: "doc.fill")
.font(.bitchatSystem(size: 22, weight: .semibold))
.foregroundColor(palette.primary)
.accessibilityHidden(true)
VStack(alignment: .leading, spacing: 2) {
Text(verbatim: url.lastPathComponent)
.bitchatFont(size: 13, weight: .medium)
.foregroundColor(palette.primary)
.lineLimit(2)
if fileByteCount > 0 {
Text(verbatim: byteCountLabel)
.bitchatFont(size: 11)
.foregroundColor(palette.secondary)
}
}
Spacer(minLength: 8)
if isSending {
if let onCancel {
Button(action: onCancel) {
Image(systemName: "xmark")
.font(.bitchatSystem(size: 12, weight: .bold))
.padding(8)
.background(Circle().fill(Color.black.opacity(0.7)))
.foregroundColor(.white)
}
.buttonStyle(.plain)
.accessibilityLabel(Strings.cancelSend)
}
} else {
ShareLink(item: url) {
Image(systemName: "square.and.arrow.up")
.font(.bitchatSystem(size: 14, weight: .semibold))
.foregroundColor(palette.secondary)
}
}
}
.padding(10)
.background(
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(palette.secondary.opacity(0.12))
)
.overlay(alignment: .bottom) {
if let sendProgress, isSending {
GeometryReader { geo in
Rectangle()
.fill(palette.primary.opacity(0.35))
.frame(width: geo.size.width * CGFloat(max(0, min(1, sendProgress))))
}
.frame(height: 3)
.clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
}
}
.contextMenu {
if isSending {
if let onCancel {
Button(Strings.cancelSend, action: onCancel)
}
} else if onDelete != nil {
Button(Strings.delete, role: .destructive) {
showDeleteConfirmation = true
}
}
}
.confirmationDialog(
Strings.deleteConfirmTitle,
isPresented: $showDeleteConfirmation,
titleVisibility: .visible
) {
Button(Strings.delete, role: .destructive) {
onDelete?()
}
Button("common.cancel", role: .cancel) {}
} message: {
Text(verbatim: Strings.deleteConfirmMessage)
}
.accessibilityElement(children: .combine)
.accessibilityLabel(isSending ? Strings.sending : url.lastPathComponent)
.frame(maxWidth: 280)
}
private var fileByteCount: Int {
let values = try? url.resourceValues(forKeys: [.fileSizeKey])
return values?.fileSize ?? 0
}
private var byteCountLabel: String {
ByteCountFormatter.string(fromByteCount: Int64(fileByteCount), countStyle: .file)
}
}

View File

@ -123,6 +123,14 @@ struct MediaMessageView: View {
onDelete: !isFromMe ? { conversationUIModel.deleteMediaMessage(messageID: message.id) } : nil
)
.frame(maxWidth: 280)
case .file(let url):
FileAttachmentView(
url: url,
isSending: state.isSending,
sendProgress: state.progress,
onCancel: cancelAction,
onDelete: !isFromMe ? { conversationUIModel.deleteMediaMessage(messageID: message.id) } : nil
)
}
}
}

View File

@ -37,6 +37,20 @@ struct BitchatMessageMediaTests {
#expect(ownImageURL.deletingLastPathComponent().path.hasSuffix("/files/images/outgoing"))
}
@Test
func meshFileTransfersResolveInTheirOwnDirection() {
guard case .file(let incoming)? = message("[file] notes.pdf").mediaAttachment(for: "me"),
case .file(let outgoing)? = message("[file] notes.pdf", sender: "me").mediaAttachment(for: "me")
else {
Issue.record("Expected mesh file transfers to resolve to file rows")
return
}
#expect(incoming.lastPathComponent == "notes.pdf")
#expect(incoming.deletingLastPathComponent().path.hasSuffix("/files/incoming"))
#expect(outgoing.lastPathComponent == "notes.pdf")
#expect(outgoing.deletingLastPathComponent().path.hasSuffix("/files/outgoing"))
}
@Test
func nostrSendersNeverGetMediaRows() {
// Geochats, geo DMs and bridged messages carry no files, so a
@ -49,7 +63,7 @@ struct BitchatMessageMediaTests {
#expect(geoDMSender.isGeoDM)
#expect(bridgedSender.isBridge)
for content in ["[image] img_1.jpg", "[voice] note.m4a"] {
for content in ["[image] img_1.jpg", "[voice] note.m4a", "[file] notes.pdf"] {
#expect(message(content, senderPeerID: geoChatSender).mediaAttachment(for: "me") == nil)
#expect(message(content, senderPeerID: geoDMSender).mediaAttachment(for: "me") == nil)
#expect(message(content, senderPeerID: bridgedSender).mediaAttachment(for: "me") == nil)
@ -76,6 +90,7 @@ struct BitchatMessageMediaTests {
#expect(!BitchatMessage.isSafeMediaFilename(name), "\(name)")
#expect(message("[image] \(name)").mediaAttachment(for: "me") == nil, "\(name)")
#expect(message("[voice] \(name)").mediaAttachment(for: "me") == nil, "\(name)")
#expect(message("[file] \(name)").mediaAttachment(for: "me") == nil, "\(name)")
}
#expect(!BitchatMessage.isSafeMediaFilename(""))
#expect(message("[image] ").mediaAttachment(for: "me") == nil)