NexusV1 commited on
Commit
d73e2a9
·
verified ·
1 Parent(s): 1c2124d

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +79 -0
index.js ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const multer = require('multer');
3
+ const axios = require('axios');
4
+ const fs = require('fs-extra');
5
+ const path = require('path');
6
+ const { v4: uuidv4 } = require('uuid');
7
+
8
+ const app = express();
9
+ const PORT = 7860;
10
+ const DATA_FILE = './data/files.json';
11
+
12
+ const upload = multer({ dest: 'uploads/' });
13
+
14
+ // Load or initialize DB
15
+ let db = fs.existsSync(DATA_FILE) ? JSON.parse(fs.readFileSync(DATA_FILE)) : {};
16
+ fs.ensureFileSync(DATA_FILE);
17
+
18
+ // ⏫ Upload to Litterbox
19
+ async function uploadToLitterbox(filepath, originalname) {
20
+ const formData = new FormData();
21
+ formData.append('reqtype', 'fileupload');
22
+ formData.append('time', '72h');
23
+ formData.append('fileToUpload', fs.createReadStream(filepath), originalname);
24
+
25
+ const res = await axios.post('https://litterbox.catbox.moe/resources/internals/api.php', formData, {
26
+ headers: formData.getHeaders(),
27
+ });
28
+ return res.data;
29
+ }
30
+
31
+ // 📤 Upload Route
32
+ app.post('/upload', upload.single('file'), async (req, res) => {
33
+ const file = req.file;
34
+ const id = uuidv4();
35
+ const litterUrl = await uploadToLitterbox(file.path, file.originalname);
36
+
37
+ db[id] = {
38
+ id,
39
+ filename: file.originalname,
40
+ localPath: file.path,
41
+ litterUrl,
42
+ expiresAt: Date.now() + 1000 * 60 * 60 * 24 * 3 // 3 days
43
+ };
44
+
45
+ fs.writeFileSync(DATA_FILE, JSON.stringify(db, null, 2));
46
+ res.json({ id, viewLink: `https://nexusv1-uploadx.hf.space/view/${id}`, litterUrl });
47
+ });
48
+
49
+ // 🔗 View Redirect
50
+ app.get('/view/:id', async (req, res) => {
51
+ const id = req.params.id;
52
+ const file = db[id];
53
+
54
+ if (!file) return res.status(404).send('File not found');
55
+ res.redirect(file.litterUrl);
56
+ });
57
+
58
+ // 🔄 Periodic Reupload
59
+ setInterval(async () => {
60
+ for (let id in db) {
61
+ const file = db[id];
62
+ const timeLeft = file.expiresAt - Date.now();
63
+
64
+ if (timeLeft < 1000 * 60 * 60 * 6) { // Less than 6 hours remaining
65
+ try {
66
+ console.log(`Reuploading ${file.filename} (${id})`);
67
+ const newUrl = await uploadToLitterbox(file.localPath, file.filename);
68
+ db[id].litterUrl = newUrl;
69
+ db[id].expiresAt = Date.now() + 1000 * 60 * 60 * 24 * 3;
70
+ fs.writeFileSync(DATA_FILE, JSON.stringify(db, null, 2));
71
+ console.log(`✅ Reuploaded: ${newUrl}`);
72
+ } catch (err) {
73
+ console.error(`❌ Failed to reupload ${file.filename}:`, err.message);
74
+ }
75
+ }
76
+ }
77
+ }, 1000 * 60 * 30); // Check every 30 mins
78
+
79
+ app.listen(PORT, () => console.log(`🟢 Server running on http://localhost:${PORT}`));