File tree Expand file tree Collapse file tree
apps/sim/lib/copilot/chat Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * @vitest -environment node
3+ */
4+ import { describe , expect , it } from 'vitest'
5+ import { getMothershipAttachmentPreviewUrl } from './attachment-preview'
6+
7+ describe ( 'getMothershipAttachmentPreviewUrl' , ( ) => {
8+ it ( 'asks for a preview derivative on images' , ( ) => {
9+ const url = getMothershipAttachmentPreviewUrl ( {
10+ key : 'copilot/a.heic' ,
11+ media_type : 'image/heic' ,
12+ } )
13+ expect ( url ) . toContain ( 'preview=1' )
14+ } )
15+
16+ it ( 'omits preview on videos, which have no derivative path' , ( ) => {
17+ const url = getMothershipAttachmentPreviewUrl ( { key : 'copilot/a.mp4' , media_type : 'video/mp4' } )
18+ expect ( url ) . not . toContain ( 'preview' )
19+ expect ( url ) . toContain ( 'context=mothership' )
20+ } )
21+
22+ it ( 'returns undefined for a non-renderable attachment' , ( ) => {
23+ expect (
24+ getMothershipAttachmentPreviewUrl ( { key : 'copilot/a.pdf' , media_type : 'application/pdf' } )
25+ ) . toBeUndefined ( )
26+ } )
27+
28+ it ( 'encodes the key so a slashed storage key stays one path segment' , ( ) => {
29+ const url = getMothershipAttachmentPreviewUrl ( {
30+ key : 'copilot/nested/a b.png' ,
31+ media_type : 'image/png' ,
32+ } )
33+ expect ( url ) . toContain ( encodeURIComponent ( 'copilot/nested/a b.png' ) )
34+ } )
35+ } )
Original file line number Diff line number Diff line change @@ -2,10 +2,13 @@ export function getMothershipAttachmentPreviewUrl(file: {
22 key : string
33 media_type : string
44} ) : string | undefined {
5- if ( ! file . media_type . startsWith ( 'image/' ) && ! file . media_type . startsWith ( 'video/' ) ) {
5+ const isImage = file . media_type . startsWith ( 'image/' )
6+ if ( ! isImage && ! file . media_type . startsWith ( 'video/' ) ) {
67 return undefined
78 }
8- // `preview=1`: this URL only ever backs a rendered thumbnail, so the serve route may
9- // substitute a browser-renderable derivative for a format no browser decodes (HEIC).
10- return `/api/files/serve/${ encodeURIComponent ( file . key ) } ?context=mothership&preview=1`
9+ // `preview=1` only for images: this URL backs a rendered thumbnail, so the serve route
10+ // may substitute a browser-renderable derivative for a format no browser decodes (HEIC).
11+ // A video has no derivative path, so asking would only spend a brand sniff per request.
12+ const preview = isImage ? '&preview=1' : ''
13+ return `/api/files/serve/${ encodeURIComponent ( file . key ) } ?context=mothership${ preview } `
1114}
You can’t perform that action at this time.
0 commit comments