-
Notifications
You must be signed in to change notification settings - Fork 1
/
flatten.ts
88 lines (70 loc) · 3 KB
/
flatten.ts
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
import express, { Request, Response } from 'express';
import multer from 'multer';
import { Level } from 'level';
import crypto from 'crypto';
import cors from 'cors';
const app = express();
app.use(cors());
const db: Level = new Level("./data"); // Change './data' to your desired LevelDB storage path
// Set up multer for handling file uploads
const upload = multer();
// Function to generate Ethereum-style hash
function generateEthereumStyleHash(data: string): string {
const hash = crypto.createHash('sha256').update(data).digest('hex');
return 'nlg' + hash.slice(0, 40);
}
// Endpoint to store images
app.post('/upload/image', upload.single('image'), async (req: Request, res: Response) => {
try {
if (!req.file) {
return res.status(400).json({ message: 'No file uploaded' });
}
const { buffer: imageBuffer, mimetype, originalname } = req.file;
// Generate SHA256 hash of the file data
const hash = generateEthereumStyleHash(imageBuffer.toString('base64'));
// Store the file data and metadata in LevelDB
await db.put(hash, JSON.stringify({ mimetype, originalname, data: imageBuffer.toString('base64') }));
return res.status(200).json({ hash });
} catch (error) {
console.error('Error uploading image:', error);
res.status(500).json({ message: 'Internal server error' });
}
});
// Endpoint to store videos
app.post('/upload/video', upload.single('video'), async (req: Request, res: Response) => {
try {
if (!req.file) {
return res.status(400).json({ message: 'No file uploaded' });
}
const { buffer: videoBuffer, mimetype, originalname } = req.file;
// Generate SHA256 hash of the file data
const hash = generateEthereumStyleHash(videoBuffer.toString('base64'));
// Store the file data and metadata in LevelDB
await db.put(hash, JSON.stringify({ mimetype, originalname, data: videoBuffer.toString('base64') }));
return res.status(200).json({ hash });
} catch (error) {
console.error('Error uploading video:', error);
res.status(500).json({ message: 'Internal server error' });
}
});
// Gateway endpoint to access images and videos
app.get('/gateway/:hash', async (req: Request, res: Response) => {
try {
const { hash } = req.params;
// Check if the content exists in LevelDB
const metadataStr = await db.get(hash);
const metadata = JSON.parse(metadataStr);
// Decode the file data from Base64
const fileBuffer = Buffer.from(metadata.data, 'base64');
// Set the appropriate content type and send the file content
res.set('Content-Type', metadata.mimetype);
res.send(fileBuffer);
} catch (error) {
console.error('Error retrieving content:', error);
res.status(404).json({ message: 'Content not found' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});