-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomf.ts
281 lines (242 loc) · 8.12 KB
/
pomf.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const TELEGRAM_BOT_TOKEN = '';
const TELEGRAM_CHAT_ID = '';
const HTML_CONTENT = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Pomf <%= pkg.version %>" />
<meta name="description" content="Uncensored File Host" />
<title>Pomf =3</title>
<link rel="icon" href="data:," />
<link rel="stylesheet" href="/pomf.min.css" />
<script src="/pomf.min.js"></script>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Pomf =3</h1>
<p>Uncensored File Host</p>
<p class="lead">Max upload size is 50 MiB</p>
<form id="upload-form" enctype="multipart/form-data" method="post" action="/upload.php">
<button id="upload-btn" class="btn" type="button">Select or drop file(s)</button>
<input type="file" id="upload-input" name="files[]" multiple data-max-size="50MiB">
<input type="submit" value="Submit">
</form>
<ul id="upload-filelist"></ul>
<p>All Files Allowed</p>
</div>
<div class="jumbotron">
<p class="alert alert-primary"><a href="/pomf.sxcu">ShareX Config</a></p>
<p class="alert alert-error">This website is suck please <a href="https://paypal.me/iqbalrifai">Donate</a> to give my coffee.</p>
</div>
</div>
</body>
</html>`;
const MAX_FILE_SIZE = 50 * 1024 * 1024;
// In-memory storage as a replacement for KV
const fileStorage = {};
// Handle HTTP requests
async function handleRequest(request) {
const url = new URL(request.url);
// Serve HTML at root route
if (url.pathname === '/') {
return new Response(HTML_CONTENT, {
headers: {
'Content-Type': 'text/html',
},
});
}
// Reverse proxy for CSS
if (url.pathname === '/pomf.min.css') {
const response = await fetch('https://pomf.lain.la/pomf.min.css');
return new Response(response.body, {
headers: {
'Content-Type': 'text/css',
'Cache-Control': 'public, max-age=31536000',
},
});
}
// Reverse proxy for JS
if (url.pathname === '/pomf.min.js') {
const response = await fetch('https://pomf.lain.la/pomf.min.js');
return new Response(response.body, {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'public, max-age=31536000',
},
});
}
if (url.pathname === '/img/bg.png') {
const response = await fetch('https://pomf.lain.la/img/bg.png');
return new Response(response.body, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=31536000',
},
});
}
if (url.pathname === '/grill.php') {
const response = await fetch('https://github.com/user-attachments/assets/d95ecfe8-1ddf-492a-9712-c2c519cf61df');
return new Response(response.body, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=31536000',
},
});
}
if (url.pathname === '/pomf.sxcu') {
return servePomfSxcu();
}
// Upload route
if (url.pathname === '/upload.php' && request.method === 'POST') {
return handleUpload(request);
}
// Download route
if (url.pathname.startsWith('/f/')) {
const publicId = url.pathname.slice(3);
return handleDownload(request, publicId);
}
return jsonError('Not found', 404);
}
// Handle file uploads
async function handleUpload(request) {
try {
const contentType = request.headers.get('Content-Type') || '';
if (!contentType.startsWith('multipart/form-data')) {
return jsonError('Invalid Content-Type. Must be multipart/form-data', 400);
}
const formData = await request.formData();
const files = formData.getAll('files[]').filter((f) => f instanceof File);
if (files.length === 0) {
return jsonError('No valid files uploaded', 400);
}
const results = [];
for (const file of files) {
if (file.size > MAX_FILE_SIZE) {
return jsonError(`File ${file.name} exceeds 50MB limit`, 400);
}
const publicId = generatePublicId(file.name);
const hashPart = publicId.split('.')[0];
// Use sendDocument for all file types
const formDataTelegram = new FormData();
formDataTelegram.append('chat_id', TELEGRAM_CHAT_ID);
formDataTelegram.append(
'document',
new Blob([await file.arrayBuffer()], { type: file.type }),
file.name
);
const sendResponse = await fetch(
`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument`,
{ method: 'POST', body: formDataTelegram }
);
const sendResult = await sendResponse.json();
if (!sendResult.ok) {
return jsonError(`Telegram API error: ${sendResult.description}`, 500);
}
// Extract file info from document field
const mediaObject = sendResult.result.document;
if (!mediaObject?.file_id) {
return jsonError('Failed to retrieve file ID from Telegram', 500);
}
// Store file metadata in in-memory storage
fileStorage[publicId] = {
fileId: mediaObject.file_id,
fileName: file.name,
mimeType: file.type,
fileSize: mediaObject.file_size,
};
results.push({
hash: hashPart,
name: file.name,
url: `${new URL(request.url).origin}/f/${publicId}`,
size: mediaObject.file_size,
});
}
return new Response(
JSON.stringify({
success: true,
files: results,
}),
{
headers: { 'Content-Type': 'application/json' },
}
);
} catch (error) {
console.error('Upload error:', error);
return jsonError('Internal server error', 500);
}
}
// Handle file downloads
async function handleDownload(request, publicId) {
try {
const fileMetadata = fileStorage[publicId];
if (!fileMetadata) return jsonError('File not found', 404);
const fileResponse = await fetch(
`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getFile`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ file_id: fileMetadata.fileId }),
}
);
const fileData = await fileResponse.json();
if (!fileData.ok) throw new Error('Telegram API error');
const fileUrl = `https://api.telegram.org/file/bot${TELEGRAM_BOT_TOKEN}/${fileData.result.file_path}`;
const response = await fetch(fileUrl);
const headers = new Headers(response.headers);
headers.set('Content-Disposition', `attachment; filename="${fileMetadata.fileName}"`);
headers.set('Content-Type', fileMetadata.mimeType || 'application/octet-stream');
headers.set('Cache-Control', 'public, max-age=31536000');
return new Response(response.body, { headers });
} catch (error) {
console.error('Download error:', error);
return jsonError('Download failed', 500);
}
}
function servePomfSxcu() {
const sxcuData = {
Version: "17.0.0",
Name: "pomf",
DestinationType: "ImageUploader, FileUploader",
RequestMethod: "POST",
RequestURL: "https://pomf.deno.dev/upload.php",
Body: "MultipartFormData",
FileFormName: "files[]",
URL: "{json:files[0].url}",
ThumbnailURL: "{json:files[0].url}",
};
return new Response(JSON.stringify(sxcuData, null, 2), {
headers: {
'Content-Type': 'application/json',
'Content-Disposition': 'attachment; filename="pomf.sxcu"',
},
});
}
// Generate a unique public ID for a file
function generatePublicId(filename) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let id = '';
for (let i = 0; i < 8; i++) {
id += chars[Math.floor(Math.random() * chars.length)];
}
const lastDotIndex = filename.lastIndexOf('.');
if (lastDotIndex !== -1 && lastDotIndex < filename.length - 1) {
const ext = filename.slice(lastDotIndex);
return `${id}${ext}`;
}
return id;
}
function jsonError(message, status) {
return new Response(
JSON.stringify({
success: false,
error: message,
}),
{
status,
headers: { 'Content-Type': 'application/json' },
}
);
}
Deno.serve({ port: 8000 }, handleRequest);