Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ac571463a | ||
|
|
d52b24372a | ||
|
|
742b3006c9 | ||
|
|
01a979c00a | ||
|
|
f1e16b7abe | ||
|
|
0ebff0ec17 | ||
|
|
c8ab06d864 | ||
|
|
d5ff7757c8 | ||
|
|
cd604eb10a | ||
|
|
abcf5d9940 | ||
|
|
0190bda853 | ||
|
|
fc48500412 | ||
|
|
d31ad0b525 | ||
|
|
da4bba95be | ||
|
|
e07ab220cb | ||
|
|
3da2f6e6c3 | ||
|
|
ded7ce65bc | ||
|
|
17550be698 | ||
|
|
0b269aef7f | ||
|
|
497de296f2 | ||
|
|
134b216f4c | ||
|
|
06759b3c12 | ||
|
|
c51552ea29 | ||
|
|
00645fc8ff | ||
|
|
760f7bc35d | ||
|
|
8480a73839 | ||
|
|
302ed684e7 | ||
|
|
af35e0480a | ||
|
|
5717486197 | ||
|
|
0061fc54ae | ||
|
|
4eefe83072 | ||
|
|
f578a50007 | ||
|
|
f40d9c1abd | ||
|
|
3d8845cf99 | ||
|
|
87e1443e7d | ||
|
|
f45c57e901 | ||
|
|
41686d1dd1 | ||
|
|
cc351bb992 | ||
|
|
c3abbe0a3b | ||
|
|
127caeedea | ||
|
|
113bc56351 | ||
|
|
c7727c72d9 | ||
|
|
85b3aa6b81 | ||
|
|
62b44ddb43 | ||
|
|
60a53ef3d3 | ||
|
|
acd38c2235 | ||
|
|
2f38f15afa | ||
|
|
0fc28e56bb | ||
|
|
22b0e503e0 | ||
|
|
e3a4ecdcc2 | ||
|
|
9f363e3f66 | ||
|
|
8270dd0cc2 | ||
|
|
1af6cd82fe | ||
|
|
0bc2a12cfa | ||
|
|
9e07a3f40d | ||
|
|
2867b1075d | ||
|
|
4fc8e29cc1 | ||
|
|
55f9117b3e | ||
|
|
2b6746c55e | ||
|
|
1ed3779933 | ||
|
|
a95cf7be94 | ||
|
|
d18501f633 | ||
|
|
ed697db871 | ||
|
|
856e8c4cc8 | ||
|
|
f8a6eacb06 | ||
|
|
e9a82e84ad | ||
|
|
ccd05b53f4 | ||
|
|
032ad13b97 | ||
|
|
4eae75c84b | ||
|
|
252b31a293 | ||
|
|
feff202161 | ||
|
|
d15bfc86c4 |
@@ -1,156 +1,175 @@
|
|||||||
# Cista Web Storage
|
# Cista Web Storage
|
||||||
|
|
||||||
<img src="https://git.zi.fi/Vasanko/cista-storage/raw/branch/main/docs/cista.webp" align=left width=250>
|
<img src="https://git.zi.fi/Vasanko/cista-storage/raw/branch/main/docs/cista.webp" align=left width=250>
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
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**.
|
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**.
|
||||||
|
|
||||||
**Built-in document and media previews** let you quickly view files without downloading them. Cista shows PDF and other documents, video and image thumbnails, with **HDR10 support** video previews and image formats, including HEIC and AVIF. It also has a player for music and video files.
|
**Built-in document and media previews** let you quickly view files without downloading them. Cista shows PDF and other documents, video and image thumbnails, with **HDR10 support** video previews and image formats, including HEIC and AVIF. It also has a player for music and video files.
|
||||||
|
|
||||||
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.
|
The Cista project started as an inevitable remake of [Droppy](https://github.com/droppyjs/droppy) which was not being developed at the time. Now they have picked up pace too, feel free to try both and compare.
|
||||||
|
|
||||||
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.
|
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...
|
Experience Cista by visiting [Cista Demo](https://drop.zi.fi) for a test run and perhaps upload something...
|
||||||
|
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
### Running the Server
|
### Running the Server
|
||||||
|
|
||||||
We recommend using [UV](https://docs.astral.sh/uv/getting-started/installation/) to directly run Cista:
|
We recommend using [UV](https://docs.astral.sh/uv/getting-started/installation/) to directly run Cista:
|
||||||
|
|
||||||
Create an account: (otherwise the server is public for all)
|
Try it out locally at http://localhost:8000 (serves the current directory):
|
||||||
```fish
|
```fish
|
||||||
uvx cista --user yourname --privileged
|
uvx cista
|
||||||
```
|
```
|
||||||
|
|
||||||
Serve your files at http://localhost:8000:
|
Create an account: (otherwise the server is public for all)
|
||||||
```fish
|
```fish
|
||||||
uvx cista -l :8000 /path/to/files
|
uvx cista --user yourname --privileged
|
||||||
```
|
```
|
||||||
|
|
||||||
Alternatively, you can install with `pip` or `uv pip`. This enables using the `cista` command directly without `uvx` or `uv run`.
|
Serve your files at http://localhost:8000:
|
||||||
|
```fish
|
||||||
```fish
|
uvx cista -l :8000 /path/to/files
|
||||||
pip install cista --break-system-packages
|
```
|
||||||
```
|
|
||||||
|
Alternatively, you can install with `pip` or `uv pip`. This enables using the `cista` command directly without `uvx` or `uv run`.
|
||||||
The server remembers its settings in the config folder (default `~/.local/share/cista/`), including the listen port and directory, for future runs without arguments.
|
|
||||||
|
```fish
|
||||||
## Authentication
|
pip install cista --break-system-packages
|
||||||
|
```
|
||||||
Cista supports three authentication modes:
|
|
||||||
|
The server remembers its settings in the config folder (default `~/.local/share/cista/`), including the listen port and directory, for future runs without arguments.
|
||||||
### Built-in Authentication (default)
|
|
||||||
|
## Authentication
|
||||||
User accounts are managed directly by Cista. Create users with the `--user` flag:
|
|
||||||
|
Cista supports two authenticatioon mode, each of which supporting ordinary and privileged users. Either one can be combined with the public mode.
|
||||||
```fish
|
|
||||||
uvx cista --user admin --privileged # Create admin user
|
### Public Mode
|
||||||
uvx cista --user guest # Create regular user
|
|
||||||
```
|
In public mode, anyone can read, send and even delete files without without logging in. Users entering the service won't be asked to authenticate. Privileged users can still log in via the menu to access admin settings, from where the public mode can be toggled on or off.
|
||||||
|
|
||||||
Privileged users can manage other users and change settings via the Admin Settings menu.
|
### Built-in Password Authentication (default)
|
||||||
|
|
||||||
### Public Mode
|
User accounts are managed directly by Cista. Create users with the `--user` flag:
|
||||||
|
|
||||||
In public mode, anyone can read, send and even delete files without without logging in. Privileged users can still log in via the menu to access admin settings, from where the public mode can be toggled on or off.
|
```fish
|
||||||
|
uvx cista --user admin --privileged # Create admin user
|
||||||
### Paskia SSO Authentication
|
uvx cista --user guest # Create regular user
|
||||||
|
```
|
||||||
For centralized authentication, Cista can integrate with [Paskia](https://git.zi.fi/LeoVasanko/paskia) SSO server. Set the `PASKIA_BACKEND_URL` environment variable:
|
|
||||||
|
Privileged users can manage other users and change settings via the Admin Settings menu.
|
||||||
```fish
|
|
||||||
PASKIA_BACKEND_URL=http://localhost:4401 uvx cista
|
### Passkey Authentication and SSO
|
||||||
```
|
|
||||||
|
For centralized authentication, Cista can integrate with [Paskia](https://git.zi.fi/LeoVasanko/paskia) SSO server. This allows user account and permission management at the corporate level, without bothering Cista with it.
|
||||||
In Paskia mode:
|
|
||||||
- All `/auth/*` requests are proxied to the Paskia backend
|
Set the `PASKIA_BACKEND_URL` environment variable:
|
||||||
- Users with `cista:login` permission can access files
|
|
||||||
- Users with `cista:admin` permission get privileged access (Admin Settings)
|
```fish
|
||||||
- Public mode works with Paskia: unauthenticated users can browse, while the menu has option to login
|
PASKIA_BACKEND_URL=http://localhost:4401 uvx cista
|
||||||
|
```
|
||||||
### Internet Access
|
|
||||||
|
Run the Paskia backend on the same machine (to use that default URL):
|
||||||
Most admins 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 but different (sub)domains.
|
```fish
|
||||||
|
uvx paskia
|
||||||
`/etc/caddy/Caddyfile`:
|
```
|
||||||
|
|
||||||
```Caddyfile
|
In Paskia mode:
|
||||||
cista.example.com {
|
- All `/auth/*` requests are proxied to the Paskia backend
|
||||||
reverse_proxy :8000
|
- Cista backend verifies access by `/auth/api/validate` endpoint and shows a login dialog if needed
|
||||||
}
|
- Users with `cista:login` permission can access files
|
||||||
```
|
- Users with `cista:admin` permission get privileged access (Admin Settings)
|
||||||
|
|
||||||
Nxing or other proxy may be similarly used, or alternatively you can place cert and key in cista config dir and run `cista -l cista.example.com`
|
### Internet Access
|
||||||
|
|
||||||
## System Deployment
|
Most admins 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 but different (sub)domains.
|
||||||
|
|
||||||
This setup allows easy addition of storages, each with its own domain, configuration, and files.
|
`/etc/caddy/Caddyfile`:
|
||||||
|
|
||||||
Assuming a restricted user account `storage` for serving files and that UV is installed system-wide or on this account. Only UV is required: this does not use git or bun/npm.
|
```Caddyfile
|
||||||
|
cista.example.com {
|
||||||
Create `/etc/systemd/system/cista@.service`:
|
reverse_proxy :8000
|
||||||
|
}
|
||||||
```ini
|
```
|
||||||
[Unit]
|
|
||||||
Description=Cista storage %i
|
Nxing or other proxy may be similarly used, or alternatively you can place cert and key in cista config dir and run `cista -l cista.example.com`
|
||||||
|
|
||||||
[Service]
|
## System Deployment
|
||||||
User=storage
|
|
||||||
ExecStart=uvx cista -c /srv/cista/%i -l /srv/cista/%i/socket /media/storage/%i
|
This setup allows easy addition of storages, each with its own domain, configuration, and files.
|
||||||
Restart=always
|
|
||||||
|
Assuming a restricted user account `storage` for serving files and that UV is installed system-wide or on this account. Only UV is required: this does not use git or javascript runtimes.
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
Create (edit) a systemd unit:
|
||||||
```
|
|
||||||
|
```fish
|
||||||
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.
|
sudo systemctl edit --force --full cista@.service
|
||||||
|
```
|
||||||
```fish
|
|
||||||
systemctl daemon-reload
|
Paste the following:
|
||||||
systemctl enable --now cista@foo.example.com
|
|
||||||
systemctl enable --now cista@bar.example.com
|
```ini
|
||||||
```
|
[Unit]
|
||||||
|
Description=Cista storage %i
|
||||||
Public exposure is easiest using the Caddy web server.
|
|
||||||
|
[Service]
|
||||||
`/etc/caddy/Caddyfile`:
|
User=storage
|
||||||
|
ExecStart=uvx cista -c /srv/cista/%i -l /srv/cista/%i/socket /media/storage/%i
|
||||||
```Caddyfile
|
Restart=always
|
||||||
foo.example.com, bar.example.com {
|
#Environment=PASKIA_BACKEND_URL=http://localhost:4401
|
||||||
reverse_proxy unix//srv/cista/{host}/socket
|
|
||||||
}
|
[Install]
|
||||||
```
|
WantedBy=multi-user.target
|
||||||
|
```
|
||||||
## Development setup
|
|
||||||
|
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.
|
||||||
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
|
||||||
Make sure you have git, uv and bun (or npm) installed.
|
systemctl daemon-reload
|
||||||
|
systemctl enable --now cista@foo.example.com
|
||||||
Backend (Python) – setup and run:
|
systemctl enable --now cista@bar.example.com
|
||||||
|
```
|
||||||
```fish
|
|
||||||
git clone https://git.zi.fi/Vasanko/cista-storage.git
|
Public exposure is easiest using the Caddy web server.
|
||||||
cd cista-storage
|
|
||||||
uv sync --dev
|
`/etc/caddy/Caddyfile`:
|
||||||
uv run cista --dev -l :8000 /path/to/files
|
|
||||||
```
|
```Caddyfile
|
||||||
|
foo.example.com, bar.example.com {
|
||||||
Frontend (Vue/Vite) – run the dev server in another terminal:
|
reverse_proxy unix//srv/cista/{host}/socket
|
||||||
|
}
|
||||||
```fish
|
```
|
||||||
cd frontend
|
|
||||||
bun install
|
## Development setup
|
||||||
bun run dev
|
|
||||||
```
|
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.
|
||||||
|
|
||||||
Building the package for release (frontend + Python wheel/sdist):
|
Make sure you have git, uv and bun (or npm) installed.
|
||||||
|
|
||||||
```fish
|
Backend (Python) – setup and run:
|
||||||
uv build
|
|
||||||
```
|
```fish
|
||||||
|
git clone https://git.zi.fi/Vasanko/cista-storage.git
|
||||||
Vue is used to build files in `cista/wwwroot`, included prebuilt in the Python package. `uv build` runs the project build hooks to bundle the frontend and produce a NodeJS-independent Python package.
|
cd cista-storage
|
||||||
|
uv sync --dev
|
||||||
|
uv run cista --dev -l :8000 /path/to/files
|
||||||
|
```
|
||||||
|
|
||||||
|
Frontend (Vue/Vite) – run the dev server in another terminal:
|
||||||
|
|
||||||
|
```fish
|
||||||
|
cd frontend
|
||||||
|
bun install
|
||||||
|
bun run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Building the package for release (frontend + Python wheel/sdist):
|
||||||
|
|
||||||
|
```fish
|
||||||
|
uv build
|
||||||
|
```
|
||||||
|
|
||||||
|
Vue is used to build files in `cista/frontend-build`, included prebuilt in the Python package. `uv build` runs the project build hooks to bundle the frontend and produce a NodeJS-independent Python package.
|
||||||
|
|||||||
+21
-10
@@ -25,14 +25,22 @@ def create_banner():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def create_startup_box(*, folder, url, unix=None, dev=False, paskia_url=None):
|
def create_startup_box(
|
||||||
|
*, folder, url, unix=None, dev=False, paskia_url=None, public=False
|
||||||
|
):
|
||||||
"""Create a framed startup box with server information."""
|
"""Create a framed startup box with server information."""
|
||||||
title = f"Cista {cista.__version__}"
|
title = f"Cista {cista.__version__}"
|
||||||
listen = unix if unix else url
|
listen = unix if unix else url
|
||||||
location = f"{folder} @ {listen}"
|
location = f"{folder} @ {listen}"
|
||||||
lines = [title, location]
|
lines = [title, location]
|
||||||
|
# Auth line: Paskia <url> or Password, with optional Public suffix
|
||||||
if paskia_url:
|
if paskia_url:
|
||||||
lines.append(f"Paskia: {paskia_url}")
|
auth_line = f"Auth: Paskia {paskia_url}"
|
||||||
|
else:
|
||||||
|
auth_line = "Auth: Password"
|
||||||
|
if public:
|
||||||
|
auth_line += ", Public"
|
||||||
|
lines.append(auth_line)
|
||||||
if dev:
|
if dev:
|
||||||
lines.append("dev mode")
|
lines.append("dev mode")
|
||||||
|
|
||||||
@@ -53,11 +61,13 @@ doc = """\
|
|||||||
Usage:
|
Usage:
|
||||||
cista [-c <confdir>] [-l <host>] [--import-droppy] [--dev] [<path>]
|
cista [-c <confdir>] [-l <host>] [--import-droppy] [--dev] [<path>]
|
||||||
cista [-c <confdir>] --user <name> [--privileged] [--password]
|
cista [-c <confdir>] --user <name> [--privileged] [--password]
|
||||||
|
cista --version
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-c CONFDIR Custom config directory
|
-c CONFDIR Custom config directory
|
||||||
-l LISTEN-ADDR Listen on
|
-l, --listen LISTEN-ADDR
|
||||||
:8000 (localhost port, plain http)
|
Listen on
|
||||||
|
:8989 (localhost port, plain http)
|
||||||
<addr>:3000 (bind another address, port)
|
<addr>:3000 (bind another address, port)
|
||||||
/path/to/unix.sock (unix socket)
|
/path/to/unix.sock (unix socket)
|
||||||
example.com (run on 80 and 443 with LetsEncrypt)
|
example.com (run on 80 and 443 with LetsEncrypt)
|
||||||
@@ -80,7 +90,7 @@ Environment:
|
|||||||
first_time_help = """\
|
first_time_help = """\
|
||||||
No config file found! Get started with:
|
No config file found! Get started with:
|
||||||
cista --user yourname --privileged # If you want user accounts
|
cista --user yourname --privileged # If you want user accounts
|
||||||
cista -l :8000 /path/to/files # Run the server on localhost:8000
|
cista -l :8989 /path/to/files # Run the server on localhost:8989
|
||||||
|
|
||||||
See cista --help for other options!
|
See cista --help for other options!
|
||||||
"""
|
"""
|
||||||
@@ -109,7 +119,7 @@ def _main():
|
|||||||
args = docopt(doc)
|
args = docopt(doc)
|
||||||
if args["--user"]:
|
if args["--user"]:
|
||||||
return _user(args)
|
return _user(args)
|
||||||
listen = args["-l"]
|
listen = args["--listen"]
|
||||||
# Validate arguments first
|
# Validate arguments first
|
||||||
if args["<path>"]:
|
if args["<path>"]:
|
||||||
path = Path(args["<path>"]).resolve()
|
path = Path(args["<path>"]).resolve()
|
||||||
@@ -140,8 +150,8 @@ def _main():
|
|||||||
if listen:
|
if listen:
|
||||||
settings["listen"] = listen
|
settings["listen"] = listen
|
||||||
elif not exists:
|
elif not exists:
|
||||||
settings["listen"] = ":8000"
|
settings["listen"] = ":8989"
|
||||||
operation = config.update_config(settings)
|
config.update_config(settings)
|
||||||
# Prepare to serve
|
# Prepare to serve
|
||||||
url, opts = serve.parse_listen(config.config.listen)
|
url, opts = serve.parse_listen(config.config.listen)
|
||||||
if not config.config.path.is_dir():
|
if not config.config.path.is_dir():
|
||||||
@@ -157,6 +167,7 @@ def _main():
|
|||||||
unix=opts.get("unix"),
|
unix=opts.get("unix"),
|
||||||
dev=dev,
|
dev=dev,
|
||||||
paskia_url=PASKIA_BACKEND_URL or None,
|
paskia_url=PASKIA_BACKEND_URL or None,
|
||||||
|
public=config.config.public,
|
||||||
)
|
)
|
||||||
sys.stderr.write(startup_box)
|
sys.stderr.write(startup_box)
|
||||||
# Run the server
|
# Run the server
|
||||||
@@ -186,7 +197,7 @@ def _user(args):
|
|||||||
# Defaults for new config when user is created
|
# Defaults for new config when user is created
|
||||||
operation = config.update_config(
|
operation = config.update_config(
|
||||||
{
|
{
|
||||||
"listen": ":8000",
|
"listen": ":8989",
|
||||||
"path": Path.home() / "Downloads",
|
"path": Path.home() / "Downloads",
|
||||||
"public": False,
|
"public": False,
|
||||||
}
|
}
|
||||||
@@ -215,7 +226,7 @@ def _user(args):
|
|||||||
|
|
||||||
if operation == "created":
|
if operation == "created":
|
||||||
sys.stderr.write(
|
sys.stderr.write(
|
||||||
"Now you can run the server:\n cista # defaults set: -l :8000 ~/Downloads\n"
|
"Now you can run the server:\n cista # defaults set: -l :8989 ~/Downloads\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+32
-58
@@ -1,5 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import typing
|
from pathlib import PurePosixPath
|
||||||
from secrets import token_bytes
|
from secrets import token_bytes
|
||||||
|
|
||||||
import msgspec
|
import msgspec
|
||||||
@@ -8,7 +8,7 @@ from sanic.exceptions import BadRequest
|
|||||||
|
|
||||||
from cista import __version__, auth, config, sso, watching
|
from cista import __version__, auth, config, sso, watching
|
||||||
from cista.fileio import FileServer
|
from cista.fileio import FileServer
|
||||||
from cista.protocol import ControlTypes, FileRange, StatusMsg
|
from cista.protocol import ControlTypes, StatusMsg
|
||||||
from cista.util.apphelpers import asend, websocket_wrapper
|
from cista.util.apphelpers import asend, websocket_wrapper
|
||||||
|
|
||||||
bp = Blueprint("api", url_prefix="/api")
|
bp = Blueprint("api", url_prefix="/api")
|
||||||
@@ -25,68 +25,14 @@ async def stop_fileserver(app):
|
|||||||
await fileserver.stop()
|
await fileserver.stop()
|
||||||
|
|
||||||
|
|
||||||
@bp.websocket("upload")
|
|
||||||
@websocket_wrapper
|
|
||||||
async def upload(req, ws):
|
|
||||||
alink = fileserver.alink
|
|
||||||
while True:
|
|
||||||
req = None
|
|
||||||
text = await ws.recv()
|
|
||||||
if not isinstance(text, str):
|
|
||||||
raise ValueError(
|
|
||||||
f"Expected JSON control, got binary len(data) = {len(text)}",
|
|
||||||
)
|
|
||||||
req = msgspec.json.decode(text, type=FileRange)
|
|
||||||
pos = req.start
|
|
||||||
while True:
|
|
||||||
data = await ws.recv()
|
|
||||||
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))
|
|
||||||
pos += typing.cast(int, sentsize)
|
|
||||||
if pos >= req.end:
|
|
||||||
break
|
|
||||||
if pos != req.end:
|
|
||||||
d = f"{len(data)} bytes" if isinstance(data, bytes) else data
|
|
||||||
raise ValueError(f"Expected {req.end - pos} more bytes, got {d}")
|
|
||||||
# Report success
|
|
||||||
res = StatusMsg(status="ack", req=req)
|
|
||||||
await asend(ws, res)
|
|
||||||
|
|
||||||
|
|
||||||
@bp.websocket("download")
|
|
||||||
@websocket_wrapper
|
|
||||||
async def download(req, ws):
|
|
||||||
alink = fileserver.alink
|
|
||||||
while True:
|
|
||||||
req = None
|
|
||||||
text = await ws.recv()
|
|
||||||
if not isinstance(text, str):
|
|
||||||
raise ValueError(
|
|
||||||
f"Expected JSON control, got binary len(data) = {len(text)}",
|
|
||||||
)
|
|
||||||
req = msgspec.json.decode(text, type=FileRange)
|
|
||||||
pos = req.start
|
|
||||||
while pos < req.end:
|
|
||||||
end = min(req.end, pos + (1 << 20))
|
|
||||||
data = typing.cast(bytes, await alink(("download", req.name, pos, end)))
|
|
||||||
await asend(ws, data)
|
|
||||||
pos += len(data)
|
|
||||||
# Report success
|
|
||||||
res = StatusMsg(status="ack", req=req)
|
|
||||||
await asend(ws, res)
|
|
||||||
|
|
||||||
|
|
||||||
@bp.websocket("control")
|
@bp.websocket("control")
|
||||||
@websocket_wrapper
|
@websocket_wrapper
|
||||||
async def control(req, ws):
|
async def control(req, ws):
|
||||||
while True:
|
while True:
|
||||||
cmd = msgspec.json.decode(await ws.recv(), type=ControlTypes)
|
cmd = msgspec.json.decode(await ws.recv(), type=ControlTypes)
|
||||||
await asyncio.to_thread(cmd)
|
await asyncio.to_thread(cmd)
|
||||||
|
# Signal the watcher about affected paths
|
||||||
|
watching.notify_change(*cmd.affected_paths())
|
||||||
await asend(ws, StatusMsg(status="ack", req=cmd))
|
await asend(ws, StatusMsg(status="ack", req=cmd))
|
||||||
|
|
||||||
|
|
||||||
@@ -157,6 +103,17 @@ def subscribe(uuid, ws):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.get("config")
|
||||||
|
async def get_config(request):
|
||||||
|
await auth.verify(request, privileged=True)
|
||||||
|
return json(
|
||||||
|
{
|
||||||
|
"name": config.config.name,
|
||||||
|
"public": config.config.public,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@bp.put("config/public")
|
@bp.put("config/public")
|
||||||
async def update_public(request):
|
async def update_public(request):
|
||||||
await auth.verify(request, privileged=True)
|
await auth.verify(request, privileged=True)
|
||||||
@@ -170,3 +127,20 @@ async def update_public(request):
|
|||||||
raise BadRequest(str(e)) from None
|
raise BadRequest(str(e)) from None
|
||||||
config.update_config({"public": public})
|
config.update_config({"public": public})
|
||||||
return json({"message": "Public access setting updated", "public": public})
|
return json({"message": "Public access setting updated", "public": public})
|
||||||
|
|
||||||
|
|
||||||
|
@bp.put("config/name")
|
||||||
|
async def update_name(request):
|
||||||
|
await auth.verify(request, privileged=True)
|
||||||
|
try:
|
||||||
|
name = request.json["name"]
|
||||||
|
if not isinstance(name, str):
|
||||||
|
raise ValueError("name must be a string")
|
||||||
|
except KeyError:
|
||||||
|
raise BadRequest("Missing name field") from None
|
||||||
|
except ValueError as e:
|
||||||
|
raise BadRequest(str(e)) from None
|
||||||
|
config.update_config({"name": name})
|
||||||
|
# Return the effective name (fallback to path.name if empty)
|
||||||
|
effective_name = name or config.config.path.name
|
||||||
|
return json({"message": "Server name updated", "name": effective_name})
|
||||||
|
|||||||
+129
-38
@@ -1,7 +1,8 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import threading
|
import re
|
||||||
|
import time
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from multiprocessing import cpu_count
|
from multiprocessing import cpu_count
|
||||||
from pathlib import Path, PurePath, PurePosixPath
|
from pathlib import Path, PurePath, PurePosixPath
|
||||||
@@ -11,21 +12,27 @@ from wsgiref.handlers import format_date_time
|
|||||||
|
|
||||||
import sanic.helpers
|
import sanic.helpers
|
||||||
from blake3 import blake3
|
from blake3 import blake3
|
||||||
from sanic import Blueprint, Sanic, empty, raw, redirect
|
from sanic import Blueprint, Sanic, empty, json, raw, redirect
|
||||||
from sanic.exceptions import Forbidden, NotFound
|
from sanic.exceptions import BadRequest, Forbidden, NotFound
|
||||||
from sanic.log import logger
|
from sanic.log import logger
|
||||||
from setproctitle import setproctitle
|
from setproctitle import setproctitle
|
||||||
from stream_zip import ZIP_AUTO, stream_zip
|
from stream_zip import ZIP_AUTO, stream_zip
|
||||||
from zstandard import ZstdCompressor
|
from zstandard import ZstdCompressor
|
||||||
|
|
||||||
from cista import auth, config, preview, session, sso, watching
|
from cista import auth, config, preview, session, sso, watching
|
||||||
from cista.api import bp
|
from cista.preview import shutdown_preview_workers, start_preview_workers
|
||||||
|
from cista.api import bp, fileserver
|
||||||
|
from cista.sanic_logging import configure_access_logging, configure_main_logging, format_access_log
|
||||||
|
from cista.sanic_logging import logger as access_logger
|
||||||
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
|
||||||
sanic.helpers._ENTITY_HEADERS = frozenset()
|
sanic.helpers._ENTITY_HEADERS = frozenset()
|
||||||
|
|
||||||
|
configure_access_logging()
|
||||||
|
|
||||||
app = Sanic("cista", strict_slashes=True)
|
app = Sanic("cista", strict_slashes=True)
|
||||||
|
configure_main_logging()
|
||||||
# Register either SSO proxy or built-in auth routes based on PASKIA_BACKEND_URL
|
# Register either SSO proxy or built-in auth routes based on PASKIA_BACKEND_URL
|
||||||
if sso.paskia_enabled():
|
if sso.paskia_enabled():
|
||||||
app.blueprint(sso.bp) # SSO proxy for /auth/* routes
|
app.blueprint(sso.bp) # SSO proxy for /auth/* routes
|
||||||
@@ -43,21 +50,20 @@ setproctitle("cista-main")
|
|||||||
async def main_start(app):
|
async def main_start(app):
|
||||||
config.load_config()
|
config.load_config()
|
||||||
setproctitle(f"cista {config.config.path.name}")
|
setproctitle(f"cista {config.config.path.name}")
|
||||||
# Small pool for memory-intensive preview generation
|
|
||||||
preview_workers = max(2, min(8, cpu_count()))
|
|
||||||
app.ctx.threadexec = ThreadPoolExecutor(
|
app.ctx.threadexec = ThreadPoolExecutor(
|
||||||
max_workers=preview_workers, thread_name_prefix="cista-preview"
|
max_workers=4, thread_name_prefix="cista-worker"
|
||||||
)
|
)
|
||||||
# Larger pool for long-running but low-memory zip operations
|
# Larger pool for long-running but low-memory zip operations
|
||||||
app.ctx.zipexec = ThreadPoolExecutor(max_workers=32, thread_name_prefix="cista-zip")
|
app.ctx.zipexec = ThreadPoolExecutor(max_workers=32, thread_name_prefix="cista-zip")
|
||||||
|
await start_preview_workers()
|
||||||
watching.start(app)
|
watching.start(app)
|
||||||
|
|
||||||
|
|
||||||
# Sanic sometimes fails to execute after_server_stop, so we do it before instead (potentially interrupting handlers)
|
# Sanic sometimes fails to execute after_server_stop, so we do it before instead (potentially interrupting handlers)
|
||||||
@app.before_server_stop
|
@app.before_server_stop
|
||||||
async def main_stop(app):
|
async def main_stop(app):
|
||||||
quit.set()
|
|
||||||
watching.stop(app)
|
watching.stop(app)
|
||||||
|
await shutdown_preview_workers()
|
||||||
app.ctx.threadexec.shutdown()
|
app.ctx.threadexec.shutdown()
|
||||||
app.ctx.zipexec.shutdown(cancel_futures=True)
|
app.ctx.zipexec.shutdown(cancel_futures=True)
|
||||||
await sso.close_client()
|
await sso.close_client()
|
||||||
@@ -66,6 +72,7 @@ async def main_stop(app):
|
|||||||
|
|
||||||
@app.on_request
|
@app.on_request
|
||||||
async def use_session(req):
|
async def use_session(req):
|
||||||
|
req.ctx._log_start = time.perf_counter()
|
||||||
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"] # type: ignore
|
||||||
@@ -83,6 +90,27 @@ async def use_session(req):
|
|||||||
raise Forbidden("Invalid origin: Cross-Site requests not permitted")
|
raise Forbidden("Invalid origin: Cross-Site requests not permitted")
|
||||||
|
|
||||||
|
|
||||||
|
@app.on_response
|
||||||
|
async def log_access(req, res):
|
||||||
|
"""Log HTTP access in a clean single-line format."""
|
||||||
|
if req.headers.get("upgrade", "").lower() == "websocket":
|
||||||
|
return res
|
||||||
|
start = getattr(req.ctx, "_log_start", None)
|
||||||
|
duration_ms = (time.perf_counter() - start) * 1000 if start is not None else 0.0
|
||||||
|
client = req.client_ip or "-"
|
||||||
|
host = req.host or "-"
|
||||||
|
path = req.path
|
||||||
|
if req.query_string:
|
||||||
|
qs = req.query_string
|
||||||
|
if isinstance(qs, bytes):
|
||||||
|
qs = qs.decode(errors="replace")
|
||||||
|
path = f"{path}?{qs}"
|
||||||
|
extra = getattr(req.ctx, "_log_extra", None)
|
||||||
|
line = format_access_log(client, res.status, req.method, host, path, duration_ms, extra=extra)
|
||||||
|
access_logger.info(line)
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
@app.on_response
|
@app.on_response
|
||||||
async def forward_sso_cookies(req, res):
|
async def forward_sso_cookies(req, res):
|
||||||
"""Forward Set-Cookie headers from SSO validation to client."""
|
"""Forward Set-Cookie headers from SSO validation to client."""
|
||||||
@@ -100,6 +128,68 @@ def http_fileserver(app):
|
|||||||
"""Verify access to file server routes."""
|
"""Verify access to file server routes."""
|
||||||
await auth.verify(request)
|
await auth.verify(request)
|
||||||
|
|
||||||
|
@bp.put("/files/<name:path>")
|
||||||
|
async def upload_file_chunk(request, *args, **kwargs):
|
||||||
|
body = request.body
|
||||||
|
header = request.headers.get("content-range")
|
||||||
|
if header:
|
||||||
|
start, end, total = _parse_content_range(header, len(body))
|
||||||
|
else:
|
||||||
|
start = 0
|
||||||
|
end = len(body)
|
||||||
|
total = end
|
||||||
|
raw_name = kwargs.get("name")
|
||||||
|
if raw_name is None and args:
|
||||||
|
raw_name = args[0]
|
||||||
|
if not isinstance(raw_name, str) or not raw_name:
|
||||||
|
prefix = "/files/"
|
||||||
|
if not request.path.startswith(prefix):
|
||||||
|
raise BadRequest("Invalid upload path")
|
||||||
|
raw_name = request.path[len(prefix) :]
|
||||||
|
rel_name = unquote(raw_name)
|
||||||
|
upload_info = await asyncio.to_thread(
|
||||||
|
fileserver.upload_info,
|
||||||
|
rel_name,
|
||||||
|
start,
|
||||||
|
body,
|
||||||
|
total,
|
||||||
|
)
|
||||||
|
extras = []
|
||||||
|
chunk_len = end - start
|
||||||
|
whole_file = start == 0 and end == total
|
||||||
|
if not whole_file:
|
||||||
|
start_mib = _to_mib_int(start)
|
||||||
|
chunk_mib = _to_mib_int(chunk_len)
|
||||||
|
# Keep range logs compact for fixed-size upload blocks.
|
||||||
|
if chunk_mib == 16:
|
||||||
|
extras.append(f"{start_mib}MiB")
|
||||||
|
else:
|
||||||
|
extras.append(f"{start_mib}+{chunk_mib}MiB")
|
||||||
|
if upload_info.get("created"):
|
||||||
|
extras.append(f"created {_to_mib_int(total)}MiB")
|
||||||
|
size_before = upload_info.get("size_before")
|
||||||
|
size_after = upload_info.get("size_after")
|
||||||
|
if (
|
||||||
|
size_before is not None
|
||||||
|
and size_after is not None
|
||||||
|
and size_before != size_after
|
||||||
|
):
|
||||||
|
extras.append("resized")
|
||||||
|
request.ctx._log_extra = " ".join(extras) if extras else None
|
||||||
|
path = PurePosixPath(rel_name)
|
||||||
|
watching.notify_change(path, *path.parents)
|
||||||
|
return json(
|
||||||
|
{
|
||||||
|
"status": "ack",
|
||||||
|
"req": {
|
||||||
|
"name": rel_name,
|
||||||
|
"size": total,
|
||||||
|
"start": start,
|
||||||
|
"end": end,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
bp.static(
|
bp.static(
|
||||||
"/files/",
|
"/files/",
|
||||||
config.config.path,
|
config.config.path,
|
||||||
@@ -111,6 +201,30 @@ def http_fileserver(app):
|
|||||||
|
|
||||||
|
|
||||||
www = {}
|
www = {}
|
||||||
|
_CONTENT_RANGE_RE = re.compile(r"^bytes (\d+)-(\d+)/(\d+)$")
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_content_range(header: str, body_len: int) -> tuple[int, int, int]:
|
||||||
|
m = _CONTENT_RANGE_RE.fullmatch(header.strip())
|
||||||
|
if m is None:
|
||||||
|
raise BadRequest("Invalid Content-Range format")
|
||||||
|
start, end_inclusive, total = (int(v) for v in m.groups())
|
||||||
|
if total <= 0:
|
||||||
|
raise BadRequest("Invalid Content-Range total size")
|
||||||
|
if start > end_inclusive:
|
||||||
|
raise BadRequest("Invalid Content-Range range")
|
||||||
|
if end_inclusive >= total:
|
||||||
|
raise BadRequest("Content-Range exceeds total size")
|
||||||
|
expected_len = end_inclusive - start + 1
|
||||||
|
if expected_len != body_len:
|
||||||
|
raise BadRequest(
|
||||||
|
f"Content length mismatch for range: expected {expected_len}, got {body_len}"
|
||||||
|
)
|
||||||
|
return start, end_inclusive + 1, total
|
||||||
|
|
||||||
|
|
||||||
|
def _to_mib_int(value_bytes: int) -> int:
|
||||||
|
return round(value_bytes / (1 << 20))
|
||||||
|
|
||||||
|
|
||||||
def _load_wwwroot(www):
|
def _load_wwwroot(www):
|
||||||
@@ -174,9 +288,8 @@ def _load_wwwroot(www):
|
|||||||
|
|
||||||
@app.before_server_start
|
@app.before_server_start
|
||||||
async def start(app):
|
async def start(app):
|
||||||
await load_wwwroot(app)
|
if not app.debug:
|
||||||
if app.debug:
|
await load_wwwroot(app)
|
||||||
app.add_task(refresh_wwwroot(), name="refresh_wwwroot")
|
|
||||||
|
|
||||||
|
|
||||||
async def load_wwwroot(app):
|
async def load_wwwroot(app):
|
||||||
@@ -186,36 +299,14 @@ async def load_wwwroot(app):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
quit = threading.Event()
|
|
||||||
|
|
||||||
|
|
||||||
async def refresh_wwwroot():
|
|
||||||
try:
|
|
||||||
while not quit.is_set():
|
|
||||||
try:
|
|
||||||
wwwold = www
|
|
||||||
await load_wwwroot(app)
|
|
||||||
changes = ""
|
|
||||||
for name in sorted(www):
|
|
||||||
attr = www[name]
|
|
||||||
if wwwold.get(name) == attr:
|
|
||||||
continue
|
|
||||||
headers = attr[2]
|
|
||||||
changes += f"{headers['last-modified']} {headers['etag']} /{name}\n"
|
|
||||||
for name in sorted(set(wwwold) - set(www)):
|
|
||||||
changes += f"Deleted /{name}\n"
|
|
||||||
if changes:
|
|
||||||
logger.info(f"Updated wwwroot:\n{changes}", end="", flush=True)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Error loading wwwroot: {e!r}")
|
|
||||||
await asyncio.sleep(0.5)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<path:path>", methods=["GET", "HEAD"])
|
@app.route("/<path:path>", methods=["GET", "HEAD"])
|
||||||
async def wwwroot(req, path=""):
|
async def wwwroot(req, path=""):
|
||||||
"""Frontend files only"""
|
"""Frontend files only"""
|
||||||
|
if app.debug:
|
||||||
|
raise NotFound(
|
||||||
|
"Dev mode: frontend-build is not served on backend (you should connect vite)",
|
||||||
|
extra={"name": path},
|
||||||
|
)
|
||||||
name = unquote(path)
|
name = unquote(path)
|
||||||
if name not in www:
|
if name not in www:
|
||||||
raise NotFound(f"File not found: /{path}", extra={"name": name})
|
raise NotFound(f"File not found: /{path}", extra={"name": name})
|
||||||
|
|||||||
+2
-2
@@ -269,7 +269,7 @@ async def verify(request, *, privileged=False):
|
|||||||
raise Unauthorized(
|
raise Unauthorized(
|
||||||
f"Login required for {request.path}",
|
f"Login required for {request.path}",
|
||||||
"cookie",
|
"cookie",
|
||||||
context={"auth": {"iframe": "/auth/restricted"}},
|
context={"auth": {"iframe": "/auth/restricted/"}},
|
||||||
quiet=True,
|
quiet=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -278,7 +278,7 @@ async def verify(request, *, privileged=False):
|
|||||||
bp = Blueprint("auth", url_prefix="/auth")
|
bp = Blueprint("auth", url_prefix="/auth")
|
||||||
|
|
||||||
|
|
||||||
@bp.get("/restricted")
|
@bp.get("/restricted/")
|
||||||
async def login_page(request):
|
async def login_page(request):
|
||||||
"""Login page that works both standalone and in paskia iframe."""
|
"""Login page that works both standalone and in paskia iframe."""
|
||||||
s = session.get(request)
|
s = session.get(request)
|
||||||
|
|||||||
+23
-30
@@ -1,9 +1,8 @@
|
|||||||
import asyncio
|
|
||||||
import os
|
import os
|
||||||
|
import threading
|
||||||
|
|
||||||
from cista import config
|
from cista import config
|
||||||
from cista.util import filename
|
from cista.util import filename
|
||||||
from cista.util.asynclink import AsyncLink
|
|
||||||
from cista.util.lrucache import LRUCache
|
from cista.util.lrucache import LRUCache
|
||||||
|
|
||||||
|
|
||||||
@@ -62,38 +61,32 @@ class File:
|
|||||||
|
|
||||||
class FileServer:
|
class FileServer:
|
||||||
async def start(self):
|
async def start(self):
|
||||||
self.alink = AsyncLink()
|
|
||||||
self.worker = asyncio.get_event_loop().run_in_executor(
|
|
||||||
None,
|
|
||||||
self.worker_thread,
|
|
||||||
self.alink.to_sync,
|
|
||||||
)
|
|
||||||
self.cache = LRUCache(File, capacity=10, maxage=5.0)
|
self.cache = LRUCache(File, capacity=10, maxage=5.0)
|
||||||
|
self.cache_lock = threading.Lock()
|
||||||
|
self.file_locks: dict[str, threading.Lock] = {}
|
||||||
|
|
||||||
async def stop(self):
|
async def stop(self):
|
||||||
await self.alink.stop()
|
self.cache.close()
|
||||||
await self.worker
|
|
||||||
|
|
||||||
def worker_thread(self, slink):
|
@staticmethod
|
||||||
|
def _stat_size(path):
|
||||||
try:
|
try:
|
||||||
for req in slink:
|
return os.stat(path).st_size
|
||||||
with req as (command, *args):
|
except FileNotFoundError:
|
||||||
if command == "upload":
|
return None
|
||||||
req.set_result(self.upload(*args))
|
|
||||||
elif command == "download":
|
|
||||||
req.set_result(self.download(*args))
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(f"Unhandled {command=} {args}")
|
|
||||||
finally:
|
|
||||||
self.cache.close()
|
|
||||||
|
|
||||||
def upload(self, name, pos, data, file_size):
|
def upload_info(self, name, pos, data, file_size):
|
||||||
name = filename.sanitize(name)
|
name = filename.sanitize(name)
|
||||||
f = self.cache[name]
|
with self.cache_lock:
|
||||||
f.write(pos, data, file_size=file_size)
|
f = self.cache[name]
|
||||||
return len(data)
|
lock = self.file_locks.setdefault(name, threading.Lock())
|
||||||
|
with lock:
|
||||||
def download(self, name, start, end):
|
size_before = self._stat_size(f.path)
|
||||||
name = filename.sanitize(name)
|
f.write(pos, data, file_size=file_size)
|
||||||
f = self.cache[name]
|
size_after = self._stat_size(f.path)
|
||||||
return f[start:end]
|
return {
|
||||||
|
"written": len(data),
|
||||||
|
"created": size_before is None,
|
||||||
|
"size_before": size_before,
|
||||||
|
"size_after": size_after,
|
||||||
|
}
|
||||||
|
|||||||
+421
-77
@@ -2,45 +2,332 @@ import asyncio
|
|||||||
import gc
|
import gc
|
||||||
import io
|
import io
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
from collections import OrderedDict
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from multiprocessing import cpu_count
|
||||||
from pathlib import PurePosixPath
|
from pathlib import PurePosixPath
|
||||||
from time import perf_counter
|
from time import perf_counter
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
from wsgiref.handlers import format_date_time
|
from wsgiref.handlers import format_date_time
|
||||||
|
|
||||||
|
import msgspec
|
||||||
|
|
||||||
import av
|
import av
|
||||||
import fitz # PyMuPDF
|
import fitz # PyMuPDF
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pillow_heif
|
import pyvips
|
||||||
from PIL import Image
|
from blake3 import blake3
|
||||||
from sanic import Blueprint, empty, raw, redirect
|
from sanic import Blueprint, empty, raw, redirect
|
||||||
from sanic.exceptions import NotFound
|
from sanic.exceptions import NotFound
|
||||||
from sanic.log import logger
|
from sanic.log import logger
|
||||||
|
|
||||||
from cista import auth, config
|
from cista import auth, config
|
||||||
|
from cista.preview_worker import PreviewRequest, PreviewResponse
|
||||||
from cista.util.filename import sanitize
|
from cista.util.filename import sanitize
|
||||||
|
|
||||||
pillow_heif.register_heif_opener()
|
|
||||||
|
|
||||||
bp = Blueprint("preview", url_prefix="/preview")
|
bp = Blueprint("preview", url_prefix="/preview")
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(slots=True)
|
||||||
|
class CachedPreview:
|
||||||
|
"""Cached preview with headers and body."""
|
||||||
|
|
||||||
|
headers: dict[str, str]
|
||||||
|
body: bytes
|
||||||
|
|
||||||
|
|
||||||
|
class PreviewCache:
|
||||||
|
"""Thread-safe LRU cache for preview responses."""
|
||||||
|
|
||||||
|
def __init__(self, capacity: int = 500):
|
||||||
|
self.capacity = capacity
|
||||||
|
self._cache: OrderedDict[str, CachedPreview] = OrderedDict()
|
||||||
|
self._lock = threading.Lock()
|
||||||
|
|
||||||
|
def get(self, key: str) -> CachedPreview | None:
|
||||||
|
"""Get cached preview, moving it to end (most recently used)."""
|
||||||
|
with self._lock:
|
||||||
|
if key in self._cache:
|
||||||
|
self._cache.move_to_end(key)
|
||||||
|
return self._cache[key]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def set(self, key: str, value: CachedPreview) -> None:
|
||||||
|
"""Cache preview, evicting oldest if at capacity."""
|
||||||
|
with self._lock:
|
||||||
|
if key in self._cache:
|
||||||
|
self._cache.move_to_end(key)
|
||||||
|
else:
|
||||||
|
if len(self._cache) >= self.capacity:
|
||||||
|
self._cache.popitem(last=False)
|
||||||
|
self._cache[key] = value
|
||||||
|
|
||||||
|
def __len__(self) -> int:
|
||||||
|
with self._lock:
|
||||||
|
return len(self._cache)
|
||||||
|
|
||||||
|
|
||||||
|
# Global preview cache instance
|
||||||
|
_preview_cache = PreviewCache(capacity=500)
|
||||||
|
|
||||||
|
PREVIEW_TIMEOUT = 3.0 # seconds until preview subprocess is killed
|
||||||
|
PREVIEW_WORKERS = max(2, min(8, cpu_count()))
|
||||||
|
_active_procs: set[asyncio.subprocess.Process] = set()
|
||||||
|
_preview_pool = None
|
||||||
|
_preview_pool_lock = asyncio.Lock()
|
||||||
|
AVIF_FAST_EFFORT = 0
|
||||||
|
WORKER_CHECKSUM_BYTES = 32
|
||||||
|
WORKER_MAX_JSON_BYTES = 1_000_000
|
||||||
|
|
||||||
|
|
||||||
|
class WorkerChecksumError(Exception):
|
||||||
|
"""Raised when worker response checksum does not match the packet."""
|
||||||
|
|
||||||
|
|
||||||
|
class WorkerProtocolError(Exception):
|
||||||
|
"""Raised when worker response packet is malformed."""
|
||||||
|
|
||||||
|
|
||||||
|
class _PreviewWorker:
|
||||||
|
def __init__(self, proc: asyncio.subprocess.Process):
|
||||||
|
self.proc = proc
|
||||||
|
|
||||||
|
async def request(self, filepath, quality: int, maxsize: int, maxzoom: float):
|
||||||
|
if self.proc.returncode is not None:
|
||||||
|
raise WorkerProtocolError("worker already exited")
|
||||||
|
if self.proc.stdin is None or self.proc.stdout is None:
|
||||||
|
raise WorkerProtocolError("worker streams not available")
|
||||||
|
|
||||||
|
line = (
|
||||||
|
msgspec.json.encode(
|
||||||
|
PreviewRequest(
|
||||||
|
path=str(filepath),
|
||||||
|
quality=quality,
|
||||||
|
maxsize=maxsize,
|
||||||
|
maxzoom=maxzoom,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
+ b"\n"
|
||||||
|
)
|
||||||
|
self.proc.stdin.write(line)
|
||||||
|
await self.proc.stdin.drain()
|
||||||
|
|
||||||
|
checksum = await self.proc.stdout.readexactly(WORKER_CHECKSUM_BYTES)
|
||||||
|
header = await self.proc.stdout.readexactly(8)
|
||||||
|
json_size, data_size = struct.unpack("<II", header)
|
||||||
|
if json_size > WORKER_MAX_JSON_BYTES:
|
||||||
|
raise WorkerProtocolError(f"worker JSON too large: {json_size}")
|
||||||
|
meta_raw = await self.proc.stdout.readexactly(json_size)
|
||||||
|
payload = await self.proc.stdout.readexactly(data_size)
|
||||||
|
packet = header + meta_raw + payload
|
||||||
|
if blake3(packet).digest() != checksum:
|
||||||
|
raise WorkerChecksumError("worker checksum mismatch")
|
||||||
|
|
||||||
|
resp = msgspec.json.decode(meta_raw, type=PreviewResponse)
|
||||||
|
if not resp.ok:
|
||||||
|
raise PreviewError(
|
||||||
|
resp.error or "preview worker error",
|
||||||
|
stderr=resp.stderr,
|
||||||
|
backend=resp.backend,
|
||||||
|
)
|
||||||
|
return payload or None, resp
|
||||||
|
|
||||||
|
async def kill(self) -> None:
|
||||||
|
if self.proc.returncode is None:
|
||||||
|
try:
|
||||||
|
self.proc.kill()
|
||||||
|
except ProcessLookupError:
|
||||||
|
pass
|
||||||
|
await self.proc.wait()
|
||||||
|
_active_procs.discard(self.proc)
|
||||||
|
|
||||||
|
|
||||||
|
class _PreviewWorkerPool:
|
||||||
|
def __init__(self, size: int):
|
||||||
|
self.size = size
|
||||||
|
self._idle: asyncio.Queue[_PreviewWorker] = asyncio.Queue()
|
||||||
|
self._workers: set[_PreviewWorker] = set()
|
||||||
|
self._closed = False
|
||||||
|
|
||||||
|
async def _spawn_worker(self) -> _PreviewWorker:
|
||||||
|
proc = await asyncio.create_subprocess_exec(
|
||||||
|
sys.executable,
|
||||||
|
"-m",
|
||||||
|
"cista.preview_worker",
|
||||||
|
stdin=asyncio.subprocess.PIPE,
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.DEVNULL,
|
||||||
|
start_new_session=True,
|
||||||
|
)
|
||||||
|
_active_procs.add(proc)
|
||||||
|
return _PreviewWorker(proc)
|
||||||
|
|
||||||
|
async def _add_worker(self) -> None:
|
||||||
|
worker = await self._spawn_worker()
|
||||||
|
self._workers.add(worker)
|
||||||
|
await self._idle.put(worker)
|
||||||
|
|
||||||
|
async def _replace_worker(self, worker: _PreviewWorker) -> None:
|
||||||
|
self._workers.discard(worker)
|
||||||
|
await worker.kill()
|
||||||
|
if self._closed:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
await self._add_worker()
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Failed to replace preview worker")
|
||||||
|
|
||||||
|
async def start(self) -> None:
|
||||||
|
for _ in range(self.size):
|
||||||
|
await self._add_worker()
|
||||||
|
|
||||||
|
async def run(self, filepath, quality: int, maxsize: int, maxzoom: float):
|
||||||
|
if self._closed:
|
||||||
|
raise PreviewError("preview worker pool closed")
|
||||||
|
worker = await self._idle.get()
|
||||||
|
replace = False
|
||||||
|
try:
|
||||||
|
out, resp = await asyncio.wait_for(
|
||||||
|
worker.request(filepath, quality, maxsize, maxzoom),
|
||||||
|
timeout=PREVIEW_TIMEOUT,
|
||||||
|
)
|
||||||
|
return out, resp
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
replace = True
|
||||||
|
logger.warning(
|
||||||
|
"Preview timeout (%ds) for %s", int(PREVIEW_TIMEOUT), filepath.name
|
||||||
|
)
|
||||||
|
raise PreviewTimeout(filepath.name)
|
||||||
|
except WorkerChecksumError:
|
||||||
|
replace = True
|
||||||
|
logger.error("Preview checksum mismatch for %s", filepath.name)
|
||||||
|
raise PreviewError(f"worker checksum mismatch for {filepath.name}")
|
||||||
|
except PreviewError:
|
||||||
|
raise
|
||||||
|
except (
|
||||||
|
WorkerProtocolError,
|
||||||
|
asyncio.IncompleteReadError,
|
||||||
|
BrokenPipeError,
|
||||||
|
ConnectionResetError,
|
||||||
|
OSError,
|
||||||
|
ValueError,
|
||||||
|
msgspec.json.DecodeError,
|
||||||
|
) as e:
|
||||||
|
replace = True
|
||||||
|
logger.warning(
|
||||||
|
"Preview worker protocol failure for %s: %s", filepath.name, e
|
||||||
|
)
|
||||||
|
raise PreviewError(
|
||||||
|
f"worker protocol failure for {filepath.name}: {e}"
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
if replace:
|
||||||
|
await self._replace_worker(worker)
|
||||||
|
else:
|
||||||
|
if worker.proc.returncode is None:
|
||||||
|
await self._idle.put(worker)
|
||||||
|
else:
|
||||||
|
await self._replace_worker(worker)
|
||||||
|
|
||||||
|
async def close(self) -> None:
|
||||||
|
self._closed = True
|
||||||
|
workers = list(self._workers)
|
||||||
|
self._workers.clear()
|
||||||
|
while not self._idle.empty():
|
||||||
|
try:
|
||||||
|
self._idle.get_nowait()
|
||||||
|
except asyncio.QueueEmpty:
|
||||||
|
break
|
||||||
|
await asyncio.gather(
|
||||||
|
*(worker.kill() for worker in workers), return_exceptions=True
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def start_preview_workers() -> None:
|
||||||
|
"""Warm up persistent preview workers during server startup."""
|
||||||
|
global _preview_pool
|
||||||
|
if _preview_pool is not None:
|
||||||
|
return
|
||||||
|
async with _preview_pool_lock:
|
||||||
|
if _preview_pool is not None:
|
||||||
|
return
|
||||||
|
pool = _PreviewWorkerPool(PREVIEW_WORKERS)
|
||||||
|
await pool.start()
|
||||||
|
_preview_pool = pool
|
||||||
|
logger.info("Started %d persistent preview workers", PREVIEW_WORKERS)
|
||||||
|
|
||||||
|
|
||||||
|
async def shutdown_preview_workers() -> None:
|
||||||
|
"""Kill persistent preview workers (called during server shutdown)."""
|
||||||
|
global _preview_pool
|
||||||
|
async with _preview_pool_lock:
|
||||||
|
pool = _preview_pool
|
||||||
|
_preview_pool = None
|
||||||
|
if pool is not None:
|
||||||
|
await pool.close()
|
||||||
|
if not _active_procs:
|
||||||
|
return
|
||||||
|
for proc in list(_active_procs):
|
||||||
|
try:
|
||||||
|
proc.kill()
|
||||||
|
except ProcessLookupError:
|
||||||
|
pass
|
||||||
|
await asyncio.gather(
|
||||||
|
*(proc.wait() for proc in list(_active_procs)), return_exceptions=True
|
||||||
|
)
|
||||||
|
_active_procs.clear()
|
||||||
|
|
||||||
|
|
||||||
@bp.on_request
|
@bp.on_request
|
||||||
async def verify_preview(request):
|
async def verify_preview(request):
|
||||||
"""Verify access to preview routes."""
|
"""Verify access to preview routes."""
|
||||||
await auth.verify(request)
|
await auth.verify(request)
|
||||||
|
|
||||||
|
|
||||||
# Map EXIF Orientation value to a corresponding PIL transpose
|
class PreviewTimeout(Exception):
|
||||||
EXIF_ORI = {
|
"""Raised when the preview subprocess exceeds PREVIEW_TIMEOUT."""
|
||||||
2: Image.Transpose.FLIP_LEFT_RIGHT,
|
|
||||||
3: Image.Transpose.ROTATE_180,
|
|
||||||
4: Image.Transpose.FLIP_TOP_BOTTOM,
|
class PreviewError(Exception):
|
||||||
5: Image.Transpose.TRANSPOSE,
|
"""Raised when the preview subprocess exits with a non-zero status."""
|
||||||
6: Image.Transpose.ROTATE_270,
|
|
||||||
7: Image.Transpose.TRANSVERSE,
|
def __init__(
|
||||||
8: Image.Transpose.ROTATE_90,
|
self,
|
||||||
}
|
message: str,
|
||||||
|
*,
|
||||||
|
stderr: str | None = None,
|
||||||
|
backend: str | None = None,
|
||||||
|
):
|
||||||
|
super().__init__(message)
|
||||||
|
self.stderr = stderr
|
||||||
|
self.backend = backend
|
||||||
|
|
||||||
|
|
||||||
|
async def _run_preview_process(
|
||||||
|
filepath, quality: int, maxsize: int, maxzoom: float
|
||||||
|
) -> tuple[bytes | None, PreviewResponse | None]:
|
||||||
|
"""Run preview request in a persistent worker process."""
|
||||||
|
await start_preview_workers()
|
||||||
|
if _preview_pool is None:
|
||||||
|
raise PreviewError(f"preview worker pool unavailable for {filepath.name}")
|
||||||
|
return await _preview_pool.run(filepath, quality, maxsize, maxzoom)
|
||||||
|
|
||||||
|
|
||||||
|
DOC_PREVIEW_SUFFIXES = {".pdf", ".xps", ".epub", ".mobi"}
|
||||||
|
|
||||||
|
|
||||||
|
def is_previewable_path(path) -> bool:
|
||||||
|
suffix = path.suffix.lower()
|
||||||
|
if suffix in DOC_PREVIEW_SUFFIXES:
|
||||||
|
return True
|
||||||
|
mime_type, _ = mimetypes.guess_type(path.name)
|
||||||
|
if not mime_type:
|
||||||
|
return False
|
||||||
|
return mime_type.startswith("image/") or mime_type.startswith("video/")
|
||||||
|
|
||||||
|
|
||||||
@bp.get("/<path:path>")
|
@bp.get("/<path:path>")
|
||||||
@@ -51,82 +338,122 @@ async def preview(req, path):
|
|||||||
quality = int(req.args.get("q", 60))
|
quality = int(req.args.get("q", 60))
|
||||||
rel = PurePosixPath(sanitize(unquote(path)))
|
rel = PurePosixPath(sanitize(unquote(path)))
|
||||||
filepath = config.config.path / rel
|
filepath = config.config.path / rel
|
||||||
stat = filepath.lstat()
|
try:
|
||||||
|
stat = filepath.lstat()
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise NotFound() from None
|
||||||
|
|
||||||
|
if not is_previewable_path(filepath):
|
||||||
|
return empty(415)
|
||||||
|
|
||||||
etag = config.derived_secret(
|
etag = config.derived_secret(
|
||||||
"preview", rel, stat.st_mtime_ns, quality, maxsize, maxzoom
|
"preview", rel, stat.st_mtime_ns, quality, maxsize, maxzoom
|
||||||
).hex()
|
).hex()
|
||||||
|
|
||||||
|
if req.headers.if_none_match == etag:
|
||||||
|
# The client has it cached, respond 304 Not Modified
|
||||||
|
return empty(304, headers={"etag": etag})
|
||||||
|
|
||||||
|
# Check in-memory cache first (includes headers)
|
||||||
|
cached = _preview_cache.get(etag)
|
||||||
|
if cached is not None:
|
||||||
|
logger.debug(f"Preview cache hit: {rel}")
|
||||||
|
return raw(cached.body, headers=cached.headers)
|
||||||
|
|
||||||
|
# Generate preview
|
||||||
|
try:
|
||||||
|
img, preview_resp = await _run_preview_process(
|
||||||
|
filepath, quality, maxsize, maxzoom
|
||||||
|
)
|
||||||
|
except PreviewTimeout:
|
||||||
|
return empty(504)
|
||||||
|
except PreviewError as e:
|
||||||
|
if e.backend:
|
||||||
|
req.ctx._log_extra = e.backend
|
||||||
|
detail = str(e)
|
||||||
|
if detail == "preview worker error" and e.stderr:
|
||||||
|
captured = e.stderr.strip()
|
||||||
|
if captured:
|
||||||
|
detail = captured.splitlines()[0]
|
||||||
|
logger.error("%s preview: %s", filepath, detail)
|
||||||
|
return empty(422)
|
||||||
|
if preview_resp and preview_resp.backend:
|
||||||
|
if preview_resp.timings:
|
||||||
|
timing_detail = "/".join(
|
||||||
|
str(int(round(value))) for value in preview_resp.timings
|
||||||
|
)
|
||||||
|
req.ctx._log_extra = f"{preview_resp.backend} {timing_detail} ➛"
|
||||||
|
else:
|
||||||
|
req.ctx._log_extra = preview_resp.backend
|
||||||
|
if not img:
|
||||||
|
# Preview generation failed, redirect to the file itself
|
||||||
|
return redirect(f"/files/{path}", status=303)
|
||||||
|
|
||||||
|
# Build headers and cache the full response
|
||||||
|
preview_mime = (
|
||||||
|
preview_resp.mime
|
||||||
|
if preview_resp is not None and preview_resp.mime is not None
|
||||||
|
else "image/avif"
|
||||||
|
)
|
||||||
savename = PurePosixPath(filepath.name).with_suffix(".avif")
|
savename = PurePosixPath(filepath.name).with_suffix(".avif")
|
||||||
headers = {
|
headers = {
|
||||||
"etag": etag,
|
"etag": etag,
|
||||||
"last-modified": format_date_time(stat.st_mtime),
|
"last-modified": format_date_time(stat.st_mtime),
|
||||||
"cache-control": "max-age=604800, immutable"
|
"cache-control": "max-age=604800, immutable"
|
||||||
+ ("" if config.config.public else ", private"),
|
+ ("" if config.config.public else ", private"),
|
||||||
"content-type": "image/avif",
|
"content-type": preview_mime,
|
||||||
"content-disposition": f"inline; filename*=UTF-8''{urllib.parse.quote(savename.as_posix())}",
|
"content-disposition": f"inline; filename*=UTF-8''{urllib.parse.quote(savename.as_posix())}",
|
||||||
}
|
}
|
||||||
if req.headers.if_none_match == etag:
|
_preview_cache.set(etag, CachedPreview(headers=headers, body=img))
|
||||||
# The client has it cached, respond 304 Not Modified
|
|
||||||
return empty(304, headers=headers)
|
|
||||||
|
|
||||||
if not filepath.is_file():
|
|
||||||
raise NotFound("File not found")
|
|
||||||
|
|
||||||
img = await asyncio.get_event_loop().run_in_executor(
|
|
||||||
req.app.ctx.threadexec, dispatch, filepath, quality, maxsize, maxzoom
|
|
||||||
)
|
|
||||||
if not img:
|
|
||||||
# Preview generation failed, redirect to the file itself
|
|
||||||
return redirect(f"/files/{path}", status=303)
|
|
||||||
return raw(img, headers=headers)
|
return raw(img, headers=headers)
|
||||||
|
|
||||||
|
|
||||||
def dispatch(path, quality, maxsize, maxzoom):
|
def dispatch(path, quality, maxsize, maxzoom):
|
||||||
|
backend = "unknown"
|
||||||
try:
|
try:
|
||||||
if path.suffix.lower() in (".pdf", ".xps", ".epub", ".mobi"):
|
if path.suffix.lower() in DOC_PREVIEW_SUFFIXES:
|
||||||
|
backend = "pdf"
|
||||||
return process_pdf(path, quality=quality, maxsize=maxsize, maxzoom=maxzoom)
|
return process_pdf(path, quality=quality, maxsize=maxsize, maxzoom=maxzoom)
|
||||||
type, _ = mimetypes.guess_type(path.name)
|
mime_type, _ = mimetypes.guess_type(path.name)
|
||||||
if type and type.startswith("video/"):
|
if mime_type and mime_type.startswith("video/"):
|
||||||
|
backend = "video"
|
||||||
return process_video(path, quality=quality, maxsize=maxsize)
|
return process_video(path, quality=quality, maxsize=maxsize)
|
||||||
return process_image(path, quality=quality, maxsize=maxsize)
|
if mime_type and mime_type.startswith("image/"):
|
||||||
|
backend = "pyvips"
|
||||||
|
return process_image(path, quality=quality, maxsize=maxsize)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
logger.warning(f"Cannot generate preview for {path}: {e}")
|
return None, PreviewResponse(ok=False, backend=backend, error=str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(f"Error generating preview for {path}: {e}")
|
return None, PreviewResponse(ok=False, backend=backend, error=str(e))
|
||||||
|
return None, PreviewResponse(ok=False, backend=backend, error="preview unsupported")
|
||||||
|
|
||||||
|
|
||||||
def process_image(path, *, maxsize, quality):
|
def process_image(path, *, maxsize, quality):
|
||||||
t_load = perf_counter()
|
return process_image_pyvips(path, maxsize=maxsize, quality=quality)
|
||||||
with Image.open(path) as img:
|
|
||||||
# Force decode to include I/O in load timing
|
|
||||||
img.load()
|
|
||||||
t_proc = perf_counter()
|
|
||||||
# Resize
|
|
||||||
w, h = img.size
|
|
||||||
img.thumbnail((min(w, maxsize), min(h, maxsize)))
|
|
||||||
# Transpose pixels according to EXIF Orientation
|
|
||||||
orientation = img.getexif().get(274, 1)
|
|
||||||
if orientation in EXIF_ORI:
|
|
||||||
img = img.transpose(EXIF_ORI[orientation])
|
|
||||||
# Save as AVIF
|
|
||||||
imgdata = io.BytesIO()
|
|
||||||
t_save = perf_counter()
|
|
||||||
img.save(imgdata, format="avif", quality=quality, speed=10, max_threads=1)
|
|
||||||
|
|
||||||
t_end = perf_counter()
|
|
||||||
ret = imgdata.getvalue()
|
|
||||||
|
|
||||||
load_ms = (t_proc - t_load) * 1000
|
def process_image_pyvips(path, *, maxsize, quality):
|
||||||
proc_ms = (t_save - t_proc) * 1000
|
t_start = perf_counter()
|
||||||
save_ms = (t_end - t_save) * 1000
|
img = pyvips.Image.new_from_file(str(path), access="sequential")
|
||||||
logger.debug(
|
img = img.autorot()
|
||||||
"Preview image %s: load=%.1fms process=%.1fms save=%.1fms",
|
scale = min(maxsize / img.width, maxsize / img.height, 1.0)
|
||||||
path.name,
|
if scale < 1.0:
|
||||||
load_ms,
|
img = img.resize(scale)
|
||||||
proc_ms,
|
ret = img.write_to_buffer(
|
||||||
save_ms,
|
".avif",
|
||||||
|
Q=quality,
|
||||||
|
effort=AVIF_FAST_EFFORT,
|
||||||
|
strip=True,
|
||||||
)
|
)
|
||||||
|
t_end = perf_counter()
|
||||||
|
|
||||||
return ret
|
return ret, PreviewResponse(
|
||||||
|
ok=True,
|
||||||
|
mime="image/avif",
|
||||||
|
backend="pyvips",
|
||||||
|
timings=[round((t_end - t_start) * 1000, 1)],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def process_pdf(path, *, maxsize, maxzoom, quality, page_number=0):
|
def process_pdf(path, *, maxsize, maxzoom, quality, page_number=0):
|
||||||
@@ -140,16 +467,24 @@ def process_pdf(path, *, maxsize, maxzoom, quality, page_number=0):
|
|||||||
t_load_end = perf_counter()
|
t_load_end = perf_counter()
|
||||||
|
|
||||||
t_save_start = perf_counter()
|
t_save_start = perf_counter()
|
||||||
ret = pix.pil_tobytes(format="avif", quality=quality, speed=10, max_threads=1)
|
img = pyvips.Image.new_from_memory(
|
||||||
|
pix.samples_mv, pix.width, pix.height, pix.n, "uchar"
|
||||||
|
)
|
||||||
|
ret = img.write_to_buffer(
|
||||||
|
".avif", Q=quality, effort=AVIF_FAST_EFFORT, strip=True
|
||||||
|
)
|
||||||
|
backend = "pdf+pyvips"
|
||||||
t_save_end = perf_counter()
|
t_save_end = perf_counter()
|
||||||
|
|
||||||
logger.debug(
|
return ret, PreviewResponse(
|
||||||
"Preview pdf %s: load+render=%.1fms save=%.1fms",
|
ok=True,
|
||||||
path.name,
|
mime="image/avif",
|
||||||
(t_load_end - t_load_start) * 1000,
|
backend=backend,
|
||||||
(t_save_end - t_save_start) * 1000,
|
timings=[
|
||||||
|
round((t_load_end - t_load_start) * 1000, 1),
|
||||||
|
round((t_save_end - t_save_start) * 1000, 1),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
def process_video(path, *, maxsize, quality):
|
def process_video(path, *, maxsize, quality):
|
||||||
@@ -162,7 +497,13 @@ def process_video(path, *, maxsize, quality):
|
|||||||
t_save_start = t_load_start
|
t_save_start = t_load_start
|
||||||
t_save_end = t_load_start
|
t_save_end = t_load_start
|
||||||
with (
|
with (
|
||||||
av.open(str(path)) as icontainer,
|
av.open(
|
||||||
|
str(path),
|
||||||
|
options={
|
||||||
|
"analyzeduration": "1000000", # 1 second (in microseconds)
|
||||||
|
"fflags": "fastseek",
|
||||||
|
},
|
||||||
|
) as icontainer,
|
||||||
av.open(imgdata, "w", format="avif") as ocontainer,
|
av.open(imgdata, "w", format="avif") as ocontainer,
|
||||||
):
|
):
|
||||||
istream = icontainer.streams.video[0]
|
istream = icontainer.streams.video[0]
|
||||||
@@ -254,14 +595,17 @@ def process_video(path, *, maxsize, quality):
|
|||||||
ocontainer.mux(ostream.encode(None)) # Flush the stream
|
ocontainer.mux(ostream.encode(None)) # Flush the stream
|
||||||
t_save_end = perf_counter()
|
t_save_end = perf_counter()
|
||||||
|
|
||||||
# Capture frame dimensions before cleanup
|
# Capture result before cleanup
|
||||||
ret = imgdata.getvalue()
|
ret = imgdata.getvalue()
|
||||||
logger.debug(
|
resp = PreviewResponse(
|
||||||
"Preview video %s: load+decode=%.1fms save=%.1fms",
|
ok=True,
|
||||||
path.name,
|
mime="image/avif",
|
||||||
(t_load_end - t_load_start) * 1000,
|
backend="video",
|
||||||
(t_save_end - t_save_start) * 1000,
|
timings=[
|
||||||
|
round((t_load_end - t_load_start) * 1000, 1),
|
||||||
|
round((t_save_end - t_save_start) * 1000, 1),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
del imgdata, istream, ostream, icc, occ, frame
|
del imgdata, istream, ostream, icc, occ, frame
|
||||||
gc.collect()
|
gc.collect()
|
||||||
return ret
|
return ret, resp
|
||||||
|
|||||||
@@ -0,0 +1,116 @@
|
|||||||
|
"""Preview generation worker subprocess.
|
||||||
|
|
||||||
|
Two modes are supported:
|
||||||
|
1) Legacy one-shot mode: argv has path/quality/maxsize/maxzoom.
|
||||||
|
2) Long-lived mode: read JSONL commands from stdin and write framed responses.
|
||||||
|
|
||||||
|
Framed response format:
|
||||||
|
(blake3(packet))(uint32 json size)(uint32 payload size)(json)(binary payload)
|
||||||
|
where packet = (uint32 json size)(uint32 payload size)(json)(binary payload).
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import contextlib
|
||||||
|
import io
|
||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import msgspec
|
||||||
|
from blake3 import blake3
|
||||||
|
|
||||||
|
|
||||||
|
class PreviewRequest(msgspec.Struct, omit_defaults=True):
|
||||||
|
path: str
|
||||||
|
quality: int
|
||||||
|
maxsize: int
|
||||||
|
maxzoom: float
|
||||||
|
|
||||||
|
|
||||||
|
class PreviewResponse(msgspec.Struct, omit_defaults=True):
|
||||||
|
ok: bool
|
||||||
|
mime: str | None = None
|
||||||
|
backend: str | None = None
|
||||||
|
timings: list[float] | None = None
|
||||||
|
error: str | None = None
|
||||||
|
stderr: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
_enc = msgspec.json.Encoder()
|
||||||
|
_dec_req = msgspec.json.Decoder(PreviewRequest)
|
||||||
|
|
||||||
|
|
||||||
|
def _write_response(resp: PreviewResponse, payload: bytes) -> None:
|
||||||
|
meta_bytes = _enc.encode(resp)
|
||||||
|
packet = struct.pack("<II", len(meta_bytes), len(payload)) + meta_bytes + payload
|
||||||
|
checksum = blake3(packet).digest()
|
||||||
|
sys.stdout.buffer.write(checksum)
|
||||||
|
sys.stdout.buffer.write(packet)
|
||||||
|
sys.stdout.buffer.flush()
|
||||||
|
|
||||||
|
|
||||||
|
def _run_once() -> None:
|
||||||
|
if len(sys.argv) != 5:
|
||||||
|
sys.stderr.write(f"Usage: {sys.argv[0]} <path> <quality> <maxsize> <maxzoom>\n")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
from cista.preview import dispatch
|
||||||
|
|
||||||
|
path = Path(sys.argv[1])
|
||||||
|
quality = int(sys.argv[2])
|
||||||
|
maxsize = int(sys.argv[3])
|
||||||
|
maxzoom = float(sys.argv[4])
|
||||||
|
result, _ = dispatch(path, quality, maxsize, maxzoom)
|
||||||
|
if result:
|
||||||
|
sys.stdout.buffer.write(result)
|
||||||
|
sys.stdout.buffer.flush()
|
||||||
|
|
||||||
|
|
||||||
|
def _run_loop() -> None:
|
||||||
|
from cista.preview import dispatch
|
||||||
|
|
||||||
|
while True:
|
||||||
|
line = sys.stdin.buffer.readline()
|
||||||
|
if not line:
|
||||||
|
return
|
||||||
|
stderr_capture = io.StringIO()
|
||||||
|
handler = logging.StreamHandler(stderr_capture)
|
||||||
|
root_logger = logging.getLogger()
|
||||||
|
root_logger.addHandler(handler)
|
||||||
|
try:
|
||||||
|
with contextlib.redirect_stderr(stderr_capture):
|
||||||
|
req = _dec_req.decode(line)
|
||||||
|
result, resp = dispatch(
|
||||||
|
Path(req.path), req.quality, req.maxsize, req.maxzoom
|
||||||
|
)
|
||||||
|
if not resp.ok:
|
||||||
|
captured = stderr_capture.getvalue().strip()
|
||||||
|
if captured:
|
||||||
|
resp = PreviewResponse(
|
||||||
|
ok=False,
|
||||||
|
backend=resp.backend,
|
||||||
|
error=resp.error,
|
||||||
|
stderr=captured,
|
||||||
|
)
|
||||||
|
_write_response(resp, result or b"")
|
||||||
|
except Exception as e:
|
||||||
|
captured = stderr_capture.getvalue().strip()
|
||||||
|
_write_response(
|
||||||
|
PreviewResponse(ok=False, error=str(e), stderr=captured or None), b""
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
root_logger.removeHandler(handler)
|
||||||
|
handler.close()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
# Configure all log output to stderr before any imports that may emit logs.
|
||||||
|
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
_run_once()
|
||||||
|
return
|
||||||
|
_run_loop()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
+32
-13
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import shutil
|
import shutil
|
||||||
|
from pathlib import PurePosixPath
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import msgspec
|
import msgspec
|
||||||
@@ -11,11 +12,14 @@ from cista.util import filename
|
|||||||
|
|
||||||
## Control commands
|
## Control commands
|
||||||
|
|
||||||
|
|
||||||
class ControlBase(msgspec.Struct, tag_field="op", tag=str.lower):
|
class ControlBase(msgspec.Struct, tag_field="op", tag=str.lower):
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def affected_paths(self) -> list[str]:
|
||||||
|
"""Return list of paths affected by this operation for change notification."""
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
class MkDir(ControlBase):
|
class MkDir(ControlBase):
|
||||||
path: str
|
path: str
|
||||||
@@ -24,6 +28,9 @@ class MkDir(ControlBase):
|
|||||||
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=True, exist_ok=False)
|
||||||
|
|
||||||
|
def affected_paths(self) -> list[str]:
|
||||||
|
return [filename.sanitize(self.path)]
|
||||||
|
|
||||||
|
|
||||||
class Rename(ControlBase):
|
class Rename(ControlBase):
|
||||||
path: str
|
path: str
|
||||||
@@ -36,6 +43,11 @@ class Rename(ControlBase):
|
|||||||
path = config.config.path / filename.sanitize(self.path)
|
path = config.config.path / filename.sanitize(self.path)
|
||||||
path.rename(path.with_name(to))
|
path.rename(path.with_name(to))
|
||||||
|
|
||||||
|
def affected_paths(self) -> list[str]:
|
||||||
|
sanitized = filename.sanitize(self.path)
|
||||||
|
new_path = str(PurePosixPath(sanitized).with_name(filename.sanitize(self.to)))
|
||||||
|
return [sanitized, new_path]
|
||||||
|
|
||||||
|
|
||||||
class Rm(ControlBase):
|
class Rm(ControlBase):
|
||||||
sel: list[str]
|
sel: list[str]
|
||||||
@@ -49,6 +61,9 @@ class Rm(ControlBase):
|
|||||||
else:
|
else:
|
||||||
p.unlink()
|
p.unlink()
|
||||||
|
|
||||||
|
def affected_paths(self) -> list[str]:
|
||||||
|
return [filename.sanitize(p) for p in self.sel]
|
||||||
|
|
||||||
|
|
||||||
class Mv(ControlBase):
|
class Mv(ControlBase):
|
||||||
sel: list[str]
|
sel: list[str]
|
||||||
@@ -63,6 +78,13 @@ class Mv(ControlBase):
|
|||||||
for p in sel:
|
for p in sel:
|
||||||
shutil.move(p, dst)
|
shutil.move(p, dst)
|
||||||
|
|
||||||
|
def affected_paths(self) -> list[str]:
|
||||||
|
dst = filename.sanitize(self.dst)
|
||||||
|
paths = [filename.sanitize(p) for p in self.sel]
|
||||||
|
# Include new locations in dst
|
||||||
|
paths.extend(f"{dst}/{PurePosixPath(p).name}" for p in self.sel)
|
||||||
|
return paths
|
||||||
|
|
||||||
|
|
||||||
class Cp(ControlBase):
|
class Cp(ControlBase):
|
||||||
sel: list[str]
|
sel: list[str]
|
||||||
@@ -86,23 +108,18 @@ class Cp(ControlBase):
|
|||||||
else:
|
else:
|
||||||
shutil.copy2(p, dst)
|
shutil.copy2(p, dst)
|
||||||
|
|
||||||
|
def affected_paths(self) -> list[str]:
|
||||||
|
dst = filename.sanitize(self.dst)
|
||||||
|
# Only destinations are new (sources unchanged)
|
||||||
|
return [f"{dst}/{PurePosixPath(filename.sanitize(p)).name}" for p in self.sel]
|
||||||
|
|
||||||
|
|
||||||
ControlTypes = MkDir | Rename | Rm | Mv | Cp
|
ControlTypes = MkDir | Rename | Rm | Mv | Cp
|
||||||
|
|
||||||
|
|
||||||
## File uploads and downloads
|
|
||||||
|
|
||||||
|
|
||||||
class FileRange(msgspec.Struct):
|
|
||||||
name: str
|
|
||||||
size: int
|
|
||||||
start: int
|
|
||||||
end: int
|
|
||||||
|
|
||||||
|
|
||||||
class StatusMsg(msgspec.Struct):
|
class StatusMsg(msgspec.Struct):
|
||||||
status: str
|
status: str
|
||||||
req: FileRange
|
req: Any
|
||||||
|
|
||||||
|
|
||||||
class ErrorMsg(msgspec.Struct):
|
class ErrorMsg(msgspec.Struct):
|
||||||
@@ -118,6 +135,7 @@ class FileEntry(msgspec.Struct, array_like=True, frozen=True):
|
|||||||
key: str
|
key: str
|
||||||
mtime: int
|
mtime: int
|
||||||
size: int
|
size: int
|
||||||
|
allocated: int
|
||||||
isfile: int
|
isfile: int
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@@ -149,5 +167,6 @@ class UpdateMessage(msgspec.Struct):
|
|||||||
class Space(msgspec.Struct):
|
class Space(msgspec.Struct):
|
||||||
disk: int
|
disk: int
|
||||||
free: int
|
free: int
|
||||||
usage: int
|
used: int
|
||||||
storage: int
|
storage: int
|
||||||
|
allocated: int
|
||||||
|
|||||||
@@ -0,0 +1,257 @@
|
|||||||
|
"""Custom access logging middleware for Sanic."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
import unicodedata
|
||||||
|
from ipaddress import IPv6Address
|
||||||
|
|
||||||
|
logger = logging.getLogger("cista.access")
|
||||||
|
|
||||||
|
_RESET = "\033[0m"
|
||||||
|
_STATUS_INFO = "\033[32m" # 1xx (green)
|
||||||
|
_STATUS_OK = "\033[1;92m" # 2xx (bright green)
|
||||||
|
_STATUS_REDIRECT = "\033[32m" # 3xx (green)
|
||||||
|
_STATUS_CLIENT_ERR = "\033[0;31m" # 4xx (red)
|
||||||
|
_STATUS_SERVER_ERR = "\033[1;91m" # 5xx (bold bright red)
|
||||||
|
_METHOD_READ = "\033[0;34m" # GET, HEAD, OPTIONS (blue)
|
||||||
|
_METHOD_WRITE = "\033[1;94m" # POST, PUT, DELETE, PATCH (bold bright blue)
|
||||||
|
_HOST = "\033[38;5;242m" # hostname (dark grey)
|
||||||
|
_PATH = "\033[38;5;250m" # path (light grey)
|
||||||
|
_TIMING = "\033[38;5;242m" # timing (dark grey)
|
||||||
|
_WS_OPEN = "\033[1;93m" # WebSocket connect (bold bright yellow)
|
||||||
|
_WS_CLOSE = "\033[33m" # WebSocket disconnect (yellow)
|
||||||
|
_WS_STATUS = "\033[38;5;250m" # WebSocket close status (normal white)
|
||||||
|
|
||||||
|
|
||||||
|
def format_ipv6_network(ip: str) -> str:
|
||||||
|
"""Format IPv6 address to show only network part (first 64 bits)."""
|
||||||
|
try:
|
||||||
|
ip = ip.strip("[]")
|
||||||
|
if "%" in ip:
|
||||||
|
ip = ip.split("%")[0]
|
||||||
|
addr = IPv6Address(ip)
|
||||||
|
if addr.is_loopback:
|
||||||
|
return "::1"
|
||||||
|
if addr.is_unspecified:
|
||||||
|
return "::"
|
||||||
|
if addr.ipv4_mapped:
|
||||||
|
return str(addr.ipv4_mapped)
|
||||||
|
if addr.is_link_local:
|
||||||
|
return str(addr)
|
||||||
|
network_int = int(addr) >> 64
|
||||||
|
groups = []
|
||||||
|
for _ in range(4):
|
||||||
|
groups.insert(0, format(network_int & 0xFFFF, "x"))
|
||||||
|
network_int >>= 16
|
||||||
|
result = ":".join(groups) + "::"
|
||||||
|
return str(IPv6Address(result + "0")).removesuffix("::")
|
||||||
|
except Exception:
|
||||||
|
return ip
|
||||||
|
|
||||||
|
|
||||||
|
def format_client_ip(ip: str) -> str:
|
||||||
|
"""Format client IP, compressing IPv6 to network part only."""
|
||||||
|
if not ip or ip == "-":
|
||||||
|
return "-"
|
||||||
|
stripped = ip.strip("[]")
|
||||||
|
if ":" in stripped:
|
||||||
|
return format_ipv6_network(stripped)
|
||||||
|
return stripped
|
||||||
|
|
||||||
|
|
||||||
|
def status_color(status: int) -> str:
|
||||||
|
if status < 200:
|
||||||
|
return _STATUS_INFO
|
||||||
|
if status < 300:
|
||||||
|
return _STATUS_OK
|
||||||
|
if status < 400:
|
||||||
|
return _STATUS_REDIRECT
|
||||||
|
if status < 500:
|
||||||
|
return _STATUS_CLIENT_ERR
|
||||||
|
return _STATUS_SERVER_ERR
|
||||||
|
|
||||||
|
|
||||||
|
def method_color(method: str) -> str:
|
||||||
|
if method in ("GET", "HEAD", "OPTIONS"):
|
||||||
|
return _METHOD_READ
|
||||||
|
return _METHOD_WRITE
|
||||||
|
|
||||||
|
|
||||||
|
def format_duration_ms(duration_ms: float) -> str:
|
||||||
|
rounded_ms = round(duration_ms)
|
||||||
|
if rounded_ms < 2000:
|
||||||
|
return f"{rounded_ms}ms"
|
||||||
|
total_s = round(duration_ms / 1000)
|
||||||
|
if total_s < 60:
|
||||||
|
return f"{total_s}s"
|
||||||
|
if total_s <= 3600:
|
||||||
|
minutes, seconds = divmod(total_s, 60)
|
||||||
|
return f"{minutes}m{seconds}s"
|
||||||
|
hours, remainder = divmod(total_s, 3600)
|
||||||
|
minutes = round(remainder / 60)
|
||||||
|
if minutes == 60:
|
||||||
|
hours += 1
|
||||||
|
minutes = 0
|
||||||
|
return f"{hours}h{minutes}m"
|
||||||
|
|
||||||
|
|
||||||
|
def _display_width(text: str) -> int:
|
||||||
|
width = 0
|
||||||
|
for char in text:
|
||||||
|
width += 2 if unicodedata.east_asian_width(char) in {"F", "W"} else 1
|
||||||
|
return width
|
||||||
|
|
||||||
|
|
||||||
|
def _format_left(label: str) -> str:
|
||||||
|
return label[:19].ljust(19)
|
||||||
|
|
||||||
|
|
||||||
|
def _format_method_label(label: str, *, color: str | None = None) -> str:
|
||||||
|
color_value = _METHOD_WRITE if color is None else color
|
||||||
|
padding = max(0, 7 - _display_width(label))
|
||||||
|
return f"{color_value}{label}{' ' * padding}{_RESET}"
|
||||||
|
|
||||||
|
|
||||||
|
def format_access_log(
|
||||||
|
client: str, status: int, method: str, host: str, path: str, duration_ms: float,
|
||||||
|
extra: str | None = None,
|
||||||
|
) -> str:
|
||||||
|
ip = _format_left(format_client_ip(client))
|
||||||
|
status_str = f"{status_color(status)}{str(status).rjust(3)}{_RESET}"
|
||||||
|
method_str = _format_method_label(method, color=method_color(method))
|
||||||
|
host_str = f"{_HOST}{host}{_RESET}"
|
||||||
|
path_str = f"{_PATH}{path}{_RESET}"
|
||||||
|
timing_str = f"{_TIMING}{format_duration_ms(duration_ms)}{_RESET}"
|
||||||
|
extra_str = f" {_TIMING}{extra}{_RESET}" if extra else ""
|
||||||
|
return f"{ip} {status_str} {method_str} {host_str}{path_str}{extra_str} {timing_str}"
|
||||||
|
|
||||||
|
|
||||||
|
_ws_counter = 1
|
||||||
|
|
||||||
|
|
||||||
|
def _next_ws_id() -> int:
|
||||||
|
global _ws_counter
|
||||||
|
ws_id = _ws_counter
|
||||||
|
_ws_counter += 1
|
||||||
|
return ws_id
|
||||||
|
|
||||||
|
|
||||||
|
def _format_ws_id(ws_id: int, *, bright: bool = False) -> str:
|
||||||
|
value = str(ws_id) if ws_id >= 100 else f"{ws_id:02d}"
|
||||||
|
color = _WS_OPEN if bright else _WS_CLOSE
|
||||||
|
return f"{color}{value.rjust(3)}{_RESET}"
|
||||||
|
|
||||||
|
|
||||||
|
def log_ws_open(request, extra: str | None = None) -> int:
|
||||||
|
"""Log WebSocket connection open. Returns connection ID for use in log_ws_close."""
|
||||||
|
ws_id = _next_ws_id()
|
||||||
|
|
||||||
|
client = request.client_ip or "-"
|
||||||
|
host = request.host or "-"
|
||||||
|
path = request.path
|
||||||
|
origin = request.headers.get("origin")
|
||||||
|
|
||||||
|
ip = _format_left(format_client_ip(client))
|
||||||
|
id_str = _format_ws_id(ws_id, bright=True)
|
||||||
|
|
||||||
|
origin_host = origin.split("://", 1)[-1] if origin else None
|
||||||
|
show_origin = origin_host and origin_host != host
|
||||||
|
|
||||||
|
method_str = _format_method_label("🔌", color=_WS_OPEN)
|
||||||
|
host_str = f"{_HOST}{host}{_RESET}"
|
||||||
|
path_str = f"{_PATH}{path}{_RESET}"
|
||||||
|
origin_str = f" {_RESET}from {_HOST}{origin_host}{_RESET}" if show_origin else ""
|
||||||
|
extra_str = f" {_TIMING}{extra}{_RESET}" if extra else ""
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"%s %s %s %s%s%s",
|
||||||
|
ip,
|
||||||
|
id_str,
|
||||||
|
method_str,
|
||||||
|
host_str,
|
||||||
|
path_str,
|
||||||
|
origin_str + extra_str,
|
||||||
|
)
|
||||||
|
return ws_id
|
||||||
|
|
||||||
|
|
||||||
|
WS_CLOSE_CODES = {
|
||||||
|
1000: "ok",
|
||||||
|
1001: "going away",
|
||||||
|
1002: "protocol error",
|
||||||
|
1003: "unsupported",
|
||||||
|
1005: "no status",
|
||||||
|
1006: "abnormal",
|
||||||
|
1007: "invalid data",
|
||||||
|
1008: "policy violation",
|
||||||
|
1009: "too large",
|
||||||
|
1010: "extension required",
|
||||||
|
1011: "server error",
|
||||||
|
1012: "restarting",
|
||||||
|
1013: "try again",
|
||||||
|
1014: "bad gateway",
|
||||||
|
1015: "tls error",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def log_ws_close(ws_id: int, close_code: int | None, duration: float) -> None:
|
||||||
|
"""Log WebSocket connection close with duration and status."""
|
||||||
|
id_str = _format_ws_id(ws_id)
|
||||||
|
timing = format_duration_ms(duration * 1000)
|
||||||
|
|
||||||
|
if close_code is None:
|
||||||
|
code = "----"
|
||||||
|
status = "unknown"
|
||||||
|
else:
|
||||||
|
code = str(close_code)
|
||||||
|
status = WS_CLOSE_CODES.get(close_code, f"code {close_code}")
|
||||||
|
|
||||||
|
method_str = _format_method_label("closed", color=_TIMING)
|
||||||
|
status_str = f"{_WS_STATUS}{code} {status}{_RESET}"
|
||||||
|
timing_str = f"{_TIMING}{timing}{_RESET}"
|
||||||
|
|
||||||
|
logger.info("%s %s %s %s %s", " " * 19, id_str, method_str, status_str, timing_str)
|
||||||
|
|
||||||
|
|
||||||
|
def configure_access_logging() -> None:
|
||||||
|
"""Configure the cista.access logger to output to stderr."""
|
||||||
|
handler = logging.StreamHandler(sys.stderr)
|
||||||
|
handler.setFormatter(logging.Formatter("%(message)s"))
|
||||||
|
logger.addHandler(handler)
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
logger.propagate = False
|
||||||
|
|
||||||
|
|
||||||
|
_LEVEL_EMOJI = {
|
||||||
|
logging.DEBUG: "🔍",
|
||||||
|
logging.INFO: "ℹ️",
|
||||||
|
logging.WARNING: "⚠️",
|
||||||
|
logging.ERROR: "🛑",
|
||||||
|
logging.CRITICAL: "🛑",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class _EmojiFormatter(logging.Formatter):
|
||||||
|
"""Compact formatter: emoji + message, no timestamp/level text/logger name."""
|
||||||
|
|
||||||
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
|
emoji = _LEVEL_EMOJI.get(record.levelno, "▪️")
|
||||||
|
sep = " " if record.levelno in (logging.INFO, logging.WARNING) else " "
|
||||||
|
return f"{emoji}{sep}{record.getMessage()}"
|
||||||
|
|
||||||
|
|
||||||
|
def configure_main_logging() -> None:
|
||||||
|
"""Replace Sanic's verbose 'Main yyyy-mm-dd INFO:' prefix with emoji-only format.
|
||||||
|
|
||||||
|
Patches LOGGING_CONFIG_DEFAULTS so the formatter survives every dictConfig
|
||||||
|
call Sanic makes during serve_single() / serve().
|
||||||
|
"""
|
||||||
|
from sanic.log import LOGGING_CONFIG_DEFAULTS
|
||||||
|
|
||||||
|
LOGGING_CONFIG_DEFAULTS["formatters"]["generic"] = {
|
||||||
|
"class": "cista.sanic_logging._EmojiFormatter",
|
||||||
|
}
|
||||||
|
# Also reformat any handlers already attached (covers the initial Sanic() call)
|
||||||
|
for name in ("sanic.root", "sanic.error", "sanic.server", "sanic.websockets"):
|
||||||
|
for handler in logging.getLogger(name).handlers:
|
||||||
|
handler.setFormatter(_EmojiFormatter())
|
||||||
+17
-11
@@ -2,6 +2,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from fastapi_vue.hostutil import parse_endpoint
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
|
|
||||||
from cista import config, server80
|
from cista import config, server80
|
||||||
@@ -26,7 +27,7 @@ def run(*, dev=False):
|
|||||||
motd=False,
|
motd=False,
|
||||||
dev=dev,
|
dev=dev,
|
||||||
auto_reload=dev,
|
auto_reload=dev,
|
||||||
access_log=True,
|
access_log=False,
|
||||||
) # type: ignore
|
) # type: ignore
|
||||||
if dev:
|
if dev:
|
||||||
Sanic.serve()
|
Sanic.serve()
|
||||||
@@ -44,18 +45,23 @@ def check_cert(certdir, domain):
|
|||||||
|
|
||||||
|
|
||||||
def parse_listen(listen):
|
def parse_listen(listen):
|
||||||
if listen.startswith("/"):
|
# Domain name (e.g. example.com) -> HTTPS with LetsEncrypt
|
||||||
unix = Path(listen).resolve()
|
if re.fullmatch(r"(\w+(-\w+)*\.)+\w{2,}", listen, re.UNICODE):
|
||||||
|
return f"https://{listen}", {"host": listen, "port": 443, "ssl": True}
|
||||||
|
|
||||||
|
# Use fastapi_vue's parse_endpoint for everything else
|
||||||
|
endpoints = parse_endpoint(listen, default_port=8989)
|
||||||
|
ep = endpoints[0]
|
||||||
|
|
||||||
|
if "uds" in ep:
|
||||||
|
unix = Path(ep["uds"]).resolve()
|
||||||
if not unix.parent.exists():
|
if not unix.parent.exists():
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Directory for unix socket does not exist: {unix.parent}/",
|
f"Directory for unix socket does not exist: {unix.parent}/",
|
||||||
)
|
)
|
||||||
return "http://localhost", {"unix": unix.as_posix()}
|
return "http://localhost", {"unix": unix.as_posix()}
|
||||||
if re.fullmatch(r"(\w+(-\w+)*\.)+\w{2,}", listen, re.UNICODE):
|
|
||||||
return f"https://{listen}", {"host": listen, "port": 443, "ssl": True}
|
host, port = ep["host"], ep["port"]
|
||||||
try:
|
if len(endpoints) > 1:
|
||||||
addr, _port = listen.split(":", 1)
|
return f"http://localhost:{port}", {"host": host, "port": port}
|
||||||
port = int(_port)
|
return f"http://{host}:{port}", {"host": host, "port": port}
|
||||||
except Exception:
|
|
||||||
raise ValueError(f"Invalid listen address: {listen}") from None
|
|
||||||
return f"http://localhost:{port}", {"host": addr, "port": port}
|
|
||||||
|
|||||||
+30
-38
@@ -15,7 +15,8 @@ import re
|
|||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import websockets
|
import websockets
|
||||||
from sanic import Blueprint
|
from sanic import Blueprint, json
|
||||||
|
from sanic import raw as raw_response
|
||||||
from sanic.exceptions import Forbidden, SanicException, Unauthorized
|
from sanic.exceptions import Forbidden, SanicException, Unauthorized
|
||||||
from sanic.log import logger
|
from sanic.log import logger
|
||||||
|
|
||||||
@@ -48,6 +49,8 @@ async def get_client() -> httpx.AsyncClient:
|
|||||||
global _client
|
global _client
|
||||||
if _client is None or _client.is_closed:
|
if _client is None or _client.is_closed:
|
||||||
_client = httpx.AsyncClient(timeout=1.0)
|
_client = httpx.AsyncClient(timeout=1.0)
|
||||||
|
if "user-agent" in _client.headers:
|
||||||
|
del _client.headers["user-agent"] # No httpx UA
|
||||||
return _client
|
return _client
|
||||||
|
|
||||||
|
|
||||||
@@ -171,10 +174,10 @@ async def proxy_auth_request(request):
|
|||||||
"upgrade",
|
"upgrade",
|
||||||
"proxy-authorization",
|
"proxy-authorization",
|
||||||
"proxy-authenticate",
|
"proxy-authenticate",
|
||||||
"forwarded",
|
|
||||||
"x-forwarded-for",
|
"x-forwarded-for",
|
||||||
"x-forwarded-host",
|
"x-forwarded-host",
|
||||||
"x-forwarded-proto",
|
"x-forwarded-proto",
|
||||||
|
"forwarded",
|
||||||
}
|
}
|
||||||
|
|
||||||
headers = [
|
headers = [
|
||||||
@@ -182,9 +185,17 @@ async def proxy_auth_request(request):
|
|||||||
for key, value in request.headers.items()
|
for key, value in request.headers.items()
|
||||||
if key.lower() not in skip_headers
|
if key.lower() not in skip_headers
|
||||||
]
|
]
|
||||||
headers.append(("x-forwarded-for", request.client_ip))
|
|
||||||
|
# Set Forwarded headers (strip IPv6 brackets for x-forwarded-for)
|
||||||
|
headers.append(("x-forwarded-for", request.client_ip.strip("[]")))
|
||||||
headers.append(("x-forwarded-host", request.host))
|
headers.append(("x-forwarded-host", request.host))
|
||||||
headers.append(("x-forwarded-proto", request.scheme))
|
headers.append(("x-forwarded-proto", request.scheme))
|
||||||
|
headers.append(
|
||||||
|
(
|
||||||
|
"forwarded",
|
||||||
|
f"by=cista;for={request.client_ip};host={request.host};proto={request.scheme}",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with client.stream(
|
async with client.stream(
|
||||||
@@ -210,8 +221,6 @@ async def proxy_auth_request(request):
|
|||||||
if key.lower() not in resp_hop_by_hop
|
if key.lower() not in resp_hop_by_hop
|
||||||
]
|
]
|
||||||
|
|
||||||
from sanic import raw as raw_response
|
|
||||||
|
|
||||||
return raw_response(
|
return raw_response(
|
||||||
raw_content,
|
raw_content,
|
||||||
status=response.status_code,
|
status=response.status_code,
|
||||||
@@ -221,35 +230,31 @@ async def proxy_auth_request(request):
|
|||||||
|
|
||||||
except httpx.RequestError as e:
|
except httpx.RequestError as e:
|
||||||
logger.error(f"Auth proxy request failed: {e}")
|
logger.error(f"Auth proxy request failed: {e}")
|
||||||
from sanic import json
|
|
||||||
|
|
||||||
return json(
|
return json(
|
||||||
{"detail": "Authentication service unavailable", "error": str(e)},
|
{"detail": "Authentication service unavailable"},
|
||||||
status=503,
|
status=503,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def proxy_auth_websocket(request, ws):
|
async def proxy_auth_websocket(request, ws):
|
||||||
"""Proxy a WebSocket connection to the auth backend."""
|
"""Proxy a WebSocket connection to the auth backend."""
|
||||||
path = request.path
|
url = f"ws{PASKIA_BACKEND_URL.removeprefix('http')}{request.path}"
|
||||||
query_string = request.query_string
|
if request.query_string:
|
||||||
ws_backend = PASKIA_BACKEND_URL.replace("http://", "ws://").replace(
|
url = f"{url}?{request.query_string}"
|
||||||
"https://", "wss://"
|
|
||||||
)
|
|
||||||
url = f"{ws_backend}{path}"
|
|
||||||
if query_string:
|
|
||||||
url = f"{url}?{query_string}"
|
|
||||||
|
|
||||||
additional_headers = {}
|
additional_headers = {}
|
||||||
if "cookie" in request.headers:
|
if "cookie" in request.headers:
|
||||||
additional_headers["cookie"] = request.headers["cookie"]
|
additional_headers["cookie"] = request.headers["cookie"]
|
||||||
if "authorization" in request.headers:
|
if "authorization" in request.headers:
|
||||||
additional_headers["authorization"] = request.headers["authorization"]
|
additional_headers["authorization"] = request.headers["authorization"]
|
||||||
|
if "host" in request.headers:
|
||||||
|
additional_headers["host"] = request.headers["host"]
|
||||||
if "origin" in request.headers:
|
if "origin" in request.headers:
|
||||||
additional_headers["origin"] = request.headers["origin"]
|
additional_headers["origin"] = request.headers["origin"]
|
||||||
if "user-agent" in request.headers:
|
if "user-agent" in request.headers:
|
||||||
additional_headers["user-agent"] = request.headers["user-agent"]
|
additional_headers["user-agent"] = request.headers["user-agent"]
|
||||||
additional_headers["x-forwarded-for"] = request.ip
|
additional_headers["x-forwarded-for"] = request.client_ip.strip("[]")
|
||||||
additional_headers["x-forwarded-host"] = request.host
|
additional_headers["x-forwarded-host"] = request.host
|
||||||
additional_headers["x-forwarded-proto"] = request.scheme
|
additional_headers["x-forwarded-proto"] = request.scheme
|
||||||
|
|
||||||
@@ -281,23 +286,20 @@ async def proxy_auth_websocket(request, ws):
|
|||||||
logger.error(f"WebSocket proxy to {url} failed: {e}")
|
logger.error(f"WebSocket proxy to {url} failed: {e}")
|
||||||
|
|
||||||
|
|
||||||
def _is_websocket_request(request) -> bool:
|
# Blueprint for auth proxy routes (only registered when paskia_enabled())
|
||||||
"""Check if the request is a WebSocket upgrade request."""
|
bp = Blueprint("sso", url_prefix="/auth")
|
||||||
connection = request.headers.get("connection", "").lower()
|
|
||||||
upgrade = request.headers.get("upgrade", "").lower()
|
|
||||||
connection_tokens = [t.strip() for t in connection.split(",")]
|
|
||||||
return "upgrade" in connection_tokens and upgrade == "websocket"
|
|
||||||
|
|
||||||
|
|
||||||
async def _handle_websocket_upgrade(request):
|
@bp.websocket("/ws/<path:path>")
|
||||||
"""Handle WebSocket upgrade and proxy the connection."""
|
async def auth_websocket_proxy(request, ws, path=""):
|
||||||
protocol = request.transport.get_protocol()
|
"""Proxy WebSocket connections to the auth backend."""
|
||||||
ws = await protocol.websocket_handshake(request, subprotocols=None)
|
|
||||||
await proxy_auth_websocket(request, ws)
|
await proxy_auth_websocket(request, ws)
|
||||||
|
|
||||||
|
|
||||||
# Blueprint for auth proxy routes (only registered when paskia_enabled())
|
@bp.websocket("/ws/")
|
||||||
bp = Blueprint("sso", url_prefix="/auth")
|
async def auth_websocket_proxy_root(request, ws):
|
||||||
|
"""Proxy root WebSocket connections to the auth backend."""
|
||||||
|
await proxy_auth_websocket(request, ws)
|
||||||
|
|
||||||
|
|
||||||
@bp.route(
|
@bp.route(
|
||||||
@@ -305,20 +307,10 @@ bp = Blueprint("sso", url_prefix="/auth")
|
|||||||
)
|
)
|
||||||
async def auth_proxy(request, path=""):
|
async def auth_proxy(request, path=""):
|
||||||
"""Proxy all auth requests to the auth backend."""
|
"""Proxy all auth requests to the auth backend."""
|
||||||
if _is_websocket_request(request):
|
|
||||||
await _handle_websocket_upgrade(request)
|
|
||||||
from sanic import empty
|
|
||||||
|
|
||||||
return empty()
|
|
||||||
return await proxy_auth_request(request)
|
return await proxy_auth_request(request)
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
|
@bp.route("/", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
|
||||||
async def auth_proxy_root(request):
|
async def auth_proxy_root(request):
|
||||||
"""Proxy root auth requests to the auth backend."""
|
"""Proxy root auth requests to the auth backend."""
|
||||||
if _is_websocket_request(request):
|
|
||||||
await _handle_websocket_upgrade(request)
|
|
||||||
from sanic import empty
|
|
||||||
|
|
||||||
return empty()
|
|
||||||
return await proxy_auth_request(request)
|
return await proxy_auth_request(request)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import time
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
import msgspec
|
import msgspec
|
||||||
@@ -8,6 +9,7 @@ from sanic.response import raw, redirect
|
|||||||
|
|
||||||
from cista import auth
|
from cista import auth
|
||||||
from cista.protocol import ErrorMsg
|
from cista.protocol import ErrorMsg
|
||||||
|
from cista.sanic_logging import log_ws_close, log_ws_open
|
||||||
|
|
||||||
|
|
||||||
def asend(ws, msg):
|
def asend(ws, msg):
|
||||||
@@ -54,6 +56,10 @@ def websocket_wrapper(handler):
|
|||||||
|
|
||||||
@wraps(handler)
|
@wraps(handler)
|
||||||
async def wrapper(request, ws, *args, **kwargs):
|
async def wrapper(request, ws, *args, **kwargs):
|
||||||
|
username = getattr(request.ctx, "username", None)
|
||||||
|
extra = username if username else None
|
||||||
|
start = time.perf_counter()
|
||||||
|
ws_id = log_ws_open(request, extra=extra)
|
||||||
try:
|
try:
|
||||||
await auth.verify(request)
|
await auth.verify(request)
|
||||||
await handler(request, ws, *args, **kwargs)
|
await handler(request, ws, *args, **kwargs)
|
||||||
@@ -67,5 +73,19 @@ def websocket_wrapper(handler):
|
|||||||
if not getattr(e, "quiet", False) or code == 500:
|
if not getattr(e, "quiet", False) or code == 500:
|
||||||
logger.exception(f"{code} {e!r}")
|
logger.exception(f"{code} {e!r}")
|
||||||
raise
|
raise
|
||||||
|
finally:
|
||||||
|
duration = time.perf_counter() - start
|
||||||
|
close_code = None
|
||||||
|
try:
|
||||||
|
p = ws.ws_proto
|
||||||
|
if p.close_rcvd is not None:
|
||||||
|
close_code = p.close_rcvd.code
|
||||||
|
elif p.close_sent is not None:
|
||||||
|
close_code = p.close_sent.code
|
||||||
|
elif getattr(p, "close_code", None) is not None:
|
||||||
|
close_code = p.close_code
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
log_ws_close(ws_id, close_code, duration)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|||||||
+453
-85
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import queue
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
@@ -16,6 +17,33 @@ from cista import config
|
|||||||
from cista.fileio import fuid
|
from cista.fileio import fuid
|
||||||
from cista.protocol import FileEntry, Space, UpdDel, UpdIns, UpdKeep
|
from cista.protocol import FileEntry, Space, UpdDel, UpdIns, UpdKeep
|
||||||
|
|
||||||
|
# Platform-specific allocated size calculation
|
||||||
|
if sys.platform == "win32":
|
||||||
|
import ctypes
|
||||||
|
from ctypes import wintypes
|
||||||
|
|
||||||
|
kernel32 = ctypes.windll.kernel32
|
||||||
|
GetCompressedFileSizeW = kernel32.GetCompressedFileSizeW
|
||||||
|
GetCompressedFileSizeW.argtypes = [wintypes.LPCWSTR, ctypes.POINTER(wintypes.DWORD)]
|
||||||
|
GetCompressedFileSizeW.restype = wintypes.DWORD
|
||||||
|
INVALID_FILE_SIZE = 0xFFFFFFFF
|
||||||
|
|
||||||
|
def get_allocated_size(path: Path, st: stat_result) -> int:
|
||||||
|
"""Get actual disk allocation on Windows using GetCompressedFileSizeW."""
|
||||||
|
high = wintypes.DWORD()
|
||||||
|
low = GetCompressedFileSizeW(str(path), ctypes.byref(high))
|
||||||
|
if low == INVALID_FILE_SIZE and ctypes.get_last_error() != 0:
|
||||||
|
raise OSError(f"GetCompressedFileSizeW failed for {path}")
|
||||||
|
return (high.value << 32) + low
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
def get_allocated_size(path: Path, st: stat_result) -> int:
|
||||||
|
"""Get actual disk allocation on Unix using st_blocks."""
|
||||||
|
# st_blocks is in 512-byte units
|
||||||
|
return st.st_blocks * 512
|
||||||
|
|
||||||
|
|
||||||
pubsub = {}
|
pubsub = {}
|
||||||
sortkey = natsort_keygen(alg=ns.LOCALE)
|
sortkey = natsort_keygen(alg=ns.LOCALE)
|
||||||
|
|
||||||
@@ -23,7 +51,7 @@ sortkey = natsort_keygen(alg=ns.LOCALE)
|
|||||||
class State:
|
class State:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.lock = threading.RLock()
|
self.lock = threading.RLock()
|
||||||
self._space = Space(0, 0, 0, 0)
|
self._space = Space(0, 0, 0, 0, 0)
|
||||||
self.root: list[FileEntry] = []
|
self.root: list[FileEntry] = []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -116,6 +144,28 @@ state = State()
|
|||||||
rootpath: Path = None # type: ignore
|
rootpath: Path = None # type: ignore
|
||||||
quit = threading.Event()
|
quit = threading.Event()
|
||||||
|
|
||||||
|
# Thread-safe queue for signaling path updates from websockets
|
||||||
|
_update_queue: queue.Queue[PurePosixPath] = queue.Queue()
|
||||||
|
|
||||||
|
|
||||||
|
def notify_change(*paths: PurePosixPath | str):
|
||||||
|
"""Signal that paths have changed. Called from control/upload websockets."""
|
||||||
|
for path in paths:
|
||||||
|
if isinstance(path, str):
|
||||||
|
path = PurePosixPath(path)
|
||||||
|
# Convert absolute paths to relative (strip leading /)
|
||||||
|
if path.is_absolute():
|
||||||
|
path = (
|
||||||
|
PurePosixPath(*path.parts[1:])
|
||||||
|
if len(path.parts) > 1
|
||||||
|
else PurePosixPath()
|
||||||
|
)
|
||||||
|
# Skip root paths (empty, '.') to avoid full tree walks
|
||||||
|
if not path.parts or path.parts == (".",):
|
||||||
|
continue
|
||||||
|
_update_queue.put(path)
|
||||||
|
|
||||||
|
|
||||||
## Filesystem scanning
|
## Filesystem scanning
|
||||||
|
|
||||||
|
|
||||||
@@ -125,12 +175,18 @@ def walk(rel: PurePosixPath, stat: stat_result | None = None) -> list[FileEntry]
|
|||||||
try:
|
try:
|
||||||
st = stat or path.stat()
|
st = stat or path.stat()
|
||||||
isfile = int(not S_ISDIR(st.st_mode))
|
isfile = int(not S_ISDIR(st.st_mode))
|
||||||
|
try:
|
||||||
|
allocated = get_allocated_size(path, st) if isfile else 0
|
||||||
|
except Exception:
|
||||||
|
logger.exception(f"get_allocated_size failed for {path}")
|
||||||
|
allocated = st.st_size if isfile else 0
|
||||||
entry = FileEntry(
|
entry = FileEntry(
|
||||||
level=len(rel.parts),
|
level=len(rel.parts),
|
||||||
name=rel.name,
|
name=rel.name,
|
||||||
key=fuid(st),
|
key=fuid(st),
|
||||||
mtime=int(st.st_mtime),
|
mtime=int(st.st_mtime),
|
||||||
size=st.st_size if isfile else 0,
|
size=st.st_size if isfile else 0,
|
||||||
|
allocated=allocated,
|
||||||
isfile=isfile,
|
isfile=isfile,
|
||||||
)
|
)
|
||||||
if isfile:
|
if isfile:
|
||||||
@@ -158,8 +214,9 @@ def walk(rel: PurePosixPath, stat: stat_result | None = None) -> list[FileEntry]
|
|||||||
level=entry.level,
|
level=entry.level,
|
||||||
name=entry.name,
|
name=entry.name,
|
||||||
key=entry.key,
|
key=entry.key,
|
||||||
size=entry.size + child.size,
|
|
||||||
mtime=max(entry.mtime, child.mtime),
|
mtime=max(entry.mtime, child.mtime),
|
||||||
|
size=entry.size + child.size,
|
||||||
|
allocated=entry.allocated + child.allocated,
|
||||||
isfile=entry.isfile,
|
isfile=entry.isfile,
|
||||||
)
|
)
|
||||||
ret.extend(sub)
|
ret.extend(sub)
|
||||||
@@ -204,7 +261,14 @@ def update_path(rootmod: list[FileEntry], relpath: PurePosixPath, loop):
|
|||||||
def update_space(loop):
|
def update_space(loop):
|
||||||
"""Called periodically to update the disk usage."""
|
"""Called periodically to update the disk usage."""
|
||||||
du = shutil.disk_usage(rootpath)
|
du = shutil.disk_usage(rootpath)
|
||||||
space = Space(*du, storage=state.root[0].size)
|
root = state.root[0]
|
||||||
|
space = Space(
|
||||||
|
disk=du.total,
|
||||||
|
free=du.free,
|
||||||
|
used=du.used,
|
||||||
|
storage=root.size,
|
||||||
|
allocated=root.allocated,
|
||||||
|
)
|
||||||
# Update only on difference above 1 MB
|
# Update only on difference above 1 MB
|
||||||
tol = 10**6
|
tol = 10**6
|
||||||
old = msgspec.structs.astuple(state.space)
|
old = msgspec.structs.astuple(state.space)
|
||||||
@@ -326,127 +390,431 @@ def format_root(root):
|
|||||||
|
|
||||||
|
|
||||||
def broadcast(msg, loop):
|
def broadcast(msg, loop):
|
||||||
return asyncio.run_coroutine_threadsafe(abroadcast(msg), loop).result()
|
fut = asyncio.run_coroutine_threadsafe(abroadcast(msg), loop)
|
||||||
|
return fut.result()
|
||||||
|
|
||||||
|
|
||||||
async def abroadcast(msg):
|
async def abroadcast(msg):
|
||||||
|
client_count = 0
|
||||||
try:
|
try:
|
||||||
for queue in pubsub.values():
|
for queue in pubsub.values():
|
||||||
queue.put_nowait(msg)
|
queue.put_nowait(msg)
|
||||||
|
client_count += 1
|
||||||
except Exception:
|
except Exception:
|
||||||
# Log because asyncio would silently eat the error
|
# Log because asyncio would silently eat the error
|
||||||
logger.exception("Broadcast error")
|
logger.exception("Broadcast error")
|
||||||
|
return client_count
|
||||||
|
|
||||||
|
|
||||||
## Watcher thread
|
## Watcher thread
|
||||||
|
|
||||||
|
|
||||||
def watcher_inotify(loop):
|
class PathIndex:
|
||||||
"""Inotify watcher thread (Linux only)"""
|
"""O(1) path lookup index for the flat FileEntry tree."""
|
||||||
import inotify.adapters
|
|
||||||
|
def __init__(self, root: list[FileEntry]):
|
||||||
|
self.root = root
|
||||||
|
self._index: dict[PurePosixPath, tuple[int, int]] = {}
|
||||||
|
self._rebuild()
|
||||||
|
|
||||||
|
def _rebuild(self):
|
||||||
|
"""Build path -> (start_idx, count) mapping in single O(n) pass."""
|
||||||
|
index: dict[PurePosixPath, tuple[int, int]] = {}
|
||||||
|
path_stack: list[tuple[PurePosixPath, int]] = [] # (path, start_idx)
|
||||||
|
|
||||||
|
for i, entry in enumerate(self.root):
|
||||||
|
# Pop completed paths from stack
|
||||||
|
while path_stack and entry.level <= len(path_stack[-1][0].parts):
|
||||||
|
completed_path, start_idx = path_stack.pop()
|
||||||
|
index[completed_path] = (start_idx, i - start_idx)
|
||||||
|
|
||||||
|
# Build current path
|
||||||
|
if entry.level == 0:
|
||||||
|
current_path = PurePosixPath()
|
||||||
|
else:
|
||||||
|
parent = path_stack[-1][0] if path_stack else PurePosixPath()
|
||||||
|
current_path = parent / entry.name
|
||||||
|
|
||||||
|
path_stack.append((current_path, i))
|
||||||
|
|
||||||
|
# Close remaining paths
|
||||||
|
for path, start_idx in path_stack:
|
||||||
|
index[path] = (start_idx, len(self.root) - start_idx)
|
||||||
|
|
||||||
|
self._index = index
|
||||||
|
|
||||||
|
def get(self, path: PurePosixPath) -> tuple[int | None, list[FileEntry]]:
|
||||||
|
"""O(1) lookup: returns (start_idx, entries) or (None, [])."""
|
||||||
|
if path not in self._index:
|
||||||
|
return None, []
|
||||||
|
start, count = self._index[path]
|
||||||
|
return start, self.root[start : start + count]
|
||||||
|
|
||||||
|
def find_insert_pos(self, path: PurePosixPath, isfile: int) -> int:
|
||||||
|
"""Find insertion position using index + binary search."""
|
||||||
|
if not path.parts:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
parent = path.parent
|
||||||
|
name = path.name
|
||||||
|
|
||||||
|
# Find parent's range
|
||||||
|
if parent == PurePosixPath():
|
||||||
|
# Insert at root level - scan root's direct children
|
||||||
|
start, count = 0, len(self.root)
|
||||||
|
target_level = 1
|
||||||
|
elif parent in self._index:
|
||||||
|
start, count = self._index[parent]
|
||||||
|
start += 1 # Skip parent entry itself
|
||||||
|
count -= 1
|
||||||
|
target_level = len(parent.parts) + 1
|
||||||
|
else:
|
||||||
|
# Parent doesn't exist, shouldn't happen
|
||||||
|
return len(self.root)
|
||||||
|
|
||||||
|
# Binary search among direct children at target_level
|
||||||
|
# Collect children indices first
|
||||||
|
children = []
|
||||||
|
i = start
|
||||||
|
end = start + count
|
||||||
|
while i < end:
|
||||||
|
entry = self.root[i]
|
||||||
|
if entry.level == target_level:
|
||||||
|
children.append(i)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
if not children:
|
||||||
|
return start
|
||||||
|
|
||||||
|
# Binary search for insertion point
|
||||||
|
nsort = sortkey(name)
|
||||||
|
lo, hi = 0, len(children)
|
||||||
|
while lo < hi:
|
||||||
|
mid = (lo + hi) // 2
|
||||||
|
idx = children[mid]
|
||||||
|
entry = self.root[idx]
|
||||||
|
ename = entry.name
|
||||||
|
esort = sortkey(ename)
|
||||||
|
# Compare: isfile, then sort key, then case-sensitive
|
||||||
|
cmp = (
|
||||||
|
entry.isfile - isfile
|
||||||
|
or (esort > nsort) - (esort < nsort)
|
||||||
|
or (ename > name) - (ename < name)
|
||||||
|
)
|
||||||
|
if cmp < 0:
|
||||||
|
lo = mid + 1
|
||||||
|
else:
|
||||||
|
hi = mid
|
||||||
|
|
||||||
|
if lo < len(children):
|
||||||
|
return children[lo]
|
||||||
|
elif children:
|
||||||
|
# Insert after last child's subtree
|
||||||
|
last_idx = children[-1]
|
||||||
|
last_entry = self.root[last_idx]
|
||||||
|
if last_entry.isfile:
|
||||||
|
return last_idx + 1
|
||||||
|
# Find end of last child's subtree
|
||||||
|
last_path = parent / last_entry.name
|
||||||
|
if last_path in self._index:
|
||||||
|
s, c = self._index[last_path]
|
||||||
|
return s + c
|
||||||
|
return last_idx + 1
|
||||||
|
return start
|
||||||
|
|
||||||
|
def apply_update(
|
||||||
|
self, path: PurePosixPath, new_entries: list[FileEntry]
|
||||||
|
) -> list[FileEntry]:
|
||||||
|
"""Apply an update and return the new root. Rebuilds index."""
|
||||||
|
start, old_entries = self.get(path)
|
||||||
|
|
||||||
|
if old_entries == new_entries:
|
||||||
|
return self.root
|
||||||
|
|
||||||
|
new_root = self.root[:]
|
||||||
|
|
||||||
|
if start is not None:
|
||||||
|
del new_root[start : start + len(old_entries)]
|
||||||
|
|
||||||
|
if new_entries:
|
||||||
|
# Rebuild index on modified list to find insert pos
|
||||||
|
self.root = new_root
|
||||||
|
self._rebuild()
|
||||||
|
insert_pos = self.find_insert_pos(path, new_entries[0].isfile)
|
||||||
|
new_root[insert_pos:insert_pos] = new_entries
|
||||||
|
|
||||||
|
self.root = new_root
|
||||||
|
self._rebuild()
|
||||||
|
|
||||||
|
# Recalculate sizes for ancestor folders (including root)
|
||||||
|
self._recalculate_ancestors(path)
|
||||||
|
|
||||||
|
return new_root
|
||||||
|
|
||||||
|
def _recalculate_ancestors(self, path: PurePosixPath):
|
||||||
|
"""Recalculate size/allocated for all ancestors of path, including root."""
|
||||||
|
# Build list of ancestors from deepest to root
|
||||||
|
ancestors = []
|
||||||
|
current = path.parent if path.parts else PurePosixPath()
|
||||||
|
while True:
|
||||||
|
ancestors.append(current)
|
||||||
|
if not current.parts:
|
||||||
|
break
|
||||||
|
current = current.parent
|
||||||
|
|
||||||
|
# Process from deepest ancestor to root
|
||||||
|
for ancestor_path in ancestors:
|
||||||
|
if ancestor_path not in self._index:
|
||||||
|
continue
|
||||||
|
start, count = self._index[ancestor_path]
|
||||||
|
if count == 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
ancestor = self.root[start]
|
||||||
|
if ancestor.isfile:
|
||||||
|
continue # Files don't aggregate
|
||||||
|
|
||||||
|
# Sum size/allocated of direct children
|
||||||
|
total_size = 0
|
||||||
|
total_allocated = 0
|
||||||
|
i = start + 1
|
||||||
|
while i < start + count:
|
||||||
|
child = self.root[i]
|
||||||
|
if child.level == ancestor.level + 1:
|
||||||
|
total_size += child.size
|
||||||
|
total_allocated += child.allocated
|
||||||
|
# Skip child's subtree
|
||||||
|
child_path = ancestor_path / child.name
|
||||||
|
if child_path in self._index:
|
||||||
|
_, child_count = self._index[child_path]
|
||||||
|
i += child_count
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Update ancestor entry if changed
|
||||||
|
if ancestor.size != total_size or ancestor.allocated != total_allocated:
|
||||||
|
self.root[start] = FileEntry(
|
||||||
|
level=ancestor.level,
|
||||||
|
name=ancestor.name,
|
||||||
|
key=ancestor.key,
|
||||||
|
mtime=ancestor.mtime,
|
||||||
|
size=total_size,
|
||||||
|
allocated=total_allocated,
|
||||||
|
isfile=ancestor.isfile,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def collapse_paths(paths: set[PurePosixPath]) -> set[PurePosixPath]:
|
||||||
|
"""Remove child paths if parent is in set."""
|
||||||
|
if not paths:
|
||||||
|
return paths
|
||||||
|
# Filter out root paths (empty or '.') which would cause full tree walks
|
||||||
|
paths = {p for p in paths if p.parts and p.parts != (".",)}
|
||||||
|
if not paths:
|
||||||
|
return set()
|
||||||
|
# Sort by depth (fewest parts first)
|
||||||
|
sorted_paths = sorted(paths, key=lambda p: len(p.parts))
|
||||||
|
result = set()
|
||||||
|
for path in sorted_paths:
|
||||||
|
# Check if any ancestor is already in result
|
||||||
|
is_child = False
|
||||||
|
for i in range(len(path.parts)):
|
||||||
|
ancestor = PurePosixPath(*path.parts[:i]) if i > 0 else PurePosixPath()
|
||||||
|
if ancestor in result:
|
||||||
|
is_child = True
|
||||||
|
break
|
||||||
|
if not is_child:
|
||||||
|
result.add(path)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# Debounce settings
|
||||||
|
DEBOUNCE_DELAY = 0.01 # Wait 10ms after last event
|
||||||
|
DEBOUNCE_MAX = 0.1 # But no more than 100ms total
|
||||||
|
|
||||||
|
|
||||||
|
def watcher(loop):
|
||||||
|
"""Unified watcher thread handling inotify, websocket signals, and periodic scans."""
|
||||||
|
use_inotify = sys.platform == "linux"
|
||||||
|
inotify_tree = None
|
||||||
|
modified_flags = frozenset()
|
||||||
|
|
||||||
|
if use_inotify:
|
||||||
|
import inotify.adapters
|
||||||
|
|
||||||
|
modified_flags = frozenset(
|
||||||
|
(
|
||||||
|
"IN_CREATE",
|
||||||
|
"IN_DELETE",
|
||||||
|
"IN_DELETE_SELF",
|
||||||
|
"IN_MODIFY",
|
||||||
|
"IN_MOVE_SELF",
|
||||||
|
"IN_MOVED_FROM",
|
||||||
|
"IN_MOVED_TO",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
modified_flags = (
|
|
||||||
"IN_CREATE",
|
|
||||||
"IN_DELETE",
|
|
||||||
"IN_DELETE_SELF",
|
|
||||||
"IN_MODIFY",
|
|
||||||
"IN_MOVE_SELF",
|
|
||||||
"IN_MOVED_FROM",
|
|
||||||
"IN_MOVED_TO",
|
|
||||||
)
|
|
||||||
while not quit.is_set():
|
while not quit.is_set():
|
||||||
i = inotify.adapters.InotifyTree(rootpath.as_posix())
|
if use_inotify:
|
||||||
|
import inotify.adapters
|
||||||
|
|
||||||
|
inotify_tree = inotify.adapters.InotifyTree(rootpath.as_posix())
|
||||||
|
|
||||||
# Initialize the tree from filesystem
|
# Initialize the tree from filesystem
|
||||||
update_root(loop)
|
update_root(loop)
|
||||||
|
path_index = PathIndex(state.root[:])
|
||||||
|
|
||||||
trefresh = time.monotonic() + 300.0
|
trefresh = time.monotonic() + 300.0
|
||||||
tspace = time.monotonic() + 5.0
|
tspace = time.monotonic() + 5.0
|
||||||
# Watch for changes (frequent wakeups needed for quiting)
|
|
||||||
while not quit.is_set():
|
# Pending changes: path -> {"ws": count, "inotify": count}
|
||||||
t = time.monotonic()
|
dirty_paths: dict[PurePosixPath, dict[str, int]] = {}
|
||||||
# The watching is not entirely reliable, so do a full refresh every 30 seconds
|
first_event_time: float | None = None
|
||||||
if t >= trefresh:
|
last_event_time: float | None = None
|
||||||
break
|
|
||||||
# Disk usage update
|
def add_dirty(path: PurePosixPath, source: str) -> bool:
|
||||||
if t >= tspace:
|
"""Add path to dirty set. Returns True if added, False if redundant."""
|
||||||
tspace = time.monotonic() + 5.0
|
nonlocal first_event_time, last_event_time
|
||||||
update_space(loop)
|
# Check if already covered by an existing dirty path
|
||||||
# Inotify events, update the tree
|
for existing in dirty_paths:
|
||||||
dirty = False
|
if path == existing or (
|
||||||
rootmod = state.root[:]
|
len(path.parts) > len(existing.parts)
|
||||||
for event in i.event_gen(yield_nones=False, timeout_s=0.1):
|
and path.parts[: len(existing.parts)] == existing.parts
|
||||||
assert event
|
):
|
||||||
if quit.is_set():
|
# Count the event even if skipped
|
||||||
return
|
dirty_paths[existing][source] = (
|
||||||
interesting = any(f in modified_flags for f in event[1])
|
dirty_paths[existing].get(source, 0) + 1
|
||||||
if interesting:
|
|
||||||
# Update modified path
|
|
||||||
path = PurePosixPath(event[2]) / event[3]
|
|
||||||
try:
|
|
||||||
rel_path = path.relative_to(rootpath)
|
|
||||||
update_path(rootmod, rel_path, loop)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(
|
|
||||||
f"Error processing inotify event for path {path}: {e}"
|
|
||||||
)
|
|
||||||
raise
|
|
||||||
if not dirty:
|
|
||||||
t = time.monotonic()
|
|
||||||
dirty = True
|
|
||||||
# Wait a maximum of 0.2s to push the updates
|
|
||||||
if dirty and time.monotonic() >= t + 0.2:
|
|
||||||
break
|
|
||||||
if dirty and state.root != rootmod:
|
|
||||||
try:
|
|
||||||
update = format_update(state.root, rootmod)
|
|
||||||
with state.lock:
|
|
||||||
broadcast(update, loop)
|
|
||||||
state.root = rootmod
|
|
||||||
except Exception:
|
|
||||||
logger.exception(
|
|
||||||
"format_update failed; falling back to full rescan"
|
|
||||||
)
|
)
|
||||||
# Fallback: full rescan and try diff again; last resort send full root
|
return False
|
||||||
|
# Remove any paths that would be covered by this new one
|
||||||
|
covered = {
|
||||||
|
p
|
||||||
|
for p in dirty_paths
|
||||||
|
if len(p.parts) > len(path.parts)
|
||||||
|
and p.parts[: len(path.parts)] == path.parts
|
||||||
|
}
|
||||||
|
# Aggregate counts from covered paths
|
||||||
|
counts: dict[str, int] = {source: 1}
|
||||||
|
for p in covered:
|
||||||
|
for s, c in dirty_paths[p].items():
|
||||||
|
counts[s] = counts.get(s, 0) + c
|
||||||
|
del dirty_paths[p]
|
||||||
|
dirty_paths[path] = counts
|
||||||
|
now = time.monotonic()
|
||||||
|
if first_event_time is None:
|
||||||
|
first_event_time = now
|
||||||
|
last_event_time = now
|
||||||
|
return True
|
||||||
|
|
||||||
|
while not quit.is_set():
|
||||||
|
now = time.monotonic()
|
||||||
|
|
||||||
|
# Full refresh every 300s
|
||||||
|
if now >= trefresh:
|
||||||
|
break
|
||||||
|
|
||||||
|
# Disk usage update every 5s
|
||||||
|
if now >= tspace:
|
||||||
|
tspace = now + 5.0
|
||||||
|
update_space(loop)
|
||||||
|
|
||||||
|
# Check if we should flush pending changes
|
||||||
|
should_flush = False
|
||||||
|
if dirty_paths:
|
||||||
|
time_since_last = now - last_event_time if last_event_time else 0
|
||||||
|
time_since_first = now - first_event_time if first_event_time else 0
|
||||||
|
if (
|
||||||
|
time_since_last >= DEBOUNCE_DELAY
|
||||||
|
or time_since_first >= DEBOUNCE_MAX
|
||||||
|
):
|
||||||
|
should_flush = True
|
||||||
|
|
||||||
|
if should_flush:
|
||||||
|
paths_to_process = dirty_paths.copy()
|
||||||
|
dirty_paths.clear()
|
||||||
|
first_event_time = None
|
||||||
|
last_event_time = None
|
||||||
|
|
||||||
|
# Collapse paths (remove children if parent present)
|
||||||
|
collapsed = collapse_paths(set(paths_to_process.keys()))
|
||||||
|
|
||||||
|
# Process each collapsed path
|
||||||
|
new_root = path_index.root
|
||||||
|
for path in collapsed:
|
||||||
|
new_entries = walk(path)
|
||||||
|
new_root = path_index.apply_update(path, new_entries)
|
||||||
|
|
||||||
|
# Broadcast if changed
|
||||||
|
if new_root != state.root:
|
||||||
try:
|
try:
|
||||||
fresh = walk(PurePosixPath())
|
update_msg = format_update(state.root, new_root)
|
||||||
|
with state.lock:
|
||||||
|
broadcast(update_msg, loop)
|
||||||
|
state.root = new_root
|
||||||
|
except Exception:
|
||||||
|
logger.exception("format_update failed; full rescan")
|
||||||
try:
|
try:
|
||||||
update = format_update(state.root, fresh)
|
fresh = walk(PurePosixPath())
|
||||||
|
path_index = PathIndex(fresh)
|
||||||
|
update_msg = format_update(state.root, fresh)
|
||||||
with state.lock:
|
with state.lock:
|
||||||
broadcast(update, loop)
|
broadcast(update_msg, loop)
|
||||||
state.root = fresh
|
state.root = fresh
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception(
|
logger.exception("Fallback failed; sending full root")
|
||||||
"Fallback diff failed; sending full root snapshot"
|
|
||||||
)
|
|
||||||
with state.lock:
|
with state.lock:
|
||||||
broadcast(format_root(fresh), loop)
|
broadcast(format_root(fresh), loop)
|
||||||
state.root = fresh
|
state.root = fresh
|
||||||
except Exception:
|
|
||||||
logger.exception(
|
|
||||||
"Full rescan failed; dropping this batch of updates"
|
|
||||||
)
|
|
||||||
|
|
||||||
del i # Free the inotify object
|
# Collect events from websocket signals (non-blocking)
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
path = _update_queue.get_nowait()
|
||||||
|
add_dirty(path, "ws")
|
||||||
|
except queue.Empty:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Collect inotify events if available (short timeout for responsiveness)
|
||||||
|
if inotify_tree:
|
||||||
|
for event in inotify_tree.event_gen(yield_nones=False, timeout_s=0.05):
|
||||||
|
if quit.is_set():
|
||||||
|
return
|
||||||
|
if not (modified_flags & set(event[1])):
|
||||||
|
continue
|
||||||
|
|
||||||
def watcher_poll(loop):
|
# Extract relative path
|
||||||
"""Polling version of the watcher thread."""
|
path = PurePosixPath(event[2]) / event[3]
|
||||||
while not quit.is_set():
|
try:
|
||||||
t0 = time.perf_counter()
|
rel_path = path.relative_to(rootpath)
|
||||||
update_root(loop)
|
except ValueError:
|
||||||
update_space(loop)
|
continue
|
||||||
dur = time.perf_counter() - t0
|
|
||||||
if dur > 1.0:
|
# Skip dotfiles
|
||||||
logger.debug(f"Reading the full file list took {dur:.1f}s")
|
if any(part.startswith(".") for part in rel_path.parts):
|
||||||
quit.wait(0.1 + 8 * dur)
|
continue
|
||||||
|
|
||||||
|
add_dirty(rel_path, "inotify")
|
||||||
|
|
||||||
|
# Don't block too long collecting events
|
||||||
|
now = time.monotonic()
|
||||||
|
if first_event_time and now - first_event_time >= DEBOUNCE_MAX:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# No inotify, just sleep briefly for responsiveness
|
||||||
|
time.sleep(0.05)
|
||||||
|
|
||||||
|
if inotify_tree:
|
||||||
|
del inotify_tree
|
||||||
|
|
||||||
|
|
||||||
def start(app):
|
def start(app):
|
||||||
global rootpath
|
global rootpath
|
||||||
config.load_config()
|
config.load_config()
|
||||||
rootpath = config.config.path
|
rootpath = config.config.path
|
||||||
use_inotify = sys.platform == "linux"
|
|
||||||
app.ctx.watcher = threading.Thread(
|
app.ctx.watcher = threading.Thread(
|
||||||
target=watcher_inotify if use_inotify else watcher_poll,
|
target=watcher,
|
||||||
args=[app.loop],
|
args=[app.loop],
|
||||||
# Descriptive name for system monitoring
|
# Descriptive name for system monitoring
|
||||||
name=f"cista-watcher {rootpath}",
|
name=f"cista-watcher {rootpath}",
|
||||||
|
|||||||
+82
-18
@@ -9,15 +9,14 @@
|
|||||||
<UserManagementModal />
|
<UserManagementModal />
|
||||||
<AccessDeniedModal />
|
<AccessDeniedModal />
|
||||||
<header>
|
<header>
|
||||||
<HeaderMain ref="headerMain" :path="path.pathList" :query="path.query">
|
<HeaderMain ref="headerMain" :path="path.pathList" :query="path.query" />
|
||||||
<HeaderSelected :path="path.pathList" />
|
|
||||||
</HeaderMain>
|
|
||||||
<BreadCrumb :path="path.pathList" primary />
|
<BreadCrumb :path="path.pathList" primary />
|
||||||
</header>
|
</header>
|
||||||
<main>
|
<main>
|
||||||
<RouterView :path="path.pathList" :query="path.query" />
|
<RouterView :path="path.pathList" :query="path.query" />
|
||||||
</main>
|
</main>
|
||||||
<footer>
|
<footer v-if="store.selected.size || store.uprogress.total || store.dprogress.total">
|
||||||
|
<SelectionToolbar :path="path.pathList" />
|
||||||
<TransferBar :status=store.uprogress @cancel=store.cancelUploads class=upload />
|
<TransferBar :status=store.uprogress @cancel=store.cancelUploads class=upload />
|
||||||
<TransferBar :status=store.dprogress @cancel=store.cancelDownloads class=download />
|
<TransferBar :status=store.dprogress @cancel=store.cancelDownloads class=download />
|
||||||
</footer>
|
</footer>
|
||||||
@@ -37,6 +36,7 @@ import type { SortOrder } from './utils/docsort'
|
|||||||
import type SettingsModalVue from './components/SettingsModal.vue'
|
import type SettingsModalVue from './components/SettingsModal.vue'
|
||||||
import UserManagementModal from './components/UserManagementModal.vue'
|
import UserManagementModal from './components/UserManagementModal.vue'
|
||||||
import AccessDeniedModal from './components/AccessDeniedModal.vue'
|
import AccessDeniedModal from './components/AccessDeniedModal.vue'
|
||||||
|
import SelectionToolbar from './components/SelectionToolbar.vue'
|
||||||
|
|
||||||
interface Path {
|
interface Path {
|
||||||
path: string
|
path: string
|
||||||
@@ -63,6 +63,7 @@ onUnmounted(watchDisconnect)
|
|||||||
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) => {
|
||||||
if (store.dialog) {
|
if (store.dialog) {
|
||||||
if (timer) {
|
if (timer) {
|
||||||
@@ -76,6 +77,13 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
|
|||||||
const c = fileExplorer.isCursor()
|
const c = fileExplorer.isCursor()
|
||||||
const input = (event.target as HTMLElement).tagName === 'INPUT'
|
const input = (event.target as HTMLElement).tagName === 'INPUT'
|
||||||
const keyup = event.type === 'keyup'
|
const keyup = event.type === 'keyup'
|
||||||
|
|
||||||
|
// Always clear repeat timer on arrow keyup, even if focus moved to input
|
||||||
|
if (keyup && event.key.startsWith('Arrow') && timer) {
|
||||||
|
clearTimeout(timer)
|
||||||
|
timer = null
|
||||||
|
}
|
||||||
|
|
||||||
if (event.repeat) {
|
if (event.repeat) {
|
||||||
if (
|
if (
|
||||||
event.key === 'ArrowUp' ||
|
event.key === 'ArrowUp' ||
|
||||||
@@ -91,20 +99,39 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
|
|||||||
//console.log("key pressed", event)
|
//console.log("key pressed", event)
|
||||||
/// Long if-else machina for all keys we handle here
|
/// Long if-else machina for all keys we handle here
|
||||||
let arrow = ''
|
let arrow = ''
|
||||||
if (!input && event.key.startsWith("Arrow")) arrow = event.key.slice(5).toLowerCase()
|
const inHeader = !!(event.target as HTMLElement).closest('.headermain')
|
||||||
|
const inBreadcrumb = !!(event.target as HTMLElement).closest('.breadcrumb')
|
||||||
|
// Handle arrows: in search input with text, only up/down; otherwise all arrows
|
||||||
|
const searchInput = inHeader && input
|
||||||
|
const searchHasText = searchInput && (event.target as HTMLInputElement).value
|
||||||
|
if (event.key.startsWith("Arrow")) {
|
||||||
|
const dir = event.key.slice(5).toLowerCase()
|
||||||
|
// In search with text: left/right move cursor, up/down navigate
|
||||||
|
if (searchHasText && (dir === 'left' || dir === 'right')) {
|
||||||
|
return // Let browser handle cursor movement
|
||||||
|
}
|
||||||
|
arrow = dir
|
||||||
|
}
|
||||||
|
if (arrow) {
|
||||||
|
// Arrow key handling - fall through to bottom
|
||||||
|
}
|
||||||
// Find: process on keydown so that we can bypass the built-in search hotkey
|
// Find: process on keydown so that we can bypass the built-in search hotkey
|
||||||
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)
|
// Search also on / (UNIX style) - use code to support any keyboard layout
|
||||||
else if (!input && keyup && event.key === '/') {
|
else if (!input && keyup && event.code === 'Slash') {
|
||||||
|
// Record the actual character for display (varies by keyboard layout)
|
||||||
|
if (event.key.length === 1 && event.key !== store.prefs.searchHotkey) {
|
||||||
|
store.prefs.searchHotkey = event.key
|
||||||
|
}
|
||||||
headerMain.value!.toggleSearchInput()
|
headerMain.value!.toggleSearchInput()
|
||||||
}
|
}
|
||||||
// Globally close search, clear errors on Escape
|
// Globally close search, clear errors on Escape
|
||||||
else if (keyup && event.key === 'Escape') {
|
else if (keyup && event.key === 'Escape') {
|
||||||
store.error = ''
|
store.error = ''
|
||||||
store.clearToast()
|
store.clearToast()
|
||||||
headerMain.value!.closeSearch(event)
|
headerMain.value!.clearSearch(event)
|
||||||
store.focusBreadcrumb()
|
store.focusBreadcrumb()
|
||||||
}
|
}
|
||||||
else if (!input && keyup && event.key === 'Backspace') {
|
else if (!input && keyup && event.key === 'Backspace') {
|
||||||
@@ -143,13 +170,34 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
|
|||||||
timer = null
|
timer = null
|
||||||
}
|
}
|
||||||
let f: any
|
let f: any
|
||||||
switch (arrow) {
|
// Arrow navigation - always use fileExplorer for repeatable movement
|
||||||
case 'up': f = () => fileExplorer.up(event); break
|
if (arrow && !keyup) {
|
||||||
case 'down': f = () => fileExplorer.down(event); break
|
const focusSearch = () => (document.querySelector('.headermain input[type="search"]') as HTMLElement)?.focus()
|
||||||
case 'left': f = () => fileExplorer.left(event); break
|
const focusBreadcrumb = () => (document.querySelector('.breadcrumb') as HTMLElement)?.focus()
|
||||||
case 'right': f = () => fileExplorer.right(event); break
|
|
||||||
|
if (inBreadcrumb) {
|
||||||
|
// Breadcrumb: up→header (no repeat), down→files (with repeat)
|
||||||
|
if (arrow === 'up') { focusSearch(); f = null }
|
||||||
|
else if (arrow === 'down') { fileExplorer.focusFirst?.(); f = null }
|
||||||
|
} else if (inHeader) {
|
||||||
|
// Header: left/right navigate focusable items (buttons without tabindex=-1, search input, disk space)
|
||||||
|
const items = Array.from(document.querySelectorAll('.headermain button:not([tabindex=\"-1\"]), .headermain input[type=\"search\"], .headermain [tabindex=\"0\"]')) as HTMLElement[]
|
||||||
|
const idx = items.indexOf(document.activeElement as HTMLElement)
|
||||||
|
if (arrow === 'left' && idx > 0) { items[idx - 1]?.focus(); f = null }
|
||||||
|
else if (arrow === 'right' && idx < items.length - 1) { items[idx + 1]?.focus(); f = null }
|
||||||
|
else if (arrow === 'up') f = () => fileExplorer.up({ shiftKey: false })
|
||||||
|
else if (arrow === 'down') { focusBreadcrumb(); f = null }
|
||||||
|
} else {
|
||||||
|
// File explorer: normal navigation with repeat
|
||||||
|
switch (arrow) {
|
||||||
|
case 'up': f = () => fileExplorer.up(event); break
|
||||||
|
case 'down': f = () => fileExplorer.down(event); break
|
||||||
|
case 'left': f = () => fileExplorer.left(event); break
|
||||||
|
case 'right': f = () => fileExplorer.right(event); break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (f && !keyup) {
|
if (f) {
|
||||||
// Initial move, then t0 delay until repeats at tr intervals
|
// Initial move, then t0 delay until repeats at tr intervals
|
||||||
const t0 = 200, tr = event.altKey ? 20 : 100
|
const t0 = 200, tr = event.altKey ? 20 : 100
|
||||||
f()
|
f()
|
||||||
@@ -157,12 +205,13 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
window.addEventListener('keydown', globalShortcutHandler)
|
// Use capture phase to handle events before they reach target elements
|
||||||
window.addEventListener('keyup', globalShortcutHandler)
|
window.addEventListener('keydown', globalShortcutHandler, true)
|
||||||
|
window.addEventListener('keyup', globalShortcutHandler, true)
|
||||||
})
|
})
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
window.removeEventListener('keydown', globalShortcutHandler)
|
window.removeEventListener('keydown', globalShortcutHandler, true)
|
||||||
window.removeEventListener('keyup', globalShortcutHandler)
|
window.removeEventListener('keyup', globalShortcutHandler, true)
|
||||||
})
|
})
|
||||||
export type { Path }
|
export type { Path }
|
||||||
</script>
|
</script>
|
||||||
@@ -189,4 +238,19 @@ export type { Path }
|
|||||||
background: #555;
|
background: #555;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
}
|
}
|
||||||
|
footer {
|
||||||
|
position: fixed;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
background: rgba(0, 0, 0, 0.3);
|
||||||
|
backdrop-filter: blur(4px);
|
||||||
|
z-index: 50;
|
||||||
|
}
|
||||||
|
footer > * {
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -13,8 +13,8 @@
|
|||||||
--transition-time: 0.2s;
|
--transition-time: 0.2s;
|
||||||
/* The following are overridden by responsive layouts */
|
/* The following are overridden by responsive layouts */
|
||||||
--root-font-size: 1rem;
|
--root-font-size: 1rem;
|
||||||
--header-font-size: 1rem;
|
--header-font-size: clamp(0.7rem, 2.5vw, 1rem);
|
||||||
--header-height: 4rem;
|
--header-height: clamp(2.8rem, 10vw, 4rem);
|
||||||
}
|
}
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
:root {
|
:root {
|
||||||
@@ -36,6 +36,7 @@
|
|||||||
@media screen and (min-width: 1000px) {
|
@media screen and (min-width: 1000px) {
|
||||||
:root {
|
:root {
|
||||||
--root-font-size: calc(8px + 8 * 100vw / 1000);
|
--root-font-size: calc(8px + 8 * 100vw / 1000);
|
||||||
|
--header-font-size: 16px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@media screen and (min-width: 2000px) {
|
@media screen and (min-width: 2000px) {
|
||||||
@@ -43,44 +44,14 @@
|
|||||||
--root-font-size: 1.5rem;
|
--root-font-size: 1.5rem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Low (landscape) screens: smaller header */
|
|
||||||
@media screen and (max-height: 600px) {
|
|
||||||
:root {
|
|
||||||
--header-font-size: calc(10px + 10 * 100vh / 600); /* 20px at 600px height */
|
|
||||||
--root-font-size: 0.8rem;
|
|
||||||
--header-height: 2rem;
|
|
||||||
}
|
|
||||||
header .breadcrumb > * {
|
|
||||||
padding-top: calc(8 + 8 * 100vh / 600) !important;
|
|
||||||
padding-bottom: calc(8 + 8 * 100vh / 600) !important;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@media screen and (max-height: 300px) {
|
|
||||||
:root {
|
|
||||||
--header-font-size: 15px; /* Don't go smaller than this, no benefit */
|
|
||||||
--header-height: calc(1.75 * 16px);
|
|
||||||
--root-font-size: 0.6rem;
|
|
||||||
}
|
|
||||||
header .breadcrumb > * {
|
|
||||||
padding-top: 14px !important;
|
|
||||||
padding-bottom: 14px !important;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@media screen and (orientation: landscape) and (min-width: 700px) {
|
@media screen and (orientation: landscape) and (min-width: 700px) {
|
||||||
/* Breadcrumbs and buttons side by side */
|
/* Breadcrumbs and buttons side by side */
|
||||||
:root {
|
|
||||||
--header-font-size: calc(8px + 8 * 100vh / 600); /* 16px (1rem nominal) at 600px height */
|
|
||||||
}
|
|
||||||
header {
|
header {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
}
|
}
|
||||||
header .headermain { order: 1; }
|
header .headermain { order: 1; }
|
||||||
header .breadcrumb { align-self: stretch; }
|
header .breadcrumb { align-self: stretch; }
|
||||||
header .action-button {
|
|
||||||
width: 2em;
|
|
||||||
height: 2em;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@media print {
|
@media print {
|
||||||
:root {
|
:root {
|
||||||
@@ -241,42 +212,13 @@ header nav.headermain {
|
|||||||
.spacer { flex-grow: 1 }
|
.spacer { flex-grow: 1 }
|
||||||
.smallgap { flex-shrink: 1; width: 2em }
|
.smallgap { flex-shrink: 1; width: 2em }
|
||||||
|
|
||||||
[data-tooltip]:hover:after {
|
|
||||||
z-index: 101;
|
|
||||||
content: attr(data-tooltip);
|
|
||||||
position: absolute;
|
|
||||||
pointer-events: none;
|
|
||||||
font-size: 1rem;
|
|
||||||
text-align: center;
|
|
||||||
padding: .5rem 1rem;
|
|
||||||
border-radius: 3rem 0 3rem 0;
|
|
||||||
box-shadow: 0 0 1rem var(--accent-color);
|
|
||||||
transform: translate(calc(1rem + -50%), 150%);
|
|
||||||
background-color: var(--accent-color);
|
|
||||||
color: var(--primary-color);
|
|
||||||
white-space: pre;
|
|
||||||
animation: appearbriefly calc(10 * var(--transition-time)) linear forwards;
|
|
||||||
}
|
|
||||||
@keyframes appearbriefly {
|
|
||||||
from {
|
|
||||||
opacity: 0;
|
|
||||||
}
|
|
||||||
30% {
|
|
||||||
opacity: 0;
|
|
||||||
}
|
|
||||||
40% {
|
|
||||||
opacity: 1;
|
|
||||||
}
|
|
||||||
90% {
|
|
||||||
opacity: 1;
|
|
||||||
}
|
|
||||||
to {
|
|
||||||
opacity: 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.error-message {
|
.error-message {
|
||||||
padding: .5em;
|
padding: .5em;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
background: var(--accent-color);
|
background: var(--accent-color);
|
||||||
color: #000;
|
color: #000;
|
||||||
}
|
}
|
||||||
|
.ghost {
|
||||||
|
opacity: 0.5;
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,9 +14,12 @@
|
|||||||
:class="{ current: !!isCurrent(0) }"
|
:class="{ current: !!isCurrent(0) }"
|
||||||
:aria-current="isCurrent(0)"
|
:aria-current="isCurrent(0)"
|
||||||
@click.prevent="navigate(0)"
|
@click.prevent="navigate(0)"
|
||||||
title="/"
|
@mouseenter="homeTooltip?.startHover"
|
||||||
|
@mousemove="homeTooltip?.updatePosition"
|
||||||
|
@mouseleave="homeTooltip?.endHover"
|
||||||
>
|
>
|
||||||
<component :is="home" />
|
<component :is="home" />
|
||||||
|
<CursorTooltip ref="homeTooltip" text="/">/</CursorTooltip>
|
||||||
</a>
|
</a>
|
||||||
<template v-for="(location, index) in longest" :key="index">
|
<template v-for="(location, index) in longest" :key="index">
|
||||||
<a :href="`/#/${longest.slice(0, index + 1).join('/')}/`"
|
<a :href="`/#/${longest.slice(0, index + 1).join('/')}/`"
|
||||||
@@ -24,8 +27,10 @@
|
|||||||
:aria-current="isCurrent(index + 1)"
|
:aria-current="isCurrent(index + 1)"
|
||||||
@click.prevent="navigate(index + 1)"
|
@click.prevent="navigate(index + 1)"
|
||||||
:ref="el => setLinkRef(index + 1, el)"
|
:ref="el => setLinkRef(index + 1, el)"
|
||||||
:title="`/${longest.slice(0, index + 1).join('/')}`"
|
@mouseenter="pathTooltips.get(index)?.startHover"
|
||||||
>{{ location }}</a>
|
@mousemove="pathTooltips.get(index)?.updatePosition"
|
||||||
|
@mouseleave="pathTooltips.get(index)?.endHover"
|
||||||
|
>{{ location }}<CursorTooltip :ref="el => setPathTooltipRef(index, el)" :text="`/${longest.slice(0, index + 1).join('/')}`">{{ `/${longest.slice(0, index + 1).join('/')}` }}</CursorTooltip></a>
|
||||||
</template>
|
</template>
|
||||||
</nav>
|
</nav>
|
||||||
</template>
|
</template>
|
||||||
@@ -35,6 +40,7 @@ import { Home } from '@/assets/svg'
|
|||||||
import { nextTick, onBeforeUpdate, ref, watchEffect } from 'vue'
|
import { nextTick, onBeforeUpdate, ref, watchEffect } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { exists } from '@/utils/fileutil'
|
import { exists } from '@/utils/fileutil'
|
||||||
|
import CursorTooltip from './CursorTooltip.vue'
|
||||||
|
|
||||||
const home = Home
|
const home = Home
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
@@ -43,6 +49,13 @@ const links = [] as Array<HTMLElement>
|
|||||||
const setLinkRef = (index: number, el: any) => { if (el) links[index] = el }
|
const setLinkRef = (index: number, el: any) => { if (el) links[index] = el }
|
||||||
onBeforeUpdate(() => { links.length = 1 }) // 1 to keep home
|
onBeforeUpdate(() => { links.length = 1 }) // 1 to keep home
|
||||||
|
|
||||||
|
const homeTooltip = ref<InstanceType<typeof CursorTooltip> | null>(null)
|
||||||
|
const pathTooltips = ref<Map<number, InstanceType<typeof CursorTooltip>>>(new Map())
|
||||||
|
const setPathTooltipRef = (index: number, el: any) => {
|
||||||
|
if (el) pathTooltips.value.set(index, el)
|
||||||
|
else pathTooltips.value.delete(index)
|
||||||
|
}
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
path: Array<string>
|
path: Array<string>
|
||||||
primary?: boolean
|
primary?: boolean
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<Teleport to="body">
|
<Teleport to="body">
|
||||||
<div v-if="visible" class="cursor-tooltip" :style="tooltipStyle">
|
<div v-if="visible" ref="tooltipEl" class="cursor-tooltip" :style="tooltipStyle">
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
</div>
|
</div>
|
||||||
</Teleport>
|
</Teleport>
|
||||||
@@ -10,12 +10,12 @@
|
|||||||
// Global activation state - shared across all instances
|
// Global activation state - shared across all instances
|
||||||
let globalActive = false
|
let globalActive = false
|
||||||
let globalDeactivateTimer: ReturnType<typeof setTimeout> | null = null
|
let globalDeactivateTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
// Track if we've seen real mouse movement (not touch-simulated)
|
// Track recent touch to suppress touch-triggered mouse events
|
||||||
let hasRealMouse = false
|
let lastTouchTime = 0
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, ref } from 'vue'
|
import { computed, ref, onMounted, onUnmounted } from 'vue'
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
text: string
|
text: string
|
||||||
@@ -25,6 +25,9 @@ const props = defineProps<{
|
|||||||
const visible = ref(false)
|
const visible = ref(false)
|
||||||
const mouseX = ref(0)
|
const mouseX = ref(0)
|
||||||
const mouseY = ref(0)
|
const mouseY = ref(0)
|
||||||
|
const tooltipWidth = ref(0)
|
||||||
|
const tooltipHeight = ref(0)
|
||||||
|
const tooltipEl = ref<HTMLElement | null>(null)
|
||||||
let settleTimer: ReturnType<typeof setTimeout> | null = null
|
let settleTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
let lastMoveX = 0
|
let lastMoveX = 0
|
||||||
let lastMoveY = 0
|
let lastMoveY = 0
|
||||||
@@ -32,17 +35,51 @@ let lastMoveY = 0
|
|||||||
// Movement threshold (pixels) - cursor must settle within this radius
|
// Movement threshold (pixels) - cursor must settle within this radius
|
||||||
const SETTLE_THRESHOLD = 8
|
const SETTLE_THRESHOLD = 8
|
||||||
|
|
||||||
const tooltipStyle = computed(() => ({
|
const tooltipStyle = computed(() => {
|
||||||
left: `${mouseX.value}px`,
|
// Constrain to viewport
|
||||||
top: `${mouseY.value}px`,
|
const pad = 8
|
||||||
}))
|
let x = mouseX.value
|
||||||
|
let y = mouseY.value
|
||||||
|
|
||||||
// Check if the device likely has a real mouse (fine pointer)
|
// Only constrain if we've measured the tooltip
|
||||||
const hasFinePointer = () => window.matchMedia('(pointer: fine)').matches
|
if (tooltipWidth.value > 0 && tooltipHeight.value > 0) {
|
||||||
|
// Adjust horizontal position if tooltip would overflow right edge
|
||||||
|
if (x + tooltipWidth.value + pad > window.innerWidth) {
|
||||||
|
x = window.innerWidth - tooltipWidth.value - pad
|
||||||
|
}
|
||||||
|
// Adjust vertical position if tooltip would overflow bottom edge
|
||||||
|
if (y + tooltipHeight.value + pad > window.innerHeight) {
|
||||||
|
y = window.innerHeight - tooltipHeight.value - pad
|
||||||
|
}
|
||||||
|
// Don't go past left/top edges
|
||||||
|
x = Math.max(pad, x)
|
||||||
|
y = Math.max(pad, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
left: `${x}px`,
|
||||||
|
top: `${y}px`,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Track touch events globally to suppress touch-simulated mouse events
|
||||||
|
const onTouchStart = () => { lastTouchTime = Date.now() }
|
||||||
|
onMounted(() => document.addEventListener('touchstart', onTouchStart, { passive: true }))
|
||||||
|
onUnmounted(() => document.removeEventListener('touchstart', onTouchStart))
|
||||||
|
|
||||||
|
// Check if event is likely from touch (touch happened within last 500ms)
|
||||||
|
const isTouchEvent = () => Date.now() - lastTouchTime < 500
|
||||||
|
|
||||||
const showTooltip = () => {
|
const showTooltip = () => {
|
||||||
visible.value = true
|
visible.value = true
|
||||||
globalActive = true
|
globalActive = true
|
||||||
|
// Measure tooltip after it renders
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
if (tooltipEl.value) {
|
||||||
|
tooltipWidth.value = tooltipEl.value.offsetWidth
|
||||||
|
tooltipHeight.value = tooltipEl.value.offsetHeight
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const scheduleTooltip = () => {
|
const scheduleTooltip = () => {
|
||||||
@@ -56,8 +93,8 @@ const scheduleTooltip = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const startHover = (e: MouseEvent) => {
|
const startHover = (e: MouseEvent) => {
|
||||||
// Ignore touch events (no fine pointer and no confirmed real mouse)
|
// Ignore touch-simulated mouse events
|
||||||
if (!hasFinePointer() && !hasRealMouse) return
|
if (isTouchEvent()) return
|
||||||
|
|
||||||
mouseX.value = e.clientX
|
mouseX.value = e.clientX
|
||||||
mouseY.value = e.clientY
|
mouseY.value = e.clientY
|
||||||
@@ -66,9 +103,8 @@ const startHover = (e: MouseEvent) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updatePosition = (e: MouseEvent) => {
|
const updatePosition = (e: MouseEvent) => {
|
||||||
// Detect real mouse via movement (touch events don't generate continuous mousemove)
|
// Ignore touch-simulated mouse events
|
||||||
if (e.movementX !== 0 || e.movementY !== 0) hasRealMouse = true
|
if (isTouchEvent()) return
|
||||||
if (!hasFinePointer() && !hasRealMouse) return
|
|
||||||
|
|
||||||
mouseX.value = e.clientX
|
mouseX.value = e.clientX
|
||||||
mouseY.value = e.clientY
|
mouseY.value = e.clientY
|
||||||
@@ -122,10 +158,11 @@ defineExpose({
|
|||||||
z-index: 10000;
|
z-index: 10000;
|
||||||
padding: .5rem 1rem;
|
padding: .5rem 1rem;
|
||||||
border-radius: 3rem 0 3rem 0;
|
border-radius: 3rem 0 3rem 0;
|
||||||
box-shadow: 0 0 1rem var(--accent-color);
|
box-shadow: 0 0 1rem rgba(0, 0, 0, 0.5);
|
||||||
background-color: var(--accent-color);
|
background: rgba(0, 0, 0, 0.3);
|
||||||
color: var(--primary-color);
|
backdrop-filter: blur(4px);
|
||||||
white-space: nowrap;
|
-webkit-backdrop-filter: blur(4px);
|
||||||
|
color: #fff;
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
font-size: 1rem;
|
font-size: 1rem;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,426 @@
|
|||||||
|
<template>
|
||||||
|
<div class="disk-space-container" ref="containerRef" tabindex="0" @keydown.enter="handleClick" @keydown.space.prevent="handleClick">
|
||||||
|
<div
|
||||||
|
ref="widgetRef"
|
||||||
|
class="disk-space-widget"
|
||||||
|
:class="{ expanded: isExpanded }"
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 150 150" class="pie-svg" preserveAspectRatio="xMidYMid meet">
|
||||||
|
<defs>
|
||||||
|
<filter id="pieShadow" x="-50%" y="-50%" width="200%" height="200%">
|
||||||
|
<feDropShadow dx="0" dy="4" stdDeviation="8" flood-color="rgba(0,0,0,0.4)" />
|
||||||
|
</filter>
|
||||||
|
<radialGradient id="storageGradient" cx="30%" cy="30%" r="70%">
|
||||||
|
<stop offset="0%" stop-color="#93e" />
|
||||||
|
<stop offset="100%" stop-color="#82d" />
|
||||||
|
</radialGradient>
|
||||||
|
<radialGradient id="otherGradient" cx="30%" cy="30%" r="70%">
|
||||||
|
<stop offset="0%" stop-color="#d9f" />
|
||||||
|
<stop offset="100%" stop-color="#c8e" />
|
||||||
|
</radialGradient>
|
||||||
|
<radialGradient id="highlightOverlay" cx="35%" cy="35%" r="65%">
|
||||||
|
<stop offset="0%" stop-color="rgba(255,255,255,0.15)" />
|
||||||
|
<stop offset="60%" stop-color="rgba(255,255,255,0)" />
|
||||||
|
<stop offset="100%" stop-color="rgba(0,0,0,0.08)" />
|
||||||
|
</radialGradient>
|
||||||
|
</defs>
|
||||||
|
|
||||||
|
<g :filter="isExpanded ? 'url(#pieShadow)' : 'none'">
|
||||||
|
<circle :r="midRadius" :cx="pieCx" :cy="pieCy" fill="transparent" stroke="url(#otherGradient)" :stroke-width="ringWidth" />
|
||||||
|
<circle :r="midRadius" :cx="pieCx" :cy="pieCy" fill="transparent" :stroke="freeColor" :stroke-width="ringWidth" :stroke-dasharray="pieFreeDash" :stroke-dashoffset="pieFreeOffsetVal" :transform="`rotate(-90 ${pieCx} ${pieCy})`" />
|
||||||
|
<circle :r="midRadius" :cx="pieCx" :cy="pieCy" fill="transparent" stroke="url(#storageGradient)" :stroke-width="ringWidth" :stroke-dasharray="pieStorageDash" :transform="`rotate(-90 ${pieCx} ${pieCy})`" />
|
||||||
|
<circle :r="midRadius" :cx="pieCx" :cy="pieCy" fill="transparent" stroke="url(#highlightOverlay)" :stroke-width="ringWidth" />
|
||||||
|
<circle :r="holeRadius" :cx="pieCx" :cy="pieCy" fill="rgba(0,0,0,0.5)" />
|
||||||
|
<text ref="centerLabelRef" :x="pieCx" :y="pieCy" dy="0.35em" class="pie-center-label" text-anchor="middle">GB</text>
|
||||||
|
<circle :r="pieRadius" :cx="pieCx" :cy="pieCy" fill="transparent" class="pie-hitarea" @click="handleClick" />
|
||||||
|
</g>
|
||||||
|
|
||||||
|
<g ref="labelsRef" class="pie-labels">
|
||||||
|
<text :x="storageInnerPos.x" :y="storageInnerPos.y" class="pie-label-inner" :text-anchor="getSizeAnchor(sectorInfo.storage.angle)" dominant-baseline="middle" :transform="`rotate(${getSizeRotation(sectorInfo.storage.angle)} ${storageInnerPos.x} ${storageInnerPos.y})`">{{ fmtSize(store.space.allocated, sectorInfo.storage.angle) }}</text>
|
||||||
|
<text :x="freeInnerPos.x" :y="freeInnerPos.y" class="pie-label-inner" :text-anchor="getSizeAnchor(sectorInfo.free.angle)" dominant-baseline="middle" :transform="`rotate(${getSizeRotation(sectorInfo.free.angle)} ${freeInnerPos.x} ${freeInnerPos.y})`">{{ fmtSize(store.space.free, sectorInfo.free.angle) }}</text>
|
||||||
|
<text :x="otherInnerPos.x" :y="otherInnerPos.y" class="pie-label-inner" :text-anchor="getSizeAnchor(sectorInfo.other.angle)" dominant-baseline="middle" :transform="`rotate(${getSizeRotation(sectorInfo.other.angle)} ${otherInnerPos.x} ${otherInnerPos.y})`">{{ fmtSize(store.space.used - store.space.allocated, sectorInfo.other.angle) }}</text>
|
||||||
|
|
||||||
|
<defs>
|
||||||
|
<path :id="storageLabelPath.id" :d="storageLabelPath.d" fill="none" />
|
||||||
|
<path :id="freeLabelPath.id" :d="freeLabelPath.d" fill="none" />
|
||||||
|
<path :id="otherLabelPath.id" :d="otherLabelPath.d" fill="none" />
|
||||||
|
</defs>
|
||||||
|
|
||||||
|
<text class="pie-label-sub" fill="#93e">
|
||||||
|
<textPath :href="'#' + storageLabelPath.id" startOffset="50%" text-anchor="middle" dominant-baseline="middle">{{ storageName }}</textPath>
|
||||||
|
</text>
|
||||||
|
<text class="pie-label-sub" :fill="freeColor">
|
||||||
|
<textPath :href="'#' + freeLabelPath.id" startOffset="50%" text-anchor="middle" dominant-baseline="middle">free</textPath>
|
||||||
|
</text>
|
||||||
|
<text class="pie-label-sub" fill="#d9f">
|
||||||
|
<textPath :href="'#' + otherLabelPath.id" startOffset="50%" text-anchor="middle" dominant-baseline="middle">other</textPath>
|
||||||
|
</text>
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
||||||
|
import { useMainStore } from '@/stores/main'
|
||||||
|
|
||||||
|
const store = useMainStore()
|
||||||
|
const containerRef = ref<HTMLDivElement | null>(null)
|
||||||
|
const widgetRef = ref<HTMLDivElement | null>(null)
|
||||||
|
const labelsRef = ref<SVGGElement | null>(null)
|
||||||
|
const centerLabelRef = ref<SVGTextElement | null>(null)
|
||||||
|
|
||||||
|
const isExpanded = ref(false)
|
||||||
|
let animationFrame: number | null = null
|
||||||
|
|
||||||
|
const BASE_SIZE = 48
|
||||||
|
const EXPANDED_SCALE = 320 / 48
|
||||||
|
const ANIM_DURATION = 200
|
||||||
|
const containerPos = ref({ top: 0, left: 0, width: 0 })
|
||||||
|
|
||||||
|
const formatGB = (bytes: number) => {
|
||||||
|
const gb = bytes / (1024 * 1024 * 1024)
|
||||||
|
return gb < 10 ? gb.toFixed(1) : `${Math.round(gb)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add dot suffix for ambiguous angles (within 15° of horizontal) on numbers that look same upside down
|
||||||
|
const fmtSize = (bytes: number, angle: number) => {
|
||||||
|
const s = formatGB(bytes)
|
||||||
|
const a = Math.abs(angle % 180)
|
||||||
|
return (Math.min(a, 180 - a) < 15 && /^[0689]+$/.test(s)) ? `${s}.` : s
|
||||||
|
}
|
||||||
|
|
||||||
|
const truncateLabel = (name: string, maxLen = 10): string => {
|
||||||
|
if (name.length <= maxLen) return name
|
||||||
|
const parts = name.split(/[\s\-_.,;:!?()\[\]{}]+/)
|
||||||
|
if (parts[0] && parts[0].length <= maxLen) return parts[0]
|
||||||
|
return name.slice(0, maxLen - 1) + '…'
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate max label length based on angular gap to neighbor labels
|
||||||
|
const storageMaxLen = computed(() => {
|
||||||
|
const s = store.space
|
||||||
|
if (!s.disk) return 10
|
||||||
|
// Sector spans in degrees
|
||||||
|
const storageSpan = (s.allocated / s.disk) * 360
|
||||||
|
const freeSpan = (s.free / s.disk) * 360
|
||||||
|
const otherSpan = ((s.used - s.allocated) / s.disk) * 360
|
||||||
|
// Angular gap from storage label midpoint to neighbor label midpoints
|
||||||
|
const gapToFree = (storageSpan + freeSpan) / 2
|
||||||
|
const gapToOther = (storageSpan + otherSpan) / 2
|
||||||
|
const minGap = Math.min(gapToFree, gapToOther)
|
||||||
|
// Allow longer names when there's sufficient gap to both neighbors
|
||||||
|
if (minGap > 70) return 18
|
||||||
|
if (minGap > 55) return 14
|
||||||
|
return 10
|
||||||
|
})
|
||||||
|
|
||||||
|
const storageName = computed(() => {
|
||||||
|
const name = store.server.name || 'stored'
|
||||||
|
const maxLen = storageMaxLen.value
|
||||||
|
// Use full name if it fits within the available space
|
||||||
|
if (name.length <= maxLen) return name
|
||||||
|
return truncateLabel(name, 10)
|
||||||
|
})
|
||||||
|
|
||||||
|
const TAU = 2 * Math.PI
|
||||||
|
|
||||||
|
const pieCx = 75
|
||||||
|
const pieCy = 75
|
||||||
|
const pieRadius = 55
|
||||||
|
const holeRadius = pieRadius * 0.38
|
||||||
|
const ringWidth = pieRadius - holeRadius
|
||||||
|
const midRadius = (pieRadius + holeRadius) / 2
|
||||||
|
const CIRC = TAU * midRadius
|
||||||
|
|
||||||
|
const pieStorageDash = computed(() => {
|
||||||
|
const s = store.space
|
||||||
|
if (!s.disk) return `0 ${CIRC}`
|
||||||
|
return `${(s.allocated / s.disk) * CIRC} ${CIRC}`
|
||||||
|
})
|
||||||
|
|
||||||
|
const pieFreeDash = computed(() => {
|
||||||
|
const s = store.space
|
||||||
|
if (!s.disk) return `0 ${CIRC}`
|
||||||
|
return `${(s.free / s.disk) * CIRC} ${CIRC}`
|
||||||
|
})
|
||||||
|
|
||||||
|
const pieFreeOffsetVal = computed(() => {
|
||||||
|
const s = store.space
|
||||||
|
if (!s.disk) return 0
|
||||||
|
return -(s.allocated / s.disk) * CIRC
|
||||||
|
})
|
||||||
|
|
||||||
|
const freeColor = computed(() => {
|
||||||
|
const s = store.space
|
||||||
|
if (!s.disk) return '#6c6'
|
||||||
|
const freePct = s.free / s.disk
|
||||||
|
if (freePct > 0.25) return '#5b5'
|
||||||
|
if (freePct > 0.10) return '#ff0'
|
||||||
|
return '#f00'
|
||||||
|
})
|
||||||
|
|
||||||
|
const PIE_RADIUS = 55
|
||||||
|
const LABEL_RADIUS = 62
|
||||||
|
|
||||||
|
const getPoint = (angle: number, radius: number) => {
|
||||||
|
const rad = TAU * (angle - 90) / 360
|
||||||
|
return { x: pieCx + radius * Math.cos(rad), y: pieCy + radius * Math.sin(rad) }
|
||||||
|
}
|
||||||
|
|
||||||
|
const sectorInfo = computed(() => {
|
||||||
|
const s = store.space
|
||||||
|
if (!s.disk) return {
|
||||||
|
storage: { angle: 45, pct: 0.25 },
|
||||||
|
free: { angle: 180, pct: 0.5 },
|
||||||
|
other: { angle: 270, pct: 0.25 }
|
||||||
|
}
|
||||||
|
|
||||||
|
const storagePct = s.allocated / s.disk
|
||||||
|
const freePct = s.free / s.disk
|
||||||
|
const otherPct = (s.used - s.allocated) / s.disk
|
||||||
|
|
||||||
|
const storageAngle = storagePct * 180 // midpoint of storage sector
|
||||||
|
const freeStart = storagePct * 360
|
||||||
|
const freeAngle = freeStart + freePct * 180
|
||||||
|
const otherStart = (storagePct + freePct) * 360
|
||||||
|
const otherAngle = otherStart + otherPct * 180
|
||||||
|
|
||||||
|
return {
|
||||||
|
storage: { angle: storageAngle, pct: storagePct },
|
||||||
|
free: { angle: freeAngle, pct: freePct },
|
||||||
|
other: { angle: otherAngle, pct: otherPct }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const rawAngles = computed(() => ({
|
||||||
|
storage: sectorInfo.value.storage.angle,
|
||||||
|
free: sectorInfo.value.free.angle,
|
||||||
|
other: sectorInfo.value.other.angle
|
||||||
|
}))
|
||||||
|
|
||||||
|
const getSizeRotation = (angle: number) => angle < 180 ? angle - 90 : angle + 90
|
||||||
|
const getSizeAnchor = (angle: number) => angle < 180 ? 'end' : 'start'
|
||||||
|
|
||||||
|
const INNER_LABEL_RADIUS = PIE_RADIUS * 0.95
|
||||||
|
const storageInnerPos = computed(() => getPoint(sectorInfo.value.storage.angle, INNER_LABEL_RADIUS))
|
||||||
|
const freeInnerPos = computed(() => getPoint(sectorInfo.value.free.angle, INNER_LABEL_RADIUS))
|
||||||
|
const otherInnerPos = computed(() => getPoint(sectorInfo.value.other.angle, INNER_LABEL_RADIUS))
|
||||||
|
|
||||||
|
// Collision avoidance for curved name labels
|
||||||
|
const labelLengths = computed(() => ({
|
||||||
|
storage: storageName.value.length,
|
||||||
|
free: 4,
|
||||||
|
other: 5
|
||||||
|
}))
|
||||||
|
|
||||||
|
const getGapForPair = (len1: number, len2: number) => {
|
||||||
|
return 35 + Math.max(0, len1 + len2 - 8) * 2.5
|
||||||
|
}
|
||||||
|
|
||||||
|
const adjustedLabelAngles = computed(() => {
|
||||||
|
const angles = rawAngles.value
|
||||||
|
const lens = labelLengths.value
|
||||||
|
const labels = [
|
||||||
|
{ id: 'storage', angle: angles.storage, len: lens.storage },
|
||||||
|
{ id: 'free', angle: angles.free, len: lens.free },
|
||||||
|
{ id: 'other', angle: angles.other, len: lens.other }
|
||||||
|
]
|
||||||
|
labels.sort((a, b) => a.angle - b.angle)
|
||||||
|
|
||||||
|
for (let iterations = 0; iterations < 15; iterations++) {
|
||||||
|
let moved = false
|
||||||
|
for (let i = 0; i < labels.length; i++) {
|
||||||
|
const current = labels[i]!
|
||||||
|
const next = labels[(i + 1) % labels.length]!
|
||||||
|
let angleDiff = next.angle - current.angle
|
||||||
|
if (angleDiff < 0) angleDiff += 360
|
||||||
|
const requiredGap = getGapForPair(current.len, next.len)
|
||||||
|
if (angleDiff < requiredGap) {
|
||||||
|
const push = (requiredGap - angleDiff) / 2
|
||||||
|
current.angle = (current.angle - push + 360) % 360
|
||||||
|
next.angle = (next.angle + push) % 360
|
||||||
|
moved = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!moved) break
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: Record<string, number> = {}
|
||||||
|
for (const l of labels) result[l.id] = l.angle
|
||||||
|
return result
|
||||||
|
})
|
||||||
|
|
||||||
|
// Arc path for curved text labels (CW for top half, CCW for bottom half)
|
||||||
|
const createArcPath = (centerAngle: number, id: string, labelLen: number) => {
|
||||||
|
const radius = LABEL_RADIUS
|
||||||
|
// Scale arc span based on label length: ~6° per character, minimum 45°
|
||||||
|
const arcSpan = Math.max(45, labelLen * 6)
|
||||||
|
const isBottom = centerAngle > 90 && centerAngle <= 270
|
||||||
|
const startAngle = isBottom ? centerAngle + arcSpan / 2 : centerAngle - arcSpan / 2
|
||||||
|
const endAngle = isBottom ? centerAngle - arcSpan / 2 : centerAngle + arcSpan / 2
|
||||||
|
const start = getPoint(startAngle, radius)
|
||||||
|
const end = getPoint(endAngle, radius)
|
||||||
|
const sweep = isBottom ? 0 : 1
|
||||||
|
return {
|
||||||
|
id: `label-path-${id}`,
|
||||||
|
d: `M ${start.x} ${start.y} A ${radius} ${radius} 0 0 ${sweep} ${end.x} ${end.y}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const storageLabelPath = computed(() => createArcPath(adjustedLabelAngles.value.storage!, 'storage', storageName.value.length))
|
||||||
|
const freeLabelPath = computed(() => createArcPath(adjustedLabelAngles.value.free!, 'free', 4))
|
||||||
|
const otherLabelPath = computed(() => createArcPath(adjustedLabelAngles.value.other!, 'other', 5))
|
||||||
|
|
||||||
|
const handleClick = () => isExpanded.value ? collapse() : expand()
|
||||||
|
|
||||||
|
const applyAnimState = (t: number, opacity: number) => {
|
||||||
|
const widget = widgetRef.value
|
||||||
|
const labels = labelsRef.value
|
||||||
|
const centerLabel = centerLabelRef.value
|
||||||
|
if (!widget) return
|
||||||
|
|
||||||
|
const scale = 1 + (EXPANDED_SCALE - 1) * t
|
||||||
|
// Move top-right corner of widget to top-right corner of viewport
|
||||||
|
const targetX = window.innerWidth - containerPos.value.left - containerPos.value.width
|
||||||
|
const targetY = -containerPos.value.top
|
||||||
|
|
||||||
|
widget.style.transform = `translate(${targetX * t}px, ${targetY * t}px) scale(${scale})`
|
||||||
|
if (labels) labels.style.opacity = String(opacity)
|
||||||
|
if (centerLabel) centerLabel.style.opacity = String(opacity)
|
||||||
|
}
|
||||||
|
|
||||||
|
const animate = (duration: number, expanding: boolean, onComplete?: () => void) => {
|
||||||
|
const startTime = performance.now()
|
||||||
|
const tick = (now: number) => {
|
||||||
|
const elapsed = now - startTime
|
||||||
|
const progress = Math.min(elapsed / duration, 1)
|
||||||
|
const eased = 1 - Math.pow(1 - progress, 3) // easeOutCubic
|
||||||
|
const t = expanding ? eased : 1 - eased
|
||||||
|
applyAnimState(t, t) // opacity follows position
|
||||||
|
if (progress < 1) {
|
||||||
|
animationFrame = requestAnimationFrame(tick)
|
||||||
|
} else {
|
||||||
|
animationFrame = null
|
||||||
|
onComplete?.()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
animationFrame = requestAnimationFrame(tick)
|
||||||
|
}
|
||||||
|
|
||||||
|
const expand = () => {
|
||||||
|
if (animationFrame) cancelAnimationFrame(animationFrame)
|
||||||
|
if (containerRef.value) {
|
||||||
|
const rect = containerRef.value.getBoundingClientRect()
|
||||||
|
containerPos.value = { top: rect.top, left: rect.left, width: rect.width }
|
||||||
|
}
|
||||||
|
isExpanded.value = true
|
||||||
|
animate(ANIM_DURATION, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
const collapse = () => {
|
||||||
|
if (animationFrame) cancelAnimationFrame(animationFrame)
|
||||||
|
if (containerRef.value) {
|
||||||
|
const rect = containerRef.value.getBoundingClientRect()
|
||||||
|
containerPos.value = { top: rect.top, left: rect.left, width: rect.width }
|
||||||
|
}
|
||||||
|
animate(ANIM_DURATION, false, () => {
|
||||||
|
isExpanded.value = false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleKeydown = (e: KeyboardEvent) => {
|
||||||
|
if (e.key === 'Escape' && isExpanded.value) collapse()
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
document.addEventListener('keydown', handleKeydown)
|
||||||
|
// Initialize labels as hidden
|
||||||
|
if (labelsRef.value) labelsRef.value.style.opacity = '0'
|
||||||
|
if (centerLabelRef.value) centerLabelRef.value.style.opacity = '0'
|
||||||
|
})
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
document.removeEventListener('keydown', handleKeydown)
|
||||||
|
if (animationFrame) cancelAnimationFrame(animationFrame)
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.disk-space-container {
|
||||||
|
position: relative;
|
||||||
|
width: 3em;
|
||||||
|
height: 3em;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.disk-space-container:focus .disk-space-widget:not(.expanded) {
|
||||||
|
filter: brightness(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.disk-space-widget {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
cursor: pointer;
|
||||||
|
will-change: transform;
|
||||||
|
filter: brightness(0.85);
|
||||||
|
transition: filter 0.2s ease;
|
||||||
|
transform-origin: top right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.disk-space-widget:hover,
|
||||||
|
.disk-space-widget:focus {
|
||||||
|
filter: brightness(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.disk-space-widget.expanded {
|
||||||
|
pointer-events: none;
|
||||||
|
filter: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.disk-space-widget.expanded:hover,
|
||||||
|
.disk-space-widget.expanded:focus {
|
||||||
|
filter: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pie-svg {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
overflow: visible;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pie-hitarea {
|
||||||
|
pointer-events: auto;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pie-label-inner {
|
||||||
|
fill: #eee;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 700;
|
||||||
|
stroke: #000;
|
||||||
|
stroke-width: 0.5px;
|
||||||
|
paint-order: stroke fill;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pie-center-label {
|
||||||
|
fill: #eee;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pie-label-sub {
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 600;
|
||||||
|
font-variant: small-caps;
|
||||||
|
text-transform: lowercase;
|
||||||
|
stroke: #000;
|
||||||
|
stroke-width: 1px;
|
||||||
|
paint-order: stroke fill;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<SvgButton name="download" data-tooltip="Download" @click="download" />
|
<SvgButton name="download" tooltip="Download" @click="download" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
@@ -7,7 +7,6 @@ import { useMainStore } from '@/stores/main'
|
|||||||
import { apiFetch } from '@/repositories/Client'
|
import { apiFetch } from '@/repositories/Client'
|
||||||
import type { SelectedItems } from '@/repositories/Document'
|
import type { SelectedItems } from '@/repositories/Document'
|
||||||
import { zipName } from '@/utils/fileutil'
|
import { zipName } from '@/utils/fileutil'
|
||||||
import { reactive } from 'vue';
|
|
||||||
|
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
|
|
||||||
@@ -131,39 +130,7 @@ const filesystemdl = async (sel: SelectedItems, handle: FileSystemDirectoryHandl
|
|||||||
statReset()
|
statReset()
|
||||||
}
|
}
|
||||||
|
|
||||||
const download = async () => {
|
const zipdl = (sel: SelectedItems) => {
|
||||||
const sel = store.selectedFiles
|
|
||||||
console.log('Download', sel)
|
|
||||||
if (sel.keys.length === 0) {
|
|
||||||
console.warn('Attempted download but no files found. Missing selected keys:', sel.missing)
|
|
||||||
store.showToast('No existing files selected')
|
|
||||||
store.selected.clear()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Plain old a href download if only one file (ignoring any folders)
|
|
||||||
const files = sel.recursive.filter(([rel, full, doc]) => !doc.dir)
|
|
||||||
if (files.length === 1) {
|
|
||||||
store.selected.clear()
|
|
||||||
store.showToast(`Downloading ${files[0]![0].split('/').pop()}`)
|
|
||||||
return linkdl(`/files/${files[0]![1]}`)
|
|
||||||
}
|
|
||||||
// Use FileSystem API if multiple files and the browser supports it
|
|
||||||
if ('showDirectoryPicker' in window) {
|
|
||||||
try {
|
|
||||||
// @ts-ignore
|
|
||||||
const handle = await window.showDirectoryPicker({
|
|
||||||
startIn: 'downloads',
|
|
||||||
mode: 'readwrite'
|
|
||||||
})
|
|
||||||
await filesystemdl(sel, handle)
|
|
||||||
store.selected.clear()
|
|
||||||
return
|
|
||||||
} catch (e) {
|
|
||||||
console.error('Download to folder aborted', e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Otherwise, zip and download
|
|
||||||
console.log("Falling back to zip download")
|
|
||||||
const items = sel.keys.map(k => sel.docs[k]!)
|
const items = sel.keys.map(k => sel.docs[k]!)
|
||||||
const name = zipName(items)
|
const name = zipName(items)
|
||||||
linkdl(`/zip/${Array.from(sel.keys).join('+')}/${name}.zip`)
|
linkdl(`/zip/${Array.from(sel.keys).join('+')}/${name}.zip`)
|
||||||
@@ -171,6 +138,37 @@ const download = async () => {
|
|||||||
store.selected.clear()
|
store.selected.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const download = async (e: MouseEvent) => {
|
||||||
|
const sel = store.selectedFiles
|
||||||
|
if (sel.keys.length === 0) {
|
||||||
|
store.showToast('No existing files selected')
|
||||||
|
store.selected.clear()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Single file: direct download
|
||||||
|
const files = sel.recursive.filter(([rel, full, doc]) => !doc.dir)
|
||||||
|
if (files.length === 1) {
|
||||||
|
store.selected.clear()
|
||||||
|
store.showToast(`Downloading ${files[0]![0].split('/').pop()}`)
|
||||||
|
return linkdl(`/files/${files[0]![1]}`)
|
||||||
|
}
|
||||||
|
// Alt+click: download to folder (hidden feature)
|
||||||
|
if (e.altKey && 'showDirectoryPicker' in window) {
|
||||||
|
try {
|
||||||
|
// @ts-ignore
|
||||||
|
const handle = await window.showDirectoryPicker({ startIn: 'downloads', mode: 'readwrite' })
|
||||||
|
await filesystemdl(sel, handle)
|
||||||
|
store.selected.clear()
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Download to folder failed', e)
|
||||||
|
store.showToast('Download to folder failed')
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Default: ZIP download
|
||||||
|
zipdl(sel)
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<component :is="cog" :class="['cog', { stopped: store.dialog === 'accessdenied' || store.authInProgress }]"/>
|
<component :is="cog" :class="['cog', { stopped: store.dialog === 'accessdenied' || store.authInProgress }]"/>
|
||||||
<p v-if="store.dialog === 'accessdenied'">Access Denied</p>
|
<p v-if="store.dialog === 'accessdenied'">Access Denied</p>
|
||||||
<p v-else-if="!store.connected">No Connection</p>
|
<p v-else-if="!store.connected">No Connection</p>
|
||||||
<p v-else-if="store.document.length === 0">Waiting for File List</p>
|
<p v-else-if="store.documentCount === 0">Waiting for File List</p>
|
||||||
<p v-else-if="store.query">No matches!</p>
|
<p v-else-if="store.query">No matches!</p>
|
||||||
<p v-else-if="!exists(props.path)">Folder not found</p>
|
<p v-else-if="!exists(props.path)">Folder not found</p>
|
||||||
<p v-else>Empty folder</p>
|
<p v-else>Empty folder</p>
|
||||||
|
|||||||
@@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
<tr
|
<tr
|
||||||
:id="`file-${doc.key}`"
|
:id="`file-${doc.key}`"
|
||||||
:class="{ file: !doc.dir, folder: doc.dir, cursor: store.cursor === doc.key }"
|
:class="{ file: !doc.dir, folder: doc.dir, cursor: store.cursor === doc.key, ghost: doc.ghost }"
|
||||||
@click="store.cursor = store.cursor === doc.key ? '' : doc.key"
|
@click="store.cursor = store.cursor === doc.key ? '' : doc.key"
|
||||||
@contextmenu.prevent="contextMenu($event, doc)"
|
@contextmenu.prevent="contextMenu($event, doc)"
|
||||||
>
|
>
|
||||||
@@ -72,7 +72,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed, watchEffect, shallowRef, onMounted, onUnmounted } from 'vue'
|
import { ref, computed, watchEffect, shallowRef, onMounted, onUnmounted, nextTick } from 'vue'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import { Doc } from '@/repositories/Document'
|
import { Doc } from '@/repositories/Document'
|
||||||
import FileRenameInput from './FileRenameInput.vue'
|
import FileRenameInput from './FileRenameInput.vue'
|
||||||
@@ -124,6 +124,7 @@ defineExpose({
|
|||||||
dir: true,
|
dir: true,
|
||||||
mtime: now,
|
mtime: now,
|
||||||
size: 0,
|
size: 0,
|
||||||
|
allocated: 0,
|
||||||
})
|
})
|
||||||
store.cursor = editing.value.key
|
store.cursor = editing.value.key
|
||||||
},
|
},
|
||||||
@@ -134,6 +135,17 @@ defineExpose({
|
|||||||
isCursor() {
|
isCursor() {
|
||||||
return store.cursor && editing.value === null
|
return store.cursor && editing.value === null
|
||||||
},
|
},
|
||||||
|
focusFirst() {
|
||||||
|
const docs = props.documents
|
||||||
|
if (docs.length > 0) {
|
||||||
|
store.cursor = docs[0]!.key
|
||||||
|
// Also focus the element directly (watchEffect won't trigger if cursor unchanged)
|
||||||
|
nextTick(() => {
|
||||||
|
const a = document.querySelector(`#file-${store.cursor} .name a`) as HTMLAnchorElement | null
|
||||||
|
if (a) a.focus()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
cursorRename() {
|
cursorRename() {
|
||||||
editing.value = props.documents.find(doc => doc.key === store.cursor) ?? null
|
editing.value = props.documents.find(doc => doc.key === store.cursor) ?? null
|
||||||
},
|
},
|
||||||
@@ -149,7 +161,12 @@ defineExpose({
|
|||||||
},
|
},
|
||||||
up(ev: KeyboardEvent) { this.cursorMove(-1, ev) },
|
up(ev: KeyboardEvent) { this.cursorMove(-1, ev) },
|
||||||
down(ev: KeyboardEvent) { this.cursorMove(1, ev) },
|
down(ev: KeyboardEvent) { this.cursorMove(1, ev) },
|
||||||
left(ev: KeyboardEvent) { router.back() },
|
left(ev: KeyboardEvent) {
|
||||||
|
// Only go back if we're in a subfolder (not at root)
|
||||||
|
if (props.path.length > 0) {
|
||||||
|
router.back()
|
||||||
|
}
|
||||||
|
},
|
||||||
right(ev: KeyboardEvent) {
|
right(ev: KeyboardEvent) {
|
||||||
const a = document.querySelector(`#file-${store.cursor} a`) as HTMLAnchorElement | null
|
const a = document.querySelector(`#file-${store.cursor} a`) as HTMLAnchorElement | null
|
||||||
if (a) a.click()
|
if (a) a.click()
|
||||||
@@ -189,9 +206,17 @@ defineExpose({
|
|||||||
scrolltimer = null
|
scrolltimer = null
|
||||||
}, 300)
|
}, 300)
|
||||||
}
|
}
|
||||||
if (moveto === N) focusBreadcrumb()
|
// When leaving the file list: up goes to breadcrumbs, down goes to header
|
||||||
|
if (moveto === N) {
|
||||||
|
if (d < 0) focusBreadcrumb()
|
||||||
|
else focusHeader()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
const focusHeader = () => {
|
||||||
|
const el = document.querySelector('.headermain input[type="search"]') as HTMLElement | null
|
||||||
|
if (el) el.focus()
|
||||||
|
}
|
||||||
const focusBreadcrumb = () => {
|
const focusBreadcrumb = () => {
|
||||||
const el = document.querySelector('.breadcrumb') as HTMLElement | null
|
const el = document.querySelector('.breadcrumb') as HTMLElement | null
|
||||||
if (el) el.focus()
|
if (el) el.focus()
|
||||||
@@ -209,7 +234,7 @@ watchEffect(() => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
if (!props.documents.length && store.cursor) {
|
if (!props.documents.length && store.cursor && !store.query) {
|
||||||
store.cursor = ''
|
store.cursor = ''
|
||||||
focusBreadcrumb()
|
focusBreadcrumb()
|
||||||
}
|
}
|
||||||
@@ -249,9 +274,10 @@ const mkdir = (doc: Doc, name: string) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// We should get an update from watch but this is quicker
|
|
||||||
doc.name = name
|
doc.name = name
|
||||||
doc.key = crypto.randomUUID()
|
doc.key = crypto.randomUUID()
|
||||||
|
store.addGhost(doc)
|
||||||
|
editing.value = null
|
||||||
}
|
}
|
||||||
const showFolderBreadcrumb = (i: number) => {
|
const showFolderBreadcrumb = (i: number) => {
|
||||||
const docs = props.documents
|
const docs = props.documents
|
||||||
@@ -290,13 +316,96 @@ const allSelected = computed({
|
|||||||
|
|
||||||
const loc = computed(() => props.path.join('/'))
|
const loc = computed(() => props.path.join('/'))
|
||||||
|
|
||||||
|
const downloadFile = (doc: Doc) => {
|
||||||
|
const path = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
if (doc.dir) {
|
||||||
|
// Download folder as ZIP
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.href = `/zip/${doc.key}/${doc.name}.zip`
|
||||||
|
a.download = ''
|
||||||
|
a.click()
|
||||||
|
store.showToast(`Downloading ${doc.name}.zip`)
|
||||||
|
} else {
|
||||||
|
// Download single file
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.href = `/files/${path}`
|
||||||
|
a.download = ''
|
||||||
|
a.click()
|
||||||
|
store.showToast(`Downloading ${doc.name}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const copyLink = async (doc: Doc) => {
|
||||||
|
const url = new URL(doc.url, window.location.origin).href
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(url)
|
||||||
|
store.showToast('📋 Link copied!')
|
||||||
|
} catch {
|
||||||
|
store.showToast('Failed to copy link')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const copyImage = async (doc: Doc) => {
|
||||||
|
const path = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
try {
|
||||||
|
store.showToast('Copying image...')
|
||||||
|
const res = await fetch(`/files/${path}`)
|
||||||
|
const blob = await res.blob()
|
||||||
|
// Convert to PNG if needed (clipboard only supports PNG)
|
||||||
|
if (blob.type !== 'image/png') {
|
||||||
|
const img = new Image()
|
||||||
|
img.src = URL.createObjectURL(blob)
|
||||||
|
await new Promise(r => img.onload = r)
|
||||||
|
const canvas = document.createElement('canvas')
|
||||||
|
canvas.width = img.naturalWidth
|
||||||
|
canvas.height = img.naturalHeight
|
||||||
|
canvas.getContext('2d')!.drawImage(img, 0, 0)
|
||||||
|
const pngBlob = await new Promise<Blob>(r => canvas.toBlob(b => r(b!), 'image/png'))
|
||||||
|
URL.revokeObjectURL(img.src)
|
||||||
|
await navigator.clipboard.write([new ClipboardItem({ 'image/png': pngBlob })])
|
||||||
|
} else {
|
||||||
|
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })])
|
||||||
|
}
|
||||||
|
store.showToast('📋 Image copied!')
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Copy image failed', e)
|
||||||
|
store.showToast('Failed to copy image')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteFile = (doc: Doc) => {
|
||||||
|
const path = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
store.hideDoc(path)
|
||||||
|
const control = connect(controlUrl, {
|
||||||
|
message(ev: MessageEvent) {
|
||||||
|
const res = JSON.parse(ev.data)
|
||||||
|
if ('error' in res) {
|
||||||
|
console.error('Delete failed', res.error)
|
||||||
|
store.unhideDoc(path)
|
||||||
|
store.showToast(res.error.message || 'Delete failed')
|
||||||
|
} else if (res.status === 'ack') {
|
||||||
|
store.showToast(`🗑️ Deleted ${doc.name}`)
|
||||||
|
control.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
control.onopen = () => {
|
||||||
|
control.send(JSON.stringify({ op: 'rm', sel: [path] }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const contextMenu = (ev: MouseEvent, doc: Doc) => {
|
const contextMenu = (ev: MouseEvent, doc: Doc) => {
|
||||||
store.cursor = doc.key
|
store.cursor = doc.key
|
||||||
ContextMenu.showContextMenu({
|
const items = [
|
||||||
x: ev.x, y: ev.y, items: [
|
{ label: '📥 Download', onClick: () => downloadFile(doc) },
|
||||||
{ label: 'Rename', onClick: () => { editing.value = doc } },
|
{ label: '🔗 Copy Link', onClick: () => copyLink(doc) },
|
||||||
],
|
]
|
||||||
})
|
if (doc.img) items.push({ label: '📋 Copy Image', onClick: () => copyImage(doc) })
|
||||||
|
items.push(
|
||||||
|
{ label: '✏️ Rename', onClick: () => { editing.value = doc } },
|
||||||
|
{ label: '🗑️ Delete', onClick: () => deleteFile(doc) },
|
||||||
|
)
|
||||||
|
ContextMenu.showContextMenu({ x: ev.x, y: ev.y, items })
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -392,9 +501,6 @@ tbody tr.cursor {
|
|||||||
.sortcolumn:hover {
|
.sortcolumn:hover {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
.sortcolumn:hover::after {
|
|
||||||
color: var(--accent-color);
|
|
||||||
}
|
|
||||||
.sortcolumn {
|
.sortcolumn {
|
||||||
padding-right: 1.5rem;
|
padding-right: 1.5rem;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,44 @@
|
|||||||
<template>
|
<template>
|
||||||
<td class="size right" :class=sizeClass>{{ doc.sizedisp }}</td>
|
<td
|
||||||
|
class="size right"
|
||||||
|
:class="sizeClass"
|
||||||
|
@mouseenter="doc.sparseIndicator && tooltip?.startHover($event)"
|
||||||
|
@mousemove="doc.sparseIndicator && tooltip?.updatePosition($event)"
|
||||||
|
@mouseleave="doc.sparseIndicator && tooltip?.endHover()"
|
||||||
|
>
|
||||||
|
<SparseIndicator :doc="doc" class="before-size" />{{ doc.sizedisp }}
|
||||||
|
<CursorTooltip v-if="doc.sparseIndicator" ref="tooltip" :text="tooltipText">{{ tooltipText }}</CursorTooltip>
|
||||||
|
</td>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Doc } from '@/repositories/Document'
|
import { Doc } from '@/repositories/Document'
|
||||||
import { computed } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
|
import { formatSize } from '@/utils'
|
||||||
|
import SparseIndicator from './SparseIndicator.vue'
|
||||||
|
import CursorTooltip from './CursorTooltip.vue'
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
doc: Doc
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const tooltip = ref<InstanceType<typeof CursorTooltip> | null>(null)
|
||||||
|
|
||||||
const sizeClass = computed(() => {
|
const sizeClass = computed(() => {
|
||||||
const unit = props.doc.sizedisp.split('\u202F').slice(-1)[0]!
|
const unit = props.doc.sizedisp.split('\u202F').slice(-1)[0]!
|
||||||
return +unit ? "bytes" : unit
|
return +unit ? "bytes" : unit
|
||||||
})
|
})
|
||||||
|
|
||||||
const props = defineProps<{
|
const tooltipText = computed(() => {
|
||||||
doc: Doc
|
const { size, allocated } = props.doc
|
||||||
}>()
|
return `${formatSize(allocated)} allocated of ${formatSize(size)}`
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
.before-size {
|
||||||
|
margin-right: 0.2em;
|
||||||
|
}
|
||||||
.size.empty { color: #555 }
|
.size.empty { color: #555 }
|
||||||
.size.bytes { color: #77a }
|
.size.bytes { color: #77a }
|
||||||
.size.kB { color: #474 }
|
.size.kB { color: #474 }
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed, watchEffect, shallowRef, onMounted, onUnmounted } from 'vue'
|
import { ref, computed, watchEffect, shallowRef, onMounted, onUnmounted, nextTick } from 'vue'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import { Doc } from '@/repositories/Document'
|
import { Doc } from '@/repositories/Document'
|
||||||
import { connect, controlUrl } from '@/repositories/WS'
|
import { connect, controlUrl } from '@/repositories/WS'
|
||||||
@@ -67,6 +67,7 @@ defineExpose({
|
|||||||
dir: true,
|
dir: true,
|
||||||
mtime: now,
|
mtime: now,
|
||||||
size: 0,
|
size: 0,
|
||||||
|
allocated: 0,
|
||||||
})
|
})
|
||||||
store.cursor = editing.value.key
|
store.cursor = editing.value.key
|
||||||
},
|
},
|
||||||
@@ -81,6 +82,17 @@ defineExpose({
|
|||||||
isCursor() {
|
isCursor() {
|
||||||
return store.cursor && editing.value === null
|
return store.cursor && editing.value === null
|
||||||
},
|
},
|
||||||
|
focusFirst() {
|
||||||
|
const docs = props.documents
|
||||||
|
if (docs.length > 0) {
|
||||||
|
store.cursor = docs[0]!.key
|
||||||
|
// Also focus the element directly (watchEffect won't trigger if cursor unchanged)
|
||||||
|
nextTick(() => {
|
||||||
|
const a = document.querySelector(`#file-${store.cursor}`) as HTMLAnchorElement | null
|
||||||
|
if (a) a.focus()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
cursorRename() {
|
cursorRename() {
|
||||||
editing.value = props.documents.find(doc => doc.key === store.cursor) ?? null
|
editing.value = props.documents.find(doc => doc.key === store.cursor) ?? null
|
||||||
},
|
},
|
||||||
@@ -143,9 +155,17 @@ defineExpose({
|
|||||||
scrolltimer = null
|
scrolltimer = null
|
||||||
}, 300)
|
}, 300)
|
||||||
}
|
}
|
||||||
if (moveto === N) focusBreadcrumb()
|
// When leaving the file list: up goes to breadcrumbs, down goes to header
|
||||||
|
if (moveto === N) {
|
||||||
|
if (d < 0) focusBreadcrumb()
|
||||||
|
else focusHeader()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
const focusHeader = () => {
|
||||||
|
const el = document.querySelector('.headermain input[type="search"]') as HTMLElement | null
|
||||||
|
if (el) el.focus()
|
||||||
|
}
|
||||||
const focusBreadcrumb = () => {
|
const focusBreadcrumb = () => {
|
||||||
const el = document.querySelector('.breadcrumb') as HTMLElement | null
|
const el = document.querySelector('.breadcrumb') as HTMLElement | null
|
||||||
if (el) el.focus()
|
if (el) el.focus()
|
||||||
@@ -161,7 +181,7 @@ watchEffect(() => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
if (!props.documents.length && store.cursor) {
|
if (!props.documents.length && store.cursor && !store.query) {
|
||||||
store.cursor = ''
|
store.cursor = ''
|
||||||
focusBreadcrumb()
|
focusBreadcrumb()
|
||||||
}
|
}
|
||||||
@@ -203,9 +223,10 @@ const mkdir = (doc: Doc, name: string) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// We should get an update from watch but this is quicker
|
|
||||||
doc.name = name
|
doc.name = name
|
||||||
doc.key = crypto.randomUUID()
|
doc.key = crypto.randomUUID()
|
||||||
|
store.addGhost(doc)
|
||||||
|
editing.value = null
|
||||||
}
|
}
|
||||||
const showFolderBreadcrumb = (i: number) => {
|
const showFolderBreadcrumb = (i: number) => {
|
||||||
const docs = props.documents
|
const docs = props.documents
|
||||||
@@ -213,18 +234,6 @@ const showFolderBreadcrumb = (i: number) => {
|
|||||||
return i === 0 ? docloc !== loc.value : docloc !== docs[i - 1]!.loc
|
return i === 0 ? docloc !== loc.value : docloc !== docs[i - 1]!.loc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const selectionIndeterminate = computed({
|
|
||||||
get: () => {
|
|
||||||
return (
|
|
||||||
props.documents.length > 0 &&
|
|
||||||
props.documents.some((doc: Doc) => store.selected.has(doc.key)) &&
|
|
||||||
!allSelected.value
|
|
||||||
)
|
|
||||||
},
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
set: (value: boolean) => {}
|
|
||||||
})
|
|
||||||
const allSelected = computed({
|
const allSelected = computed({
|
||||||
get: () => {
|
get: () => {
|
||||||
return (
|
return (
|
||||||
@@ -246,13 +255,96 @@ const allSelected = computed({
|
|||||||
|
|
||||||
const loc = computed(() => props.path.join('/'))
|
const loc = computed(() => props.path.join('/'))
|
||||||
|
|
||||||
|
const downloadFile = (doc: Doc) => {
|
||||||
|
const path = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
if (doc.dir) {
|
||||||
|
// Download folder as ZIP
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.href = `/zip/${doc.key}/${doc.name}.zip`
|
||||||
|
a.download = ''
|
||||||
|
a.click()
|
||||||
|
store.showToast(`Downloading ${doc.name}.zip`)
|
||||||
|
} else {
|
||||||
|
// Download single file
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.href = `/files/${path}`
|
||||||
|
a.download = ''
|
||||||
|
a.click()
|
||||||
|
store.showToast(`Downloading ${doc.name}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const copyLink = async (doc: Doc) => {
|
||||||
|
const url = new URL(doc.url, window.location.origin).href
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(url)
|
||||||
|
store.showToast('📋 Link copied!')
|
||||||
|
} catch {
|
||||||
|
store.showToast('Failed to copy link')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const copyImage = async (doc: Doc) => {
|
||||||
|
const path = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
try {
|
||||||
|
store.showToast('Copying image...')
|
||||||
|
const res = await fetch(`/files/${path}`)
|
||||||
|
const blob = await res.blob()
|
||||||
|
// Convert to PNG if needed (clipboard only supports PNG)
|
||||||
|
if (blob.type !== 'image/png') {
|
||||||
|
const img = new Image()
|
||||||
|
img.src = URL.createObjectURL(blob)
|
||||||
|
await new Promise(r => img.onload = r)
|
||||||
|
const canvas = document.createElement('canvas')
|
||||||
|
canvas.width = img.naturalWidth
|
||||||
|
canvas.height = img.naturalHeight
|
||||||
|
canvas.getContext('2d')!.drawImage(img, 0, 0)
|
||||||
|
const pngBlob = await new Promise<Blob>(r => canvas.toBlob(b => r(b!), 'image/png'))
|
||||||
|
URL.revokeObjectURL(img.src)
|
||||||
|
await navigator.clipboard.write([new ClipboardItem({ 'image/png': pngBlob })])
|
||||||
|
} else {
|
||||||
|
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })])
|
||||||
|
}
|
||||||
|
store.showToast('📋 Image copied!')
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Copy image failed', e)
|
||||||
|
store.showToast('Failed to copy image')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteFile = (doc: Doc) => {
|
||||||
|
const path = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
store.hideDoc(path)
|
||||||
|
const control = connect(controlUrl, {
|
||||||
|
message(ev: MessageEvent) {
|
||||||
|
const res = JSON.parse(ev.data)
|
||||||
|
if ('error' in res) {
|
||||||
|
console.error('Delete failed', res.error)
|
||||||
|
store.unhideDoc(path)
|
||||||
|
store.showToast(res.error.message || 'Delete failed')
|
||||||
|
} else if (res.status === 'ack') {
|
||||||
|
store.showToast(`🗑️ Deleted ${doc.name}`)
|
||||||
|
control.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
control.onopen = () => {
|
||||||
|
control.send(JSON.stringify({ op: 'rm', sel: [path] }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const contextMenu = (ev: MouseEvent, doc: Doc) => {
|
const contextMenu = (ev: MouseEvent, doc: Doc) => {
|
||||||
store.cursor = doc.key
|
store.cursor = doc.key
|
||||||
ContextMenu.showContextMenu({
|
const items = [
|
||||||
x: ev.x, y: ev.y, items: [
|
{ label: '📥 Download', onClick: () => downloadFile(doc) },
|
||||||
{ label: 'Rename', onClick: () => { editing.value = doc } },
|
{ label: '🔗 Copy Link', onClick: () => copyLink(doc) },
|
||||||
],
|
]
|
||||||
})
|
if (doc.img) items.push({ label: '📋 Copy Image', onClick: () => copyImage(doc) })
|
||||||
|
items.push(
|
||||||
|
{ label: '✏️ Rename', onClick: () => { editing.value = doc } },
|
||||||
|
{ label: '🗑️ Delete', onClick: () => deleteFile(doc) },
|
||||||
|
)
|
||||||
|
ContextMenu.showContextMenu({ x: ev.x, y: ev.y, items })
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<a :id="`file-${doc.key}`" :href=doc.url tabindex=-1
|
<a :id="`file-${doc.key}`" :href=doc.url tabindex=-1
|
||||||
:class="{ file: !doc.dir, folder: doc.dir, cursor: store.cursor === doc.key }"
|
:class="{ file: !doc.dir, folder: doc.dir, cursor: store.cursor === doc.key, ghost: doc.ghost }"
|
||||||
@contextmenu.stop
|
@contextmenu.stop
|
||||||
@focus.stop="store.cursor = doc.key"
|
@focus.stop="store.cursor = doc.key"
|
||||||
@click=onclick
|
@click=onclick
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
</template>
|
</template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<SelectBox :doc=doc @click="store.cursor = doc.key"/>
|
<SelectBox :doc=doc @click="store.cursor = doc.key"/>
|
||||||
<span>{{ doc.name }}</span>
|
<span>{{ doc.name }}<SparseIndicator :doc="doc" class="after-name" /></span>
|
||||||
<div class=namespacer></div>
|
<div class=namespacer></div>
|
||||||
</template>
|
</template>
|
||||||
</figcaption>
|
</figcaption>
|
||||||
@@ -26,6 +26,7 @@
|
|||||||
<CursorTooltip ref="tooltip" :text="tooltipText">
|
<CursorTooltip ref="tooltip" :text="tooltipText">
|
||||||
<div class="tooltip-name">{{ doc.name }}</div>
|
<div class="tooltip-name">{{ doc.name }}</div>
|
||||||
<div class="tooltip-details">{{ doc.modified }} — {{ doc.sizedisp }}</div>
|
<div class="tooltip-details">{{ doc.modified }} — {{ doc.sizedisp }}</div>
|
||||||
|
<div v-if="doc.sparseIndicator" class="tooltip-sparse">{{ sparseText }}</div>
|
||||||
</CursorTooltip>
|
</CursorTooltip>
|
||||||
</a>
|
</a>
|
||||||
</template>
|
</template>
|
||||||
@@ -34,8 +35,10 @@
|
|||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import { Doc } from '@/repositories/Document'
|
import { Doc } from '@/repositories/Document'
|
||||||
|
import { formatSize } from '@/utils'
|
||||||
import MediaPreview from '@/components/MediaPreview.vue'
|
import MediaPreview from '@/components/MediaPreview.vue'
|
||||||
import CursorTooltip from './CursorTooltip.vue'
|
import CursorTooltip from './CursorTooltip.vue'
|
||||||
|
import SparseIndicator from './SparseIndicator.vue'
|
||||||
|
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
type EditingProp = {
|
type EditingProp = {
|
||||||
@@ -52,6 +55,11 @@ const tooltip = ref<InstanceType<typeof CursorTooltip> | null>(null)
|
|||||||
|
|
||||||
const tooltipText = computed(() => props.doc.key)
|
const tooltipText = computed(() => props.doc.key)
|
||||||
|
|
||||||
|
const sparseText = computed(() => {
|
||||||
|
const { size, allocated } = props.doc
|
||||||
|
return `${formatSize(allocated)} allocated of ${formatSize(size)}`
|
||||||
|
})
|
||||||
|
|
||||||
const onclick = (ev: Event) => {
|
const onclick = (ev: Event) => {
|
||||||
if (m.value!.play()) ev.preventDefault()
|
if (m.value!.play()) ev.preventDefault()
|
||||||
store.cursor = props.doc.key
|
store.cursor = props.doc.key
|
||||||
@@ -66,6 +74,13 @@ const onclick = (ev: Event) => {
|
|||||||
.tooltip-details {
|
.tooltip-details {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
.tooltip-sparse {
|
||||||
|
text-align: center;
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
.after-name {
|
||||||
|
margin-left: 0.3em;
|
||||||
|
}
|
||||||
figure {
|
figure {
|
||||||
max-height: 15em;
|
max-height: 15em;
|
||||||
position: relative;
|
position: relative;
|
||||||
|
|||||||
@@ -3,23 +3,24 @@
|
|||||||
<UploadButton :path="props.path" />
|
<UploadButton :path="props.path" />
|
||||||
<SvgButton
|
<SvgButton
|
||||||
name="create-folder"
|
name="create-folder"
|
||||||
data-tooltip="New folder"
|
tooltip="New folder"
|
||||||
@click="() => { console.log('New', store.fileExplorer); store.fileExplorer!.newFolder(); console.log('Done')}"
|
@click="() => { store.fileExplorer!.newFolder() }"
|
||||||
/>
|
/>
|
||||||
<slot></slot>
|
<div class="smallgap"></div>
|
||||||
<div class="spacer smallgap"></div>
|
<SvgButton name="eye" @click="store.prefs.gallery = !store.prefs.gallery" tooltip="Details/Gallery" />
|
||||||
<template v-if="showSearchInput">
|
<div class="search-group">
|
||||||
|
<SvgButton name="find" tabindex="-1" @click="focusSearch" tooltip="Search" />
|
||||||
<input
|
<input
|
||||||
ref="search"
|
ref="search"
|
||||||
type="search"
|
type="search"
|
||||||
:value="query"
|
:value="query"
|
||||||
@input="updateSearch"
|
@input="updateSearch"
|
||||||
placeholder="Find files"
|
@keydown.escape="clearSearch"
|
||||||
class="margin-input"
|
|
||||||
/>
|
/>
|
||||||
</template>
|
<span v-if="!query" class="search-hint" @click="focusSearch">{{ store.prefs.searchHotkey }}</span>
|
||||||
<SvgButton ref="searchButton" name="find" @click.prevent="toggleSearchInput" />
|
</div>
|
||||||
<SvgButton name="eye" @click="store.prefs.gallery = !store.prefs.gallery" />
|
<div class="spacer smallgap"></div>
|
||||||
|
<DiskSpace v-if="store.space.disk" />
|
||||||
<SvgButton name="cog" @click="settingsMenu" />
|
<SvgButton name="cog" @click="settingsMenu" />
|
||||||
</nav>
|
</nav>
|
||||||
</template>
|
</template>
|
||||||
@@ -27,58 +28,66 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import { useSsoAuthStore } from '@/stores/ssoAuth'
|
import { useSsoAuthStore } from '@/stores/ssoAuth'
|
||||||
import { ref, nextTick, watchEffect } from 'vue'
|
import { ref } from 'vue'
|
||||||
import ContextMenu from '@imengyu/vue3-context-menu'
|
import ContextMenu from '@imengyu/vue3-context-menu'
|
||||||
import { showAuthIframe } from 'paskia'
|
import { showAuthIframe } from 'paskia'
|
||||||
import { resumeWatching } from '@/repositories/WS'
|
import { resumeWatching } from '@/repositories/WS'
|
||||||
import router from '@/router';
|
import router from '@/router';
|
||||||
|
import DiskSpace from './DiskSpace.vue'
|
||||||
|
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
const ssoStore = useSsoAuthStore()
|
const ssoStore = useSsoAuthStore()
|
||||||
const showSearchInput = ref<boolean>(false)
|
|
||||||
const search = ref<HTMLInputElement | null>()
|
const search = ref<HTMLInputElement | null>()
|
||||||
const searchButton = ref<HTMLButtonElement | null>()
|
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
path: Array<string>
|
path: Array<string>
|
||||||
query: string
|
query: string
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const closeSearch = (ev: Event) => {
|
const clearSearch = (ev: Event) => {
|
||||||
if (!showSearchInput.value) return // Already closing
|
const input = search.value
|
||||||
showSearchInput.value = false
|
if (input) {
|
||||||
|
input.value = ''
|
||||||
|
updateSearch(ev)
|
||||||
|
}
|
||||||
const breadcrumb = document.querySelector('.breadcrumb') as HTMLElement
|
const breadcrumb = document.querySelector('.breadcrumb') as HTMLElement
|
||||||
breadcrumb.focus()
|
breadcrumb.focus()
|
||||||
updateSearch(ev)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const focusSearch = () => {
|
||||||
|
search.value?.focus()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Track pending route update
|
||||||
|
let pendingRouteUpdate: number | null = null
|
||||||
|
|
||||||
const updateSearch = (ev: Event) => {
|
const updateSearch = (ev: Event) => {
|
||||||
const q = (ev.target as HTMLInputElement).value
|
const q = (ev.target as HTMLInputElement).value
|
||||||
let p = props.path.join('/')
|
const loc = props.path.join('/')
|
||||||
p = p ? `/${p}` : ''
|
|
||||||
const url = q ? `${p}//${q}` : (p || '/')
|
|
||||||
const u = url.replaceAll('?', '%3F').replaceAll('#', '%23')
|
|
||||||
|
|
||||||
// Start search immediately via store (worker handles it async)
|
// Start search immediately via store (worker handles it async)
|
||||||
store.search(q, props.path.join('/'))
|
store.search(q, loc)
|
||||||
|
|
||||||
// Update route in next frame to keep typing responsive
|
// Cancel any pending route update
|
||||||
requestAnimationFrame(() => {
|
if (pendingRouteUpdate !== null) {
|
||||||
if (!props.query && q) router.push(u)
|
cancelAnimationFrame(pendingRouteUpdate)
|
||||||
else router.replace(u)
|
}
|
||||||
|
|
||||||
|
// Schedule route update - will be cancelled if user types again
|
||||||
|
pendingRouteUpdate = requestAnimationFrame(() => {
|
||||||
|
pendingRouteUpdate = null
|
||||||
|
let p = loc
|
||||||
|
p = p ? `/${p}` : ''
|
||||||
|
const url = q ? `${p}//${q}` : (p || '/')
|
||||||
|
const u = url.replaceAll('?', '%3F').replaceAll('#', '%23')
|
||||||
|
// Use replace to avoid building up history for each keystroke
|
||||||
|
router.replace(u)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const toggleSearchInput = (ev: Event) => {
|
|
||||||
showSearchInput.value = !showSearchInput.value
|
const toggleSearchInput = () => {
|
||||||
if (!showSearchInput.value) return closeSearch(ev)
|
search.value?.focus()
|
||||||
nextTick(() => {
|
|
||||||
const input = search.value
|
|
||||||
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 = []
|
||||||
@@ -106,7 +115,7 @@ const settingsMenu = (e: Event) => {
|
|||||||
// Show login option only in public mode (non-public modes trigger auth automatically)
|
// Show login option only in public mode (non-public modes trigger auth automatically)
|
||||||
items.push({ label: '🔐 Login', onClick: async () => {
|
items.push({ label: '🔐 Login', onClick: async () => {
|
||||||
try {
|
try {
|
||||||
await showAuthIframe('/auth/restricted#theme=light')
|
await showAuthIframe('/auth/restricted/#theme=light')
|
||||||
resumeWatching()
|
resumeWatching()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Login cancelled')
|
console.log('Login cancelled')
|
||||||
@@ -121,7 +130,7 @@ const settingsMenu = (e: Event) => {
|
|||||||
}
|
}
|
||||||
defineExpose({
|
defineExpose({
|
||||||
toggleSearchInput,
|
toggleSearchInput,
|
||||||
closeSearch,
|
clearSearch,
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -132,13 +141,76 @@ defineExpose({
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
z-index: 10;
|
z-index: 10;
|
||||||
|
min-height: 3em;
|
||||||
}
|
}
|
||||||
input[type='search'] {
|
.search-group {
|
||||||
background: var(--input-background);
|
position: relative;
|
||||||
color: var(--input-color);
|
display: flex;
|
||||||
border: 0;
|
align-items: center;
|
||||||
border-radius: 0.1em;
|
background: rgba(255, 255, 255, 0.1);
|
||||||
|
border-radius: 1.5em;
|
||||||
|
padding: 0 0.3em;
|
||||||
|
transition: background 0.2s ease;
|
||||||
|
flex: 1 1 auto;
|
||||||
|
min-width: 2.5em;
|
||||||
|
max-width: 20em;
|
||||||
|
}
|
||||||
|
.search-group:hover,
|
||||||
|
.search-group:focus-within {
|
||||||
|
background: rgba(255, 255, 255, 0.2);
|
||||||
|
}
|
||||||
|
.search-group:focus-within {
|
||||||
|
box-shadow: 0 0 0 2px var(--accent-color, #f80);
|
||||||
|
}
|
||||||
|
.search-group:hover :deep(button.action-button),
|
||||||
|
.search-group:focus-within :deep(button.action-button) {
|
||||||
|
transform: scale(1.1);
|
||||||
|
}
|
||||||
|
.search-group:hover :deep(button.action-button svg),
|
||||||
|
.search-group:focus-within :deep(button.action-button svg) {
|
||||||
|
fill: #fff;
|
||||||
|
}
|
||||||
|
.search-group:focus-within .search-hint {
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
.search-group :deep(.action-button) {
|
||||||
|
width: 2.2em;
|
||||||
|
height: 2.2em;
|
||||||
|
min-width: 1.5em;
|
||||||
|
min-height: 1.5em;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
.search-group input[type='search'] {
|
||||||
|
background: transparent;
|
||||||
|
color: var(--header-color);
|
||||||
|
border: none;
|
||||||
outline: none;
|
outline: none;
|
||||||
max-width: 15ch;
|
padding: 0.2em 0.5em 0.2em 0;
|
||||||
|
font-size: inherit;
|
||||||
|
flex: 1 1 3em;
|
||||||
|
min-width: 0;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.search-hint {
|
||||||
|
position: absolute;
|
||||||
|
right: 0.5em;
|
||||||
|
font-family: system-ui, sans-serif;
|
||||||
|
font-size: 1em;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #333;
|
||||||
|
background: #ccc;
|
||||||
|
border: 1px solid #999;
|
||||||
|
border-radius: 0.3em;
|
||||||
|
padding: 0 0.45em;
|
||||||
|
line-height: 1.4;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: opacity 0.15s ease;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
@media (hover: hover) and (pointer: fine) {
|
||||||
|
.search-hint {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
<template>
|
|
||||||
<template v-if="store.selected.size">
|
|
||||||
<div class="smallgap"></div>
|
|
||||||
<p class="select-text">{{ store.selected.size }} selected ➤</p>
|
|
||||||
<DownloadButton />
|
|
||||||
<SvgButton name="copy" data-tooltip="Copy here" @click="op('cp', dst)" />
|
|
||||||
<SvgButton name="paste" data-tooltip="Move here" @click="op('mv', dst)" />
|
|
||||||
<SvgButton name="trash" data-tooltip="Delete ⚠️" @click="op('rm')" />
|
|
||||||
<button class="action-button unselect" data-tooltip="Unselect all" @click="store.selected.clear()">❌</button>
|
|
||||||
</template>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script setup lang="ts">
|
|
||||||
import {connect, controlUrl} from '@/repositories/WS'
|
|
||||||
import { useMainStore } from '@/stores/main'
|
|
||||||
import { computed } from 'vue'
|
|
||||||
|
|
||||||
const store = useMainStore()
|
|
||||||
const props = defineProps({
|
|
||||||
path: Array<string>
|
|
||||||
})
|
|
||||||
|
|
||||||
const dst = computed(() => props.path!.join('/'))
|
|
||||||
const op = (op: string, dst?: string) => {
|
|
||||||
const sel = store.selectedFiles
|
|
||||||
const msg = {
|
|
||||||
op,
|
|
||||||
sel: sel.keys.map(key => {
|
|
||||||
const doc = sel.docs[key]!
|
|
||||||
return doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
|
||||||
})
|
|
||||||
}
|
|
||||||
// @ts-ignore
|
|
||||||
if (dst !== undefined) msg.dst = dst
|
|
||||||
const control = connect(controlUrl, {
|
|
||||||
message(ev: MessageEvent) {
|
|
||||||
const res = JSON.parse(ev.data)
|
|
||||||
if ('error' in res) {
|
|
||||||
console.error('Control socket error', msg, res.error)
|
|
||||||
store.error = res.error.message
|
|
||||||
return
|
|
||||||
} else if (res.status === 'ack') {
|
|
||||||
console.log('Control ack OK', res)
|
|
||||||
control.close()
|
|
||||||
store.selected.clear()
|
|
||||||
return
|
|
||||||
} else console.log('Unknown control response', msg, res)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
control.onopen = () => {
|
|
||||||
control.send(JSON.stringify(msg))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
.select-text {
|
|
||||||
color: var(--accent-color);
|
|
||||||
white-space: nowrap;
|
|
||||||
overflow: hidden;
|
|
||||||
text-overflow: ellipsis;
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
<template>
|
<template>
|
||||||
<img v-if=preview() :src="`${doc.previewurl}?${quality}&t=${doc.mtime}`" alt="">
|
<div v-if=showProgress() class="preview-progress" aria-label="Preview pending">
|
||||||
|
<SpinnerIcon />
|
||||||
|
</div>
|
||||||
|
<img v-else-if="previewSrc && !video() && !audio()" :src="previewSrc" alt="">
|
||||||
<img v-else-if=doc.img :src=doc.url alt="">
|
<img v-else-if=doc.img :src=doc.url alt="">
|
||||||
<span v-else-if=doc.dir class="folder icon"></span>
|
<span v-else-if=doc.dir class="folder icon"></span>
|
||||||
<div v-else-if=video() class="video-container">
|
<div v-else-if=video() class="video-container" :class="{ pending: !doc.complete }">
|
||||||
<video ref=vid :src=doc.url :poster=poster preload=none @play=onplay @pause=onpaused @ended=next @seeking=media!.play()></video>
|
<video v-if=doc.complete ref=vid :src=doc.url :poster=previewSrc preload=none @play=onplay @pause=onpaused @ended=next @seeking=media!.play()></video>
|
||||||
|
<video v-else ref=vid :src=doc.url preload=none @play=onplay @pause=onpaused @ended=next @seeking=media!.play()></video>
|
||||||
<div class="play-overlay"><PlayIcon /></div>
|
<div class="play-overlay"><PlayIcon /></div>
|
||||||
</div>
|
</div>
|
||||||
<div v-else-if=audio() class="audio icon">
|
<div v-else-if=audio() class="audio icon">
|
||||||
@@ -16,16 +20,16 @@
|
|||||||
<script setup lang=ts>
|
<script setup lang=ts>
|
||||||
import { computed, ref } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
import type { Doc } from '@/repositories/Document'
|
import type { Doc } from '@/repositories/Document'
|
||||||
import { Play as PlayIcon } from '@/assets/svg'
|
import { Play as PlayIcon, Spinner as SpinnerIcon } from '@/assets/svg'
|
||||||
|
|
||||||
const aud = ref<HTMLAudioElement | null>(null)
|
const aud = ref<HTMLAudioElement | null>(null)
|
||||||
const vid = ref<HTMLVideoElement | null>(null)
|
const vid = ref<HTMLVideoElement | null>(null)
|
||||||
const media = computed(() => aud.value || vid.value)
|
const media = computed(() => aud.value || vid.value)
|
||||||
const poster = computed(() => `${props.doc.previewurl}?${props.quality}&t=${props.doc.mtime}`)
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
doc: Doc
|
doc: Doc
|
||||||
quality: string
|
quality: string
|
||||||
}>()
|
}>()
|
||||||
|
const previewSrc = computed(() => props.doc.previewurl ? `${props.doc.previewurl}?${props.quality}&t=${props.doc.mtime}` : '')
|
||||||
|
|
||||||
const onplay = () => {
|
const onplay = () => {
|
||||||
if (!media.value) return
|
if (!media.value) return
|
||||||
@@ -37,6 +41,13 @@ const onpaused = () => {
|
|||||||
media.value.controls = false
|
media.value.controls = false
|
||||||
media.value.removeAttribute('data-playing')
|
media.value.removeAttribute('data-playing')
|
||||||
}
|
}
|
||||||
|
const applyPoster = (el: HTMLVideoElement) => {
|
||||||
|
if (props.doc.complete) {
|
||||||
|
el.poster = previewSrc.value
|
||||||
|
} else {
|
||||||
|
el.removeAttribute('poster')
|
||||||
|
}
|
||||||
|
}
|
||||||
let fscurrent: HTMLVideoElement | null = null
|
let fscurrent: HTMLVideoElement | null = null
|
||||||
const next = () => {
|
const next = () => {
|
||||||
if (!media.value) return
|
if (!media.value) return
|
||||||
@@ -70,7 +81,7 @@ const next = () => {
|
|||||||
if (!elem.paused) fscurrent.play()
|
if (!elem.paused) fscurrent.play()
|
||||||
fscurrent = null
|
fscurrent = null
|
||||||
elem.src = props.doc.url
|
elem.src = props.doc.url
|
||||||
elem.poster = poster.value
|
applyPoster(elem)
|
||||||
onpaused()
|
onpaused()
|
||||||
}, {once: true})
|
}, {once: true})
|
||||||
}
|
}
|
||||||
@@ -104,6 +115,7 @@ defineExpose({
|
|||||||
const video = () => ['mkv', 'mp4', 'webm', 'mov', 'avi'].includes(props.doc.ext)
|
const video = () => ['mkv', 'mp4', 'webm', 'mov', 'avi'].includes(props.doc.ext)
|
||||||
const audio = () => ['mp3', 'flac', 'ogg', 'aac'].includes(props.doc.ext)
|
const audio = () => ['mp3', 'flac', 'ogg', 'aac'].includes(props.doc.ext)
|
||||||
const archive = () => ['zip', 'tar', 'gz', 'bz2', 'xz', '7z', 'rar'].includes(props.doc.ext)
|
const archive = () => ['zip', 'tar', 'gz', 'bz2', 'xz', '7z', 'rar'].includes(props.doc.ext)
|
||||||
|
const showProgress = () => !props.doc.complete && (preview() || props.doc.img)
|
||||||
const preview = () => (
|
const preview = () => (
|
||||||
['bmp', 'ico', 'tif', 'tiff', 'heic', 'heif', 'pdf', 'epub', 'mobi'].includes(props.doc.ext) ||
|
['bmp', 'ico', 'tif', 'tiff', 'heic', 'heif', 'pdf', 'epub', 'mobi'].includes(props.doc.ext) ||
|
||||||
props.doc.size > 500000 &&
|
props.doc.size > 500000 &&
|
||||||
@@ -120,6 +132,29 @@ img, embed, .icon, audio, video {
|
|||||||
max-height: 100%;
|
max-height: 100%;
|
||||||
border-radius: calc(.5em / 8);
|
border-radius: calc(.5em / 8);
|
||||||
}
|
}
|
||||||
|
.preview-progress {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
min-width: 50%;
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 100%;
|
||||||
|
aspect-ratio: 1;
|
||||||
|
}
|
||||||
|
.preview-progress :deep(svg) {
|
||||||
|
width: 4.5em;
|
||||||
|
height: 4.5em;
|
||||||
|
opacity: 0.8;
|
||||||
|
animation: media-preview-spin 0.9s linear infinite;
|
||||||
|
}
|
||||||
|
@keyframes media-preview-spin {
|
||||||
|
from {
|
||||||
|
transform: rotate(0deg);
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
.folder::before {
|
.folder::before {
|
||||||
content: '📁';
|
content: '📁';
|
||||||
}
|
}
|
||||||
@@ -175,9 +210,14 @@ img::before {
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
min-width: 50%;
|
min-width: 50%;
|
||||||
|
min-height: 6em;
|
||||||
|
aspect-ratio: 16 / 9;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
max-height: 100%;
|
max-height: 100%;
|
||||||
}
|
}
|
||||||
|
.video-container.pending {
|
||||||
|
background: color-mix(in srgb, var(--header-bg) 55%, transparent);
|
||||||
|
}
|
||||||
.video-container video {
|
.video-container video {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
|||||||
@@ -0,0 +1,211 @@
|
|||||||
|
<template>
|
||||||
|
<div class="selection-bar" v-if="store.selected.size">
|
||||||
|
<div class="select-info">
|
||||||
|
<template v-if="selectionDisplay.folders.length <= 5">
|
||||||
|
<span class="select-folders">
|
||||||
|
<template v-for="(folder, i) in selectionDisplay.folders" :key="folder.path">
|
||||||
|
<span v-if="i > 0" class="folder-sep">, </span>
|
||||||
|
<a :href="'/#/' + folder.path" class="folder-link" @click.prevent="navigateTo(folder.path)">{{ folder.name }}</a>
|
||||||
|
</template>
|
||||||
|
</span>
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
<span class="select-count">{{ store.selected.size }} items from {{ selectionDisplay.numFolders }} folders</span>
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
|
<span class="select-size">{{ selectionDisplay.size }}</span>
|
||||||
|
<DownloadButton />
|
||||||
|
<SvgButton name="copy" tooltip="Copy here" @click="op('cp', dst)" />
|
||||||
|
<SvgButton name="paste" tooltip="Move here" @click="op('mv', dst)" />
|
||||||
|
<SvgButton name="trash" tooltip="Delete ⚠️" @click="op('rm')" />
|
||||||
|
<button
|
||||||
|
class="action-button unselect"
|
||||||
|
@click="store.selected.clear()"
|
||||||
|
@mouseenter="unselectTooltip?.startHover"
|
||||||
|
@mousemove="unselectTooltip?.updatePosition"
|
||||||
|
@mouseleave="unselectTooltip?.endHover"
|
||||||
|
>✖ selection</button>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import {connect, controlUrl} from '@/repositories/WS'
|
||||||
|
import { useMainStore } from '@/stores/main'
|
||||||
|
import { computed, ref } from 'vue'
|
||||||
|
import { formatSize } from '@/utils'
|
||||||
|
import CursorTooltip from './CursorTooltip.vue'
|
||||||
|
import router from '@/router'
|
||||||
|
|
||||||
|
const unselectTooltip = ref<InstanceType<typeof CursorTooltip> | null>(null)
|
||||||
|
|
||||||
|
const store = useMainStore()
|
||||||
|
const props = defineProps({
|
||||||
|
path: Array<string>
|
||||||
|
})
|
||||||
|
|
||||||
|
const dst = computed(() => props.path!.join('/'))
|
||||||
|
|
||||||
|
const navigateTo = (path: string) => {
|
||||||
|
router.push('/' + path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Truncate long names to reasonable length
|
||||||
|
const truncateName = (name: string, maxLen = 20): string => {
|
||||||
|
if (name.length <= maxLen) return name
|
||||||
|
return name.slice(0, maxLen - 1) + '…'
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FolderInfo {
|
||||||
|
name: string
|
||||||
|
path: string
|
||||||
|
count: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SelectionDisplay {
|
||||||
|
folders: FolderInfo[]
|
||||||
|
numFolders: number
|
||||||
|
size: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectionDisplay = computed<SelectionDisplay>(() => {
|
||||||
|
const sel = store.selectedFiles
|
||||||
|
|
||||||
|
// Calculate total size
|
||||||
|
const totalSize = sel.keys.reduce((sum, key) => {
|
||||||
|
const doc = sel.docs[key]
|
||||||
|
return sum + (doc ? doc.size : 0)
|
||||||
|
}, 0)
|
||||||
|
const sizeStr = formatSize(totalSize)
|
||||||
|
|
||||||
|
// Group by folder location, storing file names
|
||||||
|
const folderGroups = new Map<string, string[]>()
|
||||||
|
for (const key of sel.keys) {
|
||||||
|
const doc = sel.docs[key]
|
||||||
|
if (!doc) continue
|
||||||
|
const loc = doc.loc || ''
|
||||||
|
if (!folderGroups.has(loc)) folderGroups.set(loc, [])
|
||||||
|
folderGroups.get(loc)!.push(doc.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
const numFolders = folderGroups.size
|
||||||
|
|
||||||
|
const folders = Array.from(folderGroups.entries())
|
||||||
|
.sort((a, b) => a[0].localeCompare(b[0]))
|
||||||
|
.map(([loc, names]) => {
|
||||||
|
const count = names.length
|
||||||
|
// For single file, display truncated filename; for multiple, display folder name with count
|
||||||
|
let displayName: string
|
||||||
|
if (count === 1) {
|
||||||
|
displayName = truncateName(names[0]!)
|
||||||
|
} else {
|
||||||
|
const folderName = loc ? loc.split('/').pop()! : (store.server.name || 'Root')
|
||||||
|
displayName = `${truncateName(folderName)} (${count})`
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
name: displayName,
|
||||||
|
path: loc,
|
||||||
|
count
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
folders,
|
||||||
|
numFolders,
|
||||||
|
size: sizeStr
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const op = (opName: string, dst?: string) => {
|
||||||
|
const sel = store.selectedFiles
|
||||||
|
const paths = sel.keys.map(key => {
|
||||||
|
const doc = sel.docs[key]!
|
||||||
|
return doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
})
|
||||||
|
const msg = {
|
||||||
|
op: opName,
|
||||||
|
sel: paths
|
||||||
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
if (dst !== undefined) msg.dst = dst
|
||||||
|
// Hide items being deleted or moved (optimistic update)
|
||||||
|
if (opName === 'rm' || opName === 'mv') {
|
||||||
|
for (const path of paths) store.hideDoc(path)
|
||||||
|
}
|
||||||
|
const control = connect(controlUrl, {
|
||||||
|
message(ev: MessageEvent) {
|
||||||
|
const res = JSON.parse(ev.data)
|
||||||
|
if ('error' in res) {
|
||||||
|
console.error('Control socket error', msg, res.error)
|
||||||
|
store.error = res.error.message
|
||||||
|
// Restore hidden items on error
|
||||||
|
if (opName === 'rm' || opName === 'mv') {
|
||||||
|
for (const path of paths) store.unhideDoc(path)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else if (res.status === 'ack') {
|
||||||
|
console.log('Control ack OK', res)
|
||||||
|
control.close()
|
||||||
|
store.selected.clear()
|
||||||
|
return
|
||||||
|
} else console.log('Unknown control response', msg, res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
control.onopen = () => {
|
||||||
|
control.send(JSON.stringify(msg))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.selection-bar {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 0.3em 0.5em;
|
||||||
|
background: transparent;
|
||||||
|
color: var(--header-color);
|
||||||
|
font-size: var(--header-font-size);
|
||||||
|
gap: 0.3em;
|
||||||
|
flex-wrap: nowrap;
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
.select-info {
|
||||||
|
color: var(--accent-color);
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
margin: 0;
|
||||||
|
flex-shrink: 1;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
.select-count {
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
.select-folders {
|
||||||
|
display: inline;
|
||||||
|
}
|
||||||
|
.folder-link,
|
||||||
|
.folder-link:link,
|
||||||
|
.folder-link:visited,
|
||||||
|
.folder-link:active {
|
||||||
|
color: var(--accent-color);
|
||||||
|
text-decoration: none;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.folder-link:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
color: var(--accent-color);
|
||||||
|
}
|
||||||
|
.folder-sep {
|
||||||
|
color: var(--header-color);
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
.select-size {
|
||||||
|
color: var(--header-color);
|
||||||
|
opacity: 0.8;
|
||||||
|
font-family: 'Roboto Mono', monospace;
|
||||||
|
font-size: 0.9em;
|
||||||
|
margin-left: 0.5em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<template>
|
||||||
|
<span v-if="doc.sparseIndicator" class="sparse-indicator">{{ doc.sparseIndicator }}</span>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { Doc } from '@/repositories/Document'
|
||||||
|
|
||||||
|
defineProps<{
|
||||||
|
doc: Doc
|
||||||
|
}>()
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.sparse-indicator {
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -1,16 +1,30 @@
|
|||||||
<template>
|
<template>
|
||||||
<button class="action-button">
|
<button
|
||||||
|
class="action-button"
|
||||||
|
:tabindex="tabindex"
|
||||||
|
@mouseenter="tooltip?.startHover"
|
||||||
|
@mousemove="tooltip?.updatePosition"
|
||||||
|
@mouseleave="tooltip?.endHover"
|
||||||
|
>
|
||||||
<component :is="icons[name]" />
|
<component :is="icons[name]" />
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
|
<CursorTooltip v-if="tooltipText" ref="tooltip" :text="tooltipText">{{ tooltipText }}</CursorTooltip>
|
||||||
</button>
|
</button>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { icons, type IconName } from '@/assets/svg'
|
import { icons, type IconName } from '@/assets/svg'
|
||||||
|
import { ref } from 'vue'
|
||||||
|
import CursorTooltip from './CursorTooltip.vue'
|
||||||
|
|
||||||
defineProps<{
|
const props = defineProps<{
|
||||||
name: IconName
|
name: IconName
|
||||||
|
tooltip?: string
|
||||||
|
tabindex?: string | number
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
|
const tooltip = ref<InstanceType<typeof CursorTooltip> | null>(null)
|
||||||
|
const tooltipText = props.tooltip ?? ''
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
@@ -20,18 +34,29 @@ defineProps<{
|
|||||||
color: #ccc;
|
color: #ccc;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: all 0.2s ease;
|
transition: all 0.2s ease;
|
||||||
padding: 0.2em;
|
margin: 0 0.2em;
|
||||||
width: 3em;
|
padding: 0;
|
||||||
height: 3em;
|
width: 2.7em;
|
||||||
|
height: 2.7em;
|
||||||
|
min-width: 1.9em;
|
||||||
|
min-height: 1.9em;
|
||||||
|
flex-shrink: 0;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
}
|
}
|
||||||
.action-button:hover,
|
.action-button:hover,
|
||||||
.action-button:focus {
|
.action-button:focus {
|
||||||
color: #fff;
|
color: #fff;
|
||||||
transform: scale(1.1);
|
transform: scale(1.1);
|
||||||
}
|
}
|
||||||
svg {
|
.action-button svg {
|
||||||
fill: #ccc;
|
fill: #ccc;
|
||||||
transform: fill 0.2s ease;
|
transition: fill 0.2s ease;
|
||||||
|
width: auto;
|
||||||
|
height: auto;
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 100%;
|
||||||
}
|
}
|
||||||
.action-button:hover svg,
|
.action-button:hover svg,
|
||||||
.action-button:focus svg {
|
.action-button:focus svg {
|
||||||
|
|||||||
@@ -53,15 +53,17 @@ const speeddisp = computed(() => speed.value ? speed.value.toFixed(speed.value <
|
|||||||
<style scoped>
|
<style scoped>
|
||||||
.transferprogress {
|
.transferprogress {
|
||||||
--bar: var(--accent-color);
|
--bar: var(--accent-color);
|
||||||
--nobar: var(--header-background);
|
--nobar: transparent;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
color: var(--primary-color);
|
color: var(--primary-color);
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
.statustext {
|
.statustext {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
margin: 0 .5em;
|
margin: 0 .5em;
|
||||||
padding: 0.5rem 0;
|
padding: 0.5rem 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,19 @@
|
|||||||
<input ref="fileInput" @change="uploadHandler" type="file" multiple>
|
<input ref="fileInput" @change="uploadHandler" type="file" multiple>
|
||||||
<input ref="folderInput" @change="uploadHandler" type="file" webkitdirectory>
|
<input ref="folderInput" @change="uploadHandler" type="file" webkitdirectory>
|
||||||
</template>
|
</template>
|
||||||
<SvgButton name="add-file" data-tooltip="Upload files" @click="fileInput.click()" />
|
<SvgButton name="add-file" tooltip="Upload files" @click="fileInput.click()" />
|
||||||
<SvgButton name="add-folder" data-tooltip="Upload folder" @click="folderInput.click()" />
|
<SvgButton name="add-folder" tooltip="Upload folder" @click="folderInput.click()" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { connect, uploadUrl } from '@/repositories/WS';
|
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
|
import { getDocuments } from '@/stores/documentStore'
|
||||||
|
import { Doc } from '@/repositories/Document'
|
||||||
import { collator } from '@/utils';
|
import { collator } from '@/utils';
|
||||||
import { onMounted, onUnmounted, reactive, ref } from 'vue'
|
import { onMounted, onUnmounted, ref } from 'vue'
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
|
|
||||||
|
const router = useRouter()
|
||||||
const fileInput = ref()
|
const fileInput = ref()
|
||||||
const folderInput = ref()
|
const folderInput = ref()
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
@@ -25,6 +28,22 @@ type CloudFile = {
|
|||||||
cloudName: string
|
cloudName: string
|
||||||
cloudPos: number
|
cloudPos: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UploadRange = {
|
||||||
|
name: string
|
||||||
|
size: number
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
}
|
||||||
|
|
||||||
|
type InflightBlock = {
|
||||||
|
name: string
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
startedAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const UPLOAD_BLOCK_SIZE = 16 << 20 // 16 MiB
|
||||||
function pasteHandler(event: ClipboardEvent) {
|
function pasteHandler(event: ClipboardEvent) {
|
||||||
const items = Array.from(event.clipboardData?.items ?? [])
|
const items = Array.from(event.clipboardData?.items ?? [])
|
||||||
const infiles = [] as File[]
|
const infiles = [] as File[]
|
||||||
@@ -42,7 +61,8 @@ function pasteHandler(event: ClipboardEvent) {
|
|||||||
if (infiles.length || dirs.length) {
|
if (infiles.length || dirs.length) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
uploadFiles(infiles)
|
uploadFiles(infiles)
|
||||||
for (const entry of dirs) pasteDirectory(entry, `${props.path!.join('/')}/${entry.name}`)
|
const base = props.path!.join('/')
|
||||||
|
for (const entry of dirs) pasteDirectory(entry, `${base ? `${base}/` : ''}${entry.name}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const pasteDirectory = async (entry: FileSystemDirectoryEntry, loc: string) => {
|
const pasteDirectory = async (entry: FileSystemDirectoryEntry, loc: string) => {
|
||||||
@@ -72,24 +92,53 @@ function uploadHandler(event: Event) {
|
|||||||
const uploadFiles = (infiles: File[]) => {
|
const uploadFiles = (infiles: File[]) => {
|
||||||
const loc = props.path!.join('/')
|
const loc = props.path!.join('/')
|
||||||
let files = []
|
let files = []
|
||||||
|
let folderName = ''
|
||||||
for (const file of infiles) {
|
for (const file of infiles) {
|
||||||
|
const relPath = file.webkitRelativePath || file.name
|
||||||
|
if (!folderName && file.webkitRelativePath) folderName = relPath.split('/')[0] ?? ''
|
||||||
files.push({
|
files.push({
|
||||||
file,
|
file,
|
||||||
cloudName: loc + '/' + (file.webkitRelativePath || file.name),
|
cloudName: `${loc ? `${loc}/` : ''}${relPath}`,
|
||||||
cloudPos: 0,
|
cloudPos: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
uploadCloudFiles(files)
|
uploadCloudFiles(files)
|
||||||
|
if (folderName) router.push('/' + (loc ? loc + '/' : '') + folderName + '/')
|
||||||
}
|
}
|
||||||
const uploadCloudFiles = (files: CloudFile[]) => {
|
const uploadCloudFiles = (files: CloudFile[]) => {
|
||||||
const dotfiles = files.filter(f => f.cloudName.includes('/.'))
|
const dotfiles = files.filter(f => f.cloudName.includes('/.'))
|
||||||
if (dotfiles.length) {
|
if (dotfiles.length) {
|
||||||
store.showToast("Won't upload dotfiles")
|
store.showToast("Won't upload dotfiles")
|
||||||
console.log("Dotfiles omitted", dotfiles)
|
|
||||||
files = files.filter(f => !f.cloudName.includes('/.'))
|
files = files.filter(f => !f.cloudName.includes('/.'))
|
||||||
}
|
}
|
||||||
if (!files.length) return
|
if (!files.length) return
|
||||||
files.sort((a, b) => collator.compare(a.cloudName, b.cloudName))
|
files.sort((a, b) => collator.compare(a.cloudName, b.cloudName))
|
||||||
|
// Optimistic update: ghost folders and files
|
||||||
|
const now = Math.floor(Date.now() / 1000)
|
||||||
|
const docs = getDocuments()
|
||||||
|
const byPath = new Map(docs.map(d => [d.loc ? `${d.loc}/${d.name}` : d.name, d]))
|
||||||
|
// Also check existing ghosts
|
||||||
|
for (const g of store.ghosts) {
|
||||||
|
byPath.set(g.loc ? `${g.loc}/${g.name}` : g.name, g)
|
||||||
|
}
|
||||||
|
const added = new Set<string>()
|
||||||
|
for (const f of files) {
|
||||||
|
const lastSlash = f.cloudName.lastIndexOf('/')
|
||||||
|
const loc = lastSlash > 0 ? f.cloudName.slice(0, lastSlash) : ''
|
||||||
|
const name = f.cloudName.slice(lastSlash + 1)
|
||||||
|
// Ghost folders for intermediate directories
|
||||||
|
const parts = loc.split('/')
|
||||||
|
for (let i = 0; i < parts.length; i++) {
|
||||||
|
const folderPath = parts.slice(0, i + 1).join('/')
|
||||||
|
if (folderPath && !byPath.has(folderPath) && !added.has(folderPath)) {
|
||||||
|
store.addGhost(new Doc({ loc: parts.slice(0, i).join('/'), name: parts[i], key: crypto.randomUUID(), size: 0, allocated: 0, mtime: now, dir: true }))
|
||||||
|
added.add(folderPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Ghost file or update existing (overwrite case doesn't need ghost, file already visible)
|
||||||
|
const existing = byPath.get(f.cloudName)
|
||||||
|
if (!existing) store.addGhost(new Doc({ loc, name, key: crypto.randomUUID(), size: f.file.size, allocated: 0, mtime: now, dir: false }))
|
||||||
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
upqueue = [...upqueue, ...files]
|
upqueue = [...upqueue, ...files]
|
||||||
statsAdd(files)
|
statsAdd(files)
|
||||||
@@ -97,7 +146,12 @@ const uploadCloudFiles = (files: CloudFile[]) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cancelUploads = () => {
|
const cancelUploads = () => {
|
||||||
|
uploadRunId += 1
|
||||||
upqueue = []
|
upqueue = []
|
||||||
|
blockQueue = []
|
||||||
|
inflightBlocks.clear()
|
||||||
|
uploadedBytes.clear()
|
||||||
|
store.uprogress.status = 'idle'
|
||||||
statReset()
|
statReset()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,27 +172,100 @@ const uprogress_init = {
|
|||||||
status: 'idle',
|
status: 'idle',
|
||||||
}
|
}
|
||||||
store.uprogress = {...uprogress_init}
|
store.uprogress = {...uprogress_init}
|
||||||
|
// Track uploaded bytes for each file to handle out-of-order uploads
|
||||||
|
const uploadedBytes = new Map<string, Set<number>>()
|
||||||
|
const inflightBlocks = new Map<string, InflightBlock>()
|
||||||
|
let smoothedBlockMs = 1500
|
||||||
|
let lastProgressTick = Date.now()
|
||||||
|
let lastVisualUploaded = 0
|
||||||
|
|
||||||
|
const inflightKey = (name: string, start: number) => `${name}:${start}`
|
||||||
|
|
||||||
|
const completedUploadedBytes = (name: string, size: number) => {
|
||||||
|
const uploaded = uploadedBytes.get(name)
|
||||||
|
if (!uploaded) return 0
|
||||||
|
const blockSize = UPLOAD_BLOCK_SIZE
|
||||||
|
let total = 0
|
||||||
|
for (let i = 0; i < size; i += blockSize) {
|
||||||
|
if (uploaded.has(i)) total += Math.min(blockSize, size - i)
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
const simulatedInflightBytes = (name: string, now: number) => {
|
||||||
|
let total = 0
|
||||||
|
for (const block of inflightBlocks.values()) {
|
||||||
|
if (block.name !== name) continue
|
||||||
|
const size = block.end - block.start
|
||||||
|
const elapsed = Math.max(0, now - block.startedAt)
|
||||||
|
const fraction = Math.min(0.98, elapsed / Math.max(200, smoothedBlockMs))
|
||||||
|
total += size * fraction
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
const refreshProgress = (now: number) => {
|
||||||
|
const name = store.uprogress.filename
|
||||||
|
const size = store.uprogress.filesize
|
||||||
|
if (!name || !size) {
|
||||||
|
lastProgressTick = now
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
const completed = completedUploadedBytes(name, size)
|
||||||
|
const estimated = simulatedInflightBytes(name, now)
|
||||||
|
const visualUploaded = Math.min(size, Math.round(completed + estimated))
|
||||||
|
const delta = Math.max(0, visualUploaded - lastVisualUploaded)
|
||||||
|
const dt = Math.max(1, now - lastProgressTick)
|
||||||
|
|
||||||
|
store.uprogress.filepos = visualUploaded
|
||||||
|
store.uprogress.xfer = store.uprogress.filestart + visualUploaded
|
||||||
|
|
||||||
|
if (delta > 0) {
|
||||||
|
store.uprogress.statbytes += delta
|
||||||
|
store.uprogress.statdur += dt
|
||||||
|
store.uprogress.tlast = now
|
||||||
|
}
|
||||||
|
|
||||||
|
lastVisualUploaded = visualUploaded
|
||||||
|
lastProgressTick = now
|
||||||
|
return delta
|
||||||
|
}
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
if (Date.now() - store.uprogress.tlast > 3000) {
|
const now = Date.now()
|
||||||
// Reset
|
const delta = refreshProgress(now)
|
||||||
|
if (delta > 0) return
|
||||||
|
if (now - store.uprogress.tlast > 3000) {
|
||||||
store.uprogress.statbytes = 0
|
store.uprogress.statbytes = 0
|
||||||
store.uprogress.statdur = 1
|
store.uprogress.statdur = 1
|
||||||
} else {
|
} else {
|
||||||
// Running average by decay
|
store.uprogress.statbytes *= .95
|
||||||
store.uprogress.statbytes *= .9
|
store.uprogress.statdur *= .95
|
||||||
store.uprogress.statdur *= .9
|
|
||||||
}
|
}
|
||||||
}, 100)
|
}, 100)
|
||||||
const statUpdate = ({name, size, start, end}: {name: string, size: number, start: number, end: number}) => {
|
|
||||||
|
const statUpdate = ({name, size, start, end}: UploadRange) => {
|
||||||
if (name !== store.uprogress.filename) return // If stats have been reset
|
if (name !== store.uprogress.filename) return // If stats have been reset
|
||||||
const now = Date.now()
|
|
||||||
store.uprogress.xfer = store.uprogress.filestart + end
|
// Track which bytes have been uploaded (using start to end range)
|
||||||
store.uprogress.filepos = end
|
if (!uploadedBytes.has(name)) uploadedBytes.set(name, new Set())
|
||||||
store.uprogress.statbytes += end - start
|
const uploaded = uploadedBytes.get(name)!
|
||||||
store.uprogress.statdur += now - store.uprogress.tlast
|
const blockSize = UPLOAD_BLOCK_SIZE
|
||||||
store.uprogress.tlast = now
|
|
||||||
// File finished?
|
// Mark all bytes in this block as uploaded
|
||||||
if (end === size) {
|
for (let i = start; i < end; i += blockSize) {
|
||||||
|
uploaded.add(i)
|
||||||
|
}
|
||||||
|
refreshProgress(Date.now())
|
||||||
|
|
||||||
|
// Check if file is fully uploaded by examining the block queue
|
||||||
|
const currentUpload = blockQueue[0]
|
||||||
|
if (!currentUpload) return
|
||||||
|
|
||||||
|
if (currentUpload.file.cloudName === name && currentUpload.completed >= currentUpload.blocks.length) {
|
||||||
|
// All blocks for this file have been uploaded
|
||||||
|
uploadedBytes.delete(name) // Clean up tracking
|
||||||
store.uprogress.filestart += size
|
store.uprogress.filestart += size
|
||||||
statNextFile()
|
statNextFile()
|
||||||
if (++store.uprogress.fileidx >= store.uprogress.filecount) statReset()
|
if (++store.uprogress.fileidx >= store.uprogress.filecount) statReset()
|
||||||
@@ -150,11 +277,15 @@ const statNextFile = () => {
|
|||||||
store.uprogress.filepos = 0
|
store.uprogress.filepos = 0
|
||||||
store.uprogress.filesize = f.file.size
|
store.uprogress.filesize = f.file.size
|
||||||
store.uprogress.filename = f.cloudName
|
store.uprogress.filename = f.cloudName
|
||||||
|
lastVisualUploaded = 0
|
||||||
|
lastProgressTick = Date.now()
|
||||||
}
|
}
|
||||||
const statReset = () => {
|
const statReset = () => {
|
||||||
Object.assign(store.uprogress, uprogress_init)
|
Object.assign(store.uprogress, uprogress_init)
|
||||||
store.uprogress.t0 = Date.now()
|
store.uprogress.t0 = Date.now()
|
||||||
store.uprogress.tlast = store.uprogress.t0 + 1
|
store.uprogress.tlast = store.uprogress.t0 + 1
|
||||||
|
lastVisualUploaded = 0
|
||||||
|
lastProgressTick = store.uprogress.t0
|
||||||
}
|
}
|
||||||
const statsAdd = (f: CloudFile[]) => {
|
const statsAdd = (f: CloudFile[]) => {
|
||||||
if (store.uprogress.files.length === 0) statReset()
|
if (store.uprogress.files.length === 0) statReset()
|
||||||
@@ -164,69 +295,201 @@ const statsAdd = (f: CloudFile[]) => {
|
|||||||
statNextFile()
|
statNextFile()
|
||||||
}
|
}
|
||||||
let upqueue = [] as CloudFile[]
|
let upqueue = [] as CloudFile[]
|
||||||
|
const MAX_PARALLEL_REQUESTS = 4
|
||||||
|
const RETRY_DELAY_MS = 400
|
||||||
|
|
||||||
// TODO: Rewrite as WebSocket class
|
// Helper function to get upload blocks for a file, prioritizing final 4 blocks if file >= 32 MiB
|
||||||
const WSCreate = async () => await new Promise<WebSocket>(resolve => {
|
const getUploadBlocks = (file: CloudFile): {start: number, end: number}[] => {
|
||||||
const ws = connect(uploadUrl, {
|
const BLOCK_SIZE = UPLOAD_BLOCK_SIZE
|
||||||
open(ev: Event) { resolve(ws) },
|
const MIN_SIZE_FOR_REORDER = 32 * BLOCK_SIZE // 32 MiB = 33554432 bytes
|
||||||
error(ev: Event) {
|
const FINAL_BLOCKS_COUNT = 2
|
||||||
console.error('Upload socket error', ev)
|
|
||||||
store.error = 'Upload socket error'
|
const fileSize = file.file.size
|
||||||
},
|
const blocks: {start: number, end: number}[] = []
|
||||||
message(ev: MessageEvent) {
|
|
||||||
const res = JSON.parse(ev!.data)
|
if (fileSize >= MIN_SIZE_FOR_REORDER) {
|
||||||
if ('error' in res) {
|
// File is large enough, prioritize final blocks
|
||||||
console.error('Upload socket error', res.error)
|
const finalBlocksStart = fileSize - (FINAL_BLOCKS_COUNT * BLOCK_SIZE)
|
||||||
store.error = res.error.message
|
|
||||||
return
|
// Add final blocks first
|
||||||
}
|
for (let i = 0; i < FINAL_BLOCKS_COUNT; i++) {
|
||||||
if (res.status === 'ack') {
|
const start = finalBlocksStart + (i * BLOCK_SIZE)
|
||||||
statUpdate(res.req)
|
const end = Math.min(start + BLOCK_SIZE, fileSize)
|
||||||
} else console.log('Unknown upload response', res)
|
blocks.push({start, end})
|
||||||
},
|
}
|
||||||
})
|
|
||||||
// @ts-ignore
|
// Add remaining blocks from beginning
|
||||||
ws.sendMsg = (msg: any) => ws.send(JSON.stringify(msg))
|
for (let start = 0; start < finalBlocksStart; start += BLOCK_SIZE) {
|
||||||
// @ts-ignore
|
const end = Math.min(start + BLOCK_SIZE, finalBlocksStart)
|
||||||
ws.sendData = async (data: any) => {
|
blocks.push({start, end})
|
||||||
// Wait until the WS is ready to send another message
|
}
|
||||||
store.uprogress.status = "uploading"
|
} else {
|
||||||
await new Promise(resolve => {
|
// File is smaller, use sequential upload
|
||||||
const t = setInterval(() => {
|
for (let start = 0; start < fileSize; start += BLOCK_SIZE) {
|
||||||
if (ws.bufferedAmount > 1<<20) return
|
const end = Math.min(start + BLOCK_SIZE, fileSize)
|
||||||
resolve(undefined)
|
blocks.push({start, end})
|
||||||
clearInterval(t)
|
}
|
||||||
}, 1)
|
|
||||||
})
|
|
||||||
store.uprogress.status = "processing"
|
|
||||||
ws.send(data)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
const worker = async () => {
|
return blocks
|
||||||
const ws = await WSCreate()
|
|
||||||
while (upqueue.length) {
|
|
||||||
const f = upqueue[0]!
|
|
||||||
const start = f.cloudPos
|
|
||||||
const end = Math.min(f.file.size, start + (1<<20))
|
|
||||||
const control = { name: f.cloudName, size: f.file.size, start, end }
|
|
||||||
const data = f.file.slice(start, end)
|
|
||||||
f.cloudPos = end
|
|
||||||
// Note: files may get modified during I/O
|
|
||||||
// @ts-ignore FIXME proper WebSocket class, avoid attaching functions to WebSocket object
|
|
||||||
ws.sendMsg(control)
|
|
||||||
// @ts-ignore
|
|
||||||
await ws.sendData(data)
|
|
||||||
if (f.cloudPos === f.file.size) upqueue.shift()
|
|
||||||
}
|
|
||||||
if (upqueue.length) startWorker()
|
|
||||||
store.uprogress.status = "idle"
|
|
||||||
workerRunning = false
|
|
||||||
}
|
}
|
||||||
let workerRunning: any = false
|
|
||||||
|
type BlockUpload = {
|
||||||
|
file: CloudFile
|
||||||
|
blocks: {start: number, end: number}[]
|
||||||
|
nextIndex: number
|
||||||
|
completed: number
|
||||||
|
runId: number
|
||||||
|
}
|
||||||
|
|
||||||
|
let blockQueue = [] as BlockUpload[]
|
||||||
|
let workerRunning = false
|
||||||
|
let uploadRunId = 0
|
||||||
|
|
||||||
|
const enqueuePendingUploads = () => {
|
||||||
|
while (upqueue.length) {
|
||||||
|
const file = upqueue.shift()!
|
||||||
|
const blocks = getUploadBlocks(file)
|
||||||
|
blockQueue.push({ file, blocks, nextIndex: 0, completed: 0, runId: uploadRunId })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const uploadUrlForFile = (cloudName: string) => {
|
||||||
|
const normalized = cloudName.replace(/^\/+/, '')
|
||||||
|
const encoded = normalized.split('/').map(encodeURIComponent).join('/')
|
||||||
|
return `/files/${encoded}`
|
||||||
|
}
|
||||||
|
|
||||||
|
const uploadBlock = async (upload: BlockUpload, block: {start: number, end: number}) => {
|
||||||
|
const body = upload.file.file.slice(block.start, block.end)
|
||||||
|
const range = `bytes ${block.start}-${block.end - 1}/${upload.file.file.size}`
|
||||||
|
const fallbackReq = {
|
||||||
|
name: upload.file.cloudName,
|
||||||
|
size: upload.file.file.size,
|
||||||
|
start: block.start,
|
||||||
|
end: block.end,
|
||||||
|
}
|
||||||
|
let attempt = 0
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
attempt += 1
|
||||||
|
if (upload.runId !== uploadRunId) throw new Error('Upload cancelled')
|
||||||
|
try {
|
||||||
|
const res = await fetch(uploadUrlForFile(upload.file.cloudName), {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/octet-stream',
|
||||||
|
'Content-Range': range,
|
||||||
|
},
|
||||||
|
body,
|
||||||
|
})
|
||||||
|
if (!res.ok) {
|
||||||
|
const message = await res.text().catch(() => '')
|
||||||
|
const retryable = res.status >= 500 || res.status === 408 || res.status === 429
|
||||||
|
if (!retryable) throw new Error(message || `HTTP ${res.status}`)
|
||||||
|
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const payload = await res.json().catch(() => null)
|
||||||
|
return payload?.status === 'ack' && payload.req ? payload.req : fallbackReq
|
||||||
|
} catch (err: any) {
|
||||||
|
const message = err instanceof Error ? err.message : String(err)
|
||||||
|
if (message === 'Upload cancelled') throw err
|
||||||
|
if (upload.runId !== uploadRunId) throw new Error('Upload cancelled')
|
||||||
|
if (attempt % 10 === 0) {
|
||||||
|
console.warn(`Upload retry ${attempt} for ${upload.file.cloudName}: ${message}`)
|
||||||
|
}
|
||||||
|
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const startInflightBlock = (name: string, block: {start: number, end: number}) => {
|
||||||
|
inflightBlocks.set(inflightKey(name, block.start), {
|
||||||
|
name,
|
||||||
|
start: block.start,
|
||||||
|
end: block.end,
|
||||||
|
startedAt: Date.now(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const finishInflightBlock = (name: string, block: {start: number, end: number}) => {
|
||||||
|
const key = inflightKey(name, block.start)
|
||||||
|
const info = inflightBlocks.get(key)
|
||||||
|
if (!info) return
|
||||||
|
const elapsed = Math.max(1, Date.now() - info.startedAt)
|
||||||
|
smoothedBlockMs = smoothedBlockMs * 0.85 + elapsed * 0.15
|
||||||
|
inflightBlocks.delete(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
const worker = async (runId: number) => {
|
||||||
|
try {
|
||||||
|
while (runId === uploadRunId) {
|
||||||
|
enqueuePendingUploads()
|
||||||
|
if (!blockQueue.length) break
|
||||||
|
|
||||||
|
const upload = blockQueue[0]!
|
||||||
|
const inflight = new Set<Promise<void>>()
|
||||||
|
|
||||||
|
while (runId === uploadRunId && upload.completed < upload.blocks.length) {
|
||||||
|
while (
|
||||||
|
runId === uploadRunId
|
||||||
|
&& upload.nextIndex < upload.blocks.length
|
||||||
|
&& inflight.size < MAX_PARALLEL_REQUESTS
|
||||||
|
) {
|
||||||
|
const block = upload.blocks[upload.nextIndex++]!
|
||||||
|
store.uprogress.status = 'uploading'
|
||||||
|
startInflightBlock(upload.file.cloudName, block)
|
||||||
|
let task: Promise<void>
|
||||||
|
task = uploadBlock(upload, block)
|
||||||
|
.then(req => {
|
||||||
|
finishInflightBlock(upload.file.cloudName, block)
|
||||||
|
upload.completed += 1
|
||||||
|
statUpdate(req)
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
finishInflightBlock(upload.file.cloudName, block)
|
||||||
|
throw err
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
inflight.delete(task)
|
||||||
|
})
|
||||||
|
inflight.add(task)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inflight.size) break
|
||||||
|
await Promise.race(inflight)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runId !== uploadRunId) return
|
||||||
|
|
||||||
|
if (upload.completed >= upload.blocks.length) {
|
||||||
|
blockQueue.shift()
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err: any) {
|
||||||
|
if (runId !== uploadRunId) return
|
||||||
|
console.error('Upload error', err)
|
||||||
|
store.error = err?.message || 'Upload failed'
|
||||||
|
uploadRunId += 1
|
||||||
|
upqueue = []
|
||||||
|
blockQueue = []
|
||||||
|
inflightBlocks.clear()
|
||||||
|
} finally {
|
||||||
|
store.uprogress.status = 'idle'
|
||||||
|
workerRunning = false
|
||||||
|
if (upqueue.length) startWorker()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const startWorker = () => {
|
const startWorker = () => {
|
||||||
if (workerRunning === false) workerRunning = setTimeout(() => {
|
if (workerRunning) return
|
||||||
workerRunning = true
|
workerRunning = true
|
||||||
worker()
|
const runId = uploadRunId
|
||||||
|
setTimeout(() => {
|
||||||
|
void worker(runId)
|
||||||
}, 0)
|
}, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,19 @@
|
|||||||
<div v-if="loading" class="loading">Loading...</div>
|
<div v-if="loading" class="loading">Loading...</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<h3>Server Settings</h3>
|
<h3>Server Settings</h3>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="serverName">Server name</label>
|
||||||
|
<div class="input-with-hint">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="serverName"
|
||||||
|
v-model="serverSettings.name"
|
||||||
|
@input="debouncedUpdateServerName"
|
||||||
|
:placeholder="store.server.name"
|
||||||
|
/>
|
||||||
|
<small>Leave empty to use the share folder name</small>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="publicAccess">
|
<label for="publicAccess">
|
||||||
<input
|
<input
|
||||||
@@ -62,7 +75,7 @@
|
|||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref, reactive, onMounted, watch } from 'vue'
|
import { ref, reactive, onMounted, watch } from 'vue'
|
||||||
import { listUsers, createUser, updateUser, deleteUser, updatePublic } from '@/repositories/User'
|
import { listUsers, createUser, updateUser, deleteUser, updatePublic, updateServerName, getServerConfig } from '@/repositories/User'
|
||||||
import type { ISimpleError } from '@/repositories/Client'
|
import type { ISimpleError } from '@/repositories/Client'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
|
|
||||||
@@ -78,9 +91,12 @@ const users = ref<User[]>([])
|
|||||||
const success = ref('')
|
const success = ref('')
|
||||||
const copyButtonText = ref('📋')
|
const copyButtonText = ref('📋')
|
||||||
const serverSettings = reactive({
|
const serverSettings = reactive({
|
||||||
public: false
|
public: false,
|
||||||
|
name: '',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let nameDebounceTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
|
||||||
const close = () => {
|
const close = () => {
|
||||||
store.dialog = ''
|
store.dialog = ''
|
||||||
success.value = ''
|
success.value = ''
|
||||||
@@ -206,15 +222,48 @@ const updateServerSettings = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updateServerNameSetting = async () => {
|
||||||
|
try {
|
||||||
|
const result = await updateServerName(serverSettings.name)
|
||||||
|
// Update store with the effective name returned by the server
|
||||||
|
store.server.name = result.name
|
||||||
|
} catch (e) {
|
||||||
|
const httpError = e as ISimpleError
|
||||||
|
store.showToast(httpError.message || 'Failed to update server name')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const debouncedUpdateServerName = () => {
|
||||||
|
if (nameDebounceTimer) clearTimeout(nameDebounceTimer)
|
||||||
|
nameDebounceTimer = setTimeout(updateServerNameSetting, 400)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load server config from admin API
|
||||||
|
const loadServerConfig = async () => {
|
||||||
|
try {
|
||||||
|
const config = await getServerConfig()
|
||||||
|
serverSettings.name = config.name
|
||||||
|
serverSettings.public = config.public
|
||||||
|
} catch (e) {
|
||||||
|
// Fallback to store values if API fails
|
||||||
|
serverSettings.public = store.server.public || false
|
||||||
|
serverSettings.name = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
serverSettings.public = store.server.public || false
|
serverSettings.public = store.server.public || false
|
||||||
|
serverSettings.name = ''
|
||||||
loading.value = false
|
loading.value = false
|
||||||
})
|
})
|
||||||
|
|
||||||
// Load users when dialog opens (only in built-in auth mode)
|
// Load users and config when dialog opens
|
||||||
watch(() => store.dialog, (newVal) => {
|
watch(() => store.dialog, (newVal) => {
|
||||||
if (newVal === 'usermgmt' && !store.server.paskia) {
|
if (newVal === 'usermgmt') {
|
||||||
loadUsers()
|
loadServerConfig()
|
||||||
|
if (!store.server.paskia) {
|
||||||
|
loadUsers()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -225,4 +274,13 @@ watch(() => store.server.public, (newVal) => {
|
|||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
/* Component-specific styles - most styling comes from ModalDialog.vue global styles */
|
/* Component-specific styles - most styling comes from ModalDialog.vue global styles */
|
||||||
|
.input-with-hint {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.25rem;
|
||||||
|
}
|
||||||
|
.input-with-hint small {
|
||||||
|
color: #666;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -7,16 +7,22 @@ export type DocProps = {
|
|||||||
name: string
|
name: string
|
||||||
key: FUID
|
key: FUID
|
||||||
size: number
|
size: number
|
||||||
|
allocated: number
|
||||||
mtime: number
|
mtime: number
|
||||||
dir: boolean
|
dir: boolean
|
||||||
|
ghost?: boolean
|
||||||
|
expires?: number // Unix timestamp for ghost expiry
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Doc {
|
export class Doc {
|
||||||
public loc: string = ""
|
public loc: string = ""
|
||||||
public key: FUID = ""
|
public key: FUID = ""
|
||||||
public size: number = 0
|
public size: number = 0
|
||||||
|
public allocated: number = 0
|
||||||
public mtime: number = 0
|
public mtime: number = 0
|
||||||
public dir: boolean = false
|
public dir: boolean = false
|
||||||
|
public ghost: boolean = false
|
||||||
|
public expires: number = 0 // Unix timestamp for ghost expiry (0 = no expiry)
|
||||||
/** @internal Use the name getter/setter instead */
|
/** @internal Use the name getter/setter instead */
|
||||||
public _name: string = ""
|
public _name: string = ""
|
||||||
|
|
||||||
@@ -31,6 +37,15 @@ export class Doc {
|
|||||||
this._name = name
|
this._name = name
|
||||||
}
|
}
|
||||||
get sizedisp(): string { return formatSize(this.size) }
|
get sizedisp(): string { return formatSize(this.size) }
|
||||||
|
/** Returns a sparse allocation indicator symbol, or empty string if fully allocated */
|
||||||
|
get sparseIndicator(): string {
|
||||||
|
if (this.dir || this.size <= this.allocated) return ''
|
||||||
|
if (this.allocated === 0) return '⭕' // exactly zero
|
||||||
|
const ratio = this.allocated / this.size
|
||||||
|
// Round to nearest 25%: ◔◑◕⬤
|
||||||
|
const rounded = Math.round(ratio * 4) // 0,1,2,3,4
|
||||||
|
return ['◔', '◔', '◑', '◕', '⬤'][rounded]! // 0 maps to ◔ since we handled exact 0 above
|
||||||
|
}
|
||||||
get modified(): string { return formatUnixDate(this.mtime) }
|
get modified(): string { return formatUnixDate(this.mtime) }
|
||||||
get url(): string {
|
get url(): string {
|
||||||
const p = this.loc ? `${this.loc}/${this.name}` : this.name
|
const p = this.loc ? `${this.loc}/${this.name}` : this.name
|
||||||
@@ -44,6 +59,9 @@ export class Doc {
|
|||||||
if (this.dir) return false
|
if (this.dir) return false
|
||||||
return ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'heic', 'heif', 'svg'].includes(this.ext)
|
return ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'heic', 'heif', 'svg'].includes(this.ext)
|
||||||
}
|
}
|
||||||
|
get complete(): boolean {
|
||||||
|
return !this.ghost && (this.dir || this.size <= this.allocated)
|
||||||
|
}
|
||||||
get previewable(): boolean {
|
get previewable(): boolean {
|
||||||
// Folders cannot be previewable
|
// Folders cannot be previewable
|
||||||
if (this.dir) return false
|
if (this.dir) return false
|
||||||
@@ -52,6 +70,7 @@ export class Doc {
|
|||||||
return ['mp4', 'mkv', 'webm', 'ogg', 'mp3', 'flac', 'aac', 'pdf'].includes(this.ext)
|
return ['mp4', 'mkv', 'webm', 'ogg', 'mp3', 'flac', 'aac', 'pdf'].includes(this.ext)
|
||||||
}
|
}
|
||||||
get previewurl(): string {
|
get previewurl(): string {
|
||||||
|
if (!this.complete || !this.previewable) return ''
|
||||||
return this.url.replace(/^\/files/, '/preview')
|
return this.url.replace(/^\/files/, '/preview')
|
||||||
}
|
}
|
||||||
get ext(): string {
|
get ext(): string {
|
||||||
@@ -74,9 +93,10 @@ export type FileEntry = [
|
|||||||
number, // level
|
number, // level
|
||||||
string, // name
|
string, // name
|
||||||
FUID,
|
FUID,
|
||||||
number, //mtime
|
number, // mtime
|
||||||
number, // size
|
number, // size
|
||||||
number, // isfile
|
number, // allocated (actual disk usage)
|
||||||
|
number, // isfile
|
||||||
]
|
]
|
||||||
|
|
||||||
export type UpdateEntry = ['k', number] | ['d', number] | ['i', Array<FileEntry>]
|
export type UpdateEntry = ['k', number] | ['d', number] | ['i', Array<FileEntry>]
|
||||||
|
|||||||
@@ -55,3 +55,13 @@ export async function updatePublic(isPublic: boolean) {
|
|||||||
const data = await Client.put('/api/config/public', { public: isPublic })
|
const data = await Client.put('/api/config/public', { public: isPublic })
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function updateServerName(name: string) {
|
||||||
|
const data = await Client.put('/api/config/name', { name })
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getServerConfig() {
|
||||||
|
const data = await Client.get('/api/config')
|
||||||
|
return data as { name: string, public: boolean }
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { showAuthIframe, AuthCancelledError, isAuthIframeOpen } from 'paskia'
|
|||||||
import type { FileEntry, UpdateEntry, errorEvent } from "./Document"
|
import type { FileEntry, UpdateEntry, errorEvent } from "./Document"
|
||||||
|
|
||||||
export const controlUrl = '/api/control'
|
export const controlUrl = '/api/control'
|
||||||
export const uploadUrl = '/api/upload'
|
|
||||||
export const watchUrl = '/api/watch'
|
export const watchUrl = '/api/watch'
|
||||||
|
|
||||||
let tree = [] as FileEntry[]
|
let tree = [] as FileEntry[]
|
||||||
@@ -164,7 +163,8 @@ const handleWatchMessage = (event: MessageEvent) => {
|
|||||||
handleUpdateMessage(msg)
|
handleUpdateMessage(msg)
|
||||||
break
|
break
|
||||||
case !!msg.space:
|
case !!msg.space:
|
||||||
console.log('Watch space', msg.space)
|
const store = useMainStore()
|
||||||
|
store.space = msg.space
|
||||||
break
|
break
|
||||||
case !!msg.error:
|
case !!msg.error:
|
||||||
handleError(msg)
|
handleError(msg)
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
// Non-reactive document storage for the full file list
|
||||||
|
// This avoids Vue reactivity overhead on large arrays
|
||||||
|
|
||||||
|
import type { Doc } from '@/repositories/Document'
|
||||||
|
import { shallowRef, triggerRef } from 'vue'
|
||||||
|
|
||||||
|
// The main document list - shallowRef means only the reference is reactive, not the contents
|
||||||
|
const documents = shallowRef<Doc[]>([])
|
||||||
|
|
||||||
|
// Version counter for manual reactivity triggering
|
||||||
|
let version = 0
|
||||||
|
|
||||||
|
export function getDocuments(): Doc[] {
|
||||||
|
return documents.value
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setDocuments(docs: Doc[]): void {
|
||||||
|
documents.value = docs
|
||||||
|
version++
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getVersion(): number {
|
||||||
|
return version
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trigger reactivity manually (e.g., after modifications)
|
||||||
|
export function triggerUpdate(): void {
|
||||||
|
version++
|
||||||
|
triggerRef(documents)
|
||||||
|
}
|
||||||
|
|
||||||
|
// For computed dependencies that need to react to document changes
|
||||||
|
export const documentRef = documents
|
||||||
+175
-23
@@ -5,11 +5,15 @@ import { collator } from '@/utils'
|
|||||||
import { watchConnect, resumeWatching } from '@/repositories/WS'
|
import { watchConnect, resumeWatching } from '@/repositories/WS'
|
||||||
import { sorted, type SortOrder } from '@/utils/docsort'
|
import { sorted, type SortOrder } from '@/utils/docsort'
|
||||||
import SearchWorker from '@/workers/searchWorker?worker'
|
import SearchWorker from '@/workers/searchWorker?worker'
|
||||||
|
import { getDocuments, setDocuments, documentRef } from './documentStore'
|
||||||
|
|
||||||
// Singleton search worker instance
|
// Singleton search worker instance
|
||||||
let searchWorker: Worker | null = null
|
let searchWorker: Worker | null = null
|
||||||
let searchId = 0
|
let searchId = 0
|
||||||
let searchStore: ReturnType<typeof useMainStore> | null = null
|
let searchStore: ReturnType<typeof useMainStore> | null = null
|
||||||
|
let loadingTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
let clearOldResultsTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
let lastResultUpdate = 0
|
||||||
|
|
||||||
function getSearchWorker(): Worker {
|
function getSearchWorker(): Worker {
|
||||||
if (!searchWorker) {
|
if (!searchWorker) {
|
||||||
@@ -18,14 +22,30 @@ function getSearchWorker(): Worker {
|
|||||||
searchWorker.onmessage = (e) => {
|
searchWorker.onmessage = (e) => {
|
||||||
if (!searchStore || e.data.id !== searchId) return // Stale result
|
if (!searchStore || e.data.id !== searchId) return // Stale result
|
||||||
|
|
||||||
// Convert plain data back to Doc instances (constructor is now lightweight)
|
// Convert plain data back to Doc instances
|
||||||
const docs = []
|
const docs = e.data.docs.map((d: any) => new Doc(d))
|
||||||
for (const d of e.data.docs) {
|
|
||||||
docs.push(new Doc(d))
|
// Cancel the clear-old-results timer since we have new results
|
||||||
|
if (clearOldResultsTimer) {
|
||||||
|
clearTimeout(clearOldResultsTimer)
|
||||||
|
clearOldResultsTimer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Throttle rapid intermediate updates to reduce UI flicker
|
||||||
|
const now = performance.now()
|
||||||
|
if (!e.data.done && now - lastResultUpdate < 50) {
|
||||||
|
return // Skip intermediate update if too recent
|
||||||
|
}
|
||||||
|
lastResultUpdate = now
|
||||||
|
|
||||||
searchStore.searchResults = docs
|
searchStore.searchResults = docs
|
||||||
|
|
||||||
if (e.data.done) {
|
if (e.data.done) {
|
||||||
|
// Clear the loading timer and hide spinner
|
||||||
|
if (loadingTimer) {
|
||||||
|
clearTimeout(loadingTimer)
|
||||||
|
loadingTimer = null
|
||||||
|
}
|
||||||
searchStore.searchLoading = false
|
searchStore.searchLoading = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,9 +53,20 @@ function getSearchWorker(): Worker {
|
|||||||
return searchWorker
|
return searchWorker
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ghost expiry time in seconds
|
||||||
|
const GHOST_TTL = 30
|
||||||
|
|
||||||
|
// Periodic cleanup interval
|
||||||
|
let cleanupInterval: ReturnType<typeof setInterval> | null = null
|
||||||
|
|
||||||
export const useMainStore = defineStore('main', {
|
export const useMainStore = defineStore('main', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
document: [] as Doc[],
|
// Ghosts are temporary optimistic-update files/folders shown until server confirms
|
||||||
|
ghosts: [] as Doc[],
|
||||||
|
// Hidden paths for optimistic delete (path -> expiry timestamp)
|
||||||
|
hiddenPaths: new Map<string, number>(),
|
||||||
|
// Version counter to trigger reactivity when external document list changes
|
||||||
|
docVersion: 0,
|
||||||
selected: new Set<FUID>([]),
|
selected: new Set<FUID>([]),
|
||||||
query: '' as string,
|
query: '' as string,
|
||||||
searchResults: [] as Doc[],
|
searchResults: [] as Doc[],
|
||||||
@@ -56,11 +87,19 @@ export const useMainStore = defineStore('main', {
|
|||||||
gallery: false,
|
gallery: false,
|
||||||
sortListing: '' as SortOrder,
|
sortListing: '' as SortOrder,
|
||||||
sortFiltered: '' as SortOrder,
|
sortFiltered: '' as SortOrder,
|
||||||
|
searchHotkey: '/', // Character shown for search hotkey (Slash key)
|
||||||
},
|
},
|
||||||
user: {
|
user: {
|
||||||
username: '' as string,
|
username: '' as string,
|
||||||
privileged: false as boolean,
|
privileged: false as boolean,
|
||||||
isLoggedIn: false as boolean,
|
isLoggedIn: false as boolean,
|
||||||
|
},
|
||||||
|
space: {
|
||||||
|
disk: 0,
|
||||||
|
free: 0,
|
||||||
|
used: 0,
|
||||||
|
storage: 0,
|
||||||
|
allocated: 0,
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
persist: {
|
persist: {
|
||||||
@@ -81,22 +120,79 @@ export const useMainStore = defineStore('main', {
|
|||||||
updateRoot(root: FileEntry[]) {
|
updateRoot(root: FileEntry[]) {
|
||||||
const docs = []
|
const docs = []
|
||||||
let loc = [] as string[]
|
let loc = [] as string[]
|
||||||
for (const [level, name, key, mtime, size, isfile] of root) {
|
for (const [level, name, key, mtime, size, allocated, isfile] of root) {
|
||||||
loc = loc.slice(0, level - 1)
|
loc = loc.slice(0, level - 1)
|
||||||
docs.push(new Doc({
|
docs.push(new Doc({
|
||||||
name,
|
name,
|
||||||
loc: level ? loc.join('/') : '/',
|
loc: level ? loc.join('/') : '/',
|
||||||
key,
|
key,
|
||||||
size,
|
size,
|
||||||
|
allocated,
|
||||||
mtime,
|
mtime,
|
||||||
dir: !isfile,
|
dir: !isfile,
|
||||||
}))
|
}))
|
||||||
loc.push(name)
|
loc.push(name)
|
||||||
}
|
}
|
||||||
this.document = docs
|
// Store in non-reactive external storage
|
||||||
|
setDocuments(docs)
|
||||||
|
// Clear ghosts that now exist in the real list
|
||||||
|
const realPaths = new Set(docs.map(d => d.loc ? `${d.loc}/${d.name}` : d.name))
|
||||||
|
this.ghosts = this.ghosts.filter(g => !realPaths.has(g.loc ? `${g.loc}/${g.name}` : g.name))
|
||||||
|
// Clear hidden paths that no longer exist (deletion confirmed)
|
||||||
|
for (const path of this.hiddenPaths.keys()) {
|
||||||
|
if (!realPaths.has(path)) this.hiddenPaths.delete(path)
|
||||||
|
}
|
||||||
|
// Start cleanup timer if not running
|
||||||
|
this.startCleanupTimer()
|
||||||
|
// Bump version to trigger reactive updates
|
||||||
|
this.docVersion++
|
||||||
// Sync documents to search worker
|
// Sync documents to search worker
|
||||||
this.syncSearchWorker()
|
this.syncSearchWorker()
|
||||||
},
|
},
|
||||||
|
/** Add a ghost file/folder for optimistic UI updates */
|
||||||
|
addGhost(doc: Doc) {
|
||||||
|
doc.ghost = true
|
||||||
|
doc.expires = Math.floor(Date.now() / 1000) + GHOST_TTL
|
||||||
|
this.ghosts.push(doc)
|
||||||
|
},
|
||||||
|
/** Clear all ghosts (e.g., on navigation or refresh) */
|
||||||
|
clearGhosts() {
|
||||||
|
this.ghosts = []
|
||||||
|
},
|
||||||
|
/** Hide a document path (optimistic delete) */
|
||||||
|
hideDoc(path: string) {
|
||||||
|
this.hiddenPaths.set(path, Math.floor(Date.now() / 1000) + GHOST_TTL)
|
||||||
|
},
|
||||||
|
/** Unhide a document path (delete failed, restore visibility) */
|
||||||
|
unhideDoc(path: string) {
|
||||||
|
this.hiddenPaths.delete(path)
|
||||||
|
},
|
||||||
|
/** Start the periodic cleanup timer */
|
||||||
|
startCleanupTimer() {
|
||||||
|
if (cleanupInterval) return
|
||||||
|
cleanupInterval = setInterval(() => this.cleanupExpired(), 5000)
|
||||||
|
},
|
||||||
|
/** Stop the cleanup timer */
|
||||||
|
stopCleanupTimer() {
|
||||||
|
if (cleanupInterval) {
|
||||||
|
clearInterval(cleanupInterval)
|
||||||
|
cleanupInterval = null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
/** Remove expired ghosts and hidden paths */
|
||||||
|
cleanupExpired() {
|
||||||
|
const now = Math.floor(Date.now() / 1000)
|
||||||
|
const ghostsBefore = this.ghosts.length
|
||||||
|
const hiddenBefore = this.hiddenPaths.size
|
||||||
|
this.ghosts = this.ghosts.filter(g => g.expires > now)
|
||||||
|
for (const [path, expires] of this.hiddenPaths) {
|
||||||
|
if (expires <= now) this.hiddenPaths.delete(path)
|
||||||
|
}
|
||||||
|
// Stop timer if nothing to clean up
|
||||||
|
if (this.ghosts.length === 0 && this.hiddenPaths.size === 0) {
|
||||||
|
this.stopCleanupTimer()
|
||||||
|
}
|
||||||
|
},
|
||||||
/** Show a temporary toast message that auto-dismisses */
|
/** Show a temporary toast message that auto-dismisses */
|
||||||
showToast(message: string, duration = 3000) {
|
showToast(message: string, duration = 3000) {
|
||||||
if (this.toastTimeout) {
|
if (this.toastTimeout) {
|
||||||
@@ -120,11 +216,13 @@ export const useMainStore = defineStore('main', {
|
|||||||
syncSearchWorker() {
|
syncSearchWorker() {
|
||||||
const worker = getSearchWorker()
|
const worker = getSearchWorker()
|
||||||
// Send plain data to worker (no class instances)
|
// Send plain data to worker (no class instances)
|
||||||
const docData = this.document.map(doc => ({
|
const docs = getDocuments()
|
||||||
|
const docData = docs.map(doc => ({
|
||||||
loc: doc.loc,
|
loc: doc.loc,
|
||||||
name: doc.name,
|
name: doc.name,
|
||||||
key: doc.key,
|
key: doc.key,
|
||||||
size: doc.size,
|
size: doc.size,
|
||||||
|
allocated: doc.allocated,
|
||||||
mtime: doc.mtime,
|
mtime: doc.mtime,
|
||||||
dir: doc.dir,
|
dir: doc.dir,
|
||||||
}))
|
}))
|
||||||
@@ -135,13 +233,43 @@ export const useMainStore = defineStore('main', {
|
|||||||
const id = ++searchId
|
const id = ++searchId
|
||||||
searchStore = this // Store reference for worker callback
|
searchStore = this // Store reference for worker callback
|
||||||
|
|
||||||
|
// Update query immediately so watchers know we're handling this
|
||||||
|
this.query = query
|
||||||
|
|
||||||
|
// Cancel pending timers
|
||||||
|
if (loadingTimer) {
|
||||||
|
clearTimeout(loadingTimer)
|
||||||
|
loadingTimer = null
|
||||||
|
}
|
||||||
|
if (clearOldResultsTimer) {
|
||||||
|
clearTimeout(clearOldResultsTimer)
|
||||||
|
clearOldResultsTimer = null
|
||||||
|
}
|
||||||
|
|
||||||
if (!query) {
|
if (!query) {
|
||||||
|
// Clear results only when search is closed
|
||||||
this.searchResults = []
|
this.searchResults = []
|
||||||
this.searchLoading = false
|
this.searchLoading = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.searchLoading = true
|
// Keep old results briefly to avoid flicker on fast cached searches
|
||||||
|
// But clear them after 50ms if no new results have arrived
|
||||||
|
clearOldResultsTimer = setTimeout(() => {
|
||||||
|
if (searchId === id) {
|
||||||
|
this.searchResults = []
|
||||||
|
}
|
||||||
|
clearOldResultsTimer = null
|
||||||
|
}, 50)
|
||||||
|
|
||||||
|
// Delay showing loading indicator to avoid flicker on fast searches
|
||||||
|
loadingTimer = setTimeout(() => {
|
||||||
|
if (searchId === id) { // Still the current search
|
||||||
|
this.searchLoading = true
|
||||||
|
}
|
||||||
|
loadingTimer = null
|
||||||
|
}, 100)
|
||||||
|
|
||||||
worker.postMessage({ type: 'search', query, loc, id })
|
worker.postMessage({ type: 'search', query, loc, id })
|
||||||
},
|
},
|
||||||
login(username: string, privileged: boolean) {
|
login(username: string, privileged: boolean) {
|
||||||
@@ -154,7 +282,11 @@ export const useMainStore = defineStore('main', {
|
|||||||
clearSensitiveData() {
|
clearSensitiveData() {
|
||||||
// Clear all sensitive state on logout or auth failure
|
// Clear all sensitive state on logout or auth failure
|
||||||
localStorage.removeItem('cista-files')
|
localStorage.removeItem('cista-files')
|
||||||
this.document = []
|
setDocuments([])
|
||||||
|
this.ghosts = []
|
||||||
|
this.hiddenPaths.clear()
|
||||||
|
this.stopCleanupTimer()
|
||||||
|
this.docVersion++
|
||||||
this.selected.clear()
|
this.selected.clear()
|
||||||
this.user.username = ''
|
this.user.username = ''
|
||||||
this.user.privileged = false
|
this.user.privileged = false
|
||||||
@@ -180,12 +312,25 @@ export const useMainStore = defineStore('main', {
|
|||||||
resumeWatching()
|
resumeWatching()
|
||||||
},
|
},
|
||||||
toggleSort(name: SortOrder) {
|
toggleSort(name: SortOrder) {
|
||||||
if (this.query) this.prefs.sortFiltered = this.prefs.sortFiltered === name ? '' : name
|
const current = this.query ? this.prefs.sortFiltered : this.prefs.sortListing
|
||||||
else this.prefs.sortListing = this.prefs.sortListing === name ? '' : name
|
const newOrder = current === name ? '' : name
|
||||||
|
if (this.query) this.prefs.sortFiltered = newOrder
|
||||||
|
else this.prefs.sortListing = newOrder
|
||||||
|
this.showSortToast(newOrder)
|
||||||
},
|
},
|
||||||
sort(name: SortOrder | '') {
|
sort(name: SortOrder | '') {
|
||||||
if (this.query) this.prefs.sortFiltered = name
|
if (this.query) this.prefs.sortFiltered = name
|
||||||
else this.prefs.sortListing = name
|
else this.prefs.sortListing = name
|
||||||
|
this.showSortToast(name)
|
||||||
|
},
|
||||||
|
showSortToast(order: SortOrder | '') {
|
||||||
|
const labels: Record<string, string> = {
|
||||||
|
'': 'Folders first',
|
||||||
|
'name': 'Alphabetical order',
|
||||||
|
'modified': 'Newest first',
|
||||||
|
'size': 'Largest first',
|
||||||
|
}
|
||||||
|
this.showToast(labels[order] || order, 1200)
|
||||||
},
|
},
|
||||||
focusBreadcrumb() {
|
focusBreadcrumb() {
|
||||||
(document.querySelector('.breadcrumb') as HTMLAnchorElement).focus()
|
(document.querySelector('.breadcrumb') as HTMLAnchorElement).focus()
|
||||||
@@ -200,8 +345,21 @@ export const useMainStore = defineStore('main', {
|
|||||||
getters: {
|
getters: {
|
||||||
sortOrder(): SortOrder { return this.query ? this.prefs.sortFiltered : this.prefs.sortListing },
|
sortOrder(): SortOrder { return this.query ? this.prefs.sortFiltered : this.prefs.sortListing },
|
||||||
isUserLogged(): boolean { return this.user.isLoggedIn },
|
isUserLogged(): boolean { return this.user.isLoggedIn },
|
||||||
recentDocuments(): Doc[] { return sorted(this.document, 'modified') },
|
/** Get documents count (triggers on docVersion change) */
|
||||||
|
documentCount(): number {
|
||||||
|
// Access docVersion to make this reactive
|
||||||
|
void this.docVersion
|
||||||
|
return getDocuments().length
|
||||||
|
},
|
||||||
|
recentDocuments(): Doc[] {
|
||||||
|
// Access docVersion to make this reactive
|
||||||
|
void this.docVersion
|
||||||
|
return sorted(getDocuments(), 'modified')
|
||||||
|
},
|
||||||
selectedFiles(): SelectedItems {
|
selectedFiles(): SelectedItems {
|
||||||
|
// Access docVersion to make this reactive
|
||||||
|
void this.docVersion
|
||||||
|
const docs = getDocuments()
|
||||||
const selected = this.selected
|
const selected = this.selected
|
||||||
const found = new Set<FUID>()
|
const found = new Set<FUID>()
|
||||||
const ret: SelectedItems = {
|
const ret: SelectedItems = {
|
||||||
@@ -210,7 +368,7 @@ export const useMainStore = defineStore('main', {
|
|||||||
keys: [],
|
keys: [],
|
||||||
recursive: [],
|
recursive: [],
|
||||||
}
|
}
|
||||||
for (const doc of this.document) {
|
for (const doc of docs) {
|
||||||
if (selected.has(doc.key)) {
|
if (selected.has(doc.key)) {
|
||||||
found.add(doc.key)
|
found.add(doc.key)
|
||||||
ret.keys.push(doc.key)
|
ret.keys.push(doc.key)
|
||||||
@@ -220,22 +378,16 @@ export const useMainStore = defineStore('main', {
|
|||||||
// What did we not select?
|
// What did we not select?
|
||||||
for (const key of selected) if (!found.has(key)) ret.missing.add(key)
|
for (const key of selected) if (!found.has(key)) ret.missing.add(key)
|
||||||
// Build a flat list including contents recursively
|
// Build a flat list including contents recursively
|
||||||
const relnames = new Set<string>()
|
|
||||||
function add(rel: string, full: string, doc: Doc) {
|
|
||||||
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) {
|
for (const key of ret.keys) {
|
||||||
const base = ret.docs[key]!
|
const base = ret.docs[key]!
|
||||||
const basepath = base.loc ? `${base.loc}/${base.name}` : base.name
|
const basepath = base.loc ? `${base.loc}/${base.name}` : base.name
|
||||||
const nremove = base.loc.length
|
const nremove = base.loc.length
|
||||||
add(base.name, basepath, base)
|
ret.recursive.push([base.name, basepath, base])
|
||||||
for (const doc of this.document) {
|
for (const doc of docs) {
|
||||||
if (doc.loc === basepath || doc.loc.startsWith(basepath) && doc.loc[basepath.length] === '/') {
|
if (doc.loc === basepath || doc.loc.startsWith(basepath) && doc.loc[basepath.length] === '/') {
|
||||||
const full = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
const full = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
const rel = full.slice(nremove)
|
const rel = full.slice(nremove)
|
||||||
add(rel, full, doc)
|
ret.recursive.push([rel, full, doc])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
|
import { getDocuments } from '@/stores/documentStore'
|
||||||
|
|
||||||
|
|
||||||
export const exists = (path: string[]) => {
|
export const exists = (path: string[]) => {
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
|
// Access docVersion to make this reactive
|
||||||
|
void store.docVersion
|
||||||
const p = path.join('/')
|
const p = path.join('/')
|
||||||
return store.document.some(doc => (doc.loc ? `${doc.loc}/${doc.name}` : doc.name) === p)
|
return getDocuments().some(doc => (doc.loc ? `${doc.loc}/${doc.name}` : doc.name) === p)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Strip file extension intelligently (handles .tar.gz, name.with.dots.pdf, etc.) */
|
/** Strip file extension intelligently (handles .tar.gz, name.with.dots.pdf, etc.) */
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { watchEffect, ref, computed, watch } from 'vue'
|
import { watchEffect, ref, computed, watch } from 'vue'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
|
import { getDocuments } from '@/stores/documentStore'
|
||||||
import { collator } from '@/utils'
|
import { collator } from '@/utils'
|
||||||
import { sorted, sortedGrouped } from '@/utils/docsort'
|
import { sorted, sortedGrouped } from '@/utils/docsort'
|
||||||
import FileExplorer from '@/components/FileExplorer.vue'
|
import FileExplorer from '@/components/FileExplorer.vue'
|
||||||
@@ -33,17 +34,14 @@ const props = defineProps<{
|
|||||||
|
|
||||||
// Folder path for component keys - only recreate component when folder changes, not search
|
// Folder path for component keys - only recreate component when folder changes, not search
|
||||||
const folderPath = computed(() => props.path.join('/'))
|
const folderPath = computed(() => props.path.join('/'))
|
||||||
// Trigger search when query changes (from route, e.g., page load or back button)
|
|
||||||
// Note: Direct typing triggers search immediately via HeaderMain, this is for route-based changes
|
// Handle route-based search changes (back/forward navigation, direct URL)
|
||||||
|
// Skip if store.query already matches (means we triggered this via typing)
|
||||||
watch(
|
watch(
|
||||||
() => [props.query, props.path.join('/')] as const,
|
() => [props.query, props.path.join('/')] as const,
|
||||||
([query, loc]) => {
|
([query, loc]) => {
|
||||||
// Only trigger if results don't match current query (avoid duplicate searches)
|
if (store.query === query) return // Already searching this query
|
||||||
if (query && store.searchResults.length === 0) {
|
store.search(query, loc)
|
||||||
store.search(query, loc)
|
|
||||||
} else if (!query) {
|
|
||||||
store.search('', loc) // Clear search results
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{ immediate: true }
|
{ immediate: true }
|
||||||
)
|
)
|
||||||
@@ -53,13 +51,22 @@ const documents = computed(() => {
|
|||||||
const query = props.query
|
const query = props.query
|
||||||
|
|
||||||
// List the current location (no search)
|
// List the current location (no search)
|
||||||
if (!query) return sorted(
|
if (!query) {
|
||||||
store.document.filter(doc => doc.loc === loc),
|
// Access docVersion to make this reactive to document changes
|
||||||
store.prefs.sortListing,
|
void store.docVersion
|
||||||
)
|
const hidden = store.hiddenPaths
|
||||||
|
const docs = getDocuments().filter(doc => doc.loc === loc && !hidden.has(doc.loc ? `${doc.loc}/${doc.name}` : doc.name))
|
||||||
|
// Overlay ghosts for this location (excluding hidden ones)
|
||||||
|
const ghosts = store.ghosts.filter(g => g.loc === loc && !hidden.has(g.loc ? `${g.loc}/${g.name}` : g.name))
|
||||||
|
// Merge: ghosts that don't conflict with real docs
|
||||||
|
const realNames = new Set(docs.map(d => d.name))
|
||||||
|
const merged = [...docs, ...ghosts.filter(g => !realNames.has(g.name))]
|
||||||
|
return sorted(merged, store.prefs.sortListing)
|
||||||
|
}
|
||||||
|
|
||||||
// Search results from worker
|
// Search results from worker (also filter hidden)
|
||||||
const docs = store.searchResults
|
const hidden = store.hiddenPaths
|
||||||
|
const docs = store.searchResults.filter(doc => !hidden.has(doc.loc ? `${doc.loc}/${doc.name}` : doc.name))
|
||||||
|
|
||||||
// Custom sort override in effect? Use grouped sorting to keep folders together
|
// Custom sort override in effect? Use grouped sorting to keep folders together
|
||||||
const order = store.prefs.sortFiltered
|
const order = store.prefs.sortFiltered
|
||||||
@@ -71,11 +78,10 @@ const documents = computed(() => {
|
|||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
store.fileExplorer = fileExplorer.value
|
store.fileExplorer = fileExplorer.value
|
||||||
store.query = props.query
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Only auto-switch gallery mode when entering a new folder or on initial file list load
|
// Only auto-switch gallery mode when entering a new folder or on initial file list load
|
||||||
watch([() => props.path.join('/'), () => store.document.length], ([path, len], [oldPath, oldLen]) => {
|
watch([() => props.path.join('/'), () => store.documentCount], ([path, len], [oldPath, oldLen]) => {
|
||||||
// React to path change or initial document load (0 → non-zero)
|
// React to path change or initial document load (0 → non-zero)
|
||||||
if (path === oldPath && oldLen !== undefined && oldLen > 0) return
|
if (path === oldPath && oldLen !== undefined && oldLen > 0) return
|
||||||
store.prefs.gallery = documents.value.some(d => d.previewable)
|
store.prefs.gallery = documents.value.some(d => d.previewable)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ interface DocData {
|
|||||||
name: string
|
name: string
|
||||||
key: string
|
key: string
|
||||||
size: number
|
size: number
|
||||||
|
allocated: number
|
||||||
mtime: number
|
mtime: number
|
||||||
dir: boolean
|
dir: boolean
|
||||||
}
|
}
|
||||||
@@ -36,58 +37,120 @@ interface ResultMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Worker state
|
// Worker state
|
||||||
let documents: WorkerDoc[] = []
|
|
||||||
let recentDocuments: WorkerDoc[] = [] // Sorted by mtime descending
|
let recentDocuments: WorkerDoc[] = [] // Sorted by mtime descending
|
||||||
let currentSearchId = 0
|
let currentSearchId = 0
|
||||||
|
|
||||||
// Haystack formatting (same as main thread utils)
|
// Search result cache - cleared when documents change
|
||||||
function haystackFormat(str: string): string {
|
interface CacheEntry {
|
||||||
const based = str.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase()
|
query: string // Normalized query string
|
||||||
return '^' + based + '$'
|
results: WorkerDoc[] // Matched results (up to limit)
|
||||||
|
complete: boolean // True if search scanned all documents
|
||||||
|
}
|
||||||
|
const searchCache: CacheEntry[] = []
|
||||||
|
const MAX_CACHE_SIZE = 10
|
||||||
|
const RESULT_LIMIT = 100
|
||||||
|
|
||||||
|
// Normalize string for search (remove diacritics, lowercase)
|
||||||
|
// Haystack adds ^ and $ markers to allow matching start/end of name
|
||||||
|
function normalizeHaystack(str: string): string {
|
||||||
|
return '^' + str.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase() + '$'
|
||||||
}
|
}
|
||||||
|
|
||||||
// Needle formatting
|
function normalizeQuery(str: string): string {
|
||||||
function needleFormat(query: string) {
|
return str.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase()
|
||||||
const based = query.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase()
|
|
||||||
return { based, words: based.split(/\s+/) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if haystack includes needle
|
// Test if document matches search query
|
||||||
function localeIncludes(haystack: string, filter: { based: string; words: string[] }): boolean {
|
function matches(haystack: string, query: string, words: string[]): boolean {
|
||||||
const { based, words } = filter
|
return haystack.includes(query) || words.every(word => haystack.includes(word))
|
||||||
return haystack.includes(based) || (words && words.every(word => haystack.includes(word)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collator for sorting
|
// Collator for sorting
|
||||||
const collator = new Intl.Collator('en', { sensitivity: 'base', numeric: true, usage: 'search' })
|
const collator = new Intl.Collator('en', { sensitivity: 'base', numeric: true })
|
||||||
|
|
||||||
// Sort by mtime descending
|
// Yield control to allow new messages to be processed
|
||||||
function sortByRecent(docs: WorkerDoc[]): WorkerDoc[] {
|
const yieldControl = (): Promise<void> => new Promise(resolve => setTimeout(resolve, 0))
|
||||||
return [...docs].sort((a, b) => b.mtime - a.mtime)
|
|
||||||
|
// Find best cache entry to filter from
|
||||||
|
// Returns entry if new query's results are guaranteed to be a subset of cached results
|
||||||
|
// Only valid if the cached search was complete (scanned all documents)
|
||||||
|
function findCacheSubset(query: string): CacheEntry | null {
|
||||||
|
// Look for a cached query that the new query starts with
|
||||||
|
// e.g., cached "foo" can be used for "foobar" or "foo bar"
|
||||||
|
// The longer the prefix, the better (fewer items to filter)
|
||||||
|
// IMPORTANT: Only use complete cache entries - incomplete ones may have
|
||||||
|
// missed results that would match the more specific query
|
||||||
|
let best: CacheEntry | null = null
|
||||||
|
for (const entry of searchCache) {
|
||||||
|
if (entry.complete && query.startsWith(entry.query)) {
|
||||||
|
if (!best || entry.query.length > best.query.length) {
|
||||||
|
best = entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return best
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yield control to check for new messages
|
// Add result to cache
|
||||||
function yieldControl(): Promise<void> {
|
function addToCache(query: string, results: WorkerDoc[], complete: boolean) {
|
||||||
return new Promise(resolve => setTimeout(resolve, 0))
|
// Remove existing entry for same query if any
|
||||||
|
const idx = searchCache.findIndex(e => e.query === query)
|
||||||
|
if (idx !== -1) searchCache.splice(idx, 1)
|
||||||
|
// Add to front (most recent)
|
||||||
|
searchCache.unshift({ query, results, complete })
|
||||||
|
// Trim cache
|
||||||
|
if (searchCache.length > MAX_CACHE_SIZE) searchCache.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear cache (called when documents change)
|
||||||
|
function clearCache() {
|
||||||
|
searchCache.length = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform search with incremental results
|
// Perform search with incremental results
|
||||||
async function performSearch(query: string, loc: string, searchId: number) {
|
async function performSearch(rawQuery: string, loc: string, searchId: number) {
|
||||||
const needle = needleFormat(query)
|
const query = normalizeQuery(rawQuery)
|
||||||
const limit = 100
|
const words = query.split(/\s+/)
|
||||||
const batchSize = 500 // Smaller batches for faster incremental feedback
|
|
||||||
const results: WorkerDoc[] = []
|
const results: WorkerDoc[] = []
|
||||||
let lastResultCount = 0
|
let lastResultCount = 0
|
||||||
|
|
||||||
for (let i = 0; i < recentDocuments.length && results.length < limit; i += batchSize) {
|
// Check cache for exact match
|
||||||
// Check if search was superseded
|
const exactMatch = searchCache.find(e => e.query === query)
|
||||||
if (currentSearchId !== searchId) return
|
if (exactMatch) {
|
||||||
|
if (currentSearchId === searchId) {
|
||||||
|
postResults(exactMatch.results, rawQuery, loc, searchId, true)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we can filter from a cached superset
|
||||||
|
const cacheEntry = findCacheSubset(query)
|
||||||
|
if (cacheEntry) {
|
||||||
|
// Fast path: filter from cached results (only used for complete cache entries)
|
||||||
|
for (const doc of cacheEntry.results) {
|
||||||
|
if (matches(doc.haystack, query, words)) {
|
||||||
|
results.push(doc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Cache entry was complete, so filtered results are also complete
|
||||||
|
addToCache(query, results, true)
|
||||||
|
|
||||||
|
if (currentSearchId === searchId) {
|
||||||
|
postResults(results, rawQuery, loc, searchId, true)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slow path: scan all documents
|
||||||
|
const batchSize = 500
|
||||||
|
for (let i = 0; i < recentDocuments.length && results.length < RESULT_LIMIT; i += batchSize) {
|
||||||
|
if (currentSearchId !== searchId) return // Superseded
|
||||||
|
|
||||||
// Process batch
|
// Process batch
|
||||||
const end = Math.min(i + batchSize, recentDocuments.length)
|
const end = Math.min(i + batchSize, recentDocuments.length)
|
||||||
for (let j = i; j < end && results.length < limit; j++) {
|
for (let j = i; j < end && results.length < RESULT_LIMIT; j++) {
|
||||||
const doc = recentDocuments[j]!
|
const doc = recentDocuments[j]!
|
||||||
if (localeIncludes(doc.haystack, needle)) {
|
if (matches(doc.haystack, query, words)) {
|
||||||
results.push(doc)
|
results.push(doc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,85 +158,69 @@ async function performSearch(query: string, loc: string, searchId: number) {
|
|||||||
// Post incremental results if we found new matches
|
// Post incremental results if we found new matches
|
||||||
if (results.length > lastResultCount && currentSearchId === searchId) {
|
if (results.length > lastResultCount && currentSearchId === searchId) {
|
||||||
lastResultCount = results.length
|
lastResultCount = results.length
|
||||||
const sortedResults = sortResults(results, query, loc)
|
postResults(results, rawQuery, loc, searchId, false)
|
||||||
postMessage({
|
|
||||||
type: 'results',
|
|
||||||
docs: sortedResults.map(stripHaystack),
|
|
||||||
id: searchId,
|
|
||||||
done: false
|
|
||||||
} as ResultMessage)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yield control between batches to allow new search requests to interrupt
|
// Yield control between batches
|
||||||
if (i + batchSize < recentDocuments.length && results.length < limit) {
|
if (i + batchSize < recentDocuments.length && results.length < RESULT_LIMIT) {
|
||||||
await yieldControl()
|
await yieldControl()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post final results
|
// Cache and post final results
|
||||||
|
addToCache(query, results, results.length < RESULT_LIMIT)
|
||||||
if (currentSearchId === searchId) {
|
if (currentSearchId === searchId) {
|
||||||
const sortedResults = sortResults(results, query, loc)
|
postResults(results, rawQuery, loc, searchId, true)
|
||||||
postMessage({
|
|
||||||
type: 'results',
|
|
||||||
docs: sortedResults.map(stripHaystack),
|
|
||||||
id: searchId,
|
|
||||||
done: true
|
|
||||||
} as ResultMessage)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Post results to main thread
|
||||||
|
function postResults(docs: WorkerDoc[], query: string, loc: string, id: number, done: boolean) {
|
||||||
|
const sorted = sortResults(docs, query, loc)
|
||||||
|
postMessage({
|
||||||
|
type: 'results',
|
||||||
|
docs: sorted.map(({ haystack, ...rest }) => rest),
|
||||||
|
id,
|
||||||
|
done
|
||||||
|
} as ResultMessage)
|
||||||
|
}
|
||||||
|
|
||||||
// Sort results by relevance
|
// Sort results by relevance
|
||||||
function sortResults(docs: WorkerDoc[], query: string, loc: string): WorkerDoc[] {
|
function sortResults(docs: WorkerDoc[], query: string, loc: string): WorkerDoc[] {
|
||||||
const locsub = loc + '/'
|
const locsub = loc + '/'
|
||||||
return [...docs].sort((a, b) => (
|
return [...docs].sort((a, b) => (
|
||||||
// Current folder first
|
// Current folder first
|
||||||
// @ts-ignore
|
Number(b.loc === loc) - Number(a.loc === loc) ||
|
||||||
(b.loc === loc) - (a.loc === loc) ||
|
|
||||||
// Then subfolders
|
// Then subfolders
|
||||||
// @ts-ignore
|
Number(b.loc.startsWith(locsub)) - Number(a.loc.startsWith(locsub)) ||
|
||||||
(b.loc.slice(0, locsub.length) === locsub) - (a.loc.slice(0, locsub.length) === locsub) ||
|
|
||||||
// Then by location
|
// Then by location
|
||||||
collator.compare(a.loc, b.loc) ||
|
collator.compare(a.loc, b.loc) ||
|
||||||
// Files after folders
|
// Folders before files
|
||||||
// @ts-ignore
|
Number(b.dir) - Number(a.dir) ||
|
||||||
(a.dir === false) - (b.dir === false) ||
|
|
||||||
// Exact name match first
|
// Exact name match first
|
||||||
// @ts-ignore
|
Number(b.name.includes(query)) - Number(a.name.includes(query)) ||
|
||||||
b.name.includes(query) - a.name.includes(query) ||
|
|
||||||
// Finally by name
|
// Finally by name
|
||||||
collator.compare(a.name, b.name)
|
collator.compare(a.name, b.name)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strip haystack before sending back to main thread
|
|
||||||
function stripHaystack(doc: WorkerDoc): DocData {
|
|
||||||
const { haystack, ...rest } = doc
|
|
||||||
return rest
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle incoming messages
|
// Handle incoming messages
|
||||||
self.onmessage = async (e: MessageEvent<IncomingMessage>) => {
|
self.onmessage = async (e: MessageEvent<IncomingMessage>) => {
|
||||||
const msg = e.data
|
const msg = e.data
|
||||||
|
|
||||||
if (msg.type === 'update') {
|
if (msg.type === 'update') {
|
||||||
// Update document list with haystacks
|
// Update document list with haystacks, sorted by mtime descending
|
||||||
documents = msg.documents.map(doc => ({
|
recentDocuments = msg.documents
|
||||||
...doc,
|
.map(doc => ({ ...doc, haystack: normalizeHaystack(doc.name) }))
|
||||||
haystack: haystackFormat(doc.name)
|
.sort((a, b) => b.mtime - a.mtime)
|
||||||
}))
|
clearCache()
|
||||||
recentDocuments = sortByRecent(documents)
|
|
||||||
} else if (msg.type === 'search') {
|
} else if (msg.type === 'search') {
|
||||||
currentSearchId = msg.id
|
currentSearchId = msg.id
|
||||||
if (msg.query) {
|
if (msg.query) {
|
||||||
await performSearch(msg.query, msg.loc, msg.id)
|
await performSearch(msg.query, msg.loc, msg.id)
|
||||||
} else {
|
} else {
|
||||||
// Empty query - no results needed (main thread handles folder listing)
|
// Empty query - no results needed
|
||||||
postMessage({
|
postMessage({ type: 'results', docs: [], id: msg.id, done: true } as ResultMessage)
|
||||||
type: 'results',
|
|
||||||
docs: [],
|
|
||||||
id: msg.id,
|
|
||||||
done: true
|
|
||||||
} as ResultMessage)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,10 @@
|
|||||||
* - Builds to the Python module's frontend-build directory
|
* - Builds to the Python module's frontend-build directory
|
||||||
*
|
*
|
||||||
* Environment variables (with defaults):
|
* Environment variables (with defaults):
|
||||||
* FASTAPI_VUE_BACKEND_URL=http://localhost:5180 - Backend API URL for proxying
|
* FASTAPI_VUE_BACKEND_URL=http://localhost:8999 - Backend API URL for proxying
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const backendUrl = process.env.FASTAPI_VUE_BACKEND_URL || "http://localhost:5180"
|
const backendUrl = process.env.FASTAPI_VUE_BACKEND_URL || "http://localhost:8999"
|
||||||
|
|
||||||
export default function fastapiVue({ paths = ["/api"] } = {}) {
|
export default function fastapiVue({ paths = ["/api"] } = {}) {
|
||||||
// Build proxy configuration for each path
|
// Build proxy configuration for each path
|
||||||
|
|||||||
+6
-6
@@ -28,7 +28,7 @@ dependencies = [
|
|||||||
"av>=15.0.0",
|
"av>=15.0.0",
|
||||||
"blake3>=1.0.5",
|
"blake3>=1.0.5",
|
||||||
"docopt-ng>=0.9.0",
|
"docopt-ng>=0.9.0",
|
||||||
"fastapi-vue>=0.5.1",
|
"fastapi-vue>=0.5.2",
|
||||||
"fastapi[standard]>=0.128.0",
|
"fastapi[standard]>=0.128.0",
|
||||||
"html5tagger>=1.3.0",
|
"html5tagger>=1.3.0",
|
||||||
"httpx>=0.28.0",
|
"httpx>=0.28.0",
|
||||||
@@ -41,10 +41,12 @@ dependencies = [
|
|||||||
"pillow-heif>=1.1.0",
|
"pillow-heif>=1.1.0",
|
||||||
"pyjwt>=2.10.1",
|
"pyjwt>=2.10.1",
|
||||||
"pymupdf>=1.26.3",
|
"pymupdf>=1.26.3",
|
||||||
|
"pyvips[binary]>=3.1.1",
|
||||||
"sanic>=25.12.0",
|
"sanic>=25.12.0",
|
||||||
"setproctitle>=1.3.6",
|
"setproctitle>=1.3.6",
|
||||||
"stream-zip>=0.0.83",
|
"stream-zip>=0.0.83",
|
||||||
"tomli_w>=1.2.0",
|
"tomli_w>=1.2.0",
|
||||||
|
"tracerite>=2.3.1",
|
||||||
"zstandard>=0.24.0",
|
"zstandard>=0.24.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -114,6 +116,7 @@ filterwarnings = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
|
extend-select = ["E402"]
|
||||||
isort.known-first-party = ["cista"]
|
isort.known-first-party = ["cista"]
|
||||||
per-file-ignores."tests/*" = ["S", "ANN", "D", "INP", "PLR2004"]
|
per-file-ignores."tests/*" = ["S", "ANN", "D", "INP", "PLR2004"]
|
||||||
per-file-ignores."scripts/*" = ["T20"]
|
per-file-ignores."scripts/*" = ["T20"]
|
||||||
@@ -121,16 +124,13 @@ per-file-ignores."scripts/*" = ["T20"]
|
|||||||
[dependency-groups]
|
[dependency-groups]
|
||||||
dev = [
|
dev = [
|
||||||
"pytest>=8.4.1",
|
"pytest>=8.4.1",
|
||||||
|
"pytest-asyncio>=0.25.0",
|
||||||
|
"pytest-cov>=7.0.0",
|
||||||
"ruff>=0.8.0",
|
"ruff>=0.8.0",
|
||||||
"mypy>=1.13.0",
|
"mypy>=1.13.0",
|
||||||
"pre-commit>=4.0.0",
|
"pre-commit>=4.0.0",
|
||||||
"httpx>=0.28.1",
|
"httpx>=0.28.1",
|
||||||
]
|
]
|
||||||
test = [
|
|
||||||
"pytest>=8.4.1",
|
|
||||||
"pytest-cov>=6.0.0",
|
|
||||||
"pytest-asyncio>=0.25.0",
|
|
||||||
]
|
|
||||||
|
|
||||||
[tool.coverage.run]
|
[tool.coverage.run]
|
||||||
source = ["cista"]
|
source = ["cista"]
|
||||||
|
|||||||
+25
-18
@@ -2,11 +2,13 @@
|
|||||||
"""Run Vite development server for frontend and Cista backend with auto-reload.
|
"""Run Vite development server for frontend and Cista backend with auto-reload.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
uv run scripts/devserver.py [frontend] [--backend backend]
|
uv run scripts/devserver.py [-l listen] [--backend backend] [cista_args...]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
frontend Vite frontend endpoint (default: localhost:5173)
|
-l, --listen Vite frontend endpoint (default: localhost:8989)
|
||||||
--backend Cista backend endpoint (default: from config, or :8000)
|
--backend Cista backend endpoint (default: from config, or :8999)
|
||||||
|
|
||||||
|
Any additional arguments are passed to the cista command.
|
||||||
|
|
||||||
Environment:
|
Environment:
|
||||||
JS_RUNTIME Path or name of JS runtime to use (deno, npm/node or bun).
|
JS_RUNTIME Path or name of JS runtime to use (deno, npm/node or bun).
|
||||||
@@ -26,10 +28,12 @@ from devutil import ProcessGroup, logger, ready, setup_vite # type: ignore
|
|||||||
from cista import config
|
from cista import config
|
||||||
from cista.serve import parse_listen
|
from cista.serve import parse_listen
|
||||||
|
|
||||||
DEFAULT_BACKEND_PORT = 8000
|
DEFAULT_BACKEND_PORT = 8999
|
||||||
|
|
||||||
|
|
||||||
def setup_sanic_backend(listen: str | None) -> tuple[str, list[str]]:
|
def setup_sanic_backend(
|
||||||
|
listen: str | None, extra_args: list[str]
|
||||||
|
) -> tuple[str, list[str]]:
|
||||||
"""Parse backend listen address and build cista dev command.
|
"""Parse backend listen address and build cista dev command.
|
||||||
|
|
||||||
Returns (url, cmd).
|
Returns (url, cmd).
|
||||||
@@ -40,11 +44,13 @@ def setup_sanic_backend(listen: str | None) -> tuple[str, list[str]]:
|
|||||||
port = opts.get("port", DEFAULT_BACKEND_PORT)
|
port = opts.get("port", DEFAULT_BACKEND_PORT)
|
||||||
host = opts.get("host", "localhost") or "localhost"
|
host = opts.get("host", "localhost") or "localhost"
|
||||||
|
|
||||||
cmd = ["cista", "--dev", "-l", listen]
|
cmd = ["cista", "--dev", "-l", listen] + extra_args
|
||||||
return f"http://{host}:{port}", cmd
|
return f"http://{host}:{port}", cmd
|
||||||
|
|
||||||
|
|
||||||
async def run_devserver(frontend: str | None, backend: str | None) -> None:
|
async def run_devserver(
|
||||||
|
frontend: str | None, backend: str | None, extra_args: list[str]
|
||||||
|
) -> None:
|
||||||
reporoot = Path(__file__).parent.parent
|
reporoot = Path(__file__).parent.parent
|
||||||
front = reporoot / "frontend"
|
front = reporoot / "frontend"
|
||||||
if not (front / "package.json").exists():
|
if not (front / "package.json").exists():
|
||||||
@@ -52,7 +58,7 @@ async def run_devserver(frontend: str | None, backend: str | None) -> None:
|
|||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
frontend_url, npm_install, vite = setup_vite(frontend or "")
|
frontend_url, npm_install, vite = setup_vite(frontend or "")
|
||||||
backend_url, sanic_cmd = setup_sanic_backend(backend)
|
backend_url, sanic_cmd = setup_sanic_backend(backend, extra_args)
|
||||||
|
|
||||||
# Tell vite where to proxy API requests
|
# Tell vite where to proxy API requests
|
||||||
os.environ["FASTAPI_VUE_BACKEND_URL"] = backend_url
|
os.environ["FASTAPI_VUE_BACKEND_URL"] = backend_url
|
||||||
@@ -78,26 +84,27 @@ def main():
|
|||||||
epilog=HELP_EPILOG,
|
epilog=HELP_EPILOG,
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"frontend",
|
"-l",
|
||||||
nargs="?",
|
"--listen",
|
||||||
metavar="host:port",
|
metavar="host:port",
|
||||||
help="Vite frontend endpoint (default: localhost:5173)",
|
help="Vite frontend endpoint (default: localhost:8989)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--backend",
|
"--backend",
|
||||||
"-l",
|
|
||||||
metavar="host:port",
|
metavar="host:port",
|
||||||
help="Cista backend endpoint (default: from config, or :8000)",
|
help="Cista backend endpoint (default: from config, or :8999)",
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args, unknown = parser.parse_known_args()
|
||||||
with contextlib.suppress(KeyboardInterrupt):
|
with contextlib.suppress(KeyboardInterrupt):
|
||||||
asyncio.run(run_devserver(args.frontend, args.backend))
|
asyncio.run(run_devserver(args.listen, args.backend, unknown))
|
||||||
|
|
||||||
|
|
||||||
HELP_EPILOG = """
|
HELP_EPILOG = """
|
||||||
scripts/devserver.py # Default ports
|
scripts/devserver.py # Default ports
|
||||||
scripts/devserver.py 3000 # Vite on localhost:3000
|
scripts/devserver.py -l 3000 # Vite on localhost:3000
|
||||||
scripts/devserver.py :3000 --backend 8080 # Vite on *:3000, backend on :8080
|
scripts/devserver.py -l :3000 --backend 8080 # Vite on *:3000, backend on :8080
|
||||||
|
|
||||||
|
Additional arguments are passed to the cista backend command.
|
||||||
|
|
||||||
JS_RUNTIME environment variable can be used to select the JS runtime
|
JS_RUNTIME environment variable can be used to select the JS runtime
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ def find_dev_tool() -> list[str]:
|
|||||||
Raises RuntimeError if no runtime is found.
|
Raises RuntimeError if no runtime is found.
|
||||||
"""
|
"""
|
||||||
dev_args = {
|
dev_args = {
|
||||||
"deno": ("run", "dev", "--"),
|
"deno": ("run", "-A", "npm:vite"),
|
||||||
"npm": ("--silent", "run", "dev", "--"),
|
"npm": ("--silent", "run", "dev", "--"),
|
||||||
"bun": ("run", "dev", "--"),
|
"bun": ("run", "dev", "--"),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import httpx
|
|||||||
from buildutil import find_dev_tool, find_install_tool, logger
|
from buildutil import find_dev_tool, find_install_tool, logger
|
||||||
from fastapi_vue.hostutil import parse_endpoint
|
from fastapi_vue.hostutil import parse_endpoint
|
||||||
|
|
||||||
DEFAULT_VITE_PORT = 5173
|
DEFAULT_VITE_PORT = 8989
|
||||||
DEFAULT_BACKEND_PORT = 5180
|
DEFAULT_BACKEND_PORT = 8999
|
||||||
|
|
||||||
|
|
||||||
class ProcessGroup:
|
class ProcessGroup:
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
import argparse
|
||||||
|
import mimetypes
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from cista.preview import process_image_with_timing
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args() -> argparse.Namespace:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Generate image previews for all files in a folder, one at a time.",
|
||||||
|
)
|
||||||
|
parser.add_argument("folder", type=Path, help="Folder to scan recursively")
|
||||||
|
parser.add_argument(
|
||||||
|
"--px",
|
||||||
|
type=int,
|
||||||
|
default=1024,
|
||||||
|
help="Maximum preview dimension in pixels (default: 1024)",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--quality",
|
||||||
|
type=int,
|
||||||
|
default=60,
|
||||||
|
help="AVIF quality passed to preview generation (default: 60)",
|
||||||
|
)
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def is_image_file(path: Path) -> bool:
|
||||||
|
mime_type, _ = mimetypes.guess_type(path.name)
|
||||||
|
return bool(mime_type and mime_type.startswith("image/"))
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
args = parse_args()
|
||||||
|
folder = args.folder.resolve()
|
||||||
|
if not folder.is_dir():
|
||||||
|
raise SystemExit(f"Not a directory: {folder}")
|
||||||
|
|
||||||
|
files = sorted(
|
||||||
|
path for path in folder.rglob("*") if path.is_file() and is_image_file(path)
|
||||||
|
)
|
||||||
|
if not files:
|
||||||
|
print(f"No image files found under {folder}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
total_files = 0
|
||||||
|
total_bytes = 0
|
||||||
|
total_timing_slots: list[float] = []
|
||||||
|
total_preview_ms: float = 0.0
|
||||||
|
failures = 0
|
||||||
|
|
||||||
|
print(f"Scanning {folder}")
|
||||||
|
print(f"Generating previews for {len(files)} image files")
|
||||||
|
|
||||||
|
for path in files:
|
||||||
|
total_files += 1
|
||||||
|
rel = path.relative_to(folder)
|
||||||
|
try:
|
||||||
|
preview, timing = process_image_with_timing(
|
||||||
|
path,
|
||||||
|
maxsize=args.px,
|
||||||
|
quality=args.quality,
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
failures += 1
|
||||||
|
print(f"FAIL {rel} error={exc}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
total_bytes += len(preview)
|
||||||
|
timings = timing.timings or []
|
||||||
|
if len(total_timing_slots) < len(timings):
|
||||||
|
total_timing_slots.extend([0.0] * (len(timings) - len(total_timing_slots)))
|
||||||
|
for i, value in enumerate(timings):
|
||||||
|
total_timing_slots[i] += value
|
||||||
|
total_ms = sum(timings)
|
||||||
|
total_preview_ms += total_ms
|
||||||
|
|
||||||
|
detail = " / ".join(f"{value:.1f}ms" for value in timings)
|
||||||
|
if detail:
|
||||||
|
detail = f"timings={detail} total={total_ms:.1f}ms"
|
||||||
|
else:
|
||||||
|
detail = f"total={total_ms:.1f}ms"
|
||||||
|
print(f"OK {rel} backend={timing.backend} bytes={len(preview)} {detail}")
|
||||||
|
|
||||||
|
completed = total_files - failures
|
||||||
|
print()
|
||||||
|
print("Summary")
|
||||||
|
print(f" files={total_files}")
|
||||||
|
print(f" completed={completed}")
|
||||||
|
print(f" failed={failures}")
|
||||||
|
print(f" preview_bytes={total_bytes}")
|
||||||
|
if completed:
|
||||||
|
if total_timing_slots:
|
||||||
|
for i, value in enumerate(total_timing_slots, start=1):
|
||||||
|
print(f" timing{i}_total_ms={value:.1f}")
|
||||||
|
print(f" preview_total_ms={total_preview_ms:.1f}")
|
||||||
|
print(f" preview_avg_ms={total_preview_ms / completed:.1f}")
|
||||||
|
return 0 if failures == 0 else 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -10,7 +10,9 @@ def decode(data: str):
|
|||||||
|
|
||||||
# Helper function to create a list of FileEntry objects
|
# Helper function to create a list of FileEntry objects
|
||||||
def f(count, start=0):
|
def f(count, start=0):
|
||||||
return [FileEntry(i, str(i), str(i), 0, 0, 0) for i in range(start, start + count)]
|
return [
|
||||||
|
FileEntry(i, str(i), str(i), 0, 0, 0, 0) for i in range(start, start + count)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_identical_lists():
|
def test_identical_lists():
|
||||||
@@ -35,8 +37,8 @@ def test_insertions():
|
|||||||
|
|
||||||
|
|
||||||
def test_insertion_at_end():
|
def test_insertion_at_end():
|
||||||
old_list = [*f(3), FileEntry(1, "xxx", "xxx", 0, 0, 1)]
|
old_list = [*f(3), FileEntry(1, "xxx", "xxx", 0, 0, 0, 1)]
|
||||||
newfile = FileEntry(1, "yyy", "yyy", 0, 0, 1)
|
newfile = FileEntry(1, "yyy", "yyy", 0, 0, 0, 1)
|
||||||
new_list = [*old_list, newfile]
|
new_list = [*old_list, newfile]
|
||||||
expected = [UpdKeep(4), UpdIns([newfile])]
|
expected = [UpdKeep(4), UpdIns([newfile])]
|
||||||
assert decode(format_update(old_list, new_list)) == expected
|
assert decode(format_update(old_list, new_list)) == expected
|
||||||
|
|||||||
Reference in New Issue
Block a user