Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,23 @@ public struct MultimodalAttachment: Attachment, Equatable {
}
}

extension MultimodalAttachment: View {
public var body: some View {
AttachmentPreviewCard(attachment: self)
}
}

// validate file type & mime type
extension MultimodalAttachment {
public static let supportedFileExtensions: Set<String> = [
// Documents / text
"pdf", "txt", "text",
// Images
"jpg", "jpeg", "png", "webp",
"png", "jpeg", "webp",
// Video
"flv", "mov", "qt", "mpeg", "mpg", "ps", "mp4", "webm", "wmv", "3gp", "3gpp",
"flv", "mov", "mpeg", "mpegps", "mpg", "mp4", "webm", "wmv", "3gpp",
// Audio
"aac", "flac", "mp3", "m4a", "mpga", "mp4a", "opus", "pcm", "raw", "wav", "weba",
"aac", "flac", "mp3", "mpa", "mpeg", "mpga", "mp4", "opus", "pcm", "wav", "webm",
// Documents
"pdf", "txt",
]

public static func validateFileType(url: URL) throws {
Expand Down Expand Up @@ -209,38 +215,32 @@ extension MultimodalAttachment {
let fileExtension = url.pathExtension.lowercased()

switch fileExtension {
// Documents / text
case "pdf":
return "application/pdf"
case "txt", "text":
return "text/plain"

// Images
case "jpg", "jpeg":
return "image/jpeg"
case "png":
return "image/png"
case "jpeg":
return "image/jpeg"
case "webp":
return "image/webp"

// Video
case "flv":
return "video/x-flv"
case "mov", "qt":
case "mov":
return "video/quicktime"
case "mpeg":
return "video/mpeg"
case "mpegps":
return "video/mpegps"
case "mpg":
return "video/mpg"
case "ps":
return "video/mpegps"
case "mp4":
return "video/mp4"
case "webm":
return "video/webm"
case "wmv":
return "video/wmv"
case "3gp", "3gpp":
case "3gpp":
return "video/3gpp"

// Audio
Expand All @@ -249,22 +249,28 @@ extension MultimodalAttachment {
case "flac":
return "audio/flac"
case "mp3":
return "audio/mpeg"
case "m4a":
return "audio/mp3"
case "mpa":
return "audio/m4a"
case "mpeg":
return "audio/mpeg"
case "mpga":
return "audio/mpga"
case "mp4a":
case "mp4":
return "audio/mp4"
case "opus":
return "audio/opus"
case "pcm", "raw":
return "audio/pcm"
case "wav":
return "audio/wav"
case "weba":
case "webm":
return "audio/webm"

// Documents / text
case "pdf":
return "application/pdf"
case "txt":
return "text/plain"

default:
return "application/octet-stream"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ private enum AttachmentType: String {

struct AttachmentPreviewCard: View {
let attachment: MultimodalAttachment
let onRemove: (() -> Void)?

private var attachmentType: AttachmentType {
AttachmentType(mimeType: attachment.mimeType)
Expand Down Expand Up @@ -93,16 +92,8 @@ struct AttachmentPreviewCard: View {
Spacer()
}
}

if let onRemove = onRemove {
Button(action: onRemove) {
Image(systemName: "xmark.circle.fill")
.font(.system(size: 16))
.foregroundColor(.gray)
}
.buttonStyle(PlainButtonStyle())
}
}
.frame(width: 180)
.padding(12)
.background(Color(.systemGray6))
.clipShape(RoundedRectangle(cornerRadius: 12))
Expand All @@ -127,23 +118,19 @@ struct AttachmentPreviewCard: View {

struct AttachmentPreviewScrollView: View {
let attachments: [MultimodalAttachment]
var onAttachmentRemove: ((MultimodalAttachment) -> Void)? = nil

var body: some View {
if !attachments.isEmpty {
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 8) {
HStack {
ForEach(attachments) { attachment in
AttachmentPreviewCard(
attachment: attachment,
onRemove: onAttachmentRemove == nil ? nil : { onAttachmentRemove?(attachment) }
)
.frame(width: 180)
}
}
.padding(.horizontal, 16)
.padding(.horizontal, 8)
}
.frame(height: 80)
} else {
EmptyView()
}
Expand All @@ -157,31 +144,27 @@ struct AttachmentPreviewScrollView: View {
mimeType: "image/jpeg",
data: Data()
),
onRemove: { print("Image removed") }
)

AttachmentPreviewCard(
attachment: MultimodalAttachment(
mimeType: "application/pdf",
data: Data()
),
onRemove: { print("PDF removed") }
)

AttachmentPreviewCard(
attachment: MultimodalAttachment(
mimeType: "video/mp4",
data: Data()
),
onRemove: { print("Video removed") }
)

AttachmentPreviewCard(
attachment: MultimodalAttachment(
mimeType: "audio/mpeg",
data: Data()
),
onRemove: { print("Audio removed") }
)
}
.padding()
Expand Down
Loading