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
28 changes: 18 additions & 10 deletions apps/sim/app/api/files/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { captureServerEvent } from '@/lib/posthog/server'
import type { StorageContext } from '@/lib/uploads/config'
import { generateWorkspaceFileKey } from '@/lib/uploads/contexts/workspace/workspace-file-manager'
import { isImageFileType } from '@/lib/uploads/utils/file-utils'
import { isImageFileType, resolveFileType } from '@/lib/uploads/utils/file-utils'
import {
SUPPORTED_AUDIO_EXTENSIONS,
SUPPORTED_CODE_EXTENSIONS,
SUPPORTED_DOCUMENT_EXTENSIONS,
SUPPORTED_IMAGE_EXTENSIONS,
SUPPORTED_VIDEO_EXTENSIONS,
validateFileType,
} from '@/lib/uploads/utils/validation'
Expand All @@ -28,12 +29,10 @@ import {
InvalidRequestError,
} from '@/app/api/files/utils'

const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg'] as const

const ALLOWED_EXTENSIONS = new Set<string>([
...SUPPORTED_DOCUMENT_EXTENSIONS,
...SUPPORTED_CODE_EXTENSIONS,
...IMAGE_EXTENSIONS,
...SUPPORTED_IMAGE_EXTENSIONS,
...SUPPORTED_AUDIO_EXTENSIONS,
...SUPPORTED_VIDEO_EXTENSIONS,
])
Expand Down Expand Up @@ -305,10 +304,17 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
context === 'profile-pictures' ||
context === 'workspace-logos'
) {
if (context !== 'copilot' && !isImageFileType(file.type)) {
throw new InvalidRequestError(
`Only image files (JPEG, PNG, GIF, WebP, SVG) are allowed for ${context} uploads`
if (context !== 'copilot') {
const mimeType = file.type
const isGenericMime = !mimeType || mimeType === 'application/octet-stream'
const extension = originalName.split('.').pop()?.toLowerCase() ?? ''
const extensionIsImage = (SUPPORTED_IMAGE_EXTENSIONS as readonly string[]).includes(
extension
)
const isImage = isGenericMime ? extensionIsImage : isImageFileType(mimeType)
if (!isImage) {
throw new InvalidRequestError(`Only image files are allowed for ${context} uploads`)
}
}

if (context === 'workspace-logos') {
Expand Down Expand Up @@ -344,6 +350,8 @@ export const POST = withRouteHandler(async (request: NextRequest) => {

logger.info(`Uploading ${context} file: ${originalName}`)

const resolvedContentType = resolveFileType({ type: file.type, name: originalName })

const timestamp = Date.now()
const safeFileName = sanitizeFileName(originalName)
const storageKey = `${context}/${timestamp}-${safeFileName}`
Expand All @@ -362,7 +370,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
const fileInfo = await storageService.uploadFile({
file: buffer,
fileName: storageKey,
contentType: file.type,
contentType: resolvedContentType,
context,
preserveKey: true,
customKey: storageKey,
Expand All @@ -379,7 +387,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
key: fileInfo.key,
name: originalName,
size: buffer.length,
type: file.type,
type: resolvedContentType,
},
directUploadSupported: false,
}
Expand All @@ -400,7 +408,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
fileName: originalName,
fileKey: fileInfo.key,
fileSize: buffer.length,
fileType: file.type,
fileType: resolvedContentType,
},
request,
})
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/app/api/memory/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const GET = withRouteHandler(async (request: NextRequest, context: Memory
.limit(1)

if (memories.length === 0) {
return memoryEnvelopeError('Memory not found', 404)
return NextResponse.json({ success: true, data: null }, { status: 200 })
}

const mem = memories[0]
Expand Down
17 changes: 14 additions & 3 deletions apps/sim/app/workspace/[workspaceId]/files/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
SUPPORTED_AUDIO_EXTENSIONS,
SUPPORTED_CODE_EXTENSIONS,
SUPPORTED_DOCUMENT_EXTENSIONS,
SUPPORTED_IMAGE_EXTENSIONS,
SUPPORTED_VIDEO_EXTENSIONS,
} from '@/lib/uploads/utils/validation'
import type {
Expand Down Expand Up @@ -89,6 +90,7 @@ const SUPPORTED_EXTENSIONS = [
...SUPPORTED_CODE_EXTENSIONS,
...SUPPORTED_AUDIO_EXTENSIONS,
...SUPPORTED_VIDEO_EXTENSIONS,
...SUPPORTED_IMAGE_EXTENSIONS,
Comment thread
waleedlatif1 marked this conversation as resolved.
] as const

const ACCEPT_ATTR = SUPPORTED_EXTENSIONS.map((ext) => `.${ext}`).join(',')
Expand Down Expand Up @@ -125,6 +127,7 @@ function formatFileType(mimeType: string | null, filename: string): string {

if (mimeType?.startsWith('audio/')) return 'Audio'
if (mimeType?.startsWith('video/')) return 'Video'
if (mimeType?.startsWith('image/')) return 'Image'

const ext = getFileExtension(filename)
if (ext) return ext.toUpperCase()
Expand Down Expand Up @@ -246,6 +249,7 @@ export function Files() {
if (typeFilter.includes('document') && isSupportedExtension(ext)) return true
if (typeFilter.includes('audio') && isAudioFileType(f.type)) return true
if (typeFilter.includes('video') && isVideoFileType(f.type)) return true
if (typeFilter.includes('image') && f.type?.startsWith('image/')) return true
Comment thread
waleedlatif1 marked this conversation as resolved.
return false
})
}
Expand Down Expand Up @@ -926,9 +930,14 @@ export function Files() {
typeFilter.length === 0
? 'All'
: typeFilter.length === 1
? (({ document: 'Documents', audio: 'Audio', video: 'Video' } as Record<string, string>)[
typeFilter[0]
] ?? typeFilter[0])
? ((
{
document: 'Documents',
image: 'Images',
audio: 'Audio',
video: 'Video',
} as Record<string, string>
)[typeFilter[0]] ?? typeFilter[0])
: `${typeFilter.length} selected`

const sizeDisplayLabel =
Expand All @@ -954,6 +963,7 @@ export function Files() {
<Combobox
options={[
{ value: 'document', label: 'Documents' },
{ value: 'image', label: 'Images' },
{ value: 'audio', label: 'Audio' },
{ value: 'video', label: 'Video' },
]}
Expand Down Expand Up @@ -1036,6 +1046,7 @@ export function Files() {
if (typeFilter.length > 0) {
const typeLabels: Record<string, string> = {
document: 'Documents',
image: 'Images',
audio: 'Audio',
video: 'Video',
}
Expand Down
33 changes: 33 additions & 0 deletions apps/sim/lib/uploads/utils/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export const MIME_TYPE_MAPPING: Record<string, 'image' | 'document' | 'audio' |
'image/gif': 'image',
'image/webp': 'image',
'image/svg+xml': 'image', // SVG upload is allowed; createFileContent handles it separately for Claude API
'image/bmp': 'image',
'image/tiff': 'image',
'image/heic': 'image',
'image/heif': 'image',
'image/avif': 'image',
'image/x-icon': 'image',
'image/vnd.microsoft.icon': 'image',

// Documents
'application/pdf': 'document',
Expand Down Expand Up @@ -158,6 +165,10 @@ export function createFileContent(fileBuffer: Buffer, mimeType: string): Message
return null
}

if (contentType === 'image' && !CLAUDE_SUPPORTED_IMAGE_MIME_TYPES.has(mimeType.toLowerCase())) {
return null
}

return {
type: contentType,
source: {
Expand All @@ -168,6 +179,14 @@ export function createFileContent(fileBuffer: Buffer, mimeType: string): Message
}
}

const CLAUDE_SUPPORTED_IMAGE_MIME_TYPES = new Set([
'image/jpeg',
'image/jpg',
'image/png',
'image/gif',
'image/webp',
])

/**
* Extract file extension from filename
*/
Expand All @@ -184,6 +203,13 @@ const EXTENSION_TO_MIME: Record<string, string> = {
gif: 'image/gif',
webp: 'image/webp',
svg: 'image/svg+xml',
bmp: 'image/bmp',
tif: 'image/tiff',
tiff: 'image/tiff',
heic: 'image/heic',
heif: 'image/heif',
avif: 'image/avif',
ico: 'image/x-icon',

// Documents
pdf: 'application/pdf',
Expand Down Expand Up @@ -339,6 +365,13 @@ const MIME_TO_EXTENSION: Record<string, string> = {
'image/gif': 'gif',
'image/webp': 'webp',
'image/svg+xml': 'svg',
'image/bmp': 'bmp',
'image/tiff': 'tiff',
'image/heic': 'heic',
'image/heif': 'heif',
'image/avif': 'avif',
'image/x-icon': 'ico',
'image/vnd.microsoft.icon': 'ico',

// Documents
'application/pdf': 'pdf',
Expand Down
29 changes: 26 additions & 3 deletions apps/sim/lib/uploads/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,31 @@ export const SUPPORTED_AUDIO_EXTENSIONS = [

export const SUPPORTED_VIDEO_EXTENSIONS = ['mp4', 'mov', 'avi', 'mkv', 'webm'] as const

export const SUPPORTED_IMAGE_EXTENSIONS = [
'png',
'jpg',
'jpeg',
'gif',
'webp',
'svg',
'bmp',
'tif',
'tiff',
'heic',
'heif',
'avif',
'ico',
] as const

export type SupportedDocumentExtension = (typeof SUPPORTED_DOCUMENT_EXTENSIONS)[number]
export type SupportedAudioExtension = (typeof SUPPORTED_AUDIO_EXTENSIONS)[number]
export type SupportedVideoExtension = (typeof SUPPORTED_VIDEO_EXTENSIONS)[number]
export type SupportedImageExtension = (typeof SUPPORTED_IMAGE_EXTENSIONS)[number]
export type SupportedMediaExtension =
| SupportedDocumentExtension
| SupportedAudioExtension
| SupportedVideoExtension
| SupportedImageExtension

export const SUPPORTED_MIME_TYPES: Record<SupportedDocumentExtension, string[]> = {
pdf: ['application/pdf', 'application/x-pdf'],
Expand Down Expand Up @@ -180,14 +198,19 @@ const SUPPORTED_IMAGE_MIME_TYPES = [
'image/gif',
'image/webp',
'image/svg+xml',
'image/bmp',
'image/tiff',
'image/heic',
'image/heif',
'image/avif',
'image/x-icon',
'image/vnd.microsoft.icon',
]

const SUPPORTED_IMAGE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg']

export const CHAT_ACCEPT_ATTRIBUTE = [
ACCEPT_ATTRIBUTE,
...SUPPORTED_IMAGE_MIME_TYPES,
...SUPPORTED_IMAGE_EXTENSIONS,
...SUPPORTED_IMAGE_EXTENSIONS.map((ext) => `.${ext}`),
].join(',')

export interface FileValidationError {
Expand Down
14 changes: 14 additions & 0 deletions apps/sim/tools/memory/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ describe('memoryGetTool', () => {
expect(url).toBe('/api/memory/team%2Fuser%20123?workspaceId=workspace-1')
})

it('returns empty memories when key is not found (null data)', async () => {
const result = await transformResponse(
new Response(JSON.stringify({ success: true, data: null }))
)

expect(result).toEqual({
success: true,
output: {
memories: [],
message: 'No memories found',
},
})
})

it('wraps the exact memory response as a single result', async () => {
const result = await transformResponse(
new Response(
Expand Down
Loading