-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathser.js
More file actions
154 lines (130 loc) · 4.18 KB
/
ser.js
File metadata and controls
154 lines (130 loc) · 4.18 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// server.js
import express from 'express';
import cors from 'cors';
import fs from 'fs';
import path from 'path';
import FormData from 'form-data';
import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
app.use(cors());
const PORT = 5000;
// Create downloads directory if it doesn't exist
const downloadPath = path.join('downloads');
if (!fs.existsSync(downloadPath)) {
fs.mkdirSync(downloadPath);
}
// Function to download file from Flask server
async function downloadFileFromFlask() {
try {
// No initial log to reduce console output
// Replace with the IP address of your Flask server
const flaskServerUrl = 'http://192.168.25.112:5000/download';
const response = await axios({
method: 'GET',
url: flaskServerUrl,
responseType: 'stream'
});
// Extract filename from content-disposition header or use default
const contentDisposition = response.headers['content-disposition'];
const filename = contentDisposition
? contentDisposition.split('filename=')[1].replace(/"/g, '')
: 'imp.md';
// Save the file
const filePath = path.join(downloadPath, filename);
return new Promise((resolve, reject) => {
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
writer.on('finish', () => {
// No success log to reduce console output
resolve(filePath);
});
writer.on('error', (err) => {
console.error('Download error:', err);
reject(err);
});
});
} catch (error) {
console.error('Download failed:', error.message);
throw error;
}
}
// Function to upload file to Pinata
async function uploadFileToPinata(filePath) {
try {
// No log about uploading to reduce console output
if (!fs.existsSync(filePath)) {
throw new Error(`${filePath} not found`);
}
const form = new FormData();
form.append('file', fs.createReadStream(filePath));
const response = await axios.post(
'https://api.pinata.cloud/pinning/pinFileToIPFS',
form,
{
maxBodyLength: 'Infinity',
headers: {
...form.getHeaders(),
pinata_api_key: process.env.PINATA_API_KEY,
pinata_secret_api_key: process.env.PINATA_SECRET_KEY,
},
}
);
const ipfsHash = response.data.IpfsHash;
// Silently delete the file after upload (no logging)
fs.unlinkSync(filePath);
return ipfsHash;
} catch (err) {
console.error('Upload failed:', err.message);
throw err;
}
}
// Route to manually trigger the process
app.get('/process', async (req, res) => {
try {
const filePath = await downloadFileFromFlask();
const ipfsHash = await uploadFileToPinata(filePath);
return res.json({ success: true, hash: ipfsHash });
} catch (err) {
console.error('Process failed:', err.message);
return res.status(500).json({ success: false, error: err.message });
}
});
// Auto-process on server start
async function autoProcess() {
try {
const filePath = await downloadFileFromFlask();
const ipfsHash = await uploadFileToPinata(filePath);
// No logs here to ensure hash is only printed once at the end
return ipfsHash;
} catch (err) {
console.error('Process failed:', err.message);
return null;
}
}
// Start the server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
// Run the auto-process when the server starts
console.log('Starting automatic download and upload process...');
autoProcess()
.then(hash => {
if (hash) {
// Print only the hash value without any additional text
console.log(hash);
} else {
console.log('Process failed');
}
})
.catch(err => {
console.error('Error in automatic process:', err);
});
});
// Endpoint to check status and get hash
app.get('/status', (req, res) => {
res.json({
server: 'running',
message: 'Server is running and auto-process was triggered on startup'
});
});