NexusV1 commited on
Commit
c967f83
Β·
verified Β·
1 Parent(s): d789898

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +58 -32
index.js CHANGED
@@ -9,6 +9,7 @@ const { v4: uuidv4 } = require('uuid');
9
  const app = express();
10
  const PORT = 7860;
11
  const DATA_FILE = './data/files.json';
 
12
 
13
  const upload = multer({ dest: 'uploads/' });
14
 
@@ -21,65 +22,90 @@ app.use(express.static(path.join(__dirname, 'public')));
21
  app.get('/', (req, res) => {
22
  res.sendFile(path.join(__dirname, 'public/index.html'));
23
  });
24
- // ⏫ Upload to Litterbox
25
- async function uploadToLitterbox(filepath, originalname) {
 
 
26
  const formData = new FormData();
27
- formData.append('reqtype', 'fileupload');
28
- formData.append('time', '72h');
29
- formData.append('fileToUpload', fs.createReadStream(filepath), originalname);
30
 
31
- const res = await axios.post('https://litterbox.catbox.moe/resources/internals/api.php', formData, {
32
- headers: formData.getHeaders(),
33
- });
34
- return res.data;
 
 
 
 
 
 
 
 
 
 
 
 
35
  }
36
 
37
  // πŸ“€ Upload Route
38
  app.post('/upload', upload.single('file'), async (req, res) => {
39
  const file = req.file;
40
  const id = uuidv4();
41
- const litterUrl = await uploadToLitterbox(file.path, file.originalname);
42
 
43
- db[id] = {
44
- id,
45
- filename: file.originalname,
46
- localPath: file.path,
47
- litterUrl,
48
- expiresAt: Date.now() + 1000 * 60 * 60 * 24 * 3 // 3 days
49
- };
50
 
51
- fs.writeFileSync(DATA_FILE, JSON.stringify(db, null, 2));
52
- res.json({ id, viewLink: `https://nexusv1-uploadx.hf.space/view/${id}`, litterUrl });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  });
54
 
55
- // πŸ”— View Redirect
56
  app.get('/view/:id', async (req, res) => {
57
  const id = req.params.id;
58
  const file = db[id];
59
 
60
- if (!file) return res.status(404).send('File not found');
61
- res.redirect(file.litterUrl);
 
 
 
62
  });
63
 
64
- // πŸ”„ Periodic Reupload
65
  setInterval(async () => {
66
  for (let id in db) {
67
  const file = db[id];
68
- const timeLeft = file.expiresAt - Date.now();
69
-
70
- if (timeLeft < 1000 * 60 * 60 * 6) { // Less than 6 hours remaining
71
  try {
72
- console.log(`Reuploading ${file.filename} (${id})`);
73
- const newUrl = await uploadToLitterbox(file.localPath, file.filename);
74
- db[id].litterUrl = newUrl;
75
- db[id].expiresAt = Date.now() + 1000 * 60 * 60 * 24 * 3;
 
76
  fs.writeFileSync(DATA_FILE, JSON.stringify(db, null, 2));
77
- console.log(`βœ… Reuploaded: ${newUrl}`);
78
  } catch (err) {
79
  console.error(`❌ Failed to reupload ${file.filename}:`, err.message);
80
  }
81
  }
82
  }
83
- }, 1000 * 60 * 30); // Check every 30 mins
84
 
85
  app.listen(PORT, () => console.log(`🟒 Server running on http://localhost:${PORT}`));
 
9
  const app = express();
10
  const PORT = 7860;
11
  const DATA_FILE = './data/files.json';
12
+ const MAX_CATBOX_SIZE = 200 * 1024 * 1024; // 200 MB
13
 
14
  const upload = multer({ dest: 'uploads/' });
15
 
 
22
  app.get('/', (req, res) => {
23
  res.sendFile(path.join(__dirname, 'public/index.html'));
24
  });
25
+
26
+ // πŸ“€ Upload to Catbox or Litterbox
27
+ async function uploadToCatboxOrLitterbox(filepath, originalname) {
28
+ const fileSize = (await fs.stat(filepath)).size;
29
  const formData = new FormData();
30
+ const fileStream = fs.createReadStream(filepath);
 
 
31
 
32
+ if (fileSize <= MAX_CATBOX_SIZE) {
33
+ formData.append('reqtype', 'fileupload');
34
+ formData.append('fileToUpload', fileStream, originalname);
35
+ const res = await axios.post('https://catbox.moe/user/api.php', formData, {
36
+ headers: formData.getHeaders()
37
+ });
38
+ return { url: res.data, type: 'catbox' };
39
+ } else {
40
+ formData.append('reqtype', 'fileupload');
41
+ formData.append('time', '72h');
42
+ formData.append('fileToUpload', fileStream, originalname);
43
+ const res = await axios.post('https://litterbox.catbox.moe/resources/internals/api.php', formData, {
44
+ headers: formData.getHeaders()
45
+ });
46
+ return { url: res.data, type: 'litterbox' };
47
+ }
48
  }
49
 
50
  // πŸ“€ Upload Route
51
  app.post('/upload', upload.single('file'), async (req, res) => {
52
  const file = req.file;
53
  const id = uuidv4();
 
54
 
55
+ try {
56
+ const { url, type } = await uploadToCatboxOrLitterbox(file.path, file.originalname);
 
 
 
 
 
57
 
58
+ db[id] = {
59
+ id,
60
+ filename: file.originalname,
61
+ localPath: file.path,
62
+ externalUrl: url,
63
+ source: type,
64
+ expiresAt: type === 'litterbox' ? Date.now() + 1000 * 60 * 60 * 24 * 3 : null
65
+ };
66
+
67
+ fs.writeFileSync(DATA_FILE, JSON.stringify(db, null, 2));
68
+ res.json({
69
+ id,
70
+ viewLink: `https://nexusv1-uploadx.hf.space/view/${id}`,
71
+ externalUrl: url,
72
+ source: type
73
+ });
74
+ } catch (e) {
75
+ res.status(500).json({ error: 'Upload failed', details: e.message });
76
+ }
77
  });
78
 
79
+ // πŸ”— View Route – No Redirect
80
  app.get('/view/:id', async (req, res) => {
81
  const id = req.params.id;
82
  const file = db[id];
83
 
84
+ if (!file || !(await fs.pathExists(file.localPath))) {
85
+ return res.status(404).send('File not found');
86
+ }
87
+
88
+ res.download(file.localPath, file.filename);
89
  });
90
 
91
+ // πŸ”„ Periodic Reupload for Litterbox files
92
  setInterval(async () => {
93
  for (let id in db) {
94
  const file = db[id];
95
+ if (file.source === 'litterbox' && file.expiresAt && (file.expiresAt - Date.now() < 1000 * 60 * 60 * 6)) {
 
 
96
  try {
97
+ console.log(`♻️ Reuploading ${file.filename} (${id})`);
98
+ const { url, type } = await uploadToCatboxOrLitterbox(file.localPath, file.filename);
99
+ db[id].externalUrl = url;
100
+ db[id].source = type;
101
+ db[id].expiresAt = type === 'litterbox' ? Date.now() + 1000 * 60 * 60 * 24 * 3 : null;
102
  fs.writeFileSync(DATA_FILE, JSON.stringify(db, null, 2));
103
+ console.log(`βœ… Reuploaded: ${url}`);
104
  } catch (err) {
105
  console.error(`❌ Failed to reupload ${file.filename}:`, err.message);
106
  }
107
  }
108
  }
109
+ }, 1000 * 60 * 30); // every 30 mins
110
 
111
  app.listen(PORT, () => console.log(`🟒 Server running on http://localhost:${PORT}`));