File size: 2,558 Bytes
8abcb4e
 
 
 
 
 
 
 
 
 
 
 
 
b0a25be
8abcb4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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}`);
});