1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
import { config } from "./config.ts";
import { logger } from "./logger.ts";
export async function ensureUploadDir() {
try {
await Deno.mkdir(config.uploadDir, { recursive: true });
} catch (error) {
if (!(error instanceof Deno.errors.AlreadyExists)) {
throw error;
}
}
}
export async function getUniqueFilename(filename: string): Promise<string> {
const filepath = `${config.uploadDir}/${filename}`;
try {
await Deno.stat(filepath);
// File exists, append epoch seconds
const epoch = Math.floor(Date.now() / 1000);
const parts = filename.split(".");
if (parts.length > 1) {
const ext = parts.pop();
return `${parts.join(".")}_${epoch}.${ext}`;
}
return `${filename}_${epoch}`;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return filename;
}
throw error;
}
}
export async function listUploadedFiles() {
const files: Array<{ name: string; size: number; uploaded: Date }> = [];
try {
for await (const entry of Deno.readDir(config.uploadDir)) {
if (entry.isFile) {
const stat = await Deno.stat(`${config.uploadDir}/${entry.name}`);
files.push({
name: entry.name,
size: stat.size,
uploaded: stat.mtime || new Date(),
});
}
}
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
logger.server("ERROR", "Error listing files", { error: String(error) });
}
}
return files.sort((a, b) => b.uploaded.getTime() - a.uploaded.getTime());
}
export async function cleanupExpiredFiles() {
if (config.fileExpiration === 0) return;
const now = Date.now();
let cleaned = 0;
try {
for await (const entry of Deno.readDir(config.uploadDir)) {
if (entry.isFile) {
const filepath = `${config.uploadDir}/${entry.name}`;
const stat = await Deno.stat(filepath);
const age = now - (stat.mtime?.getTime() || 0);
if (age > config.fileExpiration) {
await Deno.remove(filepath);
cleaned++;
logger.cleanup("INFO", "Expired file removed", { filename: entry.name, ageMs: age });
}
}
}
} catch (error) {
logger.cleanup("ERROR", "Error during file cleanup", { error: String(error) });
}
if (cleaned > 0) {
logger.cleanup("INFO", "File cleanup completed", { filesRemoved: cleaned });
}
}
// Run cleanup every hour
setInterval(() => cleanupExpiredFiles(), 60 * 60 * 1000);
export function isAllowedFileType(contentType: string): boolean {
if (config.allowedFileTypes.includes("*/*")) {
return true;
}
return config.allowedFileTypes.includes(contentType);
}
export function formatFileSize(bytes: number): string {
if (bytes === 0) return "0 Bytes";
const k = 1024;
const sizes = ["Bytes", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + " " + sizes[i];
}
export function getMimeType(filename: string): string {
const ext = filename.split(".").pop()?.toLowerCase();
const mimeTypes: Record<string, string> = {
// Images
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"png": "image/png",
"gif": "image/gif",
"webp": "image/webp",
"svg": "image/svg+xml",
"bmp": "image/bmp",
"ico": "image/x-icon",
// Videos
"mp4": "video/mp4",
"webm": "video/webm",
"ogv": "video/ogg",
"avi": "video/x-msvideo",
"mov": "video/quicktime",
"mkv": "video/x-matroska",
"m4v": "video/x-m4v",
"mpeg": "video/mpeg",
"mpg": "video/mpeg",
// Audio
"mp3": "audio/mpeg",
"wav": "audio/wav",
"ogg": "audio/ogg",
"m4a": "audio/mp4",
"flac": "audio/flac",
// Documents
"pdf": "application/pdf",
"txt": "text/plain",
"html": "text/html",
"css": "text/css",
"js": "application/javascript",
"json": "application/json",
// Archives
"zip": "application/zip",
"tar": "application/x-tar",
"gz": "application/gzip",
};
return mimeTypes[ext || ""] || "application/octet-stream";
}
export function isViewableInBrowser(mimeType: string): boolean {
return mimeType.startsWith("image/") ||
mimeType.startsWith("video/") ||
mimeType.startsWith("audio/") ||
mimeType === "application/pdf" ||
mimeType === "text/plain";
}
|