Compare commits
102 Commits
v0.5.0
...
37167a41a6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37167a41a6 | ||
|
|
63f6008a0a | ||
|
|
4fd769cce2 | ||
|
|
2695fc67f3 | ||
|
|
d42f0f7601 | ||
|
|
4c51029c9f | ||
|
|
4de2027959 | ||
|
|
d5e1304c0d | ||
|
|
54d6ea6332 | ||
|
|
c695c09ecc | ||
|
|
d36605cd5b | ||
|
|
fc1fb3ea5d | ||
|
|
32fa005c62 | ||
|
|
fabec4dd7e | ||
|
|
ece64f48be | ||
|
|
1f24313d23 | ||
|
|
e3af21af91 | ||
|
|
6938740b0f | ||
|
|
b25d0fc14b | ||
|
|
5386508e28 | ||
|
|
129250e072 | ||
|
|
c2be2ecd31 | ||
|
|
dd1d85f412 | ||
|
|
4c7b310f82 | ||
|
|
1250037cfd | ||
|
|
cdc936d2d5 | ||
|
|
4f370440d9 | ||
|
|
feaa8e315e | ||
|
|
14f7253ece | ||
|
|
9d3d27faf3 | ||
|
|
dd235e8f25 | ||
|
|
139ff51dcd | ||
|
|
589e5a682c | ||
|
|
8114d679ef | ||
|
|
32b8e0702c | ||
|
|
cc74912bb9 | ||
|
|
c3cf4caa9a | ||
|
|
b3eacf04f7 | ||
|
|
047facaacb | ||
|
|
41fbd3d122 | ||
|
|
40a45568c1 | ||
|
|
8c6690ea98 | ||
|
|
997e0b8549 | ||
|
|
115bb5db59 | ||
|
|
5f1eb0503a | ||
|
|
4aae194060 | ||
|
|
12eabd29c3 | ||
|
|
589b21f944 | ||
|
|
d3f584b738 | ||
|
|
225f2b0651 | ||
|
|
b759d8324c | ||
|
|
119aba2b3c | ||
|
|
f52d58d645 | ||
|
|
6cba674b30 | ||
|
|
831b2716f7 | ||
|
|
7e5901a2cf | ||
|
|
a4f95d730b | ||
|
|
56082cba15 | ||
|
|
3479a0da57 | ||
|
|
f99d92b217 | ||
|
|
68a701538b | ||
|
|
05a16e3037 | ||
|
|
52ecbc3d36 | ||
|
|
042f1b7f42 | ||
|
|
d27cb2133a | ||
|
|
a8ea43194d | ||
|
|
07fe7448cc | ||
|
|
783af44e26 | ||
|
|
0d6180e8a4 | ||
|
|
bdc0bbd44f | ||
|
|
ba36eaec1b | ||
|
|
a435a30c88 | ||
|
|
742b05ed66 | ||
|
|
a26dc42d88 | ||
|
|
9002afbc7e | ||
|
|
acdd776b92 | ||
| b3fd9637eb | |||
| 2b72508206 | |||
|
|
8cc3ed1a04 | ||
|
|
0d186726b5 | ||
|
|
63bbe84859 | ||
|
|
202f28ff15 | ||
| 41772e6c18 | |||
| e52379d515 | |||
| 74987898c9 | |||
| 859d312913 | |||
| 4bc9cf4534 | |||
| 754d779069 | |||
| 367e4ba0ea | |||
| c2e9a4af05 | |||
| 6cdc37a172 | |||
| 19699564c2 | |||
| 7baf8b3f9b | |||
| 47329ac04e | |||
| f4013d1196 | |||
| 3672156b5e | |||
| f2b37852da | |||
| 708e54d080 | |||
| d051265f40 | |||
| 5cf133465e | |||
| 1c91bf2e87 | |||
| 9cd6f83bec |
119
README.md
@@ -1,116 +1,25 @@
|
|||||||
# Cista Web Storage
|
# Web File Storage
|
||||||
|
|
||||||
<img src="https://git.zi.fi/Vasanko/cista-storage/raw/branch/main/docs/cista.jpg" align=right width=250>
|
Run directly from repository with Hatch (or use pip install as usual):
|
||||||
|
```sh
|
||||||
Cista takes its name from the ancient cistae, metal containers used by Greeks and Egyptians to safeguard valuable items. This modern application provides a browser interface for secure and accessible file storage, echoing the trust and reliability of its historical namesake.
|
hatch run cista -l :3000 /path/to/files
|
||||||
|
|
||||||
This is a cutting-edge **file and document server** designed for speed, efficiency, and unparalleled ease of use. Experience **lightning-fast browsing**, thanks to the file list maintained directly in your browser and updated from server filesystem events, coupled with our highly optimized code. Fully **keyboard-navigable** and with a responsive layout, Cista flawlessly adapts to your devices, providing a seamless experience wherever you are. Our powerful **instant search** means you're always just a few keystrokes away from finding exactly what you need. Press **1/2/3** to switch ordering, navigate with all four arrow keys (+Shift to select). Or click your way around on **breadcrumbs that remember where you were**.
|
|
||||||
|
|
||||||
The Cista project started as an inevitable remake of [Droppy](https://github.com/droppyjs/droppy) which we used and loved despite its numerous bugs. Cista Storage stands out in handling even the most exotic filenames, ensuring a smooth experience where others falter.
|
|
||||||
|
|
||||||
All of this is wrapped in an intuitive interface with automatic light and dark themes, making Cista Storage the ideal choice for anyone seeking a reliable, versatile, and quick file storage solution. Quickly setup your own Cista where your files are just a click away, safe, and always accessible.
|
|
||||||
|
|
||||||
Experience Cista by visiting [Cista Demo](https://drop.zi.fi) for a test run and perhaps upload something...
|
|
||||||
|
|
||||||
|
|
||||||
## Getting Started
|
|
||||||
### Installation
|
|
||||||
|
|
||||||
To install the cista application, use:
|
|
||||||
|
|
||||||
```fish
|
|
||||||
pip install cista
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: Some Linux distributions might need `--break-system-packages` to install Python packages, which are safely installed in the user's home folder. As an alternative to avoid installation, run it with command `pipx run cista`
|
Settings incl. these arguments are stored to config file on the first startup and later `hatch run cista` is sufficient. If the `cista` script is missing, consider `pip install -e .` (within `hatch shell`) or some other trickery (known issue with installs made prior to adding the startup script).
|
||||||
|
|
||||||
### Running the Server
|
Create your user account:
|
||||||
|
```sh
|
||||||
Create an account: (or run a public server without authentication)
|
hatch run cista --user admin --privileged
|
||||||
```fish
|
|
||||||
cista --user yourname --privileged
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Serve your files at http://localhost:8000:
|
## Build frontend
|
||||||
```fish
|
|
||||||
cista -l :8000 /path/to/files
|
|
||||||
```
|
|
||||||
|
|
||||||
The server remembers its settings in the config folder (default `~/.local/share/cista/`), including the listen port and directory, for future runs without arguments.
|
Prebuilt frontend is provided in repository but for any changes it will need to be manually rebuilt:
|
||||||
|
|
||||||
### Internet Access
|
```sh
|
||||||
|
cd cista-front
|
||||||
To use your own TLS certificates, place them in the config folder and run:
|
|
||||||
|
|
||||||
```fish
|
|
||||||
cista -l cista.example.com
|
|
||||||
```
|
|
||||||
|
|
||||||
Most admins instead find the [Caddy](https://caddyserver.com/) web server convenient for its auto TLS certificates and all. A proxy also allows running multiple web services or Cista instances on the same IP address. Caddy configuration **/etc/caddy/Caddyfile** is dead simple:
|
|
||||||
|
|
||||||
```Caddyfile
|
|
||||||
cista.example.com {
|
|
||||||
reverse_proxy :8000
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Development setup
|
|
||||||
|
|
||||||
For rapid development, we use the Vite development server for the Vue frontend, while running the backend on port 8000 that Vite proxies backend requests to. Each server live reloads whenever its code or configuration are modified.
|
|
||||||
|
|
||||||
```fish
|
|
||||||
cd frontend
|
|
||||||
npm install
|
npm install
|
||||||
npm run dev
|
npm run build
|
||||||
```
|
```
|
||||||
|
|
||||||
Concurrently, start the backend on another terminal:
|
This will place the front in `cista/wwwroot` from where the backend server delivers it, and that also gets included in the Python package built via `hatch build`.
|
||||||
|
|
||||||
```fish
|
|
||||||
hatch shell
|
|
||||||
pip install -e '.[dev]'
|
|
||||||
cista --dev -l :8000 /path/to/files
|
|
||||||
```
|
|
||||||
|
|
||||||
We use `hatch shell` for installing on a virtual environment, to not disturb the rest of the system with our hacking.
|
|
||||||
|
|
||||||
Vue is used to build files in `cista/wwwroot`, included prebuilt in the Python package. Running `hatch build` builds the frontend and creates a NodeJS-independent Python package.
|
|
||||||
|
|
||||||
## System Deployment
|
|
||||||
|
|
||||||
This setup allows easy addition of storages, each with its own domain, configuration, and files.
|
|
||||||
|
|
||||||
Assuming a restricted user account **storage** for serving files and that cista is installed system-wide or on this account (check with `sudo -u storage -s`). Alternatively, use `pipx run cista` or `hatch run cista` as the ExecStart command.
|
|
||||||
|
|
||||||
Create **/etc/systemd/system/cista@.service**:
|
|
||||||
|
|
||||||
```ini
|
|
||||||
[Unit]
|
|
||||||
Description=Cista storage %i
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
User=storage
|
|
||||||
ExecStart=cista -c /srv/cista/%i -l /srv/cista/%i/socket /media/storage/@%i/
|
|
||||||
Restart=always
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
```
|
|
||||||
|
|
||||||
This setup supports multiple storages, each under `/media/storage/<domain>` for files and `/srv/cista/<domain>/` for configuration. UNIX sockets are used instead of numeric ports for convenience.
|
|
||||||
|
|
||||||
```fish
|
|
||||||
systemctl daemon-reload
|
|
||||||
systemctl enable --now cista@foo.example.com
|
|
||||||
systemctl enable --now cista@bar.example.com
|
|
||||||
```
|
|
||||||
|
|
||||||
Public exposure is easiest using the Caddy web server, but Nginx or others also work. Run the server with -l domain.example.com if you have TLS certificates in the config folder.
|
|
||||||
|
|
||||||
**/etc/caddy/Caddyfile**:
|
|
||||||
|
|
||||||
```Caddyfile
|
|
||||||
foo.example.com, bar.example.com {
|
|
||||||
reverse_proxy unix//srv/cista/{host}/socket
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang=en>
|
<html lang=en>
|
||||||
<meta charset=UTF-8>
|
<meta charset=UTF-8>
|
||||||
<title>Cista Storage</title>
|
<title>Cista</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<link rel="icon" href="/src/assets/logo.svg">
|
<link rel="icon" href="/favicon.ico">
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@400;700&display=swap" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@400;700&display=swap" rel="stylesheet">
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "cista-frontend",
|
"name": "front",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
BIN
cista-front/public/favicon.ico
Normal file
|
After Width: | Height: | Size: 4.2 KiB |
241
cista-front/public/old-index.html
Executable file
@@ -0,0 +1,241 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<title>Storage</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: sans-serif;
|
||||||
|
max-width: 100ch;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 1em;
|
||||||
|
background-color: #333;
|
||||||
|
color: #eee;
|
||||||
|
}
|
||||||
|
td {
|
||||||
|
text-align: right;
|
||||||
|
padding: .5em;
|
||||||
|
}
|
||||||
|
td:first-child {
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
color: inherit;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<div>
|
||||||
|
<h2>Quick file upload</h2>
|
||||||
|
<p>Uses parallel WebSocket connections for increased bandwidth /api/upload</p>
|
||||||
|
<input type=file id=fileInput>
|
||||||
|
<progress id=progressBar value=0 max=1></progress>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<h2>Files</h2>
|
||||||
|
<ul id=file_list></ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
let files = {}
|
||||||
|
let flatfiles = {}
|
||||||
|
|
||||||
|
function createWatchSocket() {
|
||||||
|
const wsurl = new URL("/api/watch", location.href.replace(/^http/, 'ws'))
|
||||||
|
const ws = new WebSocket(wsurl)
|
||||||
|
ws.onmessage = event => {
|
||||||
|
msg = JSON.parse(event.data)
|
||||||
|
if (msg.update) {
|
||||||
|
tree_update(msg.update)
|
||||||
|
file_list(files)
|
||||||
|
} else {
|
||||||
|
console.log("Unkonwn message from watch socket", msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createWatchSocket()
|
||||||
|
|
||||||
|
function tree_update(msg) {
|
||||||
|
console.log("Tree update", msg)
|
||||||
|
let node = files
|
||||||
|
for (const elem of msg) {
|
||||||
|
if (elem.deleted) {
|
||||||
|
const p = node.dir[elem.name].path
|
||||||
|
delete node.dir[elem.name]
|
||||||
|
delete flatfiles[p]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (elem.name !== undefined) node = node.dir[elem.name] ||= {}
|
||||||
|
if (elem.size !== undefined) node.size = elem.size
|
||||||
|
if (elem.mtime !== undefined) node.mtime = elem.mtime
|
||||||
|
if (elem.dir !== undefined) node.dir = elem.dir
|
||||||
|
}
|
||||||
|
// Update paths and flatfiles
|
||||||
|
files.path = "/"
|
||||||
|
const nodes = [files]
|
||||||
|
flatfiles = {}
|
||||||
|
while (node = nodes.pop()) {
|
||||||
|
flatfiles[node.path] = node
|
||||||
|
if (node.dir === undefined) continue
|
||||||
|
for (const name of Object.keys(node.dir)) {
|
||||||
|
const child = node.dir[name]
|
||||||
|
child.path = node.path + name + (child.dir === undefined ? "" : "/")
|
||||||
|
nodes.push(child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
|
||||||
|
|
||||||
|
const compare_path = (a, b) => collator.compare(a.path, b.path)
|
||||||
|
const compare_time = (a, b) => a.mtime > b.mtime
|
||||||
|
|
||||||
|
function file_list(files) {
|
||||||
|
const table = document.getElementById("file_list")
|
||||||
|
const sorted = Object.values(flatfiles).sort(compare_time)
|
||||||
|
table.innerHTML = ""
|
||||||
|
for (const f of sorted) {
|
||||||
|
const {path, size, mtime} = f
|
||||||
|
const tr = document.createElement("tr")
|
||||||
|
const name_td = document.createElement("td")
|
||||||
|
const size_td = document.createElement("td")
|
||||||
|
const mtime_td = document.createElement("td")
|
||||||
|
const a = document.createElement("a")
|
||||||
|
table.appendChild(tr)
|
||||||
|
tr.appendChild(name_td)
|
||||||
|
tr.appendChild(size_td)
|
||||||
|
tr.appendChild(mtime_td)
|
||||||
|
name_td.appendChild(a)
|
||||||
|
size_td.textContent = size
|
||||||
|
mtime_td.textContent = formatUnixDate(mtime)
|
||||||
|
a.textContent = path
|
||||||
|
a.href = `/files${path}`
|
||||||
|
/*a.onclick = event => {
|
||||||
|
if (window.showSaveFilePicker) {
|
||||||
|
event.preventDefault()
|
||||||
|
download_ws(name, size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a.download = ""*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatUnixDate(t) {
|
||||||
|
const date = new Date(t * 1000)
|
||||||
|
const now = new Date()
|
||||||
|
const diff = date - now
|
||||||
|
const formatter = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
|
||||||
|
|
||||||
|
if (Math.abs(diff) <= 60000) {
|
||||||
|
return formatter.format(Math.round(diff / 1000), 'second')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.abs(diff) <= 3600000) {
|
||||||
|
return formatter.format(Math.round(diff / 60000), 'minute')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.abs(diff) <= 86400000) {
|
||||||
|
return formatter.format(Math.round(diff / 3600000), 'hour')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Math.abs(diff) <= 604800000) {
|
||||||
|
return formatter.format(Math.round(diff / 86400000), 'day')
|
||||||
|
}
|
||||||
|
|
||||||
|
return date.toLocaleDateString()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function download_ws(name, size) {
|
||||||
|
const fh = await window.showSaveFilePicker({
|
||||||
|
suggestedName: name,
|
||||||
|
})
|
||||||
|
const writer = await fh.createWritable()
|
||||||
|
writer.truncate(size)
|
||||||
|
const wsurl = new URL("/api/download", location.href.replace(/^http/, 'ws'))
|
||||||
|
const ws = new WebSocket(wsurl)
|
||||||
|
let pos = 0
|
||||||
|
ws.onopen = () => {
|
||||||
|
console.log("Downloading over WebSocket", name, size)
|
||||||
|
ws.send(JSON.stringify({name, start: 0, end: size, size}))
|
||||||
|
}
|
||||||
|
ws.onmessage = event => {
|
||||||
|
if (typeof event.data === 'string') {
|
||||||
|
const msg = JSON.parse(event.data)
|
||||||
|
console.log("Download finished", msg)
|
||||||
|
ws.close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
console.log("Received chunk", name, pos, pos + event.data.size)
|
||||||
|
pos += event.data.size
|
||||||
|
writer.write(event.data)
|
||||||
|
}
|
||||||
|
ws.onclose = () => {
|
||||||
|
if (pos < size) {
|
||||||
|
console.log("Download aborted", name, pos)
|
||||||
|
writer.truncate(pos)
|
||||||
|
}
|
||||||
|
writer.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileInput = document.getElementById("fileInput")
|
||||||
|
const progress = document.getElementById("progressBar")
|
||||||
|
const numConnections = 2
|
||||||
|
const chunkSize = 1<<20
|
||||||
|
const wsConnections = new Set()
|
||||||
|
|
||||||
|
//for (let i = 0; i < numConnections; i++) createUploadWS()
|
||||||
|
|
||||||
|
function createUploadWS() {
|
||||||
|
const wsurl = new URL("/api/upload", location.href.replace(/^http/, 'ws'))
|
||||||
|
const ws = new WebSocket(wsurl)
|
||||||
|
ws.binaryType = 'arraybuffer'
|
||||||
|
ws.onopen = () => {
|
||||||
|
wsConnections.add(ws)
|
||||||
|
console.log("Upload socket connected")
|
||||||
|
}
|
||||||
|
ws.onmessage = event => {
|
||||||
|
msg = JSON.parse(event.data)
|
||||||
|
if (msg.written) progress.value += +msg.written
|
||||||
|
else console.log(`Error: ${msg.error}`)
|
||||||
|
}
|
||||||
|
ws.onclose = () => {
|
||||||
|
wsConnections.delete(ws)
|
||||||
|
console.log("Upload socket disconnected, reconnecting...")
|
||||||
|
setTimeout(createUploadWS, 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function load(file, start, end) {
|
||||||
|
const reader = new FileReader()
|
||||||
|
const load = new Promise(resolve => reader.onload = resolve)
|
||||||
|
reader.readAsArrayBuffer(file.slice(start, end))
|
||||||
|
const event = await load
|
||||||
|
return event.target.result
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendChunk(file, start, end, ws) {
|
||||||
|
const chunk = await load(file, start, end)
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
name: file.name,
|
||||||
|
size: file.size,
|
||||||
|
start: start,
|
||||||
|
end: end
|
||||||
|
}))
|
||||||
|
ws.send(chunk)
|
||||||
|
}
|
||||||
|
|
||||||
|
fileInput.addEventListener("change", async function() {
|
||||||
|
const file = this.files[0]
|
||||||
|
const numChunks = Math.ceil(file.size / chunkSize)
|
||||||
|
progress.value = 0
|
||||||
|
progress.max = file.size
|
||||||
|
|
||||||
|
console.log(wsConnections)
|
||||||
|
for (let i = 0; i < numChunks; i++) {
|
||||||
|
const ws = Array.from(wsConnections)[i % wsConnections.size]
|
||||||
|
const start = i * chunkSize
|
||||||
|
const end = Math.min(file.size, start + chunkSize)
|
||||||
|
const res = await sendChunk(file, start, end, ws)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
</script>
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
<template>
|
<template>
|
||||||
<LoginModal />
|
<LoginModal />
|
||||||
<header>
|
<header>
|
||||||
<HeaderMain ref="headerMain" :path="path.pathList" :query="path.query">
|
<HeaderMain ref="headerMain">
|
||||||
<HeaderSelected :path="path.pathList" />
|
<HeaderSelected :path="path.pathList" />
|
||||||
</HeaderMain>
|
</HeaderMain>
|
||||||
<BreadCrumb :path="path.pathList" primary />
|
<BreadCrumb :path="path.pathList" tabindex="-1"/>
|
||||||
</header>
|
</header>
|
||||||
<main>
|
<main>
|
||||||
<RouterView :path="path.pathList" :query="path.query" />
|
<RouterView :path="path.pathList" />
|
||||||
</main>
|
</main>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -15,9 +15,9 @@
|
|||||||
import { RouterView } from 'vue-router'
|
import { RouterView } from 'vue-router'
|
||||||
import type { ComputedRef } from 'vue'
|
import type { ComputedRef } from 'vue'
|
||||||
import type HeaderMain from '@/components/HeaderMain.vue'
|
import type HeaderMain from '@/components/HeaderMain.vue'
|
||||||
import { onMounted, onUnmounted, ref, watchEffect } from 'vue'
|
import { onMounted, onUnmounted, ref } from 'vue'
|
||||||
import { loadSession, watchConnect, watchDisconnect } from '@/repositories/WS'
|
import { watchConnect, watchDisconnect } from '@/repositories/WS'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useDocumentStore } from '@/stores/documents'
|
||||||
|
|
||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import Router from '@/router/index'
|
import Router from '@/router/index'
|
||||||
@@ -25,33 +25,27 @@ import Router from '@/router/index'
|
|||||||
interface Path {
|
interface Path {
|
||||||
path: string
|
path: string
|
||||||
pathList: string[]
|
pathList: string[]
|
||||||
query: string
|
|
||||||
}
|
}
|
||||||
const store = useMainStore()
|
const documentStore = useDocumentStore()
|
||||||
const path: ComputedRef<Path> = computed(() => {
|
const path: ComputedRef<Path> = computed(() => {
|
||||||
const p = decodeURIComponent(Router.currentRoute.value.path).split('//')
|
const p = decodeURIComponent(Router.currentRoute.value.path)
|
||||||
const pathList = p[0].split('/').filter(value => value !== '')
|
const pathList = p.split('/').filter(value => value !== '')
|
||||||
const query = p.slice(1).join('//')
|
|
||||||
return {
|
return {
|
||||||
path: p[0],
|
path: p,
|
||||||
pathList,
|
pathList
|
||||||
query
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
watchEffect(() => {
|
|
||||||
document.title = path.value.path.replace(/\/$/, '').split('/').pop() || store.server.name || 'Cista Storage'
|
|
||||||
})
|
|
||||||
onMounted(loadSession)
|
|
||||||
onMounted(watchConnect)
|
onMounted(watchConnect)
|
||||||
onUnmounted(watchDisconnect)
|
onUnmounted(watchDisconnect)
|
||||||
|
// Update human-readable x seconds ago messages from mtimes
|
||||||
|
setInterval(documentStore.updateModified, 1000)
|
||||||
const headerMain = ref<typeof HeaderMain | null>(null)
|
const headerMain = ref<typeof HeaderMain | null>(null)
|
||||||
let vert = 0
|
let vert = 0
|
||||||
let timer: any = null
|
let timer: any = null
|
||||||
const globalShortcutHandler = (event: KeyboardEvent) => {
|
const globalShortcutHandler = (event: KeyboardEvent) => {
|
||||||
const fileExplorer = store.fileExplorer as any
|
const fileExplorer = documentStore.fileExplorer as any
|
||||||
if (!fileExplorer) return
|
if (!fileExplorer) return
|
||||||
const c = fileExplorer.isCursor()
|
const c = fileExplorer.isCursor()
|
||||||
const input = (event.target as HTMLElement).tagName === 'INPUT'
|
|
||||||
const keyup = event.type === 'keyup'
|
const keyup = event.type === 'keyup'
|
||||||
if (event.repeat) {
|
if (event.repeat) {
|
||||||
if (
|
if (
|
||||||
@@ -71,21 +65,13 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
|
|||||||
else if (!keyup && event.key === 'f' && (event.ctrlKey || event.metaKey)) {
|
else if (!keyup && event.key === 'f' && (event.ctrlKey || event.metaKey)) {
|
||||||
headerMain.value!.toggleSearchInput()
|
headerMain.value!.toggleSearchInput()
|
||||||
}
|
}
|
||||||
// Search also on / (UNIX style)
|
|
||||||
else if (keyup && !input && event.key === '/') {
|
|
||||||
headerMain.value!.toggleSearchInput()
|
|
||||||
}
|
|
||||||
// Globally close search on Escape
|
|
||||||
else if (keyup && event.key === 'Escape') {
|
|
||||||
headerMain.value!.closeSearch(event)
|
|
||||||
}
|
|
||||||
// Select all (toggle); keydown to prevent builtin
|
// Select all (toggle); keydown to prevent builtin
|
||||||
else if (!keyup && event.key === 'a' && (event.ctrlKey || event.metaKey)) {
|
else if (!keyup && event.key === 'a' && (event.ctrlKey || event.metaKey)) {
|
||||||
fileExplorer.toggleSelectAll()
|
fileExplorer.toggleSelectAll()
|
||||||
}
|
}
|
||||||
// Keys 1-3 to sort columns
|
// Keys 1-3 to sort columns
|
||||||
else if (
|
else if (
|
||||||
!input &&
|
c &&
|
||||||
keyup &&
|
keyup &&
|
||||||
(event.key === '1' || event.key === '2' || event.key === '3')
|
(event.key === '1' || event.key === '2' || event.key === '3')
|
||||||
) {
|
) {
|
||||||
@@ -133,4 +119,3 @@ onUnmounted(() => {
|
|||||||
})
|
})
|
||||||
export type { Path }
|
export type { Path }
|
||||||
</script>
|
</script>
|
||||||
@/stores/main
|
|
||||||
|
Before Width: | Height: | Size: 158 B After Width: | Height: | Size: 158 B |
|
Before Width: | Height: | Size: 168 B After Width: | Height: | Size: 168 B |
|
Before Width: | Height: | Size: 388 B After Width: | Height: | Size: 388 B |
|
Before Width: | Height: | Size: 128 B After Width: | Height: | Size: 128 B |
|
Before Width: | Height: | Size: 126 B After Width: | Height: | Size: 126 B |
|
Before Width: | Height: | Size: 158 B After Width: | Height: | Size: 158 B |
|
Before Width: | Height: | Size: 208 B After Width: | Height: | Size: 208 B |
|
Before Width: | Height: | Size: 563 B After Width: | Height: | Size: 563 B |
|
Before Width: | Height: | Size: 212 B After Width: | Height: | Size: 212 B |
|
Before Width: | Height: | Size: 293 B After Width: | Height: | Size: 293 B |
|
Before Width: | Height: | Size: 310 B After Width: | Height: | Size: 310 B |
|
Before Width: | Height: | Size: 193 B After Width: | Height: | Size: 193 B |
|
Before Width: | Height: | Size: 278 B After Width: | Height: | Size: 278 B |
|
Before Width: | Height: | Size: 711 B After Width: | Height: | Size: 711 B |
|
Before Width: | Height: | Size: 365 B After Width: | Height: | Size: 365 B |
|
Before Width: | Height: | Size: 783 B After Width: | Height: | Size: 783 B |
|
Before Width: | Height: | Size: 382 B After Width: | Height: | Size: 382 B |
|
Before Width: | Height: | Size: 200 B After Width: | Height: | Size: 200 B |
|
Before Width: | Height: | Size: 698 B After Width: | Height: | Size: 698 B |
|
Before Width: | Height: | Size: 156 B After Width: | Height: | Size: 156 B |
|
Before Width: | Height: | Size: 416 B After Width: | Height: | Size: 416 B |
|
Before Width: | Height: | Size: 517 B After Width: | Height: | Size: 517 B |
|
Before Width: | Height: | Size: 257 B After Width: | Height: | Size: 257 B |
|
Before Width: | Height: | Size: 297 B After Width: | Height: | Size: 297 B |
|
Before Width: | Height: | Size: 312 B After Width: | Height: | Size: 312 B |
|
Before Width: | Height: | Size: 109 B After Width: | Height: | Size: 109 B |
|
Before Width: | Height: | Size: 587 B After Width: | Height: | Size: 587 B |
|
Before Width: | Height: | Size: 269 B After Width: | Height: | Size: 269 B |
|
Before Width: | Height: | Size: 106 B After Width: | Height: | Size: 106 B |
|
Before Width: | Height: | Size: 393 B After Width: | Height: | Size: 393 B |
|
Before Width: | Height: | Size: 94 B After Width: | Height: | Size: 94 B |
|
Before Width: | Height: | Size: 229 B After Width: | Height: | Size: 229 B |
|
Before Width: | Height: | Size: 108 B After Width: | Height: | Size: 108 B |
|
Before Width: | Height: | Size: 407 B After Width: | Height: | Size: 407 B |
|
Before Width: | Height: | Size: 887 B After Width: | Height: | Size: 887 B |
|
Before Width: | Height: | Size: 908 B After Width: | Height: | Size: 908 B |
|
Before Width: | Height: | Size: 417 B After Width: | Height: | Size: 417 B |
|
Before Width: | Height: | Size: 554 B After Width: | Height: | Size: 554 B |
|
Before Width: | Height: | Size: 552 B After Width: | Height: | Size: 552 B |
|
Before Width: | Height: | Size: 114 B After Width: | Height: | Size: 114 B |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 91 B After Width: | Height: | Size: 91 B |
|
Before Width: | Height: | Size: 647 B After Width: | Height: | Size: 647 B |
|
Before Width: | Height: | Size: 95 B After Width: | Height: | Size: 95 B |
|
Before Width: | Height: | Size: 208 B After Width: | Height: | Size: 208 B |
|
Before Width: | Height: | Size: 104 B After Width: | Height: | Size: 104 B |
|
Before Width: | Height: | Size: 508 B After Width: | Height: | Size: 508 B |
|
Before Width: | Height: | Size: 1009 B After Width: | Height: | Size: 1009 B |
|
Before Width: | Height: | Size: 278 B After Width: | Height: | Size: 278 B |
|
Before Width: | Height: | Size: 753 B After Width: | Height: | Size: 753 B |
|
Before Width: | Height: | Size: 353 B After Width: | Height: | Size: 353 B |
|
Before Width: | Height: | Size: 542 B After Width: | Height: | Size: 542 B |
|
Before Width: | Height: | Size: 292 B After Width: | Height: | Size: 292 B |
|
Before Width: | Height: | Size: 621 B After Width: | Height: | Size: 621 B |
|
Before Width: | Height: | Size: 517 B After Width: | Height: | Size: 517 B |
|
Before Width: | Height: | Size: 289 B After Width: | Height: | Size: 289 B |
|
Before Width: | Height: | Size: 498 B After Width: | Height: | Size: 498 B |
|
Before Width: | Height: | Size: 464 B After Width: | Height: | Size: 464 B |
@@ -4,9 +4,7 @@
|
|||||||
aria-label="Breadcrumb"
|
aria-label="Breadcrumb"
|
||||||
@keyup.left.stop="move(-1)"
|
@keyup.left.stop="move(-1)"
|
||||||
@keyup.right.stop="move(1)"
|
@keyup.right.stop="move(1)"
|
||||||
@keyup.enter="move(0)"
|
@focus="move(0)"
|
||||||
@focus=focusCurrent
|
|
||||||
tabindex=0
|
|
||||||
>
|
>
|
||||||
<a href="#/"
|
<a href="#/"
|
||||||
:ref="el => setLinkRef(0, el)"
|
:ref="el => setLinkRef(0, el)"
|
||||||
@@ -28,7 +26,7 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import home from '@/assets/svg/home.svg'
|
import home from '@/assets/svg/home.svg'
|
||||||
import { nextTick, onBeforeUpdate, ref, watchEffect } from 'vue'
|
import { onBeforeUpdate, ref, watchEffect } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
@@ -39,33 +37,17 @@ onBeforeUpdate(() => { links.length = 1 }) // 1 to keep home
|
|||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
path: Array<string>
|
path: Array<string>
|
||||||
primary?: boolean
|
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const longest = ref<Array<string>>([])
|
const longest = ref<Array<string>>([])
|
||||||
|
|
||||||
const isCurrent = (index: number) => index == props.path.length ? 'location' : undefined
|
const isCurrent = (index: number) => index == props.path.length ? 'location' : undefined
|
||||||
|
|
||||||
const focusCurrent = () => {
|
|
||||||
nextTick(() => {
|
|
||||||
const index = props.path.length
|
|
||||||
if (index < links.length) links[index].focus()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const navigate = (index: number) => {
|
const navigate = (index: number) => {
|
||||||
const link = links[index]
|
const link = links[index]
|
||||||
if (!link) throw Error(`No link at index ${index} (path: ${props.path})`)
|
if (!link) throw Error(`No link at index ${index} (path: ${props.path})`)
|
||||||
const url = index ? `/${longest.value.slice(0, index).join('/')}/` : '/'
|
link.focus()
|
||||||
const long = longest.value.length ? `/${longest.value.join('/')}/` : '/'
|
router.replace(`/${longest.value.slice(0, index).join('/')}`)
|
||||||
const browser = decodeURIComponent(location.hash.slice(1).split('//')[0])
|
|
||||||
const u = url.replaceAll('?', '%3F').replaceAll('#', '%23')
|
|
||||||
// Clicking on current link clears the rest of the path and adds new history
|
|
||||||
if (isCurrent(index)) { longest.value.splice(index); router.push(u) }
|
|
||||||
// Moving along breadcrumbs doesn't create new history
|
|
||||||
else if (long.startsWith(browser)) router.replace(u)
|
|
||||||
// Nornal navigation from elsewhere (e.g. search result breadcrumbs)
|
|
||||||
else router.push(u)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const move = (dir: number) => {
|
const move = (dir: number) => {
|
||||||
@@ -77,16 +59,13 @@ const move = (dir: number) => {
|
|||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
const longcut = longest.value.slice(0, props.path.length)
|
const longcut = longest.value.slice(0, props.path.length)
|
||||||
const same = longcut.every((value, index) => value === props.path[index])
|
const same = longcut.every((value, index) => value === props.path[index])
|
||||||
// Navigated out of previous path, reset longest to current
|
|
||||||
if (!same) longest.value = props.path
|
if (!same) longest.value = props.path
|
||||||
else if (props.path.length > longcut.length) {
|
else if (props.path.length > longcut.length) {
|
||||||
longest.value = longcut.concat(props.path.slice(longcut.length))
|
longest.value = longcut.concat(props.path.slice(longcut.length))
|
||||||
}
|
}
|
||||||
// If needed, focus primary navigation to new location
|
})
|
||||||
if (props.primary) nextTick(() => {
|
watchEffect(() => {
|
||||||
const act = document.activeElement as HTMLElement
|
if (links.length) navigate(props.path.length)
|
||||||
if (!act || [...links, document.body].includes(act)) focusCurrent()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -3,11 +3,34 @@
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th class="selection">
|
<th class="selection">
|
||||||
<input type="checkbox" tabindex="-1" v-model="allSelected" :indeterminate="selectionIndeterminate">
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
tabindex="-1"
|
||||||
|
v-model="allSelected"
|
||||||
|
:indeterminate="selectionIndeterminate"
|
||||||
|
/>
|
||||||
|
</th>
|
||||||
|
<th
|
||||||
|
class="sortcolumn"
|
||||||
|
:class="{ sortactive: sort === 'name' }"
|
||||||
|
@click="toggleSort('name')"
|
||||||
|
>
|
||||||
|
Name
|
||||||
|
</th>
|
||||||
|
<th
|
||||||
|
class="sortcolumn modified right"
|
||||||
|
:class="{ sortactive: sort === 'modified' }"
|
||||||
|
@click="toggleSort('modified')"
|
||||||
|
>
|
||||||
|
Modified
|
||||||
|
</th>
|
||||||
|
<th
|
||||||
|
class="sortcolumn size right"
|
||||||
|
:class="{ sortactive: sort === 'size' }"
|
||||||
|
@click="toggleSort('size')"
|
||||||
|
>
|
||||||
|
Size
|
||||||
</th>
|
</th>
|
||||||
<th class="sortcolumn" :class="{ sortactive: store.sortOrder === 'name' }" @click="store.toggleSort('name')">Name</th>
|
|
||||||
<th class="sortcolumn modified right" :class="{ sortactive: store.sortOrder === 'modified' }" @click="store.toggleSort('modified')">Modified</th>
|
|
||||||
<th class="sortcolumn size right" :class="{ sortactive: store.sortOrder === 'size' }" @click="store.toggleSort('size')">Size</th>
|
|
||||||
<th class="menu"></th>
|
<th class="menu"></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@@ -15,13 +38,27 @@
|
|||||||
<tr v-if="editing?.key === 'new'" class="folder">
|
<tr v-if="editing?.key === 'new'" class="folder">
|
||||||
<td class="selection"></td>
|
<td class="selection"></td>
|
||||||
<td class="name">
|
<td class="name">
|
||||||
<FileRenameInput :doc="editing" :rename="mkdir" :exit="() => {editing = null}" />
|
<FileRenameInput
|
||||||
|
:doc="editing"
|
||||||
|
:rename="mkdir"
|
||||||
|
:exit="
|
||||||
|
() => {
|
||||||
|
editing = null
|
||||||
|
}
|
||||||
|
"
|
||||||
|
/>
|
||||||
</td>
|
</td>
|
||||||
<FileModified :doc=editing :key=nowkey />
|
<td class="modified right">
|
||||||
<FileSize :doc=editing />
|
<time :datetime="new Date(editing.mtime).toISOString().replace('.000', '')">{{
|
||||||
|
editing.modified
|
||||||
|
}}</time>
|
||||||
|
</td>
|
||||||
|
<td class="size right">{{ editing.sizedisp }}</td>
|
||||||
<td class="menu"></td>
|
<td class="menu"></td>
|
||||||
</tr>
|
</tr>
|
||||||
<template v-for="(doc, index) in documents" :key="doc.key">
|
<template
|
||||||
|
v-for="(doc, index) in sortedDocuments"
|
||||||
|
:key="doc.key">
|
||||||
<tr class="folder-change" v-if="showFolderBreadcrumb(index)">
|
<tr class="folder-change" v-if="showFolderBreadcrumb(index)">
|
||||||
<th colspan="5"><BreadCrumb :path="doc.loc ? doc.loc.split('/') : []" /></th>
|
<th colspan="5"><BreadCrumb :path="doc.loc ? doc.loc.split('/') : []" /></th>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -36,35 +73,59 @@
|
|||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
tabindex="-1"
|
tabindex="-1"
|
||||||
:checked="store.selected.has(doc.key)"
|
:checked="documentStore.selected.has(doc.key)"
|
||||||
@change="
|
@change="
|
||||||
($event.target as HTMLInputElement).checked
|
($event.target as HTMLInputElement).checked
|
||||||
? store.selected.add(doc.key)
|
? documentStore.selected.add(doc.key)
|
||||||
: store.selected.delete(doc.key)
|
: documentStore.selected.delete(doc.key)
|
||||||
"
|
"
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
<td class="name">
|
<td class="name">
|
||||||
<template v-if="editing === doc">
|
<template v-if="editing === doc"
|
||||||
<FileRenameInput :doc="doc" :rename="rename" :exit="() => {editing = null}" />
|
><FileRenameInput
|
||||||
</template>
|
:doc="doc"
|
||||||
|
:rename="rename"
|
||||||
|
:exit="
|
||||||
|
() => {
|
||||||
|
editing = null
|
||||||
|
}
|
||||||
|
"
|
||||||
|
/></template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<a
|
<a
|
||||||
:href="doc.url"
|
:href="url_for(doc)"
|
||||||
tabindex="-1"
|
tabindex="-1"
|
||||||
@contextmenu.prevent
|
@contextmenu.prevent
|
||||||
@focus.stop="cursor = doc"
|
@focus.stop="cursor = doc"
|
||||||
|
@blur="ev => { if (!editing) cursor = null }"
|
||||||
@keyup.left="router.back()"
|
@keyup.left="router.back()"
|
||||||
@keyup.right.stop="ev => { if (doc.dir) (ev.target as HTMLElement).click() }"
|
@keyup.right.stop="ev => { if (doc.dir) (ev.target as HTMLElement).click() }"
|
||||||
>{{ doc.name }}</a
|
>{{ doc.name }}</a
|
||||||
>
|
>
|
||||||
<button tabindex=-1 v-if="cursor == doc" class="rename-button" @click="() => (editing = doc)">🖊️</button>
|
<button
|
||||||
|
v-if="cursor == doc"
|
||||||
|
class="rename-button"
|
||||||
|
@click="() => (editing = doc)"
|
||||||
|
>
|
||||||
|
🖊️
|
||||||
|
</button>
|
||||||
</template>
|
</template>
|
||||||
</td>
|
</td>
|
||||||
<FileModified :doc=doc :key=nowkey />
|
<td class="modified right">
|
||||||
<FileSize :doc=doc />
|
<time
|
||||||
|
:data-tooltip="new Date(1000 * doc.mtime).toISOString().replace('T', '\n').replace('.000Z', ' UTC')"
|
||||||
|
>{{ doc.modified }}</time
|
||||||
|
>
|
||||||
|
</td>
|
||||||
|
<td class="size right">{{ doc.sizedisp }}</td>
|
||||||
<td class="menu">
|
<td class="menu">
|
||||||
<button tabindex=-1 @click.stop="contextMenu($event, doc)">⋮</button>
|
<button
|
||||||
|
tabindex="-1"
|
||||||
|
@click.stop="contextMenu($event, doc)"
|
||||||
|
>
|
||||||
|
⋮
|
||||||
|
</button>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</template>
|
</template>
|
||||||
@@ -79,26 +140,31 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed, watchEffect, shallowRef, onMounted, onUnmounted } from 'vue'
|
import { ref, computed, watchEffect } from 'vue'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useDocumentStore } from '@/stores/documents'
|
||||||
import { Doc } from '@/repositories/Document'
|
import type { Document } from '@/repositories/Document'
|
||||||
import FileRenameInput from './FileRenameInput.vue'
|
import FileRenameInput from './FileRenameInput.vue'
|
||||||
import { connect, controlUrl } from '@/repositories/WS'
|
import { connect, controlUrl } from '@/repositories/WS'
|
||||||
import { formatSize } from '@/utils'
|
import { collator, formatSize, formatUnixDate } from '@/utils'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import ContextMenu from '@imengyu/vue3-context-menu'
|
|
||||||
import type { SortOrder } from '@/utils/docsort'
|
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = withDefaults(
|
||||||
path: Array<string>
|
defineProps<{
|
||||||
documents: Doc[]
|
path: Array<string>
|
||||||
}>()
|
documents: Document[]
|
||||||
const store = useMainStore()
|
}>(),
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
const documentStore = useDocumentStore()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const cursor = shallowRef<Doc | null>(null)
|
const url_for = (doc: Document) => {
|
||||||
|
const p = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
return doc.dir ? `#/${p}/` : `/files/${p}`
|
||||||
|
}
|
||||||
|
const cursor = ref<Document | null>(null)
|
||||||
// File rename
|
// File rename
|
||||||
const editing = shallowRef<Doc | null>(null)
|
const editing = ref<Document | null>(null)
|
||||||
const rename = (doc: Doc, newName: string) => {
|
const rename = (doc: Document, newName: string) => {
|
||||||
const oldName = doc.name
|
const oldName = doc.name
|
||||||
const control = connect(controlUrl, {
|
const control = connect(controlUrl, {
|
||||||
message(ev: MessageEvent) {
|
message(ev: MessageEvent) {
|
||||||
@@ -122,25 +188,35 @@ const rename = (doc: Doc, newName: string) => {
|
|||||||
}
|
}
|
||||||
doc.name = newName // We should get an update from watch but this is quicker
|
doc.name = newName // We should get an update from watch but this is quicker
|
||||||
}
|
}
|
||||||
|
const sortedDocuments = computed(() => sorted(props.documents as Document[]))
|
||||||
|
const showFolderBreadcrumb = (i: number) => {
|
||||||
|
const docs = sortedDocuments.value
|
||||||
|
const docloc = docs[i].loc
|
||||||
|
return i === 0 ? docloc !== loc.value : docloc !== docs[i - 1].loc
|
||||||
|
}
|
||||||
defineExpose({
|
defineExpose({
|
||||||
newFolder() {
|
newFolder() {
|
||||||
const now = Math.floor(Date.now() / 1000)
|
const now = Date.now() / 1000
|
||||||
editing.value = new Doc({
|
editing.value = {
|
||||||
loc: loc.value,
|
loc: loc.value,
|
||||||
key: 'new',
|
key: 'new',
|
||||||
name: 'New Folder',
|
name: 'New Folder',
|
||||||
dir: true,
|
type: 'folder',
|
||||||
mtime: now,
|
mtime: now,
|
||||||
size: 0,
|
size: 0,
|
||||||
})
|
sizedisp: formatSize(0),
|
||||||
|
modified: formatUnixDate(now),
|
||||||
|
haystack: '',
|
||||||
|
}
|
||||||
|
console.log("New")
|
||||||
},
|
},
|
||||||
toggleSelectAll() {
|
toggleSelectAll() {
|
||||||
console.log('Select')
|
console.log('Select')
|
||||||
allSelected.value = !allSelected.value
|
allSelected.value = !allSelected.value
|
||||||
},
|
},
|
||||||
toggleSortColumn(column: number) {
|
toggleSortColumn(column: number) {
|
||||||
const order = ['', 'name', 'modified', 'size', ''][column]
|
const columns = ['', 'name', 'modified', 'size', '']
|
||||||
if (order) store.toggleSort(order as SortOrder)
|
toggleSort(columns[column])
|
||||||
},
|
},
|
||||||
isCursor() {
|
isCursor() {
|
||||||
return cursor.value !== null && editing.value === null
|
return cursor.value !== null && editing.value === null
|
||||||
@@ -151,36 +227,36 @@ defineExpose({
|
|||||||
cursorSelect() {
|
cursorSelect() {
|
||||||
const doc = cursor.value
|
const doc = cursor.value
|
||||||
if (!doc) return
|
if (!doc) return
|
||||||
if (store.selected.has(doc.key)) {
|
if (documentStore.selected.has(doc.key)) {
|
||||||
store.selected.delete(doc.key)
|
documentStore.selected.delete(doc.key)
|
||||||
} else {
|
} else {
|
||||||
store.selected.add(doc.key)
|
documentStore.selected.add(doc.key)
|
||||||
}
|
}
|
||||||
this.cursorMove(1)
|
this.cursorMove(1)
|
||||||
},
|
},
|
||||||
cursorMove(d: number, select = false) {
|
cursorMove(d: number, select = false) {
|
||||||
// Move cursor up or down (keyboard navigation)
|
// Move cursor up or down (keyboard navigation)
|
||||||
const docs = props.documents
|
const documents = sortedDocuments.value
|
||||||
if (docs.length === 0) {
|
if (documents.length === 0) {
|
||||||
cursor.value = null
|
cursor.value = null
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const N = docs.length
|
const N = documents.length
|
||||||
const mod = (a: number, b: number) => ((a % b) + b) % b
|
const mod = (a: number, b: number) => ((a % b) + b) % b
|
||||||
const increment = (i: number, d: number) => mod(i + d, N + 1)
|
const increment = (i: number, d: number) => mod(i + d, N + 1)
|
||||||
const index =
|
const index =
|
||||||
cursor.value !== null ? docs.indexOf(cursor.value) : docs.length
|
cursor.value !== null ? documents.indexOf(cursor.value) : documents.length
|
||||||
const moveto = increment(index, d)
|
const moveto = increment(index, d)
|
||||||
cursor.value = docs[moveto] ?? null
|
cursor.value = documents[moveto] ?? null
|
||||||
const tr = cursor.value ? document.getElementById(`file-${cursor.value.key}`) : null
|
const tr = cursor.value ? document.getElementById(`file-${cursor.value.key}`) : null
|
||||||
if (select) {
|
if (select) {
|
||||||
// Go forwards, possibly wrapping over the end; the last entry is not toggled
|
// Go forwards, possibly wrapping over the end; the last entry is not toggled
|
||||||
let [begin, end] = d > 0 ? [index, moveto] : [moveto, index]
|
let [begin, end] = d > 0 ? [index, moveto] : [moveto, index]
|
||||||
for (let p = begin; p !== end; p = increment(p, 1)) {
|
for (let p = begin; p !== end; p = increment(p, 1)) {
|
||||||
if (p === N) continue
|
if (p === N) continue
|
||||||
const key = docs[p].key
|
const key = documents[p].key
|
||||||
if (store.selected.has(key)) store.selected.delete(key)
|
if (documentStore.selected.has(key)) documentStore.selected.delete(key)
|
||||||
else store.selected.add(key)
|
else documentStore.selected.add(key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -217,14 +293,7 @@ watchEffect(() => {
|
|||||||
focusBreadcrumb()
|
focusBreadcrumb()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
let nowkey = ref(0)
|
const mkdir = (doc: Document, name: string) => {
|
||||||
let modifiedTimer: any = null
|
|
||||||
const updateModified = () => {
|
|
||||||
nowkey.value = Math.floor(Date.now() / 1000)
|
|
||||||
}
|
|
||||||
onMounted(() => { updateModified(); modifiedTimer = setInterval(updateModified, 1000) })
|
|
||||||
onUnmounted(() => { clearInterval(modifiedTimer) })
|
|
||||||
const mkdir = (doc: Doc, name: string) => {
|
|
||||||
const control = connect(controlUrl, {
|
const control = connect(controlUrl, {
|
||||||
open() {
|
open() {
|
||||||
control.send(
|
control.send(
|
||||||
@@ -241,24 +310,34 @@ const mkdir = (doc: Doc, name: string) => {
|
|||||||
editing.value = null
|
editing.value = null
|
||||||
} else {
|
} else {
|
||||||
console.log('mkdir', msg)
|
console.log('mkdir', msg)
|
||||||
router.push(doc.urlrouter)
|
router.push(`/${doc.loc}/${name}/`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// We should get an update from watch but this is quicker
|
doc.name = name // We should get an update from watch but this is quicker
|
||||||
doc.name = name
|
|
||||||
doc.key = crypto.randomUUID()
|
|
||||||
}
|
}
|
||||||
const showFolderBreadcrumb = (i: number) => {
|
|
||||||
const docs = props.documents
|
// Column sort
|
||||||
const docloc = docs[i].loc
|
const toggleSort = (name: string) => {
|
||||||
return i === 0 ? docloc !== loc.value : docloc !== docs[i - 1].loc
|
sort.value = sort.value === name ? '' : name
|
||||||
|
}
|
||||||
|
const sort = ref<string>('')
|
||||||
|
const sortCompare = {
|
||||||
|
name: (a: Document, b: Document) => collator.compare(a.name, b.name),
|
||||||
|
modified: (a: Document, b: Document) => b.mtime - a.mtime,
|
||||||
|
size: (a: Document, b: Document) => b.size - a.size
|
||||||
|
}
|
||||||
|
const sorted = (documents: Document[]) => {
|
||||||
|
const cmp = sortCompare[sort.value as keyof typeof sortCompare]
|
||||||
|
const sorted = [...documents]
|
||||||
|
if (cmp) sorted.sort(cmp)
|
||||||
|
return sorted
|
||||||
}
|
}
|
||||||
const selectionIndeterminate = computed({
|
const selectionIndeterminate = computed({
|
||||||
get: () => {
|
get: () => {
|
||||||
return (
|
return (
|
||||||
props.documents.length > 0 &&
|
props.documents.length > 0 &&
|
||||||
props.documents.some((doc: Doc) => store.selected.has(doc.key)) &&
|
props.documents.some((doc: Document) => documentStore.selected.has(doc.key)) &&
|
||||||
!allSelected.value
|
!allSelected.value
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
@@ -269,16 +348,16 @@ const allSelected = computed({
|
|||||||
get: () => {
|
get: () => {
|
||||||
return (
|
return (
|
||||||
props.documents.length > 0 &&
|
props.documents.length > 0 &&
|
||||||
props.documents.every((doc: Doc) => store.selected.has(doc.key))
|
props.documents.every((doc: Document) => documentStore.selected.has(doc.key))
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
set: (value: boolean) => {
|
set: (value: boolean) => {
|
||||||
console.log('Setting allSelected', value)
|
console.log('Setting allSelected', value)
|
||||||
for (const doc of props.documents) {
|
for (const doc of props.documents) {
|
||||||
if (value) {
|
if (value) {
|
||||||
store.selected.add(doc.key)
|
documentStore.selected.add(doc.key)
|
||||||
} else {
|
} else {
|
||||||
store.selected.delete(doc.key)
|
documentStore.selected.delete(doc.key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -286,13 +365,9 @@ const allSelected = computed({
|
|||||||
|
|
||||||
const loc = computed(() => props.path.join('/'))
|
const loc = computed(() => props.path.join('/'))
|
||||||
|
|
||||||
const contextMenu = (ev: MouseEvent, doc: Doc) => {
|
const contextMenu = (ev: Event, doc: Document) => {
|
||||||
cursor.value = doc
|
cursor.value = doc
|
||||||
ContextMenu.showContextMenu({
|
console.log('Context menu', ev, doc)
|
||||||
x: ev.x, y: ev.y, items: [
|
|
||||||
{ label: 'Rename', onClick: () => { editing.value = doc } },
|
|
||||||
],
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -326,7 +401,7 @@ table .selection {
|
|||||||
text-overflow: clip;
|
text-overflow: clip;
|
||||||
}
|
}
|
||||||
table .modified {
|
table .modified {
|
||||||
width: 9em;
|
width: 8em;
|
||||||
}
|
}
|
||||||
table .size {
|
table .size {
|
||||||
width: 5em;
|
width: 5em;
|
||||||
@@ -440,4 +515,3 @@ tbody .selection input {
|
|||||||
color: #888;
|
color: #888;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@/stores/main
|
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Doc } from '@/repositories/Document'
|
import type { Document } from '@/repositories/Document'
|
||||||
import { ref, onMounted, nextTick } from 'vue'
|
import { ref, onMounted, nextTick } from 'vue'
|
||||||
|
|
||||||
const input = ref<HTMLInputElement | null>(null)
|
const input = ref<HTMLInputElement | null>(null)
|
||||||
@@ -28,8 +28,8 @@ onMounted(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
doc: Doc
|
doc: Document
|
||||||
rename: (doc: Doc, newName: string) => void
|
rename: (doc: Document, newName: string) => void
|
||||||
exit: () => void
|
exit: () => void
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
@@ -46,8 +46,8 @@ const apply = () => {
|
|||||||
|
|
||||||
<style>
|
<style>
|
||||||
input#FileRenameInput {
|
input#FileRenameInput {
|
||||||
color: var(--input-color);
|
color: var(--primary-color);
|
||||||
background: var(--input-background);
|
background: var(--primary-background);
|
||||||
border: 0;
|
border: 0;
|
||||||
border-radius: 0.3rem;
|
border-radius: 0.3rem;
|
||||||
padding: 0.4rem;
|
padding: 0.4rem;
|
||||||
52
cista-front/src/components/FileViewer.vue
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<template>
|
||||||
|
<object
|
||||||
|
v-if="props.type === 'pdf'"
|
||||||
|
:data="dataURL"
|
||||||
|
type="application/pdf"
|
||||||
|
width="100%"
|
||||||
|
height="100%"
|
||||||
|
></object>
|
||||||
|
<a-image
|
||||||
|
v-else-if="props.type === 'image'"
|
||||||
|
width="50%"
|
||||||
|
:src="dataURL"
|
||||||
|
@click="() => setVisible(true)"
|
||||||
|
:previewMask="false"
|
||||||
|
:preview="{
|
||||||
|
visibleImg,
|
||||||
|
onVisibleChange: setVisible
|
||||||
|
}"
|
||||||
|
/>
|
||||||
|
<!-- Unknown case -->
|
||||||
|
<h1 v-else>Unsupported file type</h1>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { watchEffect, ref } from 'vue'
|
||||||
|
import Router from '@/router/index'
|
||||||
|
import { url_document_get } from '@/repositories/Document'
|
||||||
|
|
||||||
|
const dataURL = ref('')
|
||||||
|
watchEffect(() => {
|
||||||
|
dataURL.value = new URL(
|
||||||
|
url_document_get + Router.currentRoute.value.path,
|
||||||
|
location.origin
|
||||||
|
).toString()
|
||||||
|
})
|
||||||
|
const emit = defineEmits({
|
||||||
|
visibleImg(value: boolean) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
function setVisible(value: boolean) {
|
||||||
|
emit('visibleImg', value)
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
type?: string
|
||||||
|
visibleImg: boolean
|
||||||
|
}>()
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style></style>
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
<template>
|
<template>
|
||||||
<nav class="headermain">
|
<nav class="headermain">
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<template v-if="store.error">
|
<template v-if="documentStore.error">
|
||||||
<div class="error-message" @click="store.error = ''">{{ store.error }}</div>
|
<div class="error-message" @click="documentStore.error = ''">{{ documentStore.error }}</div>
|
||||||
<div class="smallgap"></div>
|
<div class="smallgap"></div>
|
||||||
</template>
|
</template>
|
||||||
<UploadButton :path="props.path" />
|
<UploadButton />
|
||||||
<SvgButton
|
<SvgButton
|
||||||
name="create-folder"
|
name="create-folder"
|
||||||
data-tooltip="New folder"
|
data-tooltip="New folder"
|
||||||
@click="() => store.fileExplorer!.newFolder()"
|
@click="() => documentStore.fileExplorer.newFolder()"
|
||||||
/>
|
/>
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
<div class="spacer smallgap"></div>
|
<div class="spacer smallgap"></div>
|
||||||
@@ -17,10 +17,10 @@
|
|||||||
<input
|
<input
|
||||||
ref="search"
|
ref="search"
|
||||||
type="search"
|
type="search"
|
||||||
:value="query"
|
v-model="documentStore.search"
|
||||||
@input="updateSearch"
|
|
||||||
placeholder="Search words"
|
placeholder="Search words"
|
||||||
class="margin-input"
|
class="margin-input"
|
||||||
|
@keyup.escape="closeSearch"
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
<SvgButton ref="searchButton" name="find" @click.prevent="toggleSearchInput" />
|
<SvgButton ref="searchButton" name="find" @click.prevent="toggleSearchInput" />
|
||||||
@@ -30,54 +30,38 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useDocumentStore } from '@/stores/documents'
|
||||||
import { ref, nextTick, watchEffect } from 'vue'
|
import { ref, nextTick } from 'vue'
|
||||||
import ContextMenu from '@imengyu/vue3-context-menu'
|
import ContextMenu from '@imengyu/vue3-context-menu'
|
||||||
import router from '@/router';
|
|
||||||
|
|
||||||
const store = useMainStore()
|
const documentStore = useDocumentStore()
|
||||||
const showSearchInput = ref<boolean>(false)
|
const showSearchInput = ref<boolean>(false)
|
||||||
const search = ref<HTMLInputElement | null>()
|
const search = ref<HTMLInputElement | null>()
|
||||||
const searchButton = ref<HTMLButtonElement | null>()
|
const searchButton = ref<HTMLButtonElement | null>()
|
||||||
const props = defineProps<{
|
|
||||||
path: Array<string>
|
|
||||||
query: string
|
|
||||||
}>()
|
|
||||||
|
|
||||||
const closeSearch = (ev: Event) => {
|
const closeSearch = () => {
|
||||||
if (!showSearchInput.value) return // Already closing
|
if (!showSearchInput.value) return // Already closing
|
||||||
showSearchInput.value = false
|
showSearchInput.value = false
|
||||||
|
documentStore.search = ''
|
||||||
const breadcrumb = document.querySelector('.breadcrumb') as HTMLElement
|
const breadcrumb = document.querySelector('.breadcrumb') as HTMLElement
|
||||||
breadcrumb.focus()
|
breadcrumb.focus()
|
||||||
updateSearch(ev)
|
|
||||||
}
|
}
|
||||||
const updateSearch = (ev: Event) => {
|
const toggleSearchInput = () => {
|
||||||
const q = (ev.target as HTMLInputElement).value
|
|
||||||
let p = props.path.join('/')
|
|
||||||
p = p ? `/${p}` : ''
|
|
||||||
const url = q ? `${p}//${q}` : (p || '/')
|
|
||||||
const u = url.replaceAll('?', '%3F').replaceAll('#', '%23')
|
|
||||||
if (!props.query && q) router.push(u)
|
|
||||||
else router.replace(u)
|
|
||||||
}
|
|
||||||
const toggleSearchInput = (ev: Event) => {
|
|
||||||
showSearchInput.value = !showSearchInput.value
|
showSearchInput.value = !showSearchInput.value
|
||||||
if (!showSearchInput.value) return closeSearch(ev)
|
if (!showSearchInput.value) return closeSearch()
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
const input = search.value
|
const input = search.value
|
||||||
if (input) input.focus()
|
if (input) input.focus()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
watchEffect(() => {
|
|
||||||
if (props.query) showSearchInput.value = true
|
|
||||||
})
|
|
||||||
const settingsMenu = (e: Event) => {
|
const settingsMenu = (e: Event) => {
|
||||||
// show the context menu
|
// show the context menu
|
||||||
const items = []
|
const items = []
|
||||||
if (store.user.isLoggedIn) {
|
if (documentStore.user.isLoggedIn) {
|
||||||
items.push({ label: `Logout ${store.user.username ?? ''}`, onClick: () => store.logout() })
|
items.push({ label: `Logout ${documentStore.user.username ?? ''}`, onClick: () => documentStore.logout() })
|
||||||
} else {
|
} else {
|
||||||
items.push({ label: 'Login', onClick: () => store.loginDialog() })
|
items.push({ label: 'Login', onClick: () => documentStore.loginDialog() })
|
||||||
}
|
}
|
||||||
ContextMenu.showContextMenu({
|
ContextMenu.showContextMenu({
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -85,6 +69,7 @@ const settingsMenu = (e: Event) => {
|
|||||||
items,
|
items,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
defineExpose({
|
defineExpose({
|
||||||
toggleSearchInput,
|
toggleSearchInput,
|
||||||
closeSearch,
|
closeSearch,
|
||||||
@@ -113,4 +98,3 @@ input[type='search'] {
|
|||||||
max-width: 30vw;
|
max-width: 30vw;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@/stores/main
|
|
||||||
@@ -1,29 +1,29 @@
|
|||||||
<template>
|
<template>
|
||||||
<template v-if="store.selected.size">
|
<template v-if="documentStore.selected.size">
|
||||||
<div class="smallgap"></div>
|
<div class="smallgap"></div>
|
||||||
<p class="select-text">{{ store.selected.size }} selected ➤</p>
|
<p class="select-text">{{ documentStore.selected.size }} selected ➤</p>
|
||||||
<SvgButton name="download" data-tooltip="Download" @click="download" />
|
<SvgButton name="download" data-tooltip="Download" @click="download" />
|
||||||
<SvgButton name="copy" data-tooltip="Copy here" @click="op('cp', dst)" />
|
<SvgButton name="copy" data-tooltip="Copy here" @click="op('cp', dst)" />
|
||||||
<SvgButton name="paste" data-tooltip="Move here" @click="op('mv', dst)" />
|
<SvgButton name="paste" data-tooltip="Move here" @click="op('mv', dst)" />
|
||||||
<SvgButton name="trash" data-tooltip="Delete ⚠️" @click="op('rm')" />
|
<SvgButton name="trash" data-tooltip="Delete ⚠️" @click="op('rm')" />
|
||||||
<button class="action-button unselect" data-tooltip="Unselect all" @click="store.selected.clear()">❌</button>
|
<button class="action-button unselect" data-tooltip="Unselect all" @click="documentStore.selected.clear()">❌</button>
|
||||||
</template>
|
</template>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import {connect, controlUrl} from '@/repositories/WS'
|
import {connect, controlUrl} from '@/repositories/WS'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useDocumentStore } from '@/stores/documents'
|
||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import type { SelectedItems } from '@/repositories/Document'
|
import type { SelectedItems } from '@/repositories/Document'
|
||||||
|
|
||||||
const store = useMainStore()
|
const documentStore = useDocumentStore()
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
path: Array<string>
|
path: Array<string>
|
||||||
})
|
})
|
||||||
|
|
||||||
const dst = computed(() => props.path!.join('/'))
|
const dst = computed(() => props.path!.join('/'))
|
||||||
const op = (op: string, dst?: string) => {
|
const op = (op: string, dst?: string) => {
|
||||||
const sel = store.selectedFiles
|
const sel = documentStore.selectedFiles
|
||||||
const msg = {
|
const msg = {
|
||||||
op,
|
op,
|
||||||
sel: sel.keys.map(key => {
|
sel: sel.keys.map(key => {
|
||||||
@@ -34,16 +34,16 @@ const op = (op: string, dst?: string) => {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (dst !== undefined) msg.dst = dst
|
if (dst !== undefined) msg.dst = dst
|
||||||
const control = connect(controlUrl, {
|
const control = connect(controlUrl, {
|
||||||
message(ev: MessageEvent) {
|
message(ev: WebSocmetMessageEvent) {
|
||||||
const res = JSON.parse(ev.data)
|
const res = JSON.parse(ev.data)
|
||||||
if ('error' in res) {
|
if ('error' in res) {
|
||||||
console.error('Control socket error', msg, res.error)
|
console.error('Control socket error', msg, res.error)
|
||||||
store.error = res.error.message
|
documentStore.error = res.error.message
|
||||||
return
|
return
|
||||||
} else if (res.status === 'ack') {
|
} else if (res.status === 'ack') {
|
||||||
console.log('Control ack OK', res)
|
console.log('Control ack OK', res)
|
||||||
control.close()
|
control.close()
|
||||||
store.selected.clear()
|
documentStore.selected.clear()
|
||||||
return
|
return
|
||||||
} else console.log('Unknown control response', msg, res)
|
} else console.log('Unknown control response', msg, res)
|
||||||
}
|
}
|
||||||
@@ -108,17 +108,17 @@ const filesystemdl = async (sel: SelectedItems, handle: FileSystemDirectoryHandl
|
|||||||
}
|
}
|
||||||
|
|
||||||
const download = async () => {
|
const download = async () => {
|
||||||
const sel = store.selectedFiles
|
const sel = documentStore.selectedFiles
|
||||||
console.log('Download', sel)
|
console.log('Download', sel)
|
||||||
if (sel.keys.length === 0) {
|
if (sel.keys.length === 0) {
|
||||||
console.warn('Attempted download but no files found. Missing selected keys:', sel.missing)
|
console.warn('Attempted download but no files found. Missing selected keys:', sel.missing)
|
||||||
store.selected.clear()
|
documentStore.selected.clear()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Plain old a href download if only one file (ignoring any folders)
|
// Plain old a href download if only one file (ignoring any folders)
|
||||||
const files = sel.recursive.filter(([rel, full, doc]) => !doc.dir)
|
const files = sel.recursive.filter(([rel, full, doc]) => !doc.dir)
|
||||||
if (files.length === 1) {
|
if (files.length === 1) {
|
||||||
store.selected.clear()
|
documentStore.selected.clear()
|
||||||
return linkdl(`/files/${files[0][1]}`)
|
return linkdl(`/files/${files[0][1]}`)
|
||||||
}
|
}
|
||||||
// Use FileSystem API if multiple files and the browser supports it
|
// Use FileSystem API if multiple files and the browser supports it
|
||||||
@@ -130,7 +130,7 @@ const download = async () => {
|
|||||||
mode: 'readwrite'
|
mode: 'readwrite'
|
||||||
})
|
})
|
||||||
filesystemdl(sel, handle).then(() => {
|
filesystemdl(sel, handle).then(() => {
|
||||||
store.selected.clear()
|
documentStore.selected.clear()
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -140,7 +140,7 @@ const download = async () => {
|
|||||||
// Otherwise, zip and download
|
// Otherwise, zip and download
|
||||||
const name = sel.keys.length === 1 ? sel.docs[sel.keys[0]].name : 'download'
|
const name = sel.keys.length === 1 ? sel.docs[sel.keys[0]].name : 'download'
|
||||||
linkdl(`/zip/${Array.from(sel.keys).join('+')}/${name}.zip`)
|
linkdl(`/zip/${Array.from(sel.keys).join('+')}/${name}.zip`)
|
||||||
store.selected.clear()
|
documentStore.selected.clear()
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -152,4 +152,3 @@ const download = async () => {
|
|||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@/stores/main
|
|
||||||
@@ -39,10 +39,10 @@
|
|||||||
import { reactive, ref } from 'vue'
|
import { reactive, ref } from 'vue'
|
||||||
import { loginUser } from '@/repositories/User'
|
import { loginUser } from '@/repositories/User'
|
||||||
import type { ISimpleError } from '@/repositories/Client'
|
import type { ISimpleError } from '@/repositories/Client'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useDocumentStore } from '@/stores/documents'
|
||||||
|
|
||||||
const confirmLoading = ref<boolean>(false)
|
const confirmLoading = ref<boolean>(false)
|
||||||
const store = useMainStore()
|
const store = useDocumentStore()
|
||||||
|
|
||||||
const loginForm = reactive({
|
const loginForm = reactive({
|
||||||
username: '',
|
username: '',
|
||||||
@@ -99,4 +99,3 @@ const login = async () => {
|
|||||||
height: 1em;
|
height: 1em;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@/stores/main
|
|
||||||
27
cista-front/src/components/NotificationLoading.vue
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<template>
|
||||||
|
<template v-for="upload in documentStore.uploadingDocuments" :key="upload.key">
|
||||||
|
<span>{{ upload.name }}</span>
|
||||||
|
<div class="progress-container">
|
||||||
|
<a-progress :percent="upload.progress" />
|
||||||
|
<CloseCircleOutlined class="close-button" @click="dismissUpload(upload.key)" />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</template>
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { useDocumentStore } from '@/stores/documents'
|
||||||
|
const documentStore = useDocumentStore()
|
||||||
|
|
||||||
|
function dismissUpload(key: number) {
|
||||||
|
documentStore.deleteUploadingDocument(key)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.progress-container {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.close-button:hover {
|
||||||
|
color: #b81414;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
101
cista-front/src/components/UploadButton.vue
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { useDocumentStore } from '@/stores/documents'
|
||||||
|
import { h, ref } from 'vue'
|
||||||
|
|
||||||
|
const fileUploadButton = ref()
|
||||||
|
const folderUploadButton = ref()
|
||||||
|
const documentStore = useDocumentStore()
|
||||||
|
const open = (placement: any) => openNotification(placement)
|
||||||
|
|
||||||
|
const isNotificationOpen = ref(false)
|
||||||
|
const openNotification = (placement: any) => {
|
||||||
|
if (!isNotificationOpen.value) {
|
||||||
|
/*
|
||||||
|
api.open({
|
||||||
|
message: `Uploading documents`,
|
||||||
|
description: h(NotificationLoading),
|
||||||
|
placement,
|
||||||
|
duration: 0,
|
||||||
|
onClose: () => { isNotificationOpen.value = false }
|
||||||
|
});*/
|
||||||
|
isNotificationOpen.value = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function uploadFileHandler() {
|
||||||
|
fileUploadButton.value.click()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function load(file: File, start: number, end: number): Promise<ArrayBuffer> {
|
||||||
|
const reader = new FileReader()
|
||||||
|
const load = new Promise<Event>(resolve => (reader.onload = resolve))
|
||||||
|
reader.readAsArrayBuffer(file.slice(start, end))
|
||||||
|
const event = await load
|
||||||
|
if (event.target && event.target instanceof FileReader) {
|
||||||
|
return event.target.result as ArrayBuffer
|
||||||
|
} else {
|
||||||
|
throw new Error('Error loading file')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendChunk(file: File, start: number, end: number) {
|
||||||
|
const ws = documentStore.wsUpload
|
||||||
|
if (ws) {
|
||||||
|
const chunk = await load(file, start, end)
|
||||||
|
|
||||||
|
ws.send(
|
||||||
|
JSON.stringify({
|
||||||
|
name: file.name,
|
||||||
|
size: file.size,
|
||||||
|
start: start,
|
||||||
|
end: end
|
||||||
|
})
|
||||||
|
)
|
||||||
|
ws.send(chunk)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function uploadHandler(event: Event) {
|
||||||
|
const target = event.target as HTMLInputElement
|
||||||
|
const chunkSize = 1 << 20
|
||||||
|
if (!target?.files?.length) {
|
||||||
|
documentStore.error = 'No files selected'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for (const idx in target.files) {
|
||||||
|
const file = target.files[idx]
|
||||||
|
console.log('Uploading', file)
|
||||||
|
const numChunks = Math.ceil(file.size / chunkSize)
|
||||||
|
const document = documentStore.pushUploadingDocuments(file.name)
|
||||||
|
open('bottomRight')
|
||||||
|
for (let i = 0; i < numChunks; i++) {
|
||||||
|
const start = i * chunkSize
|
||||||
|
const end = Math.min(file.size, start + chunkSize)
|
||||||
|
const res = await sendChunk(file, start, end)
|
||||||
|
console.log('progress: ' + (100 * (i + 1)) / numChunks)
|
||||||
|
console.log('Num Chunks: ' + numChunks)
|
||||||
|
documentStore.updateUploadingDocuments(document.key, (100 * (i + 1)) / numChunks)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<template>
|
||||||
|
<input
|
||||||
|
ref="fileUploadButton"
|
||||||
|
@change="uploadHandler"
|
||||||
|
class="upload-input"
|
||||||
|
type="file"
|
||||||
|
multiple
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
ref="folderUploadButton"
|
||||||
|
@change="uploadHandler"
|
||||||
|
class="upload-input"
|
||||||
|
type="file"
|
||||||
|
webkitdirectory
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
<SvgButton name="add-file" data-tooltip="Upload files" @click="fileUploadButton.click()" />
|
||||||
|
<SvgButton name="add-folder" data-tooltip="Upload folder" @click="folderUploadButton.click()" />
|
||||||
|
</template>
|
||||||
55
cista-front/src/repositories/Document.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
export type FUID = string
|
||||||
|
|
||||||
|
export type Document = {
|
||||||
|
loc: string
|
||||||
|
name: string
|
||||||
|
key: FUID
|
||||||
|
size: number
|
||||||
|
sizedisp: string
|
||||||
|
mtime: number
|
||||||
|
modified: string
|
||||||
|
haystack: string
|
||||||
|
dir: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export type errorEvent = {
|
||||||
|
error: {
|
||||||
|
code: number
|
||||||
|
message: string
|
||||||
|
redirect: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Raw types the backend /api/watch sends us
|
||||||
|
|
||||||
|
export type FileEntry = {
|
||||||
|
key: FUID
|
||||||
|
size: number
|
||||||
|
mtime: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export type DirEntry = {
|
||||||
|
key: FUID
|
||||||
|
size: number
|
||||||
|
mtime: number
|
||||||
|
dir: DirList
|
||||||
|
}
|
||||||
|
|
||||||
|
export type DirList = Record<string, FileEntry | DirEntry>
|
||||||
|
|
||||||
|
export type UpdateEntry = {
|
||||||
|
name: string
|
||||||
|
deleted?: boolean
|
||||||
|
key?: FUID
|
||||||
|
size?: number
|
||||||
|
mtime?: number
|
||||||
|
dir?: DirList
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper structure for selections
|
||||||
|
export interface SelectedItems {
|
||||||
|
keys: FUID[]
|
||||||
|
docs: Record<FUID, Document>
|
||||||
|
recursive: Array<[string, string, Document]>
|
||||||
|
missing: Set<FUID>
|
||||||
|
}
|
||||||
@@ -1,33 +1,14 @@
|
|||||||
import { useMainStore } from "@/stores/main"
|
import { useDocumentStore } from "@/stores/documents"
|
||||||
import type { FileEntry, UpdateEntry, errorEvent } from "./Document"
|
import type { DirEntry, UpdateEntry, errorEvent } from "./Document"
|
||||||
|
|
||||||
export const controlUrl = '/api/control'
|
export const controlUrl = '/api/control'
|
||||||
export const uploadUrl = '/api/upload'
|
export const uploadUrl = '/api/upload'
|
||||||
export const watchUrl = '/api/watch'
|
export const watchUrl = '/api/watch'
|
||||||
|
|
||||||
let tree = [] as FileEntry[]
|
let tree = null as DirEntry | null
|
||||||
let reconnDelay = 500
|
let reconnectDuration = 500
|
||||||
let wsWatch = null as WebSocket | null
|
let wsWatch = null as WebSocket | null
|
||||||
|
|
||||||
export const loadSession = () => {
|
|
||||||
const s = localStorage['cista-files']
|
|
||||||
if (!s) return false
|
|
||||||
const store = useMainStore()
|
|
||||||
try {
|
|
||||||
tree = JSON.parse(s)
|
|
||||||
store.updateRoot(tree)
|
|
||||||
console.log(`Loaded session with ${tree.length} items cached`)
|
|
||||||
return true
|
|
||||||
} catch (error) {
|
|
||||||
console.log("Loading session failed", error)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const saveSession = () => {
|
|
||||||
localStorage["cista-files"] = JSON.stringify(tree)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const connect = (path: string, handlers: Partial<Record<keyof WebSocketEventMap, any>>) => {
|
export const connect = (path: string, handlers: Partial<Record<keyof WebSocketEventMap, any>>) => {
|
||||||
const webSocket = new WebSocket(new URL(path, location.origin.replace(/^http/, 'ws')))
|
const webSocket = new WebSocket(new URL(path, location.origin.replace(/^http/, 'ws')))
|
||||||
for (const [event, handler] of Object.entries(handlers)) webSocket.addEventListener(event, handler)
|
for (const [event, handler] of Object.entries(handlers)) webSocket.addEventListener(event, handler)
|
||||||
@@ -39,7 +20,7 @@ export const watchConnect = () => {
|
|||||||
clearTimeout(watchTimeout)
|
clearTimeout(watchTimeout)
|
||||||
watchTimeout = null
|
watchTimeout = null
|
||||||
}
|
}
|
||||||
const store = useMainStore()
|
const store = useDocumentStore()
|
||||||
if (store.error !== 'Reconnecting...') store.error = 'Connecting...'
|
if (store.error !== 'Reconnecting...') store.error = 'Connecting...'
|
||||||
console.log(store.error)
|
console.log(store.error)
|
||||||
|
|
||||||
@@ -61,9 +42,8 @@ export const watchConnect = () => {
|
|||||||
}
|
}
|
||||||
if ("server" in msg) {
|
if ("server" in msg) {
|
||||||
console.log('Connected to backend', msg)
|
console.log('Connected to backend', msg)
|
||||||
store.server = msg.server
|
|
||||||
store.connected = true
|
store.connected = true
|
||||||
reconnDelay = 500
|
reconnectDuration = 500
|
||||||
store.error = ''
|
store.error = ''
|
||||||
if (msg.user) store.login(msg.user.username, msg.user.privileged)
|
if (msg.user) store.login(msg.user.username, msg.user.privileged)
|
||||||
else if (store.isUserLogged) store.logout()
|
else if (store.isUserLogged) store.logout()
|
||||||
@@ -81,16 +61,16 @@ export const watchDisconnect = () => {
|
|||||||
let watchTimeout: any = null
|
let watchTimeout: any = null
|
||||||
|
|
||||||
const watchReconnect = (event: MessageEvent) => {
|
const watchReconnect = (event: MessageEvent) => {
|
||||||
const store = useMainStore()
|
const store = useDocumentStore()
|
||||||
if (store.connected) {
|
if (store.connected) {
|
||||||
console.warn("Disconnected from server", event)
|
console.warn("Disconnected from server", event)
|
||||||
store.connected = false
|
store.connected = false
|
||||||
store.error = 'Reconnecting...'
|
store.error = 'Reconnecting...'
|
||||||
}
|
}
|
||||||
reconnDelay = Math.min(5000, reconnDelay + 500)
|
reconnectDuration = Math.min(5000, reconnectDuration + 500)
|
||||||
// The server closes the websocket after errors, so we need to reopen it
|
// The server closes the websocket after errors, so we need to reopen it
|
||||||
if (watchTimeout !== null) clearTimeout(watchTimeout)
|
if (watchTimeout !== null) clearTimeout(watchTimeout)
|
||||||
watchTimeout = setTimeout(watchConnect, reconnDelay)
|
watchTimeout = setTimeout(watchConnect, reconnectDuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -113,40 +93,38 @@ const handleWatchMessage = (event: MessageEvent) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleRootMessage({ root }: { root: FileEntry[] }) {
|
function handleRootMessage({ root }: { root: DirEntry }) {
|
||||||
const store = useMainStore()
|
const store = useDocumentStore()
|
||||||
console.log('Watch root', root)
|
console.log('Watch root', root)
|
||||||
store.updateRoot(root)
|
store.updateRoot(root)
|
||||||
tree = root
|
tree = root
|
||||||
saveSession()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleUpdateMessage(updateData: { update: UpdateEntry[] }) {
|
function handleUpdateMessage(updateData: { update: UpdateEntry[] }) {
|
||||||
const store = useMainStore()
|
const store = useDocumentStore()
|
||||||
const update = updateData.update
|
console.log('Watch update', updateData.update)
|
||||||
console.log('Watch update', update)
|
|
||||||
if (!tree) return console.error('Watch update before root')
|
if (!tree) return console.error('Watch update before root')
|
||||||
let newtree = []
|
let node: DirEntry = tree
|
||||||
let oidx = 0
|
for (const elem of updateData.update) {
|
||||||
|
if (elem.deleted) {
|
||||||
for (const [action, arg] of update) {
|
delete node.dir[elem.name]
|
||||||
if (action === 'k') {
|
break // Deleted elements can't have further children
|
||||||
newtree.push(...tree.slice(oidx, oidx + arg))
|
|
||||||
oidx += arg
|
|
||||||
}
|
}
|
||||||
else if (action === 'd') oidx += arg
|
if (elem.name) {
|
||||||
else if (action === 'i') newtree.push(...arg)
|
// @ts-ignore
|
||||||
else console.log("Unknown update action", action, arg)
|
console.log(node, elem.name)
|
||||||
|
node = node.dir[elem.name] ||= {}
|
||||||
|
}
|
||||||
|
if (elem.key !== undefined) node.key = elem.key
|
||||||
|
if (elem.size !== undefined) node.size = elem.size
|
||||||
|
if (elem.mtime !== undefined) node.mtime = elem.mtime
|
||||||
|
if (elem.dir !== undefined) node.dir = elem.dir
|
||||||
}
|
}
|
||||||
if (oidx != tree.length)
|
store.updateRoot(tree)
|
||||||
throw Error(`Tree update out of sync, number of entries mismatch: got ${oidx}, expected ${tree.length}, new tree ${newtree.length}`)
|
|
||||||
store.updateRoot(newtree)
|
|
||||||
tree = newtree
|
|
||||||
saveSession()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleError(msg: errorEvent) {
|
function handleError(msg: errorEvent) {
|
||||||
const store = useMainStore()
|
const store = useDocumentStore()
|
||||||
if (msg.error.code === 401) {
|
if (msg.error.code === 401) {
|
||||||
store.user.isOpenLoginModal = true
|
store.user.isOpenLoginModal = true
|
||||||
store.user.isLoggedIn = false
|
store.user.isLoggedIn = false
|
||||||
183
cista-front/src/stores/documents.ts
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
import type {
|
||||||
|
Document,
|
||||||
|
DirEntry,
|
||||||
|
FileEntry,
|
||||||
|
FUID,
|
||||||
|
SelectedItems
|
||||||
|
} from '@/repositories/Document'
|
||||||
|
import { formatSize, formatUnixDate, haystackFormat } from '@/utils'
|
||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import { collator } from '@/utils'
|
||||||
|
import { logoutUser } from '@/repositories/User'
|
||||||
|
import { watchConnect } from '@/repositories/WS'
|
||||||
|
|
||||||
|
type FileData = { id: string; mtime: number; size: number; dir: DirectoryData }
|
||||||
|
type DirectoryData = {
|
||||||
|
[filename: string]: FileData
|
||||||
|
}
|
||||||
|
type User = {
|
||||||
|
username: string
|
||||||
|
privileged: boolean
|
||||||
|
isOpenLoginModal: boolean
|
||||||
|
isLoggedIn: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useDocumentStore = defineStore({
|
||||||
|
id: 'documents',
|
||||||
|
state: () => ({
|
||||||
|
document: [] as Document[],
|
||||||
|
search: "" as string,
|
||||||
|
selected: new Set<FUID>(),
|
||||||
|
uploadingDocuments: [],
|
||||||
|
uploadCount: 0 as number,
|
||||||
|
fileExplorer: null,
|
||||||
|
error: '' as string,
|
||||||
|
connected: false,
|
||||||
|
user: {
|
||||||
|
username: '',
|
||||||
|
privileged: false,
|
||||||
|
isLoggedIn: false,
|
||||||
|
isOpenLoginModal: false
|
||||||
|
} as User
|
||||||
|
}),
|
||||||
|
persist: {
|
||||||
|
storage: sessionStorage,
|
||||||
|
paths: ['document'],
|
||||||
|
},
|
||||||
|
actions: {
|
||||||
|
updateRoot(root: DirEntry | null = null) {
|
||||||
|
if (!root) {
|
||||||
|
this.document = []
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Transform tree data to flat documents array
|
||||||
|
let loc = ""
|
||||||
|
const mapper = ([name, attr]: [string, FileEntry | DirEntry]) => ({
|
||||||
|
...attr,
|
||||||
|
loc,
|
||||||
|
name,
|
||||||
|
sizedisp: formatSize(attr.size),
|
||||||
|
modified: formatUnixDate(attr.mtime),
|
||||||
|
haystack: haystackFormat(name),
|
||||||
|
})
|
||||||
|
const queue = [...Object.entries(root.dir ?? {}).map(mapper)]
|
||||||
|
const docs = []
|
||||||
|
for (let doc; (doc = queue.shift()) !== undefined;) {
|
||||||
|
docs.push(doc)
|
||||||
|
if ("dir" in doc) {
|
||||||
|
// Recurse but replace recursive structure with boolean
|
||||||
|
loc = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
queue.push(...Object.entries(doc.dir).map(mapper))
|
||||||
|
// @ts-ignore
|
||||||
|
doc.dir = true
|
||||||
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
else doc.dir = false
|
||||||
|
}
|
||||||
|
// Pre sort directory entries folders first then files, names in natural ordering
|
||||||
|
docs.sort((a, b) =>
|
||||||
|
// @ts-ignore
|
||||||
|
b.dir - a.dir ||
|
||||||
|
collator.compare(a.name, b.name)
|
||||||
|
)
|
||||||
|
this.document = docs as Document[]
|
||||||
|
},
|
||||||
|
updateUploadingDocuments(key: number, progress: number) {
|
||||||
|
for (const d of this.uploadingDocuments) {
|
||||||
|
if (d.key === key) d.progress = progress
|
||||||
|
}
|
||||||
|
},
|
||||||
|
pushUploadingDocuments(name: string) {
|
||||||
|
this.uploadCount++
|
||||||
|
const document = {
|
||||||
|
key: this.uploadCount,
|
||||||
|
name: name,
|
||||||
|
progress: 0
|
||||||
|
}
|
||||||
|
this.uploadingDocuments.push(document)
|
||||||
|
return document
|
||||||
|
},
|
||||||
|
deleteUploadingDocument(key: number) {
|
||||||
|
this.uploadingDocuments = this.uploadingDocuments.filter(e => e.key !== key)
|
||||||
|
},
|
||||||
|
updateModified() {
|
||||||
|
for (const d of this.document) {
|
||||||
|
if ('mtime' in d) d.modified = formatUnixDate(d.mtime)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
login(username: string, privileged: boolean) {
|
||||||
|
this.user.username = username
|
||||||
|
this.user.privileged = privileged
|
||||||
|
this.user.isLoggedIn = true
|
||||||
|
this.user.isOpenLoginModal = false
|
||||||
|
if (!this.connected) watchConnect()
|
||||||
|
},
|
||||||
|
loginDialog() {
|
||||||
|
this.user.isOpenLoginModal = true
|
||||||
|
},
|
||||||
|
async logout() {
|
||||||
|
console.log("Logout")
|
||||||
|
await logoutUser()
|
||||||
|
this.$reset()
|
||||||
|
history.go() // Reload page
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getters: {
|
||||||
|
isUserLogged(): boolean {
|
||||||
|
return this.user.isLoggedIn
|
||||||
|
},
|
||||||
|
recentDocuments(): Document[] {
|
||||||
|
const ret = [...this.document]
|
||||||
|
ret.sort((a, b) => b.mtime - a.mtime)
|
||||||
|
return ret
|
||||||
|
},
|
||||||
|
largeDocuments(): Document[] {
|
||||||
|
const ret = [...this.document]
|
||||||
|
ret.sort((a, b) => b.size - a.size)
|
||||||
|
return ret
|
||||||
|
},
|
||||||
|
selectedFiles(): SelectedItems {
|
||||||
|
const selected = this.selected
|
||||||
|
const found = new Set<FUID>()
|
||||||
|
const ret: SelectedItems = {
|
||||||
|
missing: new Set(),
|
||||||
|
docs: {},
|
||||||
|
keys: [],
|
||||||
|
recursive: [],
|
||||||
|
}
|
||||||
|
for (const doc of this.document) {
|
||||||
|
if (selected.has(doc.key)) {
|
||||||
|
found.add(doc.key)
|
||||||
|
ret.keys.push(doc.key)
|
||||||
|
ret.docs[doc.key] = doc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// What did we not select?
|
||||||
|
for (const key of selected) if (!found.has(key)) ret.missing.add(key)
|
||||||
|
// Build a flat list including contents recursively
|
||||||
|
const relnames = new Set<string>()
|
||||||
|
function add(rel: string, full: string, doc: Document) {
|
||||||
|
if (!doc.dir && relnames.has(rel)) throw Error(`Multiple selections conflict for: ${rel}`)
|
||||||
|
relnames.add(rel)
|
||||||
|
ret.recursive.push([rel, full, doc])
|
||||||
|
}
|
||||||
|
for (const key of ret.keys) {
|
||||||
|
const base = ret.docs[key]
|
||||||
|
const basepath = base.loc ? `${base.loc}/${base.name}` : base.name
|
||||||
|
const nremove = base.loc.length
|
||||||
|
add(base.name, basepath, base)
|
||||||
|
for (const doc of this.document) {
|
||||||
|
if (doc.loc === basepath || doc.loc.startsWith(basepath) && doc.loc[basepath.length] === '/') {
|
||||||
|
const full = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
const rel = full.slice(nremove)
|
||||||
|
add(rel, full, doc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Sort by rel (name stored as on download)
|
||||||
|
ret.recursive.sort((a, b) => collator.compare(a[0], b[0]))
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -86,7 +86,7 @@ export function haystackFormat(str: string) {
|
|||||||
// Preformat search string for faster search
|
// Preformat search string for faster search
|
||||||
export function needleFormat(query: string) {
|
export function needleFormat(query: string) {
|
||||||
const based = query.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase()
|
const based = query.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase()
|
||||||
return {based, words: based.split(/\s+/)}
|
return {based, words: based.split(/\W+/)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if haystack includes needle
|
// Test if haystack includes needle
|
||||||
@@ -10,40 +10,33 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { watchEffect, ref, computed } from 'vue'
|
import { watchEffect, ref, computed } from 'vue'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useDocumentStore } from '@/stores/documents'
|
||||||
import Router from '@/router/index'
|
import Router from '@/router/index'
|
||||||
import { needleFormat, localeIncludes, collator } from '@/utils';
|
import { needleFormat, localeIncludes, collator } from '@/utils';
|
||||||
import { sorted } from '@/utils/docsort';
|
|
||||||
|
|
||||||
const store = useMainStore()
|
const documentStore = useDocumentStore()
|
||||||
const fileExplorer = ref()
|
const fileExplorer = ref()
|
||||||
const props = defineProps<{
|
const props = defineProps({
|
||||||
path: Array<string>
|
path: Array<string>
|
||||||
query: string
|
})
|
||||||
}>()
|
|
||||||
const documents = computed(() => {
|
const documents = computed(() => {
|
||||||
|
if (!props.path) return []
|
||||||
const loc = props.path.join('/')
|
const loc = props.path.join('/')
|
||||||
const query = props.query
|
|
||||||
// List the current location
|
// List the current location
|
||||||
if (!query) return sorted(
|
if (!documentStore.search) return documentStore.document.filter(doc => doc.loc === loc)
|
||||||
store.document.filter(doc => doc.loc === loc),
|
|
||||||
store.prefs.sortListing,
|
|
||||||
)
|
|
||||||
// Find up to 100 newest documents that match the search
|
// Find up to 100 newest documents that match the search
|
||||||
const needle = needleFormat(query)
|
const search = documentStore.search
|
||||||
|
const needle = needleFormat(search)
|
||||||
let limit = 100
|
let limit = 100
|
||||||
let docs = []
|
let docs = []
|
||||||
for (const doc of store.recentDocuments) {
|
for (const doc of documentStore.recentDocuments) {
|
||||||
if (localeIncludes(doc.haystack, needle)) {
|
if (localeIncludes(doc.haystack, needle)) {
|
||||||
docs.push(doc)
|
docs.push(doc)
|
||||||
if (--limit === 0) break
|
if (--limit === 0) break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Organize by folder, by relevance
|
||||||
const locsub = loc + '/'
|
const locsub = loc + '/'
|
||||||
// Custom sort override in effect?
|
|
||||||
const order = store.prefs.sortFiltered
|
|
||||||
if (order) return sorted(docs, order)
|
|
||||||
// Sort by relevance - current folder, then subfolders, then others
|
|
||||||
docs.sort((a, b) => (
|
docs.sort((a, b) => (
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
(b.loc === loc) - (a.loc === loc) ||
|
(b.loc === loc) - (a.loc === loc) ||
|
||||||
@@ -53,14 +46,13 @@ const documents = computed(() => {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
(a.type === 'file') - (b.type === 'file') ||
|
(a.type === 'file') - (b.type === 'file') ||
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
b.name.includes(query) - a.name.includes(query) ||
|
b.name.includes(search) - a.name.includes(search) ||
|
||||||
collator.compare(a.name, b.name)
|
collator.compare(a.name, b.name)
|
||||||
))
|
))
|
||||||
return docs
|
return docs
|
||||||
})
|
})
|
||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
store.fileExplorer = fileExplorer.value
|
documentStore.fileExplorer = fileExplorer.value
|
||||||
store.query = props.query
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
@@ -62,15 +62,12 @@ def _main():
|
|||||||
_confdir(args)
|
_confdir(args)
|
||||||
exists = config.conffile.exists()
|
exists = config.conffile.exists()
|
||||||
import_droppy = args["--import-droppy"]
|
import_droppy = args["--import-droppy"]
|
||||||
necessary_opts = exists or import_droppy or path
|
necessary_opts = exists or import_droppy or path and listen
|
||||||
if not necessary_opts:
|
if not necessary_opts:
|
||||||
# Maybe run without arguments
|
# Maybe run without arguments
|
||||||
print(doc)
|
print(doc)
|
||||||
print(
|
print(
|
||||||
"No config file found! Get started with one of:\n"
|
"No config file found! Get started with:\n cista -l :8000 /path/to/files, or\n cista -l example.com --import-droppy # Uses Droppy files\n",
|
||||||
" cista --user yourname --privileged\n"
|
|
||||||
" cista --import-droppy\n"
|
|
||||||
" cista -l :8000 /path/to/files\n"
|
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
settings = {}
|
settings = {}
|
||||||
@@ -82,15 +79,8 @@ def _main():
|
|||||||
settings = droppy.readconf()
|
settings = droppy.readconf()
|
||||||
if path:
|
if path:
|
||||||
settings["path"] = path
|
settings["path"] = path
|
||||||
elif not exists:
|
|
||||||
settings["path"] = Path.home() / "Downloads"
|
|
||||||
if listen:
|
if listen:
|
||||||
settings["listen"] = listen
|
settings["listen"] = listen
|
||||||
elif not exists:
|
|
||||||
settings["listen"] = ":8000"
|
|
||||||
if not exists and not import_droppy:
|
|
||||||
# We have no users, so make it public
|
|
||||||
settings["public"] = True
|
|
||||||
operation = config.update_config(settings)
|
operation = config.update_config(settings)
|
||||||
print(f"Config {operation}: {config.conffile}")
|
print(f"Config {operation}: {config.conffile}")
|
||||||
# Prepare to serve
|
# Prepare to serve
|
||||||
@@ -115,30 +105,18 @@ def _confdir(args):
|
|||||||
if confdir.exists() and not confdir.is_dir():
|
if confdir.exists() and not confdir.is_dir():
|
||||||
if confdir.name != config.conffile.name:
|
if confdir.name != config.conffile.name:
|
||||||
raise ValueError("Config path is not a directory")
|
raise ValueError("Config path is not a directory")
|
||||||
# Accidentally pointed to the db.toml, use parent
|
# Accidentally pointed to the cista.toml, use parent
|
||||||
confdir = confdir.parent
|
confdir = confdir.parent
|
||||||
config.conffile = confdir / config.conffile.name
|
config.conffile = config.conffile.with_parent(confdir)
|
||||||
|
|
||||||
|
|
||||||
def _user(args):
|
def _user(args):
|
||||||
_confdir(args)
|
_confdir(args)
|
||||||
if config.conffile.exists():
|
config.load_config()
|
||||||
config.load_config()
|
|
||||||
operation = False
|
|
||||||
else:
|
|
||||||
# Defaults for new config when user is created
|
|
||||||
operation = config.update_config(
|
|
||||||
{
|
|
||||||
"listen": ":8000",
|
|
||||||
"path": Path.home() / "Downloads",
|
|
||||||
"public": False,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
print(f"Config {operation}: {config.conffile}\n")
|
|
||||||
|
|
||||||
name = args["--user"]
|
name = args["--user"]
|
||||||
if not name or not name.isidentifier():
|
if not name or not name.isidentifier():
|
||||||
raise ValueError("Invalid username")
|
raise ValueError("Invalid username")
|
||||||
|
config.load_config()
|
||||||
u = config.config.users.get(name)
|
u = config.config.users.get(name)
|
||||||
info = f"User {name}" if u else f"New user {name}"
|
info = f"User {name}" if u else f"New user {name}"
|
||||||
changes = {}
|
changes = {}
|
||||||
@@ -150,17 +128,12 @@ def _user(args):
|
|||||||
info += " (admin)" if oldadmin else ""
|
info += " (admin)" if oldadmin else ""
|
||||||
if args["--password"] or not u:
|
if args["--password"] or not u:
|
||||||
changes["password"] = pw = pwgen.generate()
|
changes["password"] = pw = pwgen.generate()
|
||||||
info += f"\n Password: {pw}\n"
|
info += f"\n Password: {pw}"
|
||||||
res = config.update_user(name, changes)
|
res = config.update_user(args["--user"], changes)
|
||||||
print(info)
|
print(info)
|
||||||
if res == "read":
|
if res == "read":
|
||||||
print(" No changes")
|
print(" No changes")
|
||||||
|
|
||||||
if operation == "created":
|
|
||||||
print(
|
|
||||||
"Now you can run the server:\n cista # defaults set: -l :8000 ~/Downloads\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|||||||
20
cista/api.py
@@ -37,18 +37,10 @@ async def upload(req, ws):
|
|||||||
)
|
)
|
||||||
req = msgspec.json.decode(text, type=FileRange)
|
req = msgspec.json.decode(text, type=FileRange)
|
||||||
pos = req.start
|
pos = req.start
|
||||||
while True:
|
data = None
|
||||||
data = await ws.recv()
|
while pos < req.end and (data := await ws.recv()) and isinstance(data, bytes):
|
||||||
if not isinstance(data, bytes):
|
|
||||||
break
|
|
||||||
if len(data) > req.end - pos:
|
|
||||||
raise ValueError(
|
|
||||||
f"Expected up to {req.end - pos} bytes, got {len(data)} bytes"
|
|
||||||
)
|
|
||||||
sentsize = await alink(("upload", req.name, pos, data, req.size))
|
sentsize = await alink(("upload", req.name, pos, data, req.size))
|
||||||
pos += typing.cast(int, sentsize)
|
pos += typing.cast(int, sentsize)
|
||||||
if pos >= req.end:
|
|
||||||
break
|
|
||||||
if pos != req.end:
|
if pos != req.end:
|
||||||
d = f"{len(data)} bytes" if isinstance(data, bytes) else data
|
d = f"{len(data)} bytes" if isinstance(data, bytes) else data
|
||||||
raise ValueError(f"Expected {req.end - pos} more bytes, got {d}")
|
raise ValueError(f"Expected {req.end - pos} more bytes, got {d}")
|
||||||
@@ -96,7 +88,7 @@ async def watch(req, ws):
|
|||||||
msgspec.json.encode(
|
msgspec.json.encode(
|
||||||
{
|
{
|
||||||
"server": {
|
"server": {
|
||||||
"name": config.config.name or config.config.path.name,
|
"name": "Cista", # Should be configurable
|
||||||
"version": __version__,
|
"version": __version__,
|
||||||
"public": config.config.public,
|
"public": config.config.public,
|
||||||
},
|
},
|
||||||
@@ -111,11 +103,11 @@ async def watch(req, ws):
|
|||||||
)
|
)
|
||||||
uuid = token_bytes(16)
|
uuid = token_bytes(16)
|
||||||
try:
|
try:
|
||||||
with watching.state.lock:
|
with watching.tree_lock:
|
||||||
q = watching.pubsub[uuid] = asyncio.Queue()
|
q = watching.pubsub[uuid] = asyncio.Queue()
|
||||||
# Init with disk usage and full tree
|
# Init with disk usage and full tree
|
||||||
await ws.send(watching.format_space(watching.state.space))
|
await ws.send(watching.format_du())
|
||||||
await ws.send(watching.format_root(watching.state.root))
|
await ws.send(watching.format_tree())
|
||||||
# Send updates
|
# Send updates
|
||||||
while True:
|
while True:
|
||||||
await ws.send(await q.get())
|
await ws.send(await q.get())
|
||||||
|
|||||||
129
cista/app.py
@@ -1,8 +1,10 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
from collections import deque
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from pathlib import Path, PurePath, PurePosixPath
|
from importlib.resources import files
|
||||||
|
from pathlib import Path
|
||||||
from stat import S_IFDIR, S_IFREG
|
from stat import S_IFDIR, S_IFREG
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
from wsgiref.handlers import format_date_time
|
from wsgiref.handlers import format_date_time
|
||||||
@@ -10,6 +12,7 @@ from wsgiref.handlers import format_date_time
|
|||||||
import brotli
|
import brotli
|
||||||
import sanic.helpers
|
import sanic.helpers
|
||||||
from blake3 import blake3
|
from blake3 import blake3
|
||||||
|
from natsort import natsorted, ns
|
||||||
from sanic import Blueprint, Sanic, empty, raw
|
from sanic import Blueprint, Sanic, empty, raw
|
||||||
from sanic.exceptions import Forbidden, NotFound
|
from sanic.exceptions import Forbidden, NotFound
|
||||||
from sanic.log import logging
|
from sanic.log import logging
|
||||||
@@ -17,6 +20,7 @@ from stream_zip import ZIP_AUTO, stream_zip
|
|||||||
|
|
||||||
from cista import auth, config, session, watching
|
from cista import auth, config, session, watching
|
||||||
from cista.api import bp
|
from cista.api import bp
|
||||||
|
from cista.protocol import DirEntry
|
||||||
from cista.util.apphelpers import handle_sanic_exception
|
from cista.util.apphelpers import handle_sanic_exception
|
||||||
|
|
||||||
# Workaround until Sanic PR #2824 is merged
|
# Workaround until Sanic PR #2824 is merged
|
||||||
@@ -32,9 +36,7 @@ app.exception(Exception)(handle_sanic_exception)
|
|||||||
async def main_start(app, loop):
|
async def main_start(app, loop):
|
||||||
config.load_config()
|
config.load_config()
|
||||||
await watching.start(app, loop)
|
await watching.start(app, loop)
|
||||||
app.ctx.threadexec = ThreadPoolExecutor(
|
app.ctx.threadexec = ThreadPoolExecutor(max_workers=8)
|
||||||
max_workers=8, thread_name_prefix="cista-ioworker"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.after_server_stop
|
@app.after_server_stop
|
||||||
@@ -47,8 +49,8 @@ async def main_stop(app, loop):
|
|||||||
async def use_session(req):
|
async def use_session(req):
|
||||||
req.ctx.session = session.get(req)
|
req.ctx.session = session.get(req)
|
||||||
try:
|
try:
|
||||||
req.ctx.username = req.ctx.session["username"] # type: ignore
|
req.ctx.username = req.ctx.session["username"]
|
||||||
req.ctx.user = config.config.users[req.ctx.username]
|
req.ctx.user = config.config.users[req.ctx.session["username"]] # type: ignore
|
||||||
except (AttributeError, KeyError, TypeError):
|
except (AttributeError, KeyError, TypeError):
|
||||||
req.ctx.username = None
|
req.ctx.username = None
|
||||||
req.ctx.user = None
|
req.ctx.user = None
|
||||||
@@ -79,16 +81,22 @@ def http_fileserver(app, _):
|
|||||||
www = {}
|
www = {}
|
||||||
|
|
||||||
|
|
||||||
|
@app.before_server_start
|
||||||
|
async def load_wwwroot(*_ignored):
|
||||||
|
global www
|
||||||
|
www = await asyncio.get_event_loop().run_in_executor(None, _load_wwwroot, www)
|
||||||
|
|
||||||
|
|
||||||
def _load_wwwroot(www):
|
def _load_wwwroot(www):
|
||||||
wwwnew = {}
|
wwwnew = {}
|
||||||
base = Path(__file__).with_name("wwwroot")
|
base = files("cista") / "wwwroot"
|
||||||
paths = [PurePath()]
|
paths = ["."]
|
||||||
while paths:
|
while paths:
|
||||||
path = paths.pop(0)
|
path = paths.pop(0)
|
||||||
current = base / path
|
current = base / path
|
||||||
for p in current.iterdir():
|
for p in current.iterdir():
|
||||||
if p.is_dir():
|
if p.is_dir():
|
||||||
paths.append(p.relative_to(base))
|
paths.append(current / p.parts[-1])
|
||||||
continue
|
continue
|
||||||
name = p.relative_to(base).as_posix()
|
name = p.relative_to(base).as_posix()
|
||||||
mime = mimetypes.guess_type(name)[0] or "application/octet-stream"
|
mime = mimetypes.guess_type(name)[0] or "application/octet-stream"
|
||||||
@@ -119,44 +127,15 @@ def _load_wwwroot(www):
|
|||||||
if len(br) >= len(data):
|
if len(br) >= len(data):
|
||||||
br = False
|
br = False
|
||||||
wwwnew[name] = data, br, headers
|
wwwnew[name] = data, br, headers
|
||||||
if not wwwnew:
|
|
||||||
msg = f"Web frontend missing from {base}\n Did you forget: hatch build\n"
|
|
||||||
if not www:
|
|
||||||
logging.warning(msg)
|
|
||||||
if not app.debug:
|
|
||||||
msg = "Web frontend missing. Cista installation is broken.\n"
|
|
||||||
wwwnew[""] = (
|
|
||||||
msg.encode(),
|
|
||||||
False,
|
|
||||||
{
|
|
||||||
"etag": "error",
|
|
||||||
"content-type": "text/plain",
|
|
||||||
"cache-control": "no-store",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
return wwwnew
|
return wwwnew
|
||||||
|
|
||||||
|
|
||||||
@app.before_server_start
|
@app.add_task
|
||||||
async def start(app):
|
|
||||||
await load_wwwroot(app)
|
|
||||||
if app.debug:
|
|
||||||
app.add_task(refresh_wwwroot())
|
|
||||||
|
|
||||||
|
|
||||||
async def load_wwwroot(app):
|
|
||||||
global www
|
|
||||||
www = await asyncio.get_event_loop().run_in_executor(
|
|
||||||
app.ctx.threadexec, _load_wwwroot, www
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def refresh_wwwroot():
|
async def refresh_wwwroot():
|
||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(0.5)
|
|
||||||
try:
|
try:
|
||||||
wwwold = www
|
wwwold = www
|
||||||
await load_wwwroot(app)
|
await load_wwwroot()
|
||||||
changes = ""
|
changes = ""
|
||||||
for name in sorted(www):
|
for name in sorted(www):
|
||||||
attr = www[name]
|
attr = www[name]
|
||||||
@@ -172,6 +151,7 @@ async def refresh_wwwroot():
|
|||||||
print("Error loading wwwroot", e)
|
print("Error loading wwwroot", e)
|
||||||
if not app.debug:
|
if not app.debug:
|
||||||
return
|
return
|
||||||
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<path:path>", methods=["GET", "HEAD"])
|
@app.route("/<path:path>", methods=["GET", "HEAD"])
|
||||||
@@ -186,70 +166,66 @@ async def wwwroot(req, path=""):
|
|||||||
return empty(304, headers=headers)
|
return empty(304, headers=headers)
|
||||||
# Brotli compressed?
|
# Brotli compressed?
|
||||||
if br and "br" in req.headers.accept_encoding.split(", "):
|
if br and "br" in req.headers.accept_encoding.split(", "):
|
||||||
headers = {**headers, "content-encoding": "br"}
|
headers = {
|
||||||
|
**headers,
|
||||||
|
"content-encoding": "br",
|
||||||
|
}
|
||||||
data = br
|
data = br
|
||||||
return raw(data, headers=headers)
|
return raw(data, headers=headers)
|
||||||
|
|
||||||
|
|
||||||
def get_files(wanted: set) -> list[tuple[PurePosixPath, Path]]:
|
|
||||||
loc = PurePosixPath()
|
|
||||||
idx = 0
|
|
||||||
ret = []
|
|
||||||
level: int | None = None
|
|
||||||
parent: PurePosixPath | None = None
|
|
||||||
with watching.state.lock:
|
|
||||||
root = watching.state.root
|
|
||||||
while idx < len(root):
|
|
||||||
f = root[idx]
|
|
||||||
loc = PurePosixPath(*loc.parts[: f.level - 1]) / f.name
|
|
||||||
if parent is not None and f.level <= level:
|
|
||||||
level = parent = None
|
|
||||||
if f.key in wanted:
|
|
||||||
level, parent = f.level, loc.parent
|
|
||||||
if parent is not None:
|
|
||||||
wanted.discard(f.key)
|
|
||||||
ret.append((loc.relative_to(parent), watching.rootpath / loc))
|
|
||||||
idx += 1
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/zip/<keys>/<zipfile:ext=zip>")
|
@app.get("/zip/<keys>/<zipfile:ext=zip>")
|
||||||
async def zip_download(req, keys, zipfile, ext):
|
async def zip_download(req, keys, zipfile, ext):
|
||||||
"""Download a zip archive of the given keys"""
|
"""Download a zip archive of the given keys"""
|
||||||
|
|
||||||
wanted = set(keys.split("+"))
|
wanted = set(keys.split("+"))
|
||||||
files = get_files(wanted)
|
with watching.tree_lock:
|
||||||
|
q = deque([([], None, watching.tree[""].dir)])
|
||||||
|
files = []
|
||||||
|
while q:
|
||||||
|
locpar, relpar, d = q.pop()
|
||||||
|
for name, attr in d.items():
|
||||||
|
loc = [*locpar, name]
|
||||||
|
rel = None
|
||||||
|
if relpar or attr.key in wanted:
|
||||||
|
rel = [*relpar, name] if relpar else [name]
|
||||||
|
wanted.discard(attr.key)
|
||||||
|
isdir = isinstance(attr, DirEntry)
|
||||||
|
if isdir:
|
||||||
|
q.append((loc, rel, attr.dir))
|
||||||
|
if rel:
|
||||||
|
files.append(
|
||||||
|
("/".join(rel), Path(watching.rootpath.joinpath(*loc)))
|
||||||
|
)
|
||||||
|
|
||||||
if not files:
|
if not files:
|
||||||
raise NotFound(
|
raise NotFound(
|
||||||
"No files found",
|
"No files found",
|
||||||
context={"keys": keys, "zipfile": f"{zipfile}.{ext}", "wanted": wanted},
|
context={"keys": keys, "zipfile": zipfile, "wanted": wanted},
|
||||||
)
|
)
|
||||||
if wanted:
|
if wanted:
|
||||||
raise NotFound("Files not found", context={"missing": wanted})
|
raise NotFound("Files not found", context={"missing": wanted})
|
||||||
|
|
||||||
|
files = natsorted(files, key=lambda f: f[0], alg=ns.IGNORECASE)
|
||||||
|
|
||||||
def local_files(files):
|
def local_files(files):
|
||||||
for rel, p in files:
|
for rel, p in files:
|
||||||
s = p.stat()
|
s = p.stat()
|
||||||
size = s.st_size
|
size = s.st_size
|
||||||
modified = datetime.datetime.fromtimestamp(s.st_mtime, datetime.UTC)
|
modified = datetime.datetime.fromtimestamp(s.st_mtime, datetime.UTC)
|
||||||
name = rel.as_posix()
|
|
||||||
if p.is_dir():
|
if p.is_dir():
|
||||||
yield f"{name}/", modified, S_IFDIR | 0o755, ZIP_AUTO(size), iter(b"")
|
yield rel, modified, S_IFDIR | 0o755, ZIP_AUTO(size), b""
|
||||||
else:
|
else:
|
||||||
yield name, modified, S_IFREG | 0o644, ZIP_AUTO(size), contents(p, size)
|
yield rel, modified, S_IFREG | 0o644, ZIP_AUTO(size), contents(p)
|
||||||
|
|
||||||
def contents(name, size):
|
def contents(name):
|
||||||
with name.open("rb") as f:
|
with name.open("rb") as f:
|
||||||
while size > 0 and (chunk := f.read(min(size, 1 << 20))):
|
while chunk := f.read(65536):
|
||||||
size -= len(chunk)
|
|
||||||
yield chunk
|
yield chunk
|
||||||
assert size == 0
|
|
||||||
|
|
||||||
def worker():
|
def worker():
|
||||||
try:
|
try:
|
||||||
for chunk in stream_zip(local_files(files)):
|
for chunk in stream_zip(local_files(files)):
|
||||||
asyncio.run_coroutine_threadsafe(queue.put(chunk), loop).result()
|
asyncio.run_coroutine_threadsafe(queue.put(chunk), loop)
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception("Error streaming ZIP")
|
logging.exception("Error streaming ZIP")
|
||||||
raise
|
raise
|
||||||
@@ -262,10 +238,7 @@ async def zip_download(req, keys, zipfile, ext):
|
|||||||
thread = loop.run_in_executor(app.ctx.threadexec, worker)
|
thread = loop.run_in_executor(app.ctx.threadexec, worker)
|
||||||
|
|
||||||
# Stream the response
|
# Stream the response
|
||||||
res = await req.respond(
|
res = await req.respond(content_type="application/zip")
|
||||||
content_type="application/zip",
|
|
||||||
headers={"cache-control": "no-store"},
|
|
||||||
)
|
|
||||||
while chunk := await queue.get():
|
while chunk := await queue.get():
|
||||||
await res.send(chunk)
|
await res.send(chunk)
|
||||||
|
|
||||||
|
|||||||
@@ -68,10 +68,10 @@ def verify(request, *, privileged=False):
|
|||||||
if request.ctx.user:
|
if request.ctx.user:
|
||||||
if request.ctx.user.privileged:
|
if request.ctx.user.privileged:
|
||||||
return
|
return
|
||||||
raise Forbidden("Access Forbidden: Only for privileged users", quiet=True)
|
raise Forbidden("Access Forbidden: Only for privileged users")
|
||||||
elif config.config.public or request.ctx.user:
|
elif config.config.public or request.ctx.user:
|
||||||
return
|
return
|
||||||
raise Unauthorized("Login required", "cookie", quiet=True)
|
raise Unauthorized("Login required", "cookie")
|
||||||
|
|
||||||
|
|
||||||
bp = Blueprint("auth")
|
bp = Blueprint("auth")
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import secrets
|
import secrets
|
||||||
import sys
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
from pathlib import Path, PurePath
|
from pathlib import Path, PurePath
|
||||||
@@ -15,7 +14,6 @@ class Config(msgspec.Struct):
|
|||||||
listen: str
|
listen: str
|
||||||
secret: str = secrets.token_hex(12)
|
secret: str = secrets.token_hex(12)
|
||||||
public: bool = False
|
public: bool = False
|
||||||
name: str = ""
|
|
||||||
users: dict[str, User] = {}
|
users: dict[str, User] = {}
|
||||||
links: dict[str, Link] = {}
|
links: dict[str, Link] = {}
|
||||||
|
|
||||||
@@ -91,8 +89,6 @@ def config_update(modify):
|
|||||||
return "read"
|
return "read"
|
||||||
f.write(new)
|
f.write(new)
|
||||||
f.close()
|
f.close()
|
||||||
if sys.platform == "win32":
|
|
||||||
conffile.unlink() # Windows doesn't support atomic replace
|
|
||||||
tmpname.rename(conffile) # Atomic replace
|
tmpname.rename(conffile) # Atomic replace
|
||||||
except:
|
except:
|
||||||
f.close()
|
f.close()
|
||||||
@@ -138,7 +134,7 @@ def update_user(conf: Config, name: str, changes: dict) -> Config:
|
|||||||
# Encode into dict, update values with new, convert to Config
|
# Encode into dict, update values with new, convert to Config
|
||||||
try:
|
try:
|
||||||
u = conf.users[name].__copy__()
|
u = conf.users[name].__copy__()
|
||||||
except (KeyError, AttributeError):
|
except KeyError:
|
||||||
u = User()
|
u = User()
|
||||||
if "password" in changes:
|
if "password" in changes:
|
||||||
from . import auth
|
from . import auth
|
||||||
@@ -147,7 +143,7 @@ def update_user(conf: Config, name: str, changes: dict) -> Config:
|
|||||||
del changes["password"]
|
del changes["password"]
|
||||||
udict = msgspec.to_builtins(u, enc_hook=enc_hook)
|
udict = msgspec.to_builtins(u, enc_hook=enc_hook)
|
||||||
udict.update(changes)
|
udict.update(changes)
|
||||||
settings = msgspec.to_builtins(conf, enc_hook=enc_hook) if conf else {"users": {}}
|
settings = msgspec.to_builtins(conf, enc_hook=enc_hook)
|
||||||
settings["users"][name] = msgspec.convert(udict, User, dec_hook=dec_hook)
|
settings["users"][name] = msgspec.convert(udict, User, dec_hook=dec_hook)
|
||||||
return msgspec.convert(settings, Config, dec_hook=dec_hook)
|
return msgspec.convert(settings, Config, dec_hook=dec_hook)
|
||||||
|
|
||||||
|
|||||||
@@ -34,11 +34,9 @@ class File:
|
|||||||
self.open_rw()
|
self.open_rw()
|
||||||
assert self.fd is not None
|
assert self.fd is not None
|
||||||
if file_size is not None:
|
if file_size is not None:
|
||||||
assert pos + len(buffer) <= file_size
|
|
||||||
os.ftruncate(self.fd, file_size)
|
os.ftruncate(self.fd, file_size)
|
||||||
if buffer:
|
os.lseek(self.fd, pos, os.SEEK_SET)
|
||||||
os.lseek(self.fd, pos, os.SEEK_SET)
|
os.write(self.fd, buffer)
|
||||||
os.write(self.fd, buffer)
|
|
||||||
|
|
||||||
def __getitem__(self, slice):
|
def __getitem__(self, slice):
|
||||||
if self.fd is None:
|
if self.fd is None:
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class MkDir(ControlBase):
|
|||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
path = config.config.path / filename.sanitize(self.path)
|
path = config.config.path / filename.sanitize(self.path)
|
||||||
path.mkdir(parents=True, exist_ok=False)
|
path.mkdir(parents=False, exist_ok=False)
|
||||||
|
|
||||||
|
|
||||||
class Rename(ControlBase):
|
class Rename(ControlBase):
|
||||||
@@ -112,40 +112,56 @@ class ErrorMsg(msgspec.Struct):
|
|||||||
## Directory listings
|
## Directory listings
|
||||||
|
|
||||||
|
|
||||||
class FileEntry(msgspec.Struct, array_like=True):
|
class FileEntry(msgspec.Struct):
|
||||||
level: int
|
key: str
|
||||||
|
size: int
|
||||||
|
mtime: int
|
||||||
|
|
||||||
|
|
||||||
|
class DirEntry(msgspec.Struct):
|
||||||
|
key: str
|
||||||
|
size: int
|
||||||
|
mtime: int
|
||||||
|
dir: DirList
|
||||||
|
|
||||||
|
def __getitem__(self, name):
|
||||||
|
return self.dir[name]
|
||||||
|
|
||||||
|
def __setitem__(self, name, value):
|
||||||
|
self.dir[name] = value
|
||||||
|
|
||||||
|
def __contains__(self, name):
|
||||||
|
return name in self.dir
|
||||||
|
|
||||||
|
def __delitem__(self, name):
|
||||||
|
del self.dir[name]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def props(self):
|
||||||
|
return {k: v for k, v in self.__struct_fields__ if k != "dir"}
|
||||||
|
|
||||||
|
|
||||||
|
DirList = dict[str, FileEntry | DirEntry]
|
||||||
|
|
||||||
|
|
||||||
|
class UpdateEntry(msgspec.Struct, omit_defaults=True):
|
||||||
|
"""Updates the named entry in the tree. Fields that are set replace old values. A list of entries recurses directories."""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
key: str
|
key: str
|
||||||
mtime: int
|
deleted: bool = False
|
||||||
size: int
|
size: int | None = None
|
||||||
isfile: int
|
mtime: int | None = None
|
||||||
|
dir: DirList | None = None
|
||||||
def __repr__(self):
|
|
||||||
return self.key or "FileEntry()"
|
|
||||||
|
|
||||||
|
|
||||||
class Update(msgspec.Struct, array_like=True):
|
def make_dir_data(root):
|
||||||
...
|
if len(root) == 3:
|
||||||
|
return FileEntry(*root)
|
||||||
|
id_, size, mtime, listing = root
|
||||||
class UpdKeep(Update, tag="k"):
|
converted = {}
|
||||||
count: int
|
for name, data in listing.items():
|
||||||
|
converted[name] = make_dir_data(data)
|
||||||
|
sz = sum(x.size for x in converted.values())
|
||||||
class UpdDel(Update, tag="d"):
|
mt = max(x.mtime for x in converted.values())
|
||||||
count: int
|
return DirEntry(id_, sz, max(mt, mtime), converted)
|
||||||
|
|
||||||
|
|
||||||
class UpdIns(Update, tag="i"):
|
|
||||||
items: list[FileEntry]
|
|
||||||
|
|
||||||
|
|
||||||
class UpdateMessage(msgspec.Struct):
|
|
||||||
update: list[UpdKeep | UpdDel | UpdIns]
|
|
||||||
|
|
||||||
|
|
||||||
class Space(msgspec.Struct):
|
|
||||||
disk: int
|
|
||||||
free: int
|
|
||||||
usage: int
|
|
||||||
storage: int
|
|
||||||
|
|||||||