-
-
Notifications
You must be signed in to change notification settings - Fork 607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rendering fails when using remote pictures #338
Comments
Kinda similar to #234 |
What might work is defining a proxy in I would add a new helper utility that wraps a src string, like const originalSrc: string;
return `/proxy/${encodeURIComponent(originalSrc)}` I will have to look into how proxying works with vite. |
Proof of Concept worksClick to expand / Outdated: I basically implemented a Plugin for Vite which adds some a Middleware to Vite's Server that intercepts any calls starting with `/proxy/`.const proxyPlugin = (): Plugin => {
return {
name: "proxy-server",
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (!req.url || !req.url.startsWith("/proxy/")) {
return next()
}
if(req.method !== "GET") {
const msg = "Only GET requests are allowed"
res.writeHead(400, msg)
res.write(msg)
res.end()
return;
}
// Extract destination, e.g
function extractDestinationUrl(url: string) {
try {
const withoutPrefix = url.replace("/proxy/", "")
const asUrl = new URL(decodeURIComponent(withoutPrefix))
console.log(`"${asUrl.protocol}"`)
if(asUrl.protocol !== "http:" && asUrl.protocol !== "https:") {
throw new Error("Only supported protocols are http and https") // TODO: Is this enough to prevent access to file:// ?
}
return [asUrl, undefined] as const
}
catch(err) {
return [undefined, err+""] as const
}
}
const [sourceUrl, error] = extractDestinationUrl(req.url)
if (error || !sourceUrl) {
res.writeHead(400, error)
res.write(error)
res.end()
return
}
// Call URL on behalf of callee
const proxy_req = http.request( {
hostname: sourceUrl.hostname,
path: sourceUrl.pathname,
method: "get"
}, (proxy_res) => {
let buffer: any[] = []
console.log("ping")
proxy_res.on("data", (chunk) => {
buffer.push(chunk )
})
proxy_res.on("error", () => {
res.statusCode = 400
res.statusMessage = "Proxying failed"
res.end()
})
proxy_res.on("close", () => {
const body = Buffer.concat(buffer)
// only proxy headers we actually need
const allowedHeaders = [
"content-type", "content-length", "cache-control"
]
for(const header of allowedHeaders) {
const headerValue = proxy_res.headers[header]
if(!headerValue) {
continue // Header does not exist. Ignore
}
res.setHeader(header, headerValue)
}
// Additionally set the X-Proxied-Url Header for debugging
res.setHeader("X-Proxied-Url", sourceUrl.toString())
res.end(body)
console.log("done")
})
}).end()
})
}
}
} |
Yesterday I took the time to create a Plugin that provides a proxy server, and a utility function to wrap remote requests, so that they get sent to the proxy instead. It's basically what I shown in the Proof of Concept, with more checks, configuration and using Axios instead of raw http https://github.com/WaldemarLehner/motion-canvas-cors-proxy @Ross-Esmond mentioned it might be a good idea to bring this into the core repo instead. Relevant discussion on Discord: https://discord.com/channels/1071029581009657896/1074755895529062531/1075236819597267044 |
The image in the example should work, since it's served with the "Access-Control-Allow-Origin: *" header. I added |
…otion-canvas#338) Apply proposed changes Co-authored-by: Jacob <64662184+aarthificial@users.noreply.github.com>
fix(vite-plugin): fix remote sources breaking Rendering by implementing proxy(motion-canvas#338) feat(core): add viaProxy Utility to rewrite requests to proxy (motion-canvas#338) Apply proposed changes Co-authored-by: Jacob <64662184+aarthificial@users.noreply.github.com>
fix(vite-plugin): fix remote sources breaking Rendering by implementing proxy(motion-canvas#338) feat(core): add viaProxy Utility to rewrite requests to proxy (motion-canvas#338) Apply proposed changes Co-authored-by: Jacob <64662184+aarthificial@users.noreply.github.com>
fix(vite-plugin): fix remote sources breaking Rendering by implementing proxy(motion-canvas#338) feat(core): add viaProxy Utility to rewrite requests to proxy (motion-canvas#338) Apply proposed changes Co-authored-by: Jacob <64662184+aarthificial@users.noreply.github.com>
Describe the bug
If a scene has images from remote source, trying to render will result in an error:
To Reproduce
Have a scene with a remote Image, then try to render.
Additional context
Error occurs both on Firefox and Chromium.
The text was updated successfully, but these errors were encountered: