-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
106 lines (89 loc) · 3.12 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import express from 'express';
import fs from 'fs';
import {setTimeout} from 'timers/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 3000;
let payments = 0;
let consumerPayment = {};
// Endpoint to handle file download
app.get('/requestFile', async (req, res) => {
// Parse file hash from the query parameter
const fileHash = req.query.fileHash;
const consumerID = req.query.peerID;
if (!fileHash || !consumerID) {
// If file hash is not provided
console.log('missing field', fileHash, consumerID)
return res.status(400).send('File hash is required');
}
if (!consumerPayment.hasOwnProperty(consumerID)) {
consumerPayment[consumerID] = {}
}
if (!consumerPayment[consumerID].hasOwnProperty(fileHash)) {
consumerPayment[consumerID][fileHash] = true;
}
// Assuming your files are stored in a directory called 'files'
const filePath = path.join(__dirname, 'files', fileHash);
// Open the file for reading synchronously
const fileHandle = fs.openSync(filePath, 'r');
// Get file stats to know its size
const stat = fs.statSync(filePath);
const fileSize = stat.size;
// Set response headers
res.set({
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename=${fileHash}`,
'Content-Length': fileSize
});
// Buffer size for each chunk (you can adjust this value as needed)
const bufferSize = 1024 * 60;
// Buffer to store chunk data
const buffer = Buffer.alloc(bufferSize);
// Function to read chunks from the file and write them to the response stream
while (true) {
while (!consumerPayment[consumerID][fileHash]) {
await setTimeout(100);
console.log('waiting for pay')
}
let bytesRead;
let bytesProcessed = 0; // Track the total bytes processed
let endOfFile = false; // Flag to indicate end of file
let remainingBytes = bufferSize;
while (remainingBytes > 0) {
bytesRead = fs.readSync(fileHandle, buffer, bytesProcessed, remainingBytes, null);
if (bytesRead === 0) {
endOfFile = true; // Set endOfFile flag to true
break; // Exit inner loop if end of file
}
remainingBytes -= bytesRead;
bytesProcessed += bytesRead;
}
console.log('sent chunk')
res.write(buffer.slice(0, bytesProcessed)); // Write the chunk data to the response stream
consumerPayment[consumerID][fileHash] = false;
// If bytesRead is 0, it means we've reached the end of the file
if (endOfFile) {
// Close the file handle
fs.closeSync(fileHandle);
// End the response
res.end();
break
}
};
});
app.get('/sendTransaction', (req, res) => {
const fileHash = req.query.fileHash;
const consumerID = req.query.peerID;
payments += 1;
consumerPayment[consumerID][fileHash] = true
console.log('payed', payments)
res.setHeader('Connection', 'close');
res.end();
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});