This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
90 lines (73 loc) · 2.16 KB
/
server.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const express = require('express');
const multer = require('multer');
const AdmZip = require('adm-zip');
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const app = express();
const port = 3000;
app.use(express.static('public'));
const storage = multer.diskStorage({
destination: 'uploads/',
filename: (req, file, cb) => {
cb(null, file.originalname);
}
});
const upload = multer({ storage });
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/convert', upload.single('file'), (req, res) => {
const zip = new AdmZip(req.file.path);
const zipEntries = zip.getEntries();
const bedrockZip = new AdmZip();
const manifest = {
format_version: '2.0',
header: {
description: 'Converted texture pack from Java to Bedrock Edition',
name: 'Converted Texture Pack',
pack_id: uuidv4(),
min_engine_version: [1, 13, 0],
version: [1, 0, 0]
},
modules: [
{
type: 'resources',
uuid: uuidv4(),
version: [1, 0, 0]
}
]
};
bedrockZip.addFile('manifest.json', Buffer.from(JSON.stringify(manifest, null, 2)));
zipEntries.forEach(entry => {
const entryName = entry.entryName.toLowerCase();
if (!entryName.startsWith('assets/minecraft/textures/')) {
return;
}
const entryContent = entry.getData();
const bedrockPath = entryName
.replace('assets/minecraft/textures/', '')
.replace('.png', '.tga');
bedrockZip.addFile(bedrockPath, entryContent);
});
const outputPath = path.join('public', 'converted.mcpack');
bedrockZip.writeZip(outputPath);
res.download(outputPath, 'converted.mcpack', (err) => {
if (err) {
res.status(500).send('Error downloading the converted file');
}
fs.unlink(req.file.path, (err) => {
if (err) {
console.error('Error deleting the uploaded file:', err);
}
});
fs.unlink(outputPath, (err) => {
if (err) {
console.error('Error deleting the converted file:', err);
}
});
});
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});