fullpwerr commited on
Commit
de40a27
·
1 Parent(s): 1a5e3b5

update CRUD

Browse files
Files changed (3) hide show
  1. app.js +8 -5
  2. function +0 -0
  3. function.js +27 -0
app.js CHANGED
@@ -5,7 +5,7 @@ const fs = require("fs");
5
  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; // 24 jam dalam milidetik
@@ -65,9 +65,12 @@ app.post("/upload", upload.single("file"), (req, res) => {
65
  file_name: req.file.originalname,
66
  file_url: `${req.protocol}://${req.get("host")}/files/${filePath}`,
67
  file_size: req.file.size,
 
68
  file_type: req.file.mimetype,
69
  uploadTime: Date.now(),
70
- expiredAt: expiresAt
 
 
71
  };
72
  fs.writeFile(META_FILE, JSON.stringify(fileData, null, 2), (err) => {
73
  if (err) console.error("Gagal menulis ke api.json:", err);
@@ -98,10 +101,10 @@ app.get("/files/browse", (req, res) => {
98
  const paginatedFiles = publicFiles.slice(startIndex, endIndex).map(filename => ({
99
  filename,
100
  file_url: `${req.protocol}://${req.get("host")}/files/${filename}`,
101
- expires_in: formatRemainingTime(fileData[filename]?.expiredAt),
102
- file_size: fileData[filename]?.file_size,
103
  file_type: fileData[filename]?.file_type,
104
- uploadTime: fileData[filename]?.uploadTime
105
  }));
106
 
107
  res.json({
 
5
  const path = require("path");
6
  const os = require("os");
7
  const { execSync } = require("child_process");
8
+ const { formatDate, formatSize} = require("./function.js");
9
 
10
  const FILE_DIR = path.join(__dirname, "files");
11
  const EXPIRATION_TIME = 24 * 60 * 60 * 1000; // 24 jam dalam milidetik
 
65
  file_name: req.file.originalname,
66
  file_url: `${req.protocol}://${req.get("host")}/files/${filePath}`,
67
  file_size: req.file.size,
68
+ fileSize: formatSize(req.file.size),
69
  file_type: req.file.mimetype,
70
  uploadTime: Date.now(),
71
+ uploadTimeFormat: formatDate(Date.now()),
72
+ expiredAt: expiresAt,
73
+ expired: formatDate(expiredAt)
74
  };
75
  fs.writeFile(META_FILE, JSON.stringify(fileData, null, 2), (err) => {
76
  if (err) console.error("Gagal menulis ke api.json:", err);
 
101
  const paginatedFiles = publicFiles.slice(startIndex, endIndex).map(filename => ({
102
  filename,
103
  file_url: `${req.protocol}://${req.get("host")}/files/${filename}`,
104
+ expires_in: fileData[filename]?.expired,
105
+ file_size: fileData[filename]?.fileSize,
106
  file_type: fileData[filename]?.file_type,
107
+ uploadTime: fileData[filename]?.uploadTimeFormat
108
  }));
109
 
110
  res.json({
function ADDED
File without changes
function.js ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function formatDate(timestamp) {
2
+ const days = ["Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"];
3
+ const months = [
4
+ "Januari", "Februari", "Maret", "April", "Mei", "Juni",
5
+ "Juli", "Agustus", "September", "Oktober", "November", "Desember"
6
+ ];
7
+
8
+ const date = new Date(timestamp);
9
+ const day = days[date.getDay()];
10
+ const dd = String(date.getDate()).padStart(2, '0');
11
+ const mm = months[date.getMonth()];
12
+ const yyyy = date.getFullYear();
13
+
14
+ return `${day} ${dd} ${mm} ${yyyy}`;
15
+ }
16
+
17
+ function formatSize(bytes) {
18
+ if (bytes === 0) return '0 B';
19
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
20
+ const i = Math.floor(Math.log(bytes) / Math.log(1024));
21
+ return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
22
+ }
23
+
24
+ module.exports = {
25
+ formatDate,
26
+ formatSize
27
+ }