diff --git a/bitchat/Models/BitchatMessage+Media.swift b/bitchat/Models/BitchatMessage+Media.swift index 6d1e9380..75762d38 100644 --- a/bitchat/Models/BitchatMessage+Media.swift +++ b/bitchat/Models/BitchatMessage+Media.swift @@ -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 } diff --git a/bitchat/Views/Media/FileAttachmentView.swift b/bitchat/Views/Media/FileAttachmentView.swift new file mode 100644 index 00000000..956f663f --- /dev/null +++ b/bitchat/Views/Media/FileAttachmentView.swift @@ -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) + } +} diff --git a/bitchat/Views/Media/MediaMessageView.swift b/bitchat/Views/Media/MediaMessageView.swift index 29711f38..9f33f5f8 100644 --- a/bitchat/Views/Media/MediaMessageView.swift +++ b/bitchat/Views/Media/MediaMessageView.swift @@ -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 + ) } } } diff --git a/bitchatTests/BitchatMessageMediaTests.swift b/bitchatTests/BitchatMessageMediaTests.swift index 2703b525..9746dba4 100644 --- a/bitchatTests/BitchatMessageMediaTests.swift +++ b/bitchatTests/BitchatMessageMediaTests.swift @@ -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)