fullpwerr commited on
Commit
fffecb9
·
verified ·
1 Parent(s): 05cdd13

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +18 -17
app.js CHANGED
@@ -6,16 +6,18 @@ const path = require("path");
6
  const os = require("os");
7
  const { execSync } = require("child_process");
8
 
9
-
10
  const FILE_DIR = path.join(__dirname, "files");
11
- const EXPIRATION_TIME = 24 * 60 * 60 * 1000;
12
- const DEFAULT_EXPIRATION = 24 * 60 * 60 * 1000; // Default 24 jam// 24 jam dalam milidetik
13
  const META_FILE = path.join(__dirname, "files.json");
14
 
15
- // Load metadata file (kalau ada)
16
- let fileData = fs.existsSync(META_FILE) ? JSON.parse(fs.readFileSync(META_FILE)) : {};
17
  if (fs.existsSync(META_FILE)) {
18
- fileData = JSON.parse(fs.readFileSync(META_FILE));
 
 
 
 
 
19
  }
20
 
21
  function formatRemainingTime(expiresAt) {
@@ -30,7 +32,6 @@ function formatRemainingTime(expiresAt) {
30
  return `${hours}h ${minutes}m ${seconds}s`;
31
  }
32
 
33
-
34
  const app = express();
35
  const port = process.env.PORT || 7860;
36
 
@@ -61,7 +62,7 @@ app.post("/upload", upload.single("file"), (req, res) => {
61
  res.json({
62
  success: true,
63
  file_url: `${req.protocol}://${req.get("host")}/files/${filePath}`,
64
- expires_in: formatRemainingTime(expiresAt) // Format sesuai sisa waktu
65
  });
66
  });
67
 
@@ -72,7 +73,7 @@ app.get("/files", (req, res) => {
72
  const fileList = files.map(filename => ({
73
  filename,
74
  file_url: `${req.protocol}://${req.get("host")}/files/${filename}`,
75
- expires_in: formatRemainingTime(fileData[filename] || 0)
76
  }));
77
 
78
  res.json({ success: true, files: fileList });
@@ -142,7 +143,6 @@ app.get("/", (req, res) => {
142
  });
143
  });
144
 
145
-
146
  app.get("/status", (req, res) => {
147
  const uptime = process.uptime();
148
  const memoryUsage = process.memoryUsage();
@@ -172,7 +172,6 @@ app.get("/status", (req, res) => {
172
  });
173
  });
174
 
175
-
176
  setInterval(() => {
177
  const now = Date.now();
178
  let updatedFileData = {};
@@ -180,20 +179,22 @@ setInterval(() => {
180
  Object.keys(fileData).forEach(filename => {
181
  if (fileData[filename] <= now) {
182
  const filePath = path.join(FILE_DIR, filename);
183
- if (fs.existsSync(filePath)) {
184
- fs.unlinkSync(filePath);
185
- console.log(`🗑️ File expired dihapus: ${filename}`);
 
 
 
 
186
  }
187
  } else {
188
  updatedFileData[filename] = fileData[filename];
189
  }
190
  });
191
 
192
- // Update metadata file
193
  fileData = updatedFileData;
194
  fs.writeFileSync(META_FILE, JSON.stringify(fileData));
195
- }, 60 * 60 * 1000); // Cek tiap 1 jam
196
-
197
 
198
  app.listen(port, () => {
199
  console.log("Listening on http://localhost:" + port);
 
6
  const os = require("os");
7
  const { execSync } = require("child_process");
8
 
 
9
  const FILE_DIR = path.join(__dirname, "files");
10
+ const DEFAULT_EXPIRATION = 24 * 60 * 60 * 1000; // Default 24 jam dalam milidetik
 
11
  const META_FILE = path.join(__dirname, "files.json");
12
 
13
+ let fileData = {};
 
14
  if (fs.existsSync(META_FILE)) {
15
+ try {
16
+ fileData = JSON.parse(fs.readFileSync(META_FILE));
17
+ } catch (err) {
18
+ console.error("Error parsing files.json:", err);
19
+ fileData = {};
20
+ }
21
  }
22
 
23
  function formatRemainingTime(expiresAt) {
 
32
  return `${hours}h ${minutes}m ${seconds}s`;
33
  }
34
 
 
35
  const app = express();
36
  const port = process.env.PORT || 7860;
37
 
 
62
  res.json({
63
  success: true,
64
  file_url: `${req.protocol}://${req.get("host")}/files/${filePath}`,
65
+ expires_in: formatRemainingTime(expiresAt)
66
  });
67
  });
68
 
 
73
  const fileList = files.map(filename => ({
74
  filename,
75
  file_url: `${req.protocol}://${req.get("host")}/files/${filename}`,
76
+ expires_in: formatRemainingTime(fileData[filename] || null) // Jika tidak ada, kasih "Unknown"
77
  }));
78
 
79
  res.json({ success: true, files: fileList });
 
143
  });
144
  });
145
 
 
146
  app.get("/status", (req, res) => {
147
  const uptime = process.uptime();
148
  const memoryUsage = process.memoryUsage();
 
172
  });
173
  });
174
 
 
175
  setInterval(() => {
176
  const now = Date.now();
177
  let updatedFileData = {};
 
179
  Object.keys(fileData).forEach(filename => {
180
  if (fileData[filename] <= now) {
181
  const filePath = path.join(FILE_DIR, filename);
182
+ try {
183
+ if (fs.existsSync(filePath)) {
184
+ fs.unlinkSync(filePath);
185
+ console.log(`🗑️ File expired dihapus: ${filename}`);
186
+ }
187
+ } catch (err) {
188
+ console.error(`Gagal menghapus file ${filename}:`, err);
189
  }
190
  } else {
191
  updatedFileData[filename] = fileData[filename];
192
  }
193
  });
194
 
 
195
  fileData = updatedFileData;
196
  fs.writeFileSync(META_FILE, JSON.stringify(fileData));
197
+ }, 60 * 60 * 1000);
 
198
 
199
  app.listen(port, () => {
200
  console.log("Listening on http://localhost:" + port);