Proxy Meshy GLB download through backend to bypass CORS
Build and Deploy / build-and-push (push) Successful in 42s
Build and Deploy / build-and-push (push) Successful in 42s
This commit is contained in:
+26
-1
@@ -531,7 +531,32 @@ app.get('/api/sparkplug/status-3d/:taskId', authenticate, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
res.json(response.data);
|
||||
const data = response.data;
|
||||
|
||||
// If generation succeeded, intercept and proxy download the model to local storage to bypass CORS
|
||||
if (data.status === 'SUCCEEDED' && data.model_urls && data.model_urls.glb) {
|
||||
const glbUrl = data.model_urls.glb;
|
||||
const localFilename = `${taskId}.glb`;
|
||||
const localFilepath = path.join(uploadsDir, localFilename);
|
||||
|
||||
// Only download if we haven't already
|
||||
if (!fs.existsSync(localFilepath)) {
|
||||
try {
|
||||
const fileRes = await axios.get(glbUrl, { responseType: 'arraybuffer' });
|
||||
fs.writeFileSync(localFilepath, fileRes.data);
|
||||
} catch (downloadErr) {
|
||||
console.error("Failed to proxy download Meshy GLB:", downloadErr);
|
||||
}
|
||||
}
|
||||
|
||||
if (fs.existsSync(localFilepath)) {
|
||||
// Overwrite the returned url with our local proxy url
|
||||
const serverUrl = `${req.protocol}://${req.get('host')}`;
|
||||
data.model_urls.glb = `${serverUrl}/uploads/${localFilename}`;
|
||||
}
|
||||
}
|
||||
|
||||
res.json(data);
|
||||
} catch (err) {
|
||||
const errorMsg = err.response?.data?.message || err.message;
|
||||
res.status(500).json({ error: errorMsg });
|
||||
|
||||
Reference in New Issue
Block a user