fullpwerr commited on
Commit
04e7338
·
1 Parent(s): 50ac749

add expiration

Browse files
Files changed (2) hide show
  1. app.js +56 -7
  2. ntah.js +0 -0
app.js CHANGED
@@ -7,6 +7,27 @@ const os = require("os");
7
  const { execSync } = require("child_process");
8
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  const app = express();
11
  const port = process.env.PORT || 7860;
12
 
@@ -20,10 +41,10 @@ app.use("/files", express.static(uploadDir));
20
 
21
  const storage = multer.diskStorage({
22
  destination: (req, file, cb) => {
23
- cb(null, uploadDir);
24
  },
25
  filename: (req, file, cb) => {
26
- const uniqueName = Date.now() + "-" + file.originalname;
27
  cb(null, uniqueName);
28
  }
29
  });
@@ -34,12 +55,16 @@ app.post("/upload", upload.single("file"), (req, res) => {
34
  if (!req.file) {
35
  return res.status(400).json({ success: false, message: "No file uploaded" });
36
  }
37
-
 
 
 
 
 
38
  res.json({
39
  success: true,
40
- message: "File uploaded successfully!",
41
- file_url: `${req.protocol}://${req.get("host")}/files/${req.file.filename}`,
42
- expires_in: "24h"
43
  });
44
  });
45
 
@@ -49,7 +74,8 @@ app.get("/files", (req, res) => {
49
 
50
  const fileList = files.map(filename => ({
51
  filename,
52
- file_url: `${req.protocol}://${req.get("host")}/files/${filename}`
 
53
  }));
54
 
55
  res.json({ success: true, files: fileList });
@@ -146,6 +172,29 @@ app.get("/status", (req, res) => {
146
  });
147
  });
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  app.listen(port, () => {
150
  console.log("Listening on http://localhost:" + port);
151
  });
 
7
  const { execSync } = require("child_process");
8
 
9
 
10
+ const FILE_DIR = path.join(__dirname, "files");
11
+ const EXPIRATION_TIME = 24 * 60 * 60 * 1000; // 24 jam dalam milidetik
12
+ const META_FILE = path.join(__dirname, "files.json");
13
+
14
+ // Load metadata file (kalau ada)
15
+ let fileData = {};
16
+ if (fs.existsSync(META_FILE)) {
17
+ fileData = JSON.parse(fs.readFileSync(META_FILE));
18
+ }
19
+
20
+ function formatRemainingTime(expiresAt) {
21
+ const remainingMs = expiresAt - Date.now();
22
+ if (remainingMs <= 0) return "Expired";
23
+
24
+ const hours = Math.floor(remainingMs / (1000 * 60 * 60));
25
+ const minutes = Math.floor((remainingMs % (1000 * 60 * 60)) / (1000 * 60));
26
+ const seconds = Math.floor((remainingMs % (1000 * 60)) / 1000);
27
+
28
+ return `${hours}h ${minutes}m ${seconds}s`;
29
+ }
30
+
31
  const app = express();
32
  const port = process.env.PORT || 7860;
33
 
 
41
 
42
  const storage = multer.diskStorage({
43
  destination: (req, file, cb) => {
44
+ cb(null, FILE_DIR);
45
  },
46
  filename: (req, file, cb) => {
47
+ const uniqueName = `${Date.now()}-${file.originalname}`;
48
  cb(null, uniqueName);
49
  }
50
  });
 
55
  if (!req.file) {
56
  return res.status(400).json({ success: false, message: "No file uploaded" });
57
  }
58
+
59
+ const filePath = req.file.filename;
60
+ const expiresAt = Date.now() + EXPIRATION_TIME;
61
+ fileData[filePath] = expiresAt;
62
+ fs.writeFileSync(META_FILE, JSON.stringify(fileData));
63
+
64
  res.json({
65
  success: true,
66
+ file_url: `http://yourdomain.com/files/${filePath}`,
67
+ expires_in: formatRemainingTime(expiresAt) // Format sesuai sisa waktu
 
68
  });
69
  });
70
 
 
74
 
75
  const fileList = files.map(filename => ({
76
  filename,
77
+ file_url: `${req.protocol}://${req.get("host")}/files/${filename}`,
78
+ expires_in: formatRemainingTime(fileData[filename])
79
  }));
80
 
81
  res.json({ success: true, files: fileList });
 
172
  });
173
  });
174
 
175
+
176
+ setInterval(() => {
177
+ const now = Date.now();
178
+ let updatedFileData = {};
179
+
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);
200
  });
ntah.js ADDED
File without changes