mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
fix(premiere-uxp): preserve remote cloud storage presigned URLs in asset resolution (#408)
- Stop rewriting absolute URLs in resolveAssetUrl to prevent overwriting cloud storage hosts (e.g. Cloudflare R2, S3) with the plugin endpoint - Prepend endpoint to relative paths only (e.g. /files/..., /api/...) - Resolve download URLs through resolveAssetUrl before fetch in importAssetIntoPremiere - Update unit tests to ensure absolute URLs remain intact and relative download paths resolve correctly - Rebuild and package shumai-premiere.ccx
This commit is contained in:
Binary file not shown.
@@ -555,5 +555,65 @@ describe('import service', () => {
|
|||||||
])
|
])
|
||||||
fetchSpy.mockRestore()
|
fetchSpy.mockRestore()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('resolves relative download URL with endpoint before fetching', async () => {
|
||||||
|
// Mocking incomplete Premiere Project object in unit test
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
const mockProject = {} as any
|
||||||
|
vi.spyOn(premiereModule, 'getActiveProject').mockResolvedValue(mockProject)
|
||||||
|
|
||||||
|
const mockSaveFile = {
|
||||||
|
isFile: true as const,
|
||||||
|
isFolder: false as const,
|
||||||
|
name: 'Sample.mov',
|
||||||
|
nativePath: '/Users/test/Downloads/Sample.mov',
|
||||||
|
write: vi.fn().mockResolvedValue(undefined),
|
||||||
|
}
|
||||||
|
const mockFolder = {
|
||||||
|
isFile: false as const,
|
||||||
|
isFolder: true as const,
|
||||||
|
name: 'Downloads',
|
||||||
|
nativePath: '/Users/test/Downloads',
|
||||||
|
createFile: vi.fn().mockResolvedValue(mockSaveFile),
|
||||||
|
getEntries: vi.fn().mockResolvedValue([]),
|
||||||
|
}
|
||||||
|
vi.spyOn(premiereModule, 'promptSelectFolder').mockResolvedValue(mockFolder)
|
||||||
|
vi.spyOn(premiereModule, 'writeBinaryFile').mockResolvedValue(undefined)
|
||||||
|
vi.spyOn(premiereModule, 'importFilesIntoProject').mockResolvedValue(true)
|
||||||
|
|
||||||
|
vi.spyOn(clientModule, 'getShumaiClient').mockReturnValue({
|
||||||
|
api: {
|
||||||
|
files: {
|
||||||
|
'download-links': {
|
||||||
|
$post: vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => ({
|
||||||
|
files: [{ id: 'asset-1', name: 'Sample.mov', url: '/api/upload/local?key=test' }],
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
} as any)
|
||||||
|
|
||||||
|
const dummyBuffer = new ArrayBuffer(8)
|
||||||
|
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
arrayBuffer: async () => dummyBuffer,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
} as any)
|
||||||
|
|
||||||
|
const result = await importAssetIntoPremiere({
|
||||||
|
endpoint: 'http://test:3000',
|
||||||
|
apiKey: 'key',
|
||||||
|
asset: mockAsset,
|
||||||
|
type: 'raw',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result.success).toBe(true)
|
||||||
|
expect(fetchSpy).toHaveBeenCalledWith('http://test:3000/api/upload/local?key=test')
|
||||||
|
fetchSpy.mockRestore()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
promptSelectFolder,
|
promptSelectFolder,
|
||||||
writeBinaryFile,
|
writeBinaryFile,
|
||||||
} from './premiere'
|
} from './premiere'
|
||||||
|
import { resolveAssetUrl } from '../utils/url'
|
||||||
|
|
||||||
export interface ProxyOption {
|
export interface ProxyOption {
|
||||||
id: string
|
id: string
|
||||||
@@ -321,6 +322,8 @@ export async function importAssetIntoPremiere({
|
|||||||
defaultFileName = `${baseName}_proxy_${resTag}.mp4`
|
defaultFileName = `${baseName}_proxy_${resTag}.mp4`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const resolvedDownloadUrl = resolveAssetUrl(downloadUrl, endpoint) || downloadUrl
|
||||||
|
|
||||||
// 4. Create unique file in destination folder with collision avoidance (_1, _2)
|
// 4. Create unique file in destination folder with collision avoidance (_1, _2)
|
||||||
const { file: saveFile, name: finalFileName } = await createUniqueFileInFolder(
|
const { file: saveFile, name: finalFileName } = await createUniqueFileInFolder(
|
||||||
targetFolder,
|
targetFolder,
|
||||||
@@ -329,7 +332,7 @@ export async function importAssetIntoPremiere({
|
|||||||
|
|
||||||
// 5. Download binary data
|
// 5. Download binary data
|
||||||
onProgress?.(`Downloading ${finalFileName}...`)
|
onProgress?.(`Downloading ${finalFileName}...`)
|
||||||
const response = await fetch(downloadUrl)
|
const response = await fetch(resolvedDownloadUrl)
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Failed to download file (HTTP ${response.status})`)
|
throw new Error(`Failed to download file (HTTP ${response.status})`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,22 +22,24 @@ describe('resolveAssetUrl', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('rewrites host when server presigned with localhost but client connected via 127.0.0.1', () => {
|
it('preserves absolute URLs intact even when hosts differ', () => {
|
||||||
const raw = 'http://localhost:3000/files/shumai/files/cat.webp'
|
const localRaw = 'http://localhost:3000/files/shumai/files/cat.webp'
|
||||||
const endpoint = 'http://127.0.0.1:3000'
|
const endpoint = 'http://127.0.0.1:3000'
|
||||||
expect(resolveAssetUrl(raw, endpoint)).toBe('http://127.0.0.1:3000/files/shumai/files/cat.webp')
|
expect(resolveAssetUrl(localRaw, endpoint)).toBe(localRaw)
|
||||||
})
|
|
||||||
|
|
||||||
it('rewrites host and protocol when client connected via remote HTTPS server', () => {
|
const remoteEndpoint = 'https://shumai.example.com'
|
||||||
const raw = 'http://localhost:3000/files/shumai/files/cat.webp'
|
expect(resolveAssetUrl(localRaw, remoteEndpoint)).toBe(localRaw)
|
||||||
const endpoint = 'https://shumai.example.com'
|
|
||||||
expect(resolveAssetUrl(raw, endpoint)).toBe(
|
|
||||||
'https://shumai.example.com/files/shumai/files/cat.webp',
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('keeps matching absolute URLs intact', () => {
|
it('keeps matching absolute URLs intact', () => {
|
||||||
const raw = 'http://localhost:3000/files/shumai/cat.png'
|
const raw = 'http://localhost:3000/files/shumai/cat.png'
|
||||||
expect(resolveAssetUrl(raw, 'http://localhost:3000')).toBe(raw)
|
expect(resolveAssetUrl(raw, 'http://localhost:3000')).toBe(raw)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('preserves remote cloud storage presigned URLs without rewriting host', () => {
|
||||||
|
const r2Url =
|
||||||
|
'https://04e37a3c563610bccea6a3ce6f8618ca.r2.cloudflarestorage.com/shumai/files/01KYKNT8NEJ3Q1G6A6K08M2XHC/Float%2002-480p.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Signature=8112ab30'
|
||||||
|
const endpoint = 'https://staging.shumai.one'
|
||||||
|
expect(resolveAssetUrl(r2Url, endpoint)).toBe(r2Url)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,9 +7,8 @@
|
|||||||
* the user-configured Shumai endpoint.
|
* the user-configured Shumai endpoint.
|
||||||
*
|
*
|
||||||
* Handles:
|
* Handles:
|
||||||
* 1. Relative paths (/files/..., /api/...)
|
* - Relative paths (/files/..., /api/...) by prepending the endpoint.
|
||||||
* 2. Host/Port mismatch when the backend defaulted S3 URL to localhost:3000
|
* - Absolute URLs (http://, https://) are preserved as-is.
|
||||||
* while the client connected via 127.0.0.1 or a remote IP/domain.
|
|
||||||
*/
|
*/
|
||||||
export function resolveAssetUrl(url?: string | null, endpoint?: string): string | undefined {
|
export function resolveAssetUrl(url?: string | null, endpoint?: string): string | undefined {
|
||||||
if (!url) return undefined
|
if (!url) return undefined
|
||||||
@@ -17,28 +16,10 @@ export function resolveAssetUrl(url?: string | null, endpoint?: string): string
|
|||||||
|
|
||||||
const cleanEndpoint = endpoint.replace(/\/+$/, '')
|
const cleanEndpoint = endpoint.replace(/\/+$/, '')
|
||||||
|
|
||||||
let resolved = url
|
// Handle relative URLs (e.g. /files/..., /api/...)
|
||||||
|
if (url.startsWith('/') && !url.startsWith('//')) {
|
||||||
// Handle relative URLs
|
return `${cleanEndpoint}${url}`
|
||||||
if (url.startsWith('/')) {
|
|
||||||
resolved = `${cleanEndpoint}${url}`
|
|
||||||
} else {
|
|
||||||
// Handle absolute URLs with host mismatch
|
|
||||||
try {
|
|
||||||
const parsedUrl = new URL(url)
|
|
||||||
const parsedEndpoint = new URL(cleanEndpoint)
|
|
||||||
|
|
||||||
// If host differs (e.g. localhost vs 127.0.0.1 or remote server address)
|
|
||||||
if (parsedUrl.host !== parsedEndpoint.host) {
|
|
||||||
parsedUrl.protocol = parsedEndpoint.protocol
|
|
||||||
parsedUrl.hostname = parsedEndpoint.hostname
|
|
||||||
parsedUrl.port = parsedEndpoint.port
|
|
||||||
resolved = parsedUrl.toString()
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// If URL parsing fails, fallback to raw url
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolved
|
return url
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user