summaryrefslogtreecommitdiff
path: root/public/index.php
blob: 086bff47d9ed15a3ad45b524848ccc048f48365f (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php';

if (FILE_CATALOG_RANDOM && isset($_GET['random'])) {
    $files = glob(FILE_UPLOAD_DIRECTORY . "/*.*");
    $file = $files[random_int(0, count($files) - 1)];
    $filename = basename($file);
    header("Location: /{$filename}");
    exit();
}
?>
<html>

<head>
    <title><?= INSTANCE_NAME ?></title>
    <link rel="stylesheet" href="/static/style.css">
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">
</head>

<body>
    <main>
        <noscript>No JavaScript Mode</noscript>
        <?php html_big_navbar() ?>

        <section class="box">
            <div class="tab">
                <p>What is <?= INSTANCE_NAME ?>?</p>
            </div>
            <div class="content">
                <p>
                    <?= INSTANCE_NAME ?> is a simple, free and anonymous file sharing site.
                    We do not store anything other than the files you upload.
                    They are stored publicly until the heat death of the universe, so please encrypt confidential files
                    or don't upload them at all.
                    Users do not need an account to start uploading.
                    <br><br>
                    Click the button below and share the files with your friends today!
                    <br>
                    But, read <a href="/tos.php">TOS</a> and <a href="/privacy.php">Privacy Policy</a> before
                    interacting with the
                    website.
                </p>
            </div>
        </section>

        <section class="box column">
            <div class="tabs">
                <div class="form-upload-tab tab" id="form-tab-file">
                    <button onclick="showUploadType('file')" class="transparent">
                        <p>File Upload</p>
                    </button>
                </div>
                <div class="form-upload-tab tab disabled" id="form-tab-text">
                    <button onclick="showUploadType('text')" class="transparent">
                        <p>Text</p>
                    </button>
                </div>
            </div>
            <div class="content">
                <form class="column gap-8" action="/upload.php" method="post" enctype="multipart/form-data"
                    id="form-upload">
                    <input type="file" name="file"
                        accept="<?= implode(', ', array_unique(array_values(FILE_ACCEPTED_MIME_TYPES))) ?>"
                        id="form-file">

                    <div class="column gap-8" id="form-upload-wrapper">
                        <button type="button" style="display: none;">
                            <h1>Click, or drop files here</h1>
                        </button>
                        <?php if (FILEEXT_ENABLED): ?>
                            <div class="row gap-8">
                                <p>URL:</p>
                                <div class="column grow">
                                    <input type="url" name="url" id="form-url"
                                        placeholder="Instagram, YouTube and other links">
                                    <ul class="row gap-8 font-small" style="list-style:none">
                                        <li>
                                            <p>Max duration: <b><?= FILEEXT_MAX_DURATION / 60 ?> minutes</b></p>
                                        </li>
                                        <li><a href="https://github.com/yt-dlp/yt-dlp/blob/master/supportedsites.md"
                                                target="_blank">Supported
                                                platforms</a></li>
                                    </ul>
                                </div>
                            </div>
                        <?php endif; ?>
                        <ul class="row gap-8 font-small" style="list-style:none">
                            <li>
                                <p class="font-small">Max file size:
                                    <b><?= get_cfg_var(option: 'upload_max_filesize') ?></b>
                                </p>
                            </li>
                            <li><a href="/uploaders.php#supported-file-extensions" target="_blank">Supported file
                                    extensions</a></li>
                        </ul>
                    </div>

                    <div class="column" id="form-text-upload">
                        <textarea name="paste" placeholder="Enter your text here..."></textarea>
                    </div>

                    <button type="submit">Upload</button>
                </form>
            </div>
        </section>

        <section class="box column" style="display:none">
            <div class="tab">
                <p>Uploaded files<span title="Your file ownership is stored locally." style="cursor:help">*</span></p>
            </div>
            <div class="content grid grid-3 gap-8" id="uploaded-files">
            </div>
        </section>

        <?php html_footer() ?>
    </main>
</body>

<script>
    document.getElementById('form-text-upload').style.display = 'none';
    let file = null;

    const uploadedFiles = document.getElementById('uploaded-files');
    <?php if (FILEEXT_ENABLED): ?>
        const fileURL = document.getElementById('form-url');
    <?php endif; ?>

    const formUpload = document.getElementById('form-upload');
    formUpload.addEventListener('submit', (event) => {
        event.preventDefault();
        <?php if (FILEEXT_ENABLED): ?>
            fileUpload(fileURL.value.length != 0);
        <?php else: ?>
            fileUpload(false);
        <?php endif; ?>
    });

    const fileUploadWrapper = document.querySelector('#form-upload-wrapper>button');
    fileUploadWrapper.style.display = 'block';

    <?php if (FILEEXT_ENABLED): ?>
        const fileURLWrapper = document.querySelector('#form-upload-wrapper>div');
        fileURL.addEventListener('keyup', () => {
            fileUploadWrapper.style.display = fileURL.value.length == 0 ? 'block' : 'none';
            formSubmitButton.style.display = fileURL.value.length == 0 ? 'none' : 'block';
        });
    <?php endif; ?>

    const textArea = document.querySelector('#form-text-upload>textarea');
    textArea.addEventListener('keyup', () => {
        formSubmitButton.style.display = textArea.value.length == 0 ? 'none' : 'block';
    });

    const formSubmitButton = document.querySelector('#form-upload button[type=submit]');

    const formFile = document.getElementById('form-file');
    formFile.style.display = 'none';
    formFile.addEventListener("change", (e) => {
        file = e.target.files[0];
        if (file) {
            fileUploadWrapper.innerHTML = `<h1>File: ${file.name}</h1>`;
            formSubmitButton.style.display = 'block';
            <?php if (FILEEXT_ENABLED): ?>
                fileURLWrapper.style.display = 'none';
            <?php endif; ?>
        }
    });

    fileUploadWrapper.addEventListener("click", () => formFile.click());
    fileUploadWrapper.addEventListener("drop", (e) => {
        e.preventDefault();
        if (e.dataTransfer.items) {
            for (const item of e.dataTransfer.items) {
                if (item.kind === "file") {
                    file = item.getAsFile();
                    fileUploadWrapper.innerHTML = `<h1>File: ${file.name}</h1>`;
                    formSubmitButton.style.display = 'block';
                    <?php if (FILEEXT_ENABLED): ?>
                        fileURLWrapper.style.display = 'none';
                    <?php endif; ?>
                    break;
                }
            }
        }
    });
    fileUploadWrapper.addEventListener("dragover", (e) => {
        e.preventDefault();
        fileUploadWrapper.innerHTML = '<h1>Drop files here</h1>';
        <?php if (FILEEXT_ENABLED): ?>
            fileURLWrapper.style.display = 'none';
        <?php endif; ?>
    });
    fileUploadWrapper.addEventListener("dragleave", (e) => {
        if (file) {
            fileUploadWrapper.innerHTML = `<h1>File: ${file.name}</h1>`;
            return;
        }
        fileUploadWrapper.innerHTML = '<h1>Click, or drop files here</h1>';
        <?php if (FILEEXT_ENABLED): ?>
            fileURLWrapper.style.display = 'flex';
        <?php endif; ?>

    });

    formSubmitButton.style.display = 'none';

    if (textArea.value.length > 0) {
        formSubmitButton.style.display = 'block';
        showUploadType('text');
    }

    function fileUpload(is_url) {
        if (textArea.value.length > 0) {
            file = null;
            formFile.value = null;
        }

        const form = new FormData(formUpload);

        if (file) {
            form.set('file', file);
        }

        if (is_url) {
            fileUploadWrapper.innerHTML = `<h1>Uploading ${fileURL.value}</h1><p>This might take a while...</p>`;
        } else if (file) {
            fileUploadWrapper.innerHTML = `<h1>Uploading ${file.name}...</h1><p>This might take a while...</p>`;
        } else {
            fileUploadWrapper.innerHTML = `<h1>Uploading...</h1>`;
        }
        fileUploadWrapper.style.display = 'block';
        <?php if (FILEEXT_ENABLED): ?>
            fileURLWrapper.style.display = 'none';
            fileURL.value = '';
        <?php endif; ?>
        file = null;
        formSubmitButton.style.display = 'none';

        fetch(formUpload.getAttribute('action'), {
            'body': form,
            'method': 'POST',
            'headers': {
                'Accept': 'application/json'
            }
        })
            .catch((err) => {
                console.error(err);
                alert('Failed to send a file. More info in the console...');
                <?php if (FILEEXT_ENABLED): ?>
                    fileURLWrapper.style.display = 'flex';
                <?php endif; ?>
                fileUploadWrapper.style.display = 'block';
                fileUploadWrapper.innerHTML = '<h1>Click, or drop files here</h1>';
            })
            .then((r) => r.json())
            .then((json) => {
                fileUploadWrapper.innerHTML = '<h1>Click, or drop files here</h1>';
                <?php if (FILEEXT_ENABLED): ?>
                    fileURLWrapper.style.display = 'flex';
                <?php endif; ?>
                fileUploadWrapper.style.display = 'block';

                if (json.status_code != 201) {
                    alert(`${json.message} (${json.status_code})`);
                    return;
                }

                uploadedFiles.innerHTML = addUploadedFile(json.data) + uploadedFiles.innerHTML;
                uploadedFiles.parentElement.style.display = 'flex';
                textArea.value = '';

                // saving file
                let files = getUploadedFiles();
                files.unshift(json.data);
                localStorage.setItem('uploaded_files', JSON.stringify(files));
            });
    }

    function addUploadedFile(file) {
        let file_url = `/${file.id}.${file.extension}`;
        if (file.urls && file.urls.download_url) {
            file_url = file.urls.download_url;
        }
        let file_deletion = '';
        if (file.urls && file.urls.deletion_url) {
            file_deletion = `<button onclick="deleteUploadedFile('${file.urls.deletion_url}', '${file.id}')">Delete</button>`;
        }

        return `
        <div class="box item column gap-4 pad-4">
            <?php if (FILE_THUMBNAILS): ?>
            <div class="column align-center justify-center grow">
                <div style="max-width: 128px; max-height:128px;">
                    <p><i><img src="<?= FILE_THUMBNAIL_DIRECTORY_PREFIX ?>/${file.id}.webp" alt="No thumbnail." style="max-width:100%; max-height: 100%;"></i></p>
                </div>
            </div>
            <?php endif; ?>
            <h2>${file.id}.${file.extension}</h2>
            <div>
                <p>${file.mime}</p>
                <p title="${file.size} B">${(file.size / 1024 / 1024).toFixed(2)} MB</p>
            </div>
            <div class="row gap-8">
                <a href="${file_url}">
                    <button>Open</button>
                </a>
                ${file_deletion}
            </div>
        </div>
        `;
    }

    function deleteUploadedFile(url, id) {
        fetch(url, {
            'headers': {
                'Accept': 'application/json'
            },
            'method': 'DELETE'
        }).then((r) => r.json())
            .then((json) => {
                if (json.status_code != 200) {
                    alert(`${json.message} (${json.status_code})`);
                    return;
                }

                let files = getUploadedFiles();
                files = files.filter((x) => x.id !== id);
                localStorage.setItem('uploaded_files', JSON.stringify(files));
                loadUploadedFiles();
            })
            .catch((err) => {
                alert('Failed to delete the file. Look into the console!');
                console.error(err);
            });
    }

    // loading already existing uploaded files
    function loadUploadedFiles() {
        let files = getUploadedFiles();

        let html = '';

        for (const file of files) {
            html += addUploadedFile(file);
        }

        uploadedFiles.parentElement.style.display = html.length > 0 ? 'flex' : 'none';

        uploadedFiles.innerHTML = html;
    }

    loadUploadedFiles();

    function getUploadedFiles() {
        let files = localStorage.getItem("uploaded_files");
        if (!files) {
            files = '[]';
        }
        return JSON.parse(files);
    }

    function showUploadType(type) {
        document.getElementById('form-upload-wrapper').style.display = type == 'text' ? 'none' : 'flex';
        document.getElementById('form-text-upload').style.display = type == 'text' ? 'flex' : 'none';

        const tabs = document.querySelectorAll('.form-upload-tab');

        for (const tab of tabs) {
            if (tab.getAttribute('id') == `form-tab-${type}`) {
                tab.classList.remove('disabled');
            } else {
                tab.classList.add('disabled');
            }
        }
    }
</script>

</html>