Skip to content

Commit 7389158

Browse files
committed
pick the best things from v2 branch
1 parent f1b5f02 commit 7389158

File tree

6 files changed

+73
-65
lines changed

6 files changed

+73
-65
lines changed

assets/css/style.css

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
--active-brightness: 0.85;
55
--border-radius: 5px;
66
--box-shadow: 2px 2px 10px;
7-
--color-accent: #43afef15;
8-
--color-bg: #43afef;
7+
--color-accent: #cf431115;
8+
--color-bg: #f7feda;
99
--color-bg-secondary: #e9e9e9;
10-
--color-link: #424242;
11-
--color-secondary: #920de9;
12-
--color-secondary-accent: #920de90b;
10+
--color-link: #cf4311;
11+
--color-secondary: #008820;
12+
--color-secondary-accent: #0088200b;
1313
--color-shadow: #f4f4f4;
1414
--color-table: #424242;
15-
--color-text: #193550;
16-
--color-text-secondary: #eee;
15+
--color-text: #000;
16+
--color-text-secondary: #666;
1717
--font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
1818
--hover-brightness: 1.2;
1919
--justify-important: center;
@@ -27,16 +27,16 @@
2727

2828
@media (prefers-color-scheme: dark) {
2929
:root {
30-
--color-accent: #0097fc4f;
31-
--color-bg: #193550;;
30+
--color-accent: #cf53114f;
31+
--color-bg: #0c0c1c;
3232
--color-bg-secondary: #555;
33-
--color-link: #dfdfdf;
34-
--color-secondary: #e20de9;
35-
--color-secondary-accent: #e20de94f;
33+
--color-link: #cf5311;
34+
--color-secondary: #008820;
35+
--color-secondary-accent: #0088204f;
3636
--color-shadow: #bbbbbb20;
3737
--color-table: #dfdfdf;
38-
--color-text: #43afef;
39-
--color-text-secondary: #aaa;
38+
--color-text: #fff;
39+
--color-text-secondary: #999;
4040
}
4141
}
4242

@@ -518,6 +518,7 @@ video {
518518
padding: 0.75rem 1.5rem;
519519
border-radius: var(--border-radius);
520520
cursor: pointer;
521+
margin-top: 1rem;
521522
}
522523

523524
.progress {

assets/js/app.js

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
function prettyBytes (num) {
2+
const units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
3+
const neg = num < 0;
4+
if (neg) num = -num;
5+
if (num < 1) return (neg ? '-' : '') + num + ' B';
6+
const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1);
7+
const unit = units[exponent];
8+
num = Number((num / Math.pow(1000, exponent)).toFixed(2));
9+
return (neg ? '-' : '') + num + ' ' + unit;
10+
}
11+
112
function logError(err) {
213
console.log(err.message);
314
}
@@ -20,13 +31,14 @@ function createClient() {
2031
});
2132
client.on('error', logError);
2233
client.on('torrent', function(torrent) {
23-
document.getElementById('note').textContent = `Keep this tab open and active while transfering "${torrent.name}".`;
34+
document.getElementById('note').innerHTML = `Keep this tab active while <code id="status">${window.location.hash ? 'leeching' : 'seeding'}</code> <sup>${torrent.files.length}</sup> file(s).`;
2435
});
2536

2637
return client;
2738
}
2839

29-
function copyLink(btn) {
40+
function copyLink(event) {
41+
const btn = event.target;
3042
navigator.clipboard.writeText(btn.previousElementSibling.value);
3143
btn.textContent = 'Copied';
3244
setTimeout(() => { btn.textContent = 'Copy'; }, 1337);
@@ -39,30 +51,30 @@ function downloadTorrent(infohash) {
3951

4052
function addTorrent(torrent) {
4153
torrent.on('error', logError);
42-
torrent.on('download', function(bytes) {
54+
torrent.on('download', function() {
4355
throttle(function () {
4456
updateSpeed(torrent);
4557
}, 1000);
4658
});
47-
torrent.on('upload', function(bytes) {
59+
torrent.on('upload', function() {
4860
throttle(function () {
4961
updateSpeed(torrent);
5062
}, 1000);
5163
});
5264
torrent.on('done', () => {
5365
updateSpeed(torrent);
54-
document.getElementById('status').textContent = 'done';
66+
document.getElementById('status').textContent = 'seeding';
5567
});
5668
const torrentIds = torrent.magnetURI.split('&');
5769
const torId = torrentIds[0].split(':');
5870
const torHash = torId[3];
59-
const torrentLog = `<label for="link">Share link:</label>
60-
<input type="text" id="link" name="link" value="https://${window.location.host}/#${torHash}" readonly>
61-
<button class="copy" onclick="copyLink(this)">Copy</button>`;
62-
log('log', torrentLog);
63-
const filesLog = `<label class="files-label"><sup>${torrent.files.length}</sup> file(s) <code id="status">${window.location.hash ? 'downloading' : 'seeding'}</code></label>
64-
<ul class="file-list">
65-
</ul>`;
71+
const torrentShare = `<label for="link">Share link</label>
72+
<input type="text" id="link" name="link" value="https://${window.location.host}/#${torHash}" readonly>
73+
<button id="copy-btn">Copy</button>`;
74+
log('share', torrentShare);
75+
const filesLog = `
76+
<ul class="file-list">
77+
</ul>`;
6678
log('files', filesLog);
6779
torrent.files.forEach(file => {
6880
// Stream the file in the browser
@@ -74,31 +86,32 @@ function addTorrent(torrent) {
7486
logError(err);
7587
return;
7688
}
77-
const a = document.createElement('a');
78-
a.href = url;
79-
a.textContent = file.name + ` (${prettierBytes(file.length)})`;
80-
a.download = file.name;
81-
const link = `<li><a href="${url}" download="${file.name}" onclick="this.classList.add('visited')">${file.name} <span class="file-size">${prettierBytes(file.length)} ↓</span></a></li>`;
82-
document.getElementsByClassName('file-list')[0].insertAdjacentHTML('beforeEnd', link);
89+
const li = document.createElement('li');
90+
const link = document.createElement('a');
91+
link.href = url;
92+
link.textContent = file.name + ` (${prettyBytes(file.length)}) ↓`;
93+
link.download = file.name;
94+
li.appendChild(link);
95+
document.querySelector('.file-list').appendChild(li);
8396
});
8497
});
98+
document.getElementById('copy-btn').addEventListener('click', copyLink);
8599
}
86100

87101
function updateSpeed(torrent) {
88102
const progress = (100 * torrent.progress).toFixed(0);
89103
const speed = `
90-
<div class="transfer-info">
91-
${window.location.hash ? `<div class="progress">
92-
<div class="progress-bar" role="progressbar" aria-label="Current download progress" style="width: ${progress}%;" aria-valuenow="${progress}" aria-valuemin="0" aria-valuemax="100">${progress}%</div>
93-
</div>` : ''}
94-
<div>Peers: ${torrent.numPeers}</div>
95-
<div>Download speed: ${prettierBytes(torrent.downloadSpeed)}/s</div>
96-
<div>Upload speed: ${prettierBytes(torrent.uploadSpeed)}/s</div>
97-
${window.location.hash ? `<div class="text-truncate">Remaining: ${torrent.done ? 'done' : convertMS(torrent.timeRemaining)}</div>` : ''}
98-
</div>
99-
`
104+
<div class="progress">
105+
<div class="progress-bar" role="progressbar" aria-label="Current download progress" style="width: ${progress}%;" aria-valuenow="${progress}" aria-valuemin="0" aria-valuemax="100">${progress}%</div>
106+
</div>
107+
<div>Peers: ${torrent.numPeers}</div>
108+
<div>Download speed: ${prettyBytes(torrent.downloadSpeed)}/s</div>
109+
<div>Upload speed: ${prettyBytes(torrent.uploadSpeed)}/s</div>
110+
${window.location.hash ? `<div class="text-truncate">Remaining: ${torrent.done ? 'done' : convertMS(torrent.timeRemaining)}</div>` : ''}
111+
`
100112
const speedInfo = document.getElementById('speed');
101113
speedInfo.innerHTML = speed;
114+
speedInfo.hidden = false;
102115
}
103116

104117
function convertMS(ms) {
@@ -128,8 +141,10 @@ function convertMS(ms) {
128141
return ret;
129142
}
130143

131-
function log(id, element) {
132-
document.getElementById(id).insertAdjacentHTML('afterBegin', element);
144+
function log(id, item) {
145+
const element = document.getElementById(id);
146+
element.insertAdjacentHTML('afterBegin', item);
147+
element.hidden = false;
133148
}
134149

135150
const announce = [
@@ -163,22 +178,17 @@ window.addEventListener('DOMContentLoaded', function() {
163178
downloadTorrent(hash);
164179
}
165180
else {
166-
noteElement.textContent = 'Please select file(s) to start seeding.';
167181
upElement.classList.add('show');
168182
}
169183
}
170184
else {
171185
noteElement.textContent = 'Sorry, WebRTC is not supported in your browser.';
172186
}
173-
noteElement.classList.add('show');
174187

175-
uploadElement(document.getElementById('upload'), (err, results) => {
176-
if (err) {
177-
logError(err);
178-
return;
179-
}
180-
const files = results.map(result => result.file);
181-
if (files.length) {
188+
const fileInput = document.getElementById('upload');
189+
fileInput.addEventListener('change', () => {
190+
const files = fileInput.files;
191+
if (files.length) {
182192
upElement.remove();
183193
noteElement.textContent = 'File hashing in progress, please wait...';
184194
const client = createClient();

assets/js/prettier-bytes.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

assets/js/upload-element.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
<meta property="twitter:description" content="Peer-to-peer file sharing in your browser">
2929
<meta property="twitter:image" content="https://file.love/android-chrome-512x512.png">
3030
<script defer src='/assets/js/webtorrent.min.js'></script>
31-
<script defer src='/assets/js/prettier-bytes.min.js'></script>
32-
<script defer src='/assets/js/upload-element.min.js'></script>
3331
<script defer src="/assets/js/app.js"></script>
3432
</head>
3533
<body>
@@ -46,16 +44,20 @@ <h1><u>Minimal</u> p2p file transfer</h1>
4644
</header>
4745
<noscript>Sorry, this App needs JavaScript enabled to run properly.</noscript>
4846
<main>
47+
<article>
48+
<aside>
49+
<p id="note">Select file(s) to start seeding.</p>
50+
</aside>
51+
</article>
4952
<section id="up" class="fade">
50-
<label for="upload">Choose file(s)</label>
53+
<label for="upload">Upload file(s)</label>
5154
<input type="file" name="upload" id="upload" multiple hidden>
5255
</section>
5356
<section>
54-
<aside id="note" class="fade"></aside>
57+
<aside id="share" hidden></aside>
58+
<aside id="speed" hidden></aside>
5559
</section>
56-
<section id="log"></section>
5760
<section id="files"></section>
58-
<section id="speed"></section>
5961
<section id="output"></section>
6062
<article id="faq">
6163
<h2>FAQ</h2>

sw.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const version = '1.0.2';
1+
const version = '1.1.0';
22
const cacheName = `filelove-${version}`;
33

44
self.addEventListener('install', function(event) {
@@ -9,8 +9,6 @@ self.addEventListener('install', function(event) {
99
'/index.html',
1010
'/assets/css/style.css',
1111
'/assets/js/webtorrent.min.js',
12-
'/assets/js/prettier-bytes.min.js',
13-
'/assets/js/upload-element.min.js',
1412
'/assets/js/app.js'
1513
]
1614
);
@@ -46,4 +44,3 @@ self.addEventListener('activate', function(event) {
4644
})
4745
);
4846
});
49-

0 commit comments

Comments
 (0)