mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 23:17:37 +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()
|
||||
})
|
||||
|
||||
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,
|
||||
writeBinaryFile,
|
||||
} from './premiere'
|
||||
import { resolveAssetUrl } from '../utils/url'
|
||||
|
||||
export interface ProxyOption {
|
||||
id: string
|
||||
@@ -321,6 +322,8 @@ export async function importAssetIntoPremiere({
|
||||
defaultFileName = `${baseName}_proxy_${resTag}.mp4`
|
||||
}
|
||||
|
||||
const resolvedDownloadUrl = resolveAssetUrl(downloadUrl, endpoint) || downloadUrl
|
||||
|
||||
// 4. Create unique file in destination folder with collision avoidance (_1, _2)
|
||||
const { file: saveFile, name: finalFileName } = await createUniqueFileInFolder(
|
||||
targetFolder,
|
||||
@@ -329,7 +332,7 @@ export async function importAssetIntoPremiere({
|
||||
|
||||
// 5. Download binary data
|
||||
onProgress?.(`Downloading ${finalFileName}...`)
|
||||
const response = await fetch(downloadUrl)
|
||||
const response = await fetch(resolvedDownloadUrl)
|
||||
if (!response.ok) {
|
||||
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', () => {
|
||||
const raw = 'http://localhost:3000/files/shumai/files/cat.webp'
|
||||
it('preserves absolute URLs intact even when hosts differ', () => {
|
||||
const localRaw = 'http://localhost:3000/files/shumai/files/cat.webp'
|
||||
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 raw = 'http://localhost:3000/files/shumai/files/cat.webp'
|
||||
const endpoint = 'https://shumai.example.com'
|
||||
expect(resolveAssetUrl(raw, endpoint)).toBe(
|
||||
'https://shumai.example.com/files/shumai/files/cat.webp',
|
||||
)
|
||||
const remoteEndpoint = 'https://shumai.example.com'
|
||||
expect(resolveAssetUrl(localRaw, remoteEndpoint)).toBe(localRaw)
|
||||
})
|
||||
|
||||
it('keeps matching absolute URLs intact', () => {
|
||||
const raw = 'http://localhost:3000/files/shumai/cat.png'
|
||||
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.
|
||||
*
|
||||
* Handles:
|
||||
* 1. Relative paths (/files/..., /api/...)
|
||||
* 2. Host/Port mismatch when the backend defaulted S3 URL to localhost:3000
|
||||
* while the client connected via 127.0.0.1 or a remote IP/domain.
|
||||
* - Relative paths (/files/..., /api/...) by prepending the endpoint.
|
||||
* - Absolute URLs (http://, https://) are preserved as-is.
|
||||
*/
|
||||
export function resolveAssetUrl(url?: string | null, endpoint?: string): string | undefined {
|
||||
if (!url) return undefined
|
||||
@@ -17,28 +16,10 @@ export function resolveAssetUrl(url?: string | null, endpoint?: string): string
|
||||
|
||||
const cleanEndpoint = endpoint.replace(/\/+$/, '')
|
||||
|
||||
let resolved = url
|
||||
|
||||
// Handle relative URLs
|
||||
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
|
||||
}
|
||||
// Handle relative URLs (e.g. /files/..., /api/...)
|
||||
if (url.startsWith('/') && !url.startsWith('//')) {
|
||||
return `${cleanEndpoint}${url}`
|
||||
}
|
||||
|
||||
return resolved
|
||||
return url
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user