This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
116 lines (103 loc) · 3.52 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,"> <!-- Ignore Favicon -->
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css">
<script src="https://unpkg.com/[email protected]/dist/cdn.min.js" defer></script>
<title>Fibbin</title>
</head>
<body>
<main class="container">
<hgroup>
<h1>Welcome to Fibbin'</h1>
<h2>Upload a file and get a link to share with anyone.</h2>
</hgroup>
<article x-data="{busy: false}" x-show="$store.link.url === undefined">
<h3>Select a file to upload</h3>
<form id="uploadForm" enctype="multipart/form-data" @submit.prevent @submit="busy = true; uploadFile(); busy = false;">
<div>
<input id="fileField" name="file" type="file" @change="validateFilesize()" required>
<fieldset>
<legend>Storage Duration</legend>
<label for="1">
<input type="radio" id="1" name="expires" value="1" checked>
1 Hour
</label>
<label for="6">
<input type="radio" id="6" name="expires" value="6">
6 Hours
</label>
<label for="12">
<input type="radio" id="12" name="expires" value="12">
12 Hours
</label>
<label for="24">
<input type="radio" id="24" name="expires" value="24">
24 Hours
</label>
</fieldset>
</div>
<button type="submit" :aria-busy="busy" :disabled="busy">Upload</button>
</form>
</article>
<article x-cloak x-data="{'copied': false}" x-show="$store.link.url !== undefined" x-transition>
<p>Your file will be available at the following url.</p>
<div>
<a class="contrast" :href="$store.link.url">Go to Bucket -></a>
</div>
<input id="fileLink" type="text" :value="$store.link.url" readonly>
<div class="grid">
<button @click="copyLink(); copied = true; setTimeout(() => copied = false, 3000)">Copy Link</button>
<button @click="$store.link.url = undefined" class="contrast outline">Upload another file</button>
</div>
<div x-show="copied" x-transition.opacity>
<ins class="message">Link copied to clipboard!</ins>
</div>
</article>
</main>
<script>
document.addEventListener('alpine:init', () => {
Alpine.store('link', {
url: undefined
})
});
function uploadFile() {
const form = document.getElementById('uploadForm');
const formData = new FormData(form);
const duration = formData.get('expires')
const file = formData.get('file');
fetch(`./upload/${duration}/${file.name}`, {
method: 'PUT',
body: file
})
.then((response) => response.json())
.then((result) => {
if (result.url !== undefined) {
Alpine.store('link').url = window.location.origin + '/' + result.url;
} else {
alert(result.error);
}
})
.catch((error) => {
alert("File upload failed.");
console.error('Error:', error);
})
}
function copyLink() {
const linkText = document.getElementById('fileLink');
linkText.select()
linkText.setSelectionRange(0, 999999);
navigator.clipboard.writeText(linkText.value);
}
function validateFilesize() {
const fileField = document.getElementById("fileField");
if (fileField.files[0].size > 104_857_600) { // 100 MB
alert("File cannot be larger than 5MB.");
fileField.value = "";
}
}
</script>
</body>
</html>