2023-05-19 21:45:42 +02:00
|
|
|
<script>
|
|
|
|
import { onDestroy } from "svelte";
|
|
|
|
import { formatDataVolume, formatThousands } from "../util/Formatting.svelte"
|
|
|
|
import { fs_path_url } from "./FilesystemUtil";
|
|
|
|
|
|
|
|
export let state
|
|
|
|
|
2024-02-15 18:52:46 +01:00
|
|
|
let loading = true
|
2023-05-19 21:45:42 +02:00
|
|
|
let downloads = 0
|
2023-05-30 23:35:18 +02:00
|
|
|
let transfer_used = 0
|
2023-05-19 21:45:42 +02:00
|
|
|
let socket = null
|
2024-02-15 18:52:46 +01:00
|
|
|
let error_msg = ""
|
2023-05-19 21:45:42 +02:00
|
|
|
|
|
|
|
let connected_to = ""
|
|
|
|
|
|
|
|
$: update_base(state.base)
|
|
|
|
|
|
|
|
const update_base = async base => {
|
|
|
|
if (connected_to === base.path) {
|
|
|
|
return
|
|
|
|
}
|
2023-11-16 14:46:34 +01:00
|
|
|
if (base.type === "dir") {
|
|
|
|
console.debug("Not opening websocket for directory")
|
|
|
|
return
|
|
|
|
}
|
2023-05-19 21:45:42 +02:00
|
|
|
connected_to = base.path
|
|
|
|
|
|
|
|
// If the socket is already active we need to close it
|
|
|
|
close_socket()
|
|
|
|
|
2024-02-15 18:52:46 +01:00
|
|
|
loading = true
|
2023-05-19 21:45:42 +02:00
|
|
|
|
|
|
|
let ws_endpoint = location.origin.replace(/^http/, 'ws') +
|
2023-05-30 15:51:10 +02:00
|
|
|
fs_path_url(base.path).replace(/^http/, 'ws') +
|
2023-05-19 21:45:42 +02:00
|
|
|
"?download_stats"
|
|
|
|
|
|
|
|
console.log("Opening socket to", ws_endpoint)
|
|
|
|
socket = new WebSocket(ws_endpoint)
|
|
|
|
socket.onmessage = msg => {
|
|
|
|
let j = JSON.parse(msg.data)
|
|
|
|
console.debug("WS update", j)
|
|
|
|
|
|
|
|
error_msg = ""
|
2024-02-15 18:52:46 +01:00
|
|
|
loading = false
|
2023-05-19 21:45:42 +02:00
|
|
|
downloads = j.downloads
|
2023-05-30 23:35:18 +02:00
|
|
|
transfer_used = j.transfer_free + j.transfer_paid
|
2023-05-19 21:45:42 +02:00
|
|
|
}
|
|
|
|
socket.onerror = err => {
|
|
|
|
console.error("WS error", err)
|
|
|
|
error_msg = "failed to get stats, retrying..."
|
|
|
|
|
2024-02-16 18:02:51 +01:00
|
|
|
close_socket()
|
|
|
|
|
2023-05-19 21:45:42 +02:00
|
|
|
window.setTimeout(() => {
|
|
|
|
if (socket === null) {
|
2023-05-25 23:07:17 +02:00
|
|
|
update_base(base)
|
2023-05-19 21:45:42 +02:00
|
|
|
}
|
2023-11-16 14:46:34 +01:00
|
|
|
}, 5000)
|
2023-05-19 21:45:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const close_socket = () => {
|
|
|
|
if (socket !== null) {
|
|
|
|
// Disable the error handler so it doesn't start retrying the connection
|
|
|
|
socket.onerror = null
|
|
|
|
socket.close()
|
|
|
|
socket = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 15:50:54 +01:00
|
|
|
// Tallys
|
|
|
|
$: total_directories = state.children.reduce((acc, cur) => cur.type === "dir" ? acc + 1 : acc, 0)
|
|
|
|
$: total_files = state.children.reduce((acc, cur) => cur.type === "file" ? acc + 1 : acc, 0)
|
|
|
|
$: total_file_size = state.children.reduce((acc, cur) => acc + cur.file_size, 0)
|
|
|
|
|
2023-05-19 21:45:42 +02:00
|
|
|
onDestroy(close_socket)
|
|
|
|
</script>
|
|
|
|
|
2023-11-15 16:40:55 +01:00
|
|
|
{#if state.base.type === "file"}
|
|
|
|
{#if error_msg !== ""}
|
|
|
|
{error_msg}
|
|
|
|
{:else}
|
2023-11-15 15:50:54 +01:00
|
|
|
<div class="group">
|
2023-11-15 16:40:55 +01:00
|
|
|
<div class="label">Downloads</div>
|
2024-02-15 18:52:46 +01:00
|
|
|
<div class="stat">
|
|
|
|
{loading ? "Loading..." : formatThousands(downloads)}
|
|
|
|
</div>
|
|
|
|
|
2023-11-15 15:50:54 +01:00
|
|
|
</div>
|
|
|
|
<div class="group">
|
2023-11-15 16:40:55 +01:00
|
|
|
<div class="label">Transfer used</div>
|
2024-02-15 18:52:46 +01:00
|
|
|
<div class="stat">
|
|
|
|
{loading ? "Loading..." : formatDataVolume(transfer_used, 3)}
|
|
|
|
</div>
|
2023-11-15 15:50:54 +01:00
|
|
|
</div>
|
2023-11-15 16:40:55 +01:00
|
|
|
{/if}
|
2023-11-15 15:50:54 +01:00
|
|
|
|
2023-11-15 16:40:55 +01:00
|
|
|
<div class="group">
|
|
|
|
<div class="label">Size</div>
|
|
|
|
<div class="stat">{formatDataVolume(state.base.file_size, 3)}</div>
|
|
|
|
</div>
|
2023-11-15 15:50:54 +01:00
|
|
|
|
2023-11-15 16:40:55 +01:00
|
|
|
{:else if state.base.type === "dir" || state.base.type === "bucket"}
|
2023-05-19 21:45:42 +02:00
|
|
|
|
2023-11-15 16:40:55 +01:00
|
|
|
<div class="group">
|
|
|
|
<div class="label">Directories</div>
|
|
|
|
<div class="stat">{formatThousands(total_directories, 3)}</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="group">
|
|
|
|
<div class="label">Files</div>
|
|
|
|
<div class="stat">{formatThousands(total_files, 3)}</div>
|
|
|
|
</div>
|
2023-11-15 15:50:54 +01:00
|
|
|
|
2023-11-15 16:40:55 +01:00
|
|
|
<div class="group">
|
|
|
|
<div class="label">Total size</div>
|
|
|
|
<div class="stat">{formatDataVolume(total_file_size, 3)}</div>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
2023-11-15 15:50:54 +01:00
|
|
|
.group {
|
|
|
|
flex: 1 1 auto;
|
2023-11-16 12:17:36 +01:00
|
|
|
text-align: center;
|
2023-11-15 15:50:54 +01:00
|
|
|
}
|
2023-05-19 21:45:42 +02:00
|
|
|
.label {
|
2023-11-15 15:50:54 +01:00
|
|
|
padding-left: 0.5em;
|
2023-05-19 21:45:42 +02:00
|
|
|
text-align: left;
|
2023-11-15 15:50:54 +01:00
|
|
|
font-size: 0.75em;
|
2023-11-16 12:17:36 +01:00
|
|
|
line-height: 0.75em;
|
2023-05-19 21:45:42 +02:00
|
|
|
margin-top: 0.5em;
|
|
|
|
}
|
2024-02-16 21:07:01 +01:00
|
|
|
@media (max-width: 700px) {
|
2023-11-15 15:50:54 +01:00
|
|
|
.label {
|
|
|
|
text-align: center;
|
|
|
|
padding-left: 0;
|
|
|
|
}
|
|
|
|
}
|
2023-05-19 21:45:42 +02:00
|
|
|
</style>
|