Spaces:
Running
Running
| import express from 'express'; | |
| import multer from 'multer'; | |
| import fs from 'fs'; | |
| import path from 'path'; | |
| import { v4 as uuidv4 } from 'uuid'; | |
| import { convertGlbToSkp } from './index'; | |
| const app = express(); | |
| const port = 7860; // Standard Hugging Face Port | |
| const upload = multer({ dest: '/tmp/uploads/' }); | |
| app.get('/', (req, res) => { | |
| res.send('File Processor Service Active.'); | |
| }); | |
| app.post('/convert', upload.single('file'), async (req: any, res: any) => { | |
| if (!req.file) { | |
| return res.status(400).json({ error: 'No file uploaded' }); | |
| } | |
| const id = uuidv4(); | |
| const inputPath = req.file.path; | |
| const outputPath = path.join('/tmp', `${id}.skp`); | |
| try { | |
| console.log(`Received file: ${req.file.originalname} (${req.file.size} bytes)`); | |
| const textureFormat = req.query.textureFormat as 'jpeg' | 'png' | undefined; | |
| const stats = await convertGlbToSkp(inputPath, outputPath, { textureFormat }); | |
| res.setHeader('x-debug-vertices', stats.vertices.toString()); | |
| res.setHeader('x-debug-faces', stats.faces.toString()); | |
| res.setHeader('x-debug-textured-faces', stats.textured_faces.toString()); | |
| res.setHeader('x-debug-materials', stats.materials.toString()); | |
| res.setHeader('x-debug-textures', stats.textures.toString()); | |
| res.setHeader('x-debug-texture-dimensions', `${stats.texture_width}x${stats.texture_height}`); | |
| res.setHeader('x-debug-texture-bytes', stats.texture_bytes.toString()); | |
| res.setHeader('x-debug-geometry-bytes', stats.estimated_geometry_bytes.toString()); | |
| res.setHeader('x-debug-overhead-bytes', (stats.skp_overhead_bytes || 0).toString()); | |
| const fileSize = fs.statSync(outputPath).size; | |
| res.setHeader('x-debug-file-size', fileSize.toString()); | |
| // Allow CORS for the frontend | |
| res.setHeader('Access-Control-Allow-Origin', '*'); | |
| res.setHeader('Access-Control-Expose-Headers', '*'); | |
| res.download(outputPath, 'converted.skp', (err) => { | |
| if (err) console.error('Download error:', err); | |
| // Cleanup | |
| try { fs.unlinkSync(inputPath); } catch (e) { } | |
| try { fs.unlinkSync(outputPath); } catch (e) { } | |
| }); | |
| } catch (err: any) { | |
| console.error('Conversion Failed:', err); | |
| res.status(500).json({ error: err.message, logs: err.toString() }); | |
| try { fs.unlinkSync(inputPath); } catch (e) { } | |
| } | |
| }); | |
| app.listen(port, () => { | |
| console.log(`Microservice listening at http://localhost:${port}`); | |
| }); | |