Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd96b2c7ba | ||
|
|
3405248554 | ||
|
|
0c3c3615ce | ||
|
|
07305538dc | ||
|
|
f3b3b5efd9 | ||
|
|
8613d6c25e | ||
|
|
5ed627d9f4 | ||
|
|
f0c3f7a7f9 | ||
|
|
953ec628a0 | ||
|
|
e678c8c267 | ||
|
|
69d58f99e3 | ||
|
|
36764885ed | ||
|
|
5a82560cf2 | ||
|
|
7b1c6f6772 | ||
|
|
c025e7af95 | ||
|
|
3bad311e35 | ||
|
|
fdc4fe0a3e | ||
|
|
5df2308bdb | ||
|
|
f4c44ce1aa | ||
|
|
49232f11cc | ||
|
|
1258eff42d | ||
|
|
718d46e3f9 | ||
|
|
92d9c40a28 | ||
|
|
4f646fb344 | ||
|
|
d6304d0029 | ||
|
|
77e35cf0fc | ||
|
|
bf8a049b92 | ||
|
|
6d7f44bd88 | ||
|
|
e2097a1563 | ||
|
|
b864936eaa | ||
|
|
72b3c0d8ce | ||
|
|
07daf372e8 | ||
|
|
e979d679b2 | ||
|
|
eea66c0013 | ||
|
|
9220c457c0 | ||
|
|
d5b77932ea | ||
|
|
9b9d3e1cc1 | ||
|
|
536efc4ce1 | ||
|
|
1b2267587f | ||
|
|
2864e9f041 | ||
|
|
b5a94b5eee | ||
|
|
d4be755d46 |
+6
-3
@@ -2,11 +2,12 @@ import asyncio
|
|||||||
from secrets import token_bytes
|
from secrets import token_bytes
|
||||||
|
|
||||||
import msgspec
|
import msgspec
|
||||||
|
from mediapreview.office import is_available_cached
|
||||||
from sanic import Blueprint, json
|
from sanic import Blueprint, json
|
||||||
from sanic.exceptions import BadRequest
|
from sanic.exceptions import BadRequest
|
||||||
from sanic.log import logger
|
from sanic.log import logger
|
||||||
|
|
||||||
from cista import __version__, auth, config, onlyoffice, sharefs, sso, watching
|
from cista import __version__, auth, config, sharefs, sso, watching
|
||||||
from cista.auth import (
|
from cista.auth import (
|
||||||
create_share_token_handler,
|
create_share_token_handler,
|
||||||
create_token_handler,
|
create_token_handler,
|
||||||
@@ -40,7 +41,9 @@ async def watch(req, ws):
|
|||||||
if sso.paskia_enabled():
|
if sso.paskia_enabled():
|
||||||
# SSO auth: call validation to get user info (don't enforce auth in public mode)
|
# SSO auth: call validation to get user info (don't enforce auth in public mode)
|
||||||
try:
|
try:
|
||||||
await sso.validate_sso_request(req)
|
# WebSocket cannot forward Set-Cookie, so ask the auth backend not to
|
||||||
|
# renew the session here; renewal happens on the HTTP side instead.
|
||||||
|
await sso.validate_sso_request(req, renew=False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug("watch SSO validation failed: %s", e)
|
logger.debug("watch SSO validation failed: %s", e)
|
||||||
if sso_user := getattr(req.ctx, "sso_user", None):
|
if sso_user := getattr(req.ctx, "sso_user", None):
|
||||||
@@ -65,7 +68,7 @@ async def watch(req, ws):
|
|||||||
"version": __version__,
|
"version": __version__,
|
||||||
"public": config.config.public,
|
"public": config.config.public,
|
||||||
"paskia": sso.paskia_enabled(),
|
"paskia": sso.paskia_enabled(),
|
||||||
"office_previews": await onlyoffice.is_available_cached(),
|
"office_previews": await is_available_cached(),
|
||||||
},
|
},
|
||||||
"user": user_info,
|
"user": user_info,
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-5
@@ -10,8 +10,10 @@ from wsgiref.handlers import format_date_time
|
|||||||
|
|
||||||
import tracerite
|
import tracerite
|
||||||
from blake3 import blake3
|
from blake3 import blake3
|
||||||
|
from mediapreview.office import close_oo_client, log_reachable_info
|
||||||
|
from mediapreview.pool import shutdown_preview_workers, start_preview_workers
|
||||||
from sanic import Sanic, empty, raw, redirect
|
from sanic import Sanic, empty, raw, redirect
|
||||||
from sanic.exceptions import Forbidden, NotFound
|
from sanic.exceptions import Forbidden, NotFound, RequestCancelled
|
||||||
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
|
||||||
@@ -29,11 +31,11 @@ from cista import (
|
|||||||
watching,
|
watching,
|
||||||
)
|
)
|
||||||
from cista.api import bp
|
from cista.api import bp
|
||||||
from cista.preview import shutdown_preview_workers, start_preview_workers
|
|
||||||
from cista.sanic_logging import (
|
from cista.sanic_logging import (
|
||||||
configure_access_logging,
|
configure_access_logging,
|
||||||
configure_main_logging,
|
configure_main_logging,
|
||||||
format_access_log,
|
format_access_log,
|
||||||
|
reset_sanic_log_levels,
|
||||||
)
|
)
|
||||||
from cista.sanic_logging import logger as access_logger
|
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
|
||||||
@@ -86,7 +88,7 @@ async def log_access(req, res):
|
|||||||
path = f"{path}?{qs}"
|
path = f"{path}?{qs}"
|
||||||
extra = getattr(req.ctx, "log_extra", None)
|
extra = getattr(req.ctx, "log_extra", None)
|
||||||
line = format_access_log(
|
line = format_access_log(
|
||||||
client, res.status, req.method, host, path, duration_ms, extra=extra
|
client, res.status, req.method, host, path, duration_ms=duration_ms, extra=extra
|
||||||
)
|
)
|
||||||
access_logger.info(line)
|
access_logger.info(line)
|
||||||
return res
|
return res
|
||||||
@@ -123,12 +125,30 @@ app.blueprint(fileserver.bp)
|
|||||||
app.exception(Exception)(handle_sanic_exception)
|
app.exception(Exception)(handle_sanic_exception)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception(asyncio.CancelledError)
|
||||||
|
async def request_cancelled(req, e):
|
||||||
|
"""Request cancelled mid-flight (client disconnect or server shutdown).
|
||||||
|
|
||||||
|
Sanic wraps this as RequestCancelled (client disconnect only) — a
|
||||||
|
BaseException, so the generic Exception handler above never sees it — and
|
||||||
|
its default handler renders a 500 error page. Report 499 for client
|
||||||
|
disconnects and 503 for server-side cancellation instead; no traceback
|
||||||
|
(quiet=True), since there is nothing to fix. The connection is usually
|
||||||
|
already gone.
|
||||||
|
"""
|
||||||
|
if not getattr(req.ctx, "log_extra", None):
|
||||||
|
req.ctx.log_extra = "cancelled"
|
||||||
|
return empty(499 if isinstance(e, RequestCancelled) else 503)
|
||||||
|
|
||||||
|
|
||||||
setproctitle("cista-main")
|
setproctitle("cista-main")
|
||||||
|
|
||||||
|
|
||||||
@app.before_server_start
|
@app.before_server_start
|
||||||
async def main_start(app):
|
async def main_start(app):
|
||||||
|
reset_sanic_log_levels()
|
||||||
config.load_config()
|
config.load_config()
|
||||||
|
onlyoffice.configure()
|
||||||
setproctitle(f"cista {config.config.path.name}")
|
setproctitle(f"cista {config.config.path.name}")
|
||||||
app.ctx.threadexec = ThreadPoolExecutor(
|
app.ctx.threadexec = ThreadPoolExecutor(
|
||||||
max_workers=4, thread_name_prefix="cista-worker"
|
max_workers=4, thread_name_prefix="cista-worker"
|
||||||
@@ -142,7 +162,7 @@ async def main_start(app):
|
|||||||
@app.after_server_start
|
@app.after_server_start
|
||||||
async def main_after_start(app):
|
async def main_after_start(app):
|
||||||
_ = app
|
_ = app
|
||||||
onlyoffice.log_reachable_info()
|
log_reachable_info()
|
||||||
|
|
||||||
|
|
||||||
# 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)
|
||||||
@@ -150,7 +170,7 @@ async def main_after_start(app):
|
|||||||
async def main_stop(app):
|
async def main_stop(app):
|
||||||
async with asyncio.TaskGroup() as tg:
|
async with asyncio.TaskGroup() as tg:
|
||||||
tg.create_task(asyncio.to_thread(watching.stop, app))
|
tg.create_task(asyncio.to_thread(watching.stop, app))
|
||||||
tg.create_task(onlyoffice.close_oo_client())
|
tg.create_task(close_oo_client())
|
||||||
tg.create_task(shutdown_preview_workers())
|
tg.create_task(shutdown_preview_workers())
|
||||||
tg.create_task(sso.close_client())
|
tg.create_task(sso.close_client())
|
||||||
|
|
||||||
|
|||||||
+35
-7
@@ -574,6 +574,13 @@ def _basic_auth_login(request):
|
|||||||
if username == "token":
|
if username == "token":
|
||||||
token = config.config.tokens.get(password)
|
token = config.config.tokens.get(password)
|
||||||
if token:
|
if token:
|
||||||
|
if _allow_anonymous_share_token(token):
|
||||||
|
request.ctx.session = None
|
||||||
|
request.ctx.username = None
|
||||||
|
request.ctx.user = None
|
||||||
|
request.ctx.auth_token_id = password
|
||||||
|
request.ctx.auth_token = token
|
||||||
|
return None
|
||||||
user = config.config.users.get(token.username)
|
user = config.config.users.get(token.username)
|
||||||
if user:
|
if user:
|
||||||
request.ctx.session = None
|
request.ctx.session = None
|
||||||
@@ -873,14 +880,16 @@ async def verify(request, *, privileged=False):
|
|||||||
"""
|
"""
|
||||||
hydrate_request_auth_context(request, source="auth.verify")
|
hydrate_request_auth_context(request, source="auth.verify")
|
||||||
|
|
||||||
# Public mode: skip auth unless privileged access is required
|
|
||||||
if config.config.public and not privileged:
|
|
||||||
return
|
|
||||||
|
|
||||||
auth_header = request.headers.get("authorization", "")
|
auth_header = request.headers.get("authorization", "")
|
||||||
has_auth_header = bool(auth_header)
|
has_auth_header = bool(auth_header)
|
||||||
scheme = auth_header.split()[0].lower() if has_auth_header else None
|
scheme = auth_header.split()[0].lower() if has_auth_header else None
|
||||||
|
|
||||||
|
# Public mode: skip auth unless privileged access is required.
|
||||||
|
# Still parse explicit Authorization headers so share-token URLs can
|
||||||
|
# activate share scoping even while public access is enabled.
|
||||||
|
if config.config.public and not privileged and not has_auth_header:
|
||||||
|
return
|
||||||
|
|
||||||
# Concise auth flow for diagnostics (populated by use_session + verify)
|
# Concise auth flow for diagnostics (populated by use_session + verify)
|
||||||
auth_flow = list(getattr(request.ctx, "auth_flow", ["session:skipped"]))
|
auth_flow = list(getattr(request.ctx, "auth_flow", ["session:skipped"]))
|
||||||
tried: list[str] = []
|
tried: list[str] = []
|
||||||
@@ -941,6 +950,13 @@ async def verify(request, *, privileged=False):
|
|||||||
quiet=True,
|
quiet=True,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
token = request_share_token(request)
|
||||||
|
if (
|
||||||
|
token is not None
|
||||||
|
and _allow_anonymous_share_token(token)
|
||||||
|
and not privileged
|
||||||
|
):
|
||||||
|
return
|
||||||
elif scheme in ("ntlm", "negotiate"):
|
elif scheme in ("ntlm", "negotiate"):
|
||||||
tried.append("ntlm")
|
tried.append("ntlm")
|
||||||
try:
|
try:
|
||||||
@@ -1275,6 +1291,17 @@ def _token_belongs_to_user(token, username, sso_user_id):
|
|||||||
return bool(sso_user_id is not None and token.sso_user_id == sso_user_id)
|
return bool(sso_user_id is not None and token.sso_user_id == sso_user_id)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_anonymous_share_token(token: config.Token) -> bool:
|
||||||
|
return (
|
||||||
|
sharefs.is_share_token(token) and not token.username and not token.sso_user_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _allow_anonymous_share_token(token: config.Token) -> bool:
|
||||||
|
# Anonymous share links are intentionally coupled to public mode.
|
||||||
|
return config.config.public and _is_anonymous_share_token(token)
|
||||||
|
|
||||||
|
|
||||||
def request_token(request) -> config.Token | None:
|
def request_token(request) -> config.Token | None:
|
||||||
token = getattr(request.ctx, "auth_token", None)
|
token = getattr(request.ctx, "auth_token", None)
|
||||||
return token if isinstance(token, config.Token) else None
|
return token if isinstance(token, config.Token) else None
|
||||||
@@ -1439,10 +1466,11 @@ async def create_share_token_handler(request):
|
|||||||
raise BadRequest("Could not determine SSO user")
|
raise BadRequest("Could not determine SSO user")
|
||||||
else:
|
else:
|
||||||
username = current_username or ""
|
username = current_username or ""
|
||||||
if not username:
|
if username:
|
||||||
|
if username not in config.config.users:
|
||||||
|
raise BadRequest("User does not exist")
|
||||||
|
elif not config.config.public:
|
||||||
raise BadRequest("Could not determine user")
|
raise BadRequest("Could not determine user")
|
||||||
if username not in config.config.users:
|
|
||||||
raise BadRequest("User does not exist")
|
|
||||||
|
|
||||||
token = secrets.token_urlsafe(12)
|
token = secrets.token_urlsafe(12)
|
||||||
changes = {
|
changes = {
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
# Patched OnlyOffice Document Server with configurable converter worker count.
|
|
||||||
#
|
|
||||||
# The Community Edition hardcodes the document converter to 1 worker,
|
|
||||||
# which creates a severe bottleneck under concurrent load.
|
|
||||||
# This image patches the open-source license.js to spawn a configurable
|
|
||||||
# number of converter workers (default 8).
|
|
||||||
#
|
|
||||||
# Build:
|
|
||||||
# docker build -t onlyoffice-cista docker/onlyoffice-converter-patch
|
|
||||||
#
|
|
||||||
# Run:
|
|
||||||
# docker run -d -p 8988:80 \
|
|
||||||
# -e WORKERS=16 \
|
|
||||||
# -e JWT_SECRET=your-strong-secret \
|
|
||||||
# --name onlyoffice onlyoffice-cista
|
|
||||||
#
|
|
||||||
# JWT:
|
|
||||||
# Set JWT_SECRET to the same value you pass to Cista as ONLYOFFICE_JWT_SECRET.
|
|
||||||
# OnlyOffice will enable token validation automatically.
|
|
||||||
#
|
|
||||||
# The ONLYOFFICE_VERSION build arg lets you target a specific release.
|
|
||||||
|
|
||||||
ARG ONLYOFFICE_VERSION=9.3.1
|
|
||||||
|
|
||||||
FROM onlyoffice/documentserver:${ONLYOFFICE_VERSION}
|
|
||||||
|
|
||||||
# Prevent interactive apt prompts
|
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
|
||||||
|
|
||||||
# Install Node.js, npm, and git so we can run the FileConverter from source.
|
|
||||||
RUN apt-get update -qq && \
|
|
||||||
apt-get install -y -qq --no-install-recommends \
|
|
||||||
nodejs \
|
|
||||||
npm \
|
|
||||||
git \
|
|
||||||
ca-certificates && \
|
|
||||||
rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
# Clone the open-source server components (shallow, ~15 MB).
|
|
||||||
# The master branch is used because the Linux/web tags are not published
|
|
||||||
# in the server repo; the license.js file has been stable for years.
|
|
||||||
RUN git clone --depth 1 https://github.com/ONLYOFFICE/server.git /opt/oo-server
|
|
||||||
|
|
||||||
# Patch license.js so the converter worker count is read from an env var
|
|
||||||
# instead of being hardcoded to 1.
|
|
||||||
RUN sed -i \
|
|
||||||
's/count: 1,/count: parseInt(process.env.WORKERS, 10) || 8,/' \
|
|
||||||
/opt/oo-server/Common/sources/license.js
|
|
||||||
|
|
||||||
# Install npm dependencies for the modules the FileConverter touches.
|
|
||||||
# DocService deps are also needed because converter.js pulls in baseConnector.
|
|
||||||
RUN cd /opt/oo-server/Common && npm ci --no-audit --no-fund
|
|
||||||
RUN cd /opt/oo-server/FileConverter && npm ci --no-audit --no-fund
|
|
||||||
RUN cd /opt/oo-server/DocService && npm ci --no-audit --no-fund
|
|
||||||
|
|
||||||
# Back up the compiled pkg binary and replace it with our wrapper.
|
|
||||||
RUN mv /var/www/onlyoffice/documentserver/server/FileConverter/converter \
|
|
||||||
/var/www/onlyoffice/documentserver/server/FileConverter/converter.orig
|
|
||||||
|
|
||||||
COPY converter-wrapper.sh /var/www/onlyoffice/documentserver/server/FileConverter/converter
|
|
||||||
RUN chmod +x /var/www/onlyoffice/documentserver/server/FileConverter/converter
|
|
||||||
|
|
||||||
# Default worker count (override at runtime with -e WORKERS=16).
|
|
||||||
ENV WORKERS=8
|
|
||||||
|
|
||||||
# Use our custom entrypoint to persist the env var to a file that the
|
|
||||||
# non-root converter process (user=ds) can read.
|
|
||||||
COPY entrypoint.sh /app/ds/run-document-server-patched.sh
|
|
||||||
RUN chmod +x /app/ds/run-document-server-patched.sh
|
|
||||||
ENTRYPOINT ["/app/ds/run-document-server-patched.sh"]
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# Wrapper that runs the OnlyOffice FileConverter from patched Node.js source.
|
|
||||||
# Replaces the compiled pkg binary shipped with the Community Edition.
|
|
||||||
|
|
||||||
# The env var is not passed through supervisor to the 'ds' user, so we read
|
|
||||||
# it from a file written by the custom entrypoint.
|
|
||||||
if [ -z "${WORKERS}" ] && [ -r /tmp/oo-converter-workers.txt ]; then
|
|
||||||
export WORKERS=$(cat /tmp/oo-converter-workers.txt)
|
|
||||||
fi
|
|
||||||
|
|
||||||
cd /opt/oo-server/FileConverter || exit 1
|
|
||||||
|
|
||||||
export NODE_ENV=production-linux
|
|
||||||
export NODE_CONFIG_DIR=/etc/onlyoffice/documentserver
|
|
||||||
export NODE_DISABLE_COLORS=1
|
|
||||||
export APPLICATION_NAME=onlyoffice
|
|
||||||
export LD_LIBRARY_PATH=/var/www/onlyoffice/documentserver/server/FileConverter/bin
|
|
||||||
|
|
||||||
exec node sources/convertermaster.js "$@"
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# Custom entrypoint that persists WORKERS to a file readable by
|
|
||||||
# the non-root user that supervisor uses to run the converter.
|
|
||||||
|
|
||||||
echo "${WORKERS:-8}" > /tmp/oo-converter-workers.txt
|
|
||||||
chmod 644 /tmp/oo-converter-workers.txt
|
|
||||||
|
|
||||||
exec /app/ds/run-document-server.sh "$@"
|
|
||||||
+15
-2
@@ -1,9 +1,11 @@
|
|||||||
|
import errno
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from cista import config
|
from cista import config
|
||||||
from cista.util import filename
|
from cista.util import filename
|
||||||
|
from cista.util.diskspace import InsufficientStorageError, check_free_space
|
||||||
from cista.util.lrucache import LRUCache
|
from cista.util.lrucache import LRUCache
|
||||||
|
|
||||||
|
|
||||||
@@ -34,13 +36,24 @@ class File:
|
|||||||
self.open_rw()
|
self.open_rw()
|
||||||
if self.fd is None:
|
if self.fd is None:
|
||||||
raise RuntimeError("file descriptor is not available for write")
|
raise RuntimeError("file descriptor is not available for write")
|
||||||
|
check_free_space(self.path)
|
||||||
if file_size is not None:
|
if file_size is not None:
|
||||||
if pos + len(buffer) > file_size:
|
if pos + len(buffer) > file_size:
|
||||||
raise ValueError("write exceeds declared file size")
|
raise ValueError("write exceeds declared file size")
|
||||||
os.ftruncate(self.fd, file_size)
|
try:
|
||||||
|
os.ftruncate(self.fd, file_size)
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno == errno.ENOSPC:
|
||||||
|
raise InsufficientStorageError("No space left on device") from e
|
||||||
|
raise
|
||||||
if buffer:
|
if buffer:
|
||||||
os.lseek(self.fd, pos, os.SEEK_SET)
|
os.lseek(self.fd, pos, os.SEEK_SET)
|
||||||
os.write(self.fd, buffer)
|
try:
|
||||||
|
os.write(self.fd, buffer)
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno == errno.ENOSPC:
|
||||||
|
raise InsufficientStorageError("No space left on device") from e
|
||||||
|
raise
|
||||||
|
|
||||||
def __getitem__(self, slc):
|
def __getitem__(self, slc):
|
||||||
if self.fd is None:
|
if self.fd is None:
|
||||||
|
|||||||
+19
-8
@@ -1,5 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import errno
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@@ -12,11 +13,12 @@ from urllib.parse import unquote, urlparse
|
|||||||
from wsgiref.handlers import format_date_time
|
from wsgiref.handlers import format_date_time
|
||||||
|
|
||||||
from sanic import Blueprint, HTTPResponse, empty, json
|
from sanic import Blueprint, HTTPResponse, empty, json
|
||||||
from sanic.exceptions import BadRequest, NotFound
|
from sanic.exceptions import BadRequest, NotFound, SanicException
|
||||||
|
|
||||||
from cista import auth, config, sharefs, watching
|
from cista import auth, config, sharefs, watching
|
||||||
from cista.api import fileserver
|
from cista.api import fileserver
|
||||||
from cista.util import filename
|
from cista.util import filename
|
||||||
|
from cista.util.diskspace import InsufficientStorageError
|
||||||
|
|
||||||
bp = Blueprint("fileserver", url_prefix="/files")
|
bp = Blueprint("fileserver", url_prefix="/files")
|
||||||
|
|
||||||
@@ -52,13 +54,22 @@ async def upload_file_chunk(request, name):
|
|||||||
|
|
||||||
rel, path = _safe_relpath(name, request=request)
|
rel, path = _safe_relpath(name, request=request)
|
||||||
rel_name = rel.as_posix()
|
rel_name = rel.as_posix()
|
||||||
upload_info = await asyncio.to_thread(
|
try:
|
||||||
fileserver.upload_info,
|
upload_info = await asyncio.to_thread(
|
||||||
rel_name,
|
fileserver.upload_info,
|
||||||
start,
|
rel_name,
|
||||||
body,
|
start,
|
||||||
total,
|
body,
|
||||||
)
|
total,
|
||||||
|
)
|
||||||
|
except InsufficientStorageError as e:
|
||||||
|
raise SanicException(str(e), status_code=507, quiet=True) from e
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno == errno.ENOSPC:
|
||||||
|
raise SanicException(
|
||||||
|
"No space left on device", status_code=507, quiet=True
|
||||||
|
) from e
|
||||||
|
raise
|
||||||
extras = []
|
extras = []
|
||||||
chunk_len = end - start
|
chunk_len = end - start
|
||||||
whole_file = start == 0 and end == total
|
whole_file = start == 0 and end == total
|
||||||
|
|||||||
+12
-292
@@ -1,134 +1,27 @@
|
|||||||
"""OnlyOffice Document Server integration for office document preview.
|
"""Cista-specific OnlyOffice setup.
|
||||||
|
|
||||||
Provides server-side conversion of office documents to PNG via the
|
The conversion client itself lives in `mediapreview.office`; this module
|
||||||
OnlyOffice Document Server /ConvertService.ashx API. The resulting PNG
|
only bridges cista's config-derived JWT secret into it and wires the
|
||||||
is passed through pyvips for AVIF compression.
|
`--oosetup` Docker bootstrap to cista's config.
|
||||||
|
|
||||||
Environment requirements:
|
|
||||||
- OnlyOffice Document Server must be running and reachable.
|
|
||||||
- If Document Server runs in Docker, the callback host IP must be
|
|
||||||
reachable from the container (usually the docker bridge IP).
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import socket
|
|
||||||
import socketserver
|
|
||||||
import subprocess
|
|
||||||
import threading
|
|
||||||
import urllib.error
|
|
||||||
import urllib.request
|
|
||||||
from functools import partial
|
|
||||||
from http.server import SimpleHTTPRequestHandler
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from time import perf_counter
|
|
||||||
from urllib.parse import quote
|
|
||||||
|
|
||||||
import httpx
|
import mediapreview.office
|
||||||
import jwt
|
|
||||||
from sanic.log import logger
|
|
||||||
|
|
||||||
from cista import config
|
from cista import config
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Configuration helpers
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
def configure() -> None:
|
||||||
_httpx_client: httpx.AsyncClient | None = None
|
"""Point mediapreview's OnlyOffice client at cista's derived JWT secret."""
|
||||||
|
os.environ.setdefault(
|
||||||
|
"ONLYOFFICE_JWT_SECRET", config.derived_secret("onlyoffice", size=16).hex()
|
||||||
def _get_onlyoffice_url() -> str:
|
|
||||||
return os.environ.get("ONLYOFFICE_CISTA_URL", "http://localhost:8988")
|
|
||||||
|
|
||||||
|
|
||||||
def _get_jwt_secret() -> str:
|
|
||||||
return (
|
|
||||||
os.environ.get("ONLYOFFICE_JWT_SECRET")
|
|
||||||
or config.derived_secret("onlyoffice", size=16).hex()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _get_callback_host() -> str:
|
|
||||||
"""Return the host IP that OnlyOffice (usually in Docker) can use to reach us."""
|
|
||||||
if host := os.environ.get("ONLYOFFICE_CALLBACK_HOST"):
|
|
||||||
return host
|
|
||||||
# Try to auto-detect docker bridge IP
|
|
||||||
try:
|
|
||||||
result = subprocess.run(
|
|
||||||
["/sbin/ip", "-4", "addr", "show", "docker0"],
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
timeout=2,
|
|
||||||
check=False,
|
|
||||||
)
|
|
||||||
for line in result.stdout.splitlines():
|
|
||||||
if "inet " in line:
|
|
||||||
parts = line.strip().split()
|
|
||||||
addr_part = parts[1] # e.g. 172.17.0.1/16
|
|
||||||
return addr_part.split("/")[0]
|
|
||||||
except Exception:
|
|
||||||
logger.debug("Failed to auto-detect docker bridge IP")
|
|
||||||
return "127.0.0.1"
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Async HTTP client
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
def get_httpx_client() -> httpx.AsyncClient:
|
|
||||||
"""Return the shared async HTTP client for OnlyOffice requests."""
|
|
||||||
global _httpx_client
|
|
||||||
if _httpx_client is None:
|
|
||||||
_httpx_client = httpx.AsyncClient()
|
|
||||||
return _httpx_client
|
|
||||||
|
|
||||||
|
|
||||||
async def close_oo_client() -> None:
|
|
||||||
"""Close the shared async HTTP client."""
|
|
||||||
global _httpx_client
|
|
||||||
if _httpx_client is not None:
|
|
||||||
await _httpx_client.aclose()
|
|
||||||
_httpx_client = None
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Availability check
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
def _probe_status() -> tuple[bool, bool, str | None]:
|
|
||||||
"""Return (ok, responded, detail) for a lightweight reachability probe."""
|
|
||||||
url = _get_onlyoffice_url().rstrip("/") + "/ConvertService.ashx"
|
|
||||||
try:
|
|
||||||
with urllib.request.urlopen(url, timeout=2) as resp: # noqa: S310
|
|
||||||
status = resp.status
|
|
||||||
except urllib.error.HTTPError as e:
|
|
||||||
status = e.code
|
|
||||||
except Exception:
|
|
||||||
return False, False, None
|
|
||||||
|
|
||||||
if status in (200, 405):
|
|
||||||
return True, True, None
|
|
||||||
if status >= 500:
|
|
||||||
return False, True, f"HTTP {status}"
|
|
||||||
return False, True, f"HTTP {status}"
|
|
||||||
|
|
||||||
|
|
||||||
def log_reachable_info() -> None:
|
|
||||||
"""Log info on success, warning on responded probe errors, silent on no-response."""
|
|
||||||
ok, responded, detail = _probe_status()
|
|
||||||
if ok:
|
|
||||||
logger.info("Using OnlyOffice document server at %s", _get_onlyoffice_url())
|
|
||||||
elif responded:
|
|
||||||
suffix = f": {detail}" if detail else ""
|
|
||||||
logger.warning("OnlyOffice probe failed%s", suffix)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_docker(confdir: Path | None = None) -> int:
|
def setup_docker(confdir: Path | None = None) -> int:
|
||||||
"""Build and run the patched OnlyOffice Docker image."""
|
"""Build and run the patched OnlyOffice Docker image (via mediapreview)."""
|
||||||
if confdir is not None:
|
if confdir is not None:
|
||||||
os.environ["CISTA_HOME"] = confdir.as_posix()
|
os.environ["CISTA_HOME"] = confdir.as_posix()
|
||||||
config.init_confdir()
|
config.init_confdir()
|
||||||
@@ -142,178 +35,5 @@ def setup_docker(confdir: Path | None = None) -> int:
|
|||||||
"public": False,
|
"public": False,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
configure()
|
||||||
secret = config.derived_secret("onlyoffice", size=16).hex()
|
return mediapreview.office.setup_docker()
|
||||||
docker_dir = Path(__file__).parent / "docker"
|
|
||||||
if not docker_dir.is_dir():
|
|
||||||
raise FileNotFoundError(
|
|
||||||
f"Docker files not found at {docker_dir}. Is the package installed correctly?"
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.info("Building OnlyOffice image")
|
|
||||||
build_cmd = ["docker", "build", "-t", "onlyoffice-cista", str(docker_dir)]
|
|
||||||
logger.info("%s", " ".join(build_cmd))
|
|
||||||
result = subprocess.run(build_cmd, check=False, shell=False) # noqa: S603
|
|
||||||
if result.returncode != 0:
|
|
||||||
raise RuntimeError("Failed to build OnlyOffice image")
|
|
||||||
|
|
||||||
logger.info("Starting OnlyOffice container")
|
|
||||||
run_cmd = [
|
|
||||||
"docker",
|
|
||||||
"run",
|
|
||||||
"-d",
|
|
||||||
"-p",
|
|
||||||
"8988:80",
|
|
||||||
"-e",
|
|
||||||
f"JWT_SECRET={secret}",
|
|
||||||
"-e",
|
|
||||||
"WORKERS=8",
|
|
||||||
"--name",
|
|
||||||
"onlyoffice-cista",
|
|
||||||
"--restart",
|
|
||||||
"unless-stopped",
|
|
||||||
"onlyoffice-cista",
|
|
||||||
]
|
|
||||||
logger.info("%s", " ".join(run_cmd))
|
|
||||||
result = subprocess.run(run_cmd, check=False, shell=False) # noqa: S603
|
|
||||||
if result.returncode != 0:
|
|
||||||
raise RuntimeError("Failed to start OnlyOffice container")
|
|
||||||
logger.info("OnlyOffice is running on http://localhost:8988")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
async def is_available_async(request_timeout: float = 2.0) -> bool:
|
|
||||||
"""Return True if the configured OnlyOffice Document Server is reachable."""
|
|
||||||
url = _get_onlyoffice_url().rstrip("/") + "/ConvertService.ashx"
|
|
||||||
client = get_httpx_client()
|
|
||||||
try:
|
|
||||||
response = await client.get(url, timeout=request_timeout)
|
|
||||||
except Exception:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return response.status_code in (200, 405)
|
|
||||||
|
|
||||||
|
|
||||||
_oo_available_cache: tuple[bool, float] | None = None
|
|
||||||
OO_AVAILABILITY_CACHE_TTL = 30.0
|
|
||||||
|
|
||||||
|
|
||||||
async def is_available_cached() -> bool:
|
|
||||||
"""Return cached OnlyOffice availability, refreshed every 30 seconds."""
|
|
||||||
global _oo_available_cache
|
|
||||||
now = perf_counter()
|
|
||||||
if _oo_available_cache is not None:
|
|
||||||
result, timestamp = _oo_available_cache
|
|
||||||
if now - timestamp < OO_AVAILABILITY_CACHE_TTL:
|
|
||||||
return result
|
|
||||||
result = await is_available_async()
|
|
||||||
_oo_available_cache = (result, now)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Temporary HTTP server so OnlyOffice can download the file
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
class _QuietHandler(SimpleHTTPRequestHandler):
|
|
||||||
def log_message(self, fmt, *args) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def _get_free_port() -> int:
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
s.bind(("0.0.0.0", 0)) # noqa: S104
|
|
||||||
return s.getsockname()[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _serve_file_temporarily(file_path: Path):
|
|
||||||
"""Start a temporary HTTP server for *file_path* and return (url, server)."""
|
|
||||||
directory = str(file_path.parent)
|
|
||||||
filename = file_path.name
|
|
||||||
port = _get_free_port()
|
|
||||||
|
|
||||||
handler = partial(_QuietHandler, directory=directory)
|
|
||||||
httpd = socketserver.TCPServer(("0.0.0.0", port), handler) # noqa: S104
|
|
||||||
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
|
|
||||||
thread.start()
|
|
||||||
|
|
||||||
host = _get_callback_host()
|
|
||||||
url = f"http://{host}:{port}/{quote(filename)}"
|
|
||||||
return url, httpd
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# OnlyOffice conversion client
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
def _build_jwt_token(payload: dict) -> str | None:
|
|
||||||
secret = _get_jwt_secret()
|
|
||||||
if not secret:
|
|
||||||
return None
|
|
||||||
return jwt.encode(payload, secret, algorithm="HS256")
|
|
||||||
|
|
||||||
|
|
||||||
async def convert_to_png_async(file_path: Path, request_timeout: float = 5.0) -> bytes:
|
|
||||||
"""Convert *file_path* to PNG using OnlyOffice Document Server (async).
|
|
||||||
|
|
||||||
Returns the PNG bytes. Raises RuntimeError on failure.
|
|
||||||
"""
|
|
||||||
oo_url = _get_onlyoffice_url().rstrip("/")
|
|
||||||
convert_url = f"{oo_url}/ConvertService.ashx"
|
|
||||||
client = get_httpx_client()
|
|
||||||
|
|
||||||
# Start temporary HTTP server so OnlyOffice can fetch the file
|
|
||||||
doc_url, httpd = await asyncio.to_thread(_serve_file_temporarily, file_path)
|
|
||||||
try:
|
|
||||||
suffix = file_path.suffix.lstrip(".").lower()
|
|
||||||
payload = {
|
|
||||||
"async": False,
|
|
||||||
"filetype": suffix,
|
|
||||||
"key": f"cista_{(await asyncio.to_thread(file_path.stat)).st_mtime_ns}",
|
|
||||||
"outputtype": "png",
|
|
||||||
"title": file_path.name,
|
|
||||||
"url": doc_url,
|
|
||||||
}
|
|
||||||
|
|
||||||
headers = {"Content-Type": "application/json"}
|
|
||||||
token = _build_jwt_token(payload)
|
|
||||||
if token:
|
|
||||||
# Conversion API expects JWT in request body when token checks are enabled.
|
|
||||||
payload["token"] = token
|
|
||||||
headers["Authorization"] = token
|
|
||||||
|
|
||||||
t_start = perf_counter()
|
|
||||||
response = await client.post(
|
|
||||||
convert_url,
|
|
||||||
content=json.dumps(payload).encode(),
|
|
||||||
headers=headers,
|
|
||||||
timeout=request_timeout,
|
|
||||||
)
|
|
||||||
response.raise_for_status()
|
|
||||||
body = response.content
|
|
||||||
t_end = perf_counter()
|
|
||||||
|
|
||||||
# Parse XML response
|
|
||||||
text = body.decode("utf-8", errors="replace")
|
|
||||||
if "<Error>" in text:
|
|
||||||
code = "unknown"
|
|
||||||
if "<Error>" in text and "</Error>" in text:
|
|
||||||
code = text.split("<Error>")[1].split("</Error>")[0]
|
|
||||||
raise RuntimeError(f"OnlyOffice conversion error: {code}")
|
|
||||||
|
|
||||||
if "<FileUrl>" not in text:
|
|
||||||
raise RuntimeError("OnlyOffice response did not contain FileUrl")
|
|
||||||
|
|
||||||
file_url = text.split("<FileUrl>")[1].split("</FileUrl>")[0]
|
|
||||||
file_url = file_url.replace("&", "&")
|
|
||||||
|
|
||||||
logger.debug("OnlyOffice converted in %.2fs: %s", t_end - t_start, file_url)
|
|
||||||
|
|
||||||
# Download converted PNG
|
|
||||||
png_response = await client.get(file_url, timeout=request_timeout)
|
|
||||||
png_response.raise_for_status()
|
|
||||||
return png_response.content
|
|
||||||
finally:
|
|
||||||
await asyncio.to_thread(httpd.shutdown)
|
|
||||||
|
|||||||
+54
-567
@@ -1,412 +1,57 @@
|
|||||||
|
"""Preview HTTP blueprint: routing, caching and response building.
|
||||||
|
|
||||||
|
All conversion work is delegated to the mediapreview package (worker pool,
|
||||||
|
OnlyOffice integration, classification); this module only wires it into
|
||||||
|
Sanic with auth, etag negotiation and the in-memory response cache.
|
||||||
|
"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import contextlib
|
import re
|
||||||
import mimetypes
|
|
||||||
import struct
|
|
||||||
import sys
|
|
||||||
import threading
|
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from collections import OrderedDict
|
from pathlib import PurePosixPath
|
||||||
from dataclasses import dataclass
|
|
||||||
from multiprocessing import cpu_count
|
|
||||||
from pathlib import Path, PurePosixPath
|
|
||||||
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 httpx
|
import httpx
|
||||||
import msgspec
|
from mediapreview import CachedPreview, PreviewCache, is_previewable_path
|
||||||
from blake3 import blake3
|
from mediapreview.formats import OFFICE_PREVIEW_SUFFIXES
|
||||||
|
from mediapreview.formats import expected_backend as _expected_preview_backend
|
||||||
|
from mediapreview.office import onlyoffice_error_short_text
|
||||||
|
from mediapreview.pool import (
|
||||||
|
PREVIEW_TIMEOUT,
|
||||||
|
PreviewError,
|
||||||
|
PreviewPoolClosedError,
|
||||||
|
PreviewTimeoutError,
|
||||||
|
generate_office_preview,
|
||||||
|
run_preview,
|
||||||
|
)
|
||||||
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, onlyoffice, sharefs, watching
|
from cista import auth, config, sharefs, watching
|
||||||
from cista.fileio import fuid
|
from cista.fileio import fuid
|
||||||
from cista.preview_worker import (
|
|
||||||
DOC_PREVIEW_SUFFIXES,
|
|
||||||
OFFICE_PREVIEW_SUFFIXES,
|
|
||||||
PreviewRequest,
|
|
||||||
PreviewResponse,
|
|
||||||
)
|
|
||||||
from cista.util.filename import sanitize
|
from cista.util.filename import sanitize
|
||||||
|
|
||||||
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
|
# Global preview cache instance
|
||||||
_preview_cache = PreviewCache(capacity=500)
|
_preview_cache = PreviewCache(capacity=500)
|
||||||
|
|
||||||
PREVIEW_TIMEOUT = 10.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
|
|
||||||
|
|
||||||
|
def _shorten_error(detail: str) -> str:
|
||||||
|
"""Shorten an upstream backend error for the single-line access log.
|
||||||
|
|
||||||
class WorkerChecksumError(Exception):
|
Backend errors arrive verbatim from ffmpeg/pyvips/pymupdf and often carry
|
||||||
"""Raised when worker response checksum does not match the packet."""
|
an '[Errno N]' prefix, the input file path, and multi-line library noise —
|
||||||
|
all redundant with the URL already in the log line. Keep the first line,
|
||||||
|
drop the bracket prefix, and cut at the first ': ' separator.
|
||||||
class WorkerProtocolError(Exception):
|
"""
|
||||||
"""Raised when worker response packet is malformed."""
|
lines = detail.splitlines()
|
||||||
|
if not lines:
|
||||||
|
|
||||||
class _PreviewWorker:
|
|
||||||
def __init__(self, proc: asyncio.subprocess.Process):
|
|
||||||
self.proc = proc
|
|
||||||
|
|
||||||
async def request(
|
|
||||||
self,
|
|
||||||
filepath,
|
|
||||||
quality: int,
|
|
||||||
maxsize: int,
|
|
||||||
maxzoom: float,
|
|
||||||
data: bytes | None = None,
|
|
||||||
):
|
|
||||||
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")
|
|
||||||
|
|
||||||
meta = msgspec.json.encode(
|
|
||||||
PreviewRequest(
|
|
||||||
path=str(filepath),
|
|
||||||
quality=quality,
|
|
||||||
maxsize=maxsize,
|
|
||||||
maxzoom=maxzoom,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
payload = data or b""
|
|
||||||
packet = struct.pack("<II", len(meta), len(payload)) + meta + payload
|
|
||||||
self.proc.stdin.write(packet)
|
|
||||||
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:
|
|
||||||
with contextlib.suppress(ProcessLookupError):
|
|
||||||
self.proc.kill()
|
|
||||||
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._pending: asyncio.PriorityQueue[tuple[int, int, asyncio.Future, tuple]] = (
|
|
||||||
asyncio.PriorityQueue()
|
|
||||||
)
|
|
||||||
self._workers: set[_PreviewWorker] = set()
|
|
||||||
self._dispatchers: list[asyncio.Task] = []
|
|
||||||
self._seq = 0
|
|
||||||
self._closed = False
|
|
||||||
|
|
||||||
async def _read_startup_stderr(self, proc: asyncio.subprocess.Process) -> str:
|
|
||||||
if proc.stderr is None:
|
|
||||||
return ""
|
|
||||||
with contextlib.suppress(TimeoutError):
|
|
||||||
data = await asyncio.wait_for(proc.stderr.read(), timeout=0.5)
|
|
||||||
return data.decode(errors="replace").strip()
|
|
||||||
return ""
|
return ""
|
||||||
|
first_line = re.sub(r"^\[[^\]]*\]\s*", "", lines[0].strip())
|
||||||
async def _spawn_worker(self) -> _PreviewWorker:
|
return first_line.split(": ", 1)[0]
|
||||||
proc = await asyncio.create_subprocess_exec(
|
|
||||||
sys.executable,
|
|
||||||
"-m",
|
|
||||||
"cista.preview_worker",
|
|
||||||
stdin=asyncio.subprocess.PIPE,
|
|
||||||
stdout=asyncio.subprocess.PIPE,
|
|
||||||
stderr=asyncio.subprocess.PIPE,
|
|
||||||
start_new_session=True,
|
|
||||||
)
|
|
||||||
_active_procs.add(proc)
|
|
||||||
try:
|
|
||||||
ready = await asyncio.wait_for(proc.stdout.readexactly(1), timeout=30.0)
|
|
||||||
except TimeoutError as err:
|
|
||||||
with contextlib.suppress(ProcessLookupError):
|
|
||||||
proc.kill()
|
|
||||||
with contextlib.suppress(Exception):
|
|
||||||
await proc.wait()
|
|
||||||
stderr = await self._read_startup_stderr(proc)
|
|
||||||
if stderr:
|
|
||||||
raise WorkerProtocolError(
|
|
||||||
"preview worker failed to become ready: " + stderr.splitlines()[-1]
|
|
||||||
) from err
|
|
||||||
raise WorkerProtocolError("preview worker failed to become ready") from err
|
|
||||||
except asyncio.IncompleteReadError as err:
|
|
||||||
stderr = await self._read_startup_stderr(proc)
|
|
||||||
if stderr:
|
|
||||||
raise WorkerProtocolError(
|
|
||||||
"preview worker exited before signalling readiness: "
|
|
||||||
+ stderr.splitlines()[-1]
|
|
||||||
) from err
|
|
||||||
raise WorkerProtocolError(
|
|
||||||
"preview worker exited before signalling readiness"
|
|
||||||
) from err
|
|
||||||
if ready != b"\x01":
|
|
||||||
raise WorkerProtocolError(f"preview worker ready signal invalid: {ready!r}")
|
|
||||||
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 _dispatch_loop(self) -> None:
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
_priority, _seq, future, args = await self._pending.get()
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
return
|
|
||||||
|
|
||||||
if future.cancelled():
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
|
||||||
worker = await asyncio.wait_for(
|
|
||||||
self._idle.get(), timeout=PREVIEW_TIMEOUT
|
|
||||||
)
|
|
||||||
except TimeoutError:
|
|
||||||
logger.warning(
|
|
||||||
"Preview worker unavailable (%ds) for %s",
|
|
||||||
int(PREVIEW_TIMEOUT),
|
|
||||||
args[0].name,
|
|
||||||
)
|
|
||||||
if not future.done():
|
|
||||||
future.set_exception(
|
|
||||||
PreviewTimeoutError(
|
|
||||||
args[0].name,
|
|
||||||
backend=_expected_preview_backend(args[0]),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
|
|
||||||
filepath = args[0]
|
|
||||||
replace = False
|
|
||||||
try:
|
|
||||||
out, resp = await asyncio.wait_for(
|
|
||||||
worker.request(*args),
|
|
||||||
timeout=PREVIEW_TIMEOUT,
|
|
||||||
)
|
|
||||||
if not future.done():
|
|
||||||
future.set_result((out, resp))
|
|
||||||
except TimeoutError:
|
|
||||||
replace = True
|
|
||||||
if not future.done():
|
|
||||||
future.set_exception(
|
|
||||||
PreviewTimeoutError(
|
|
||||||
filepath.name,
|
|
||||||
backend=_expected_preview_backend(filepath),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
except WorkerChecksumError:
|
|
||||||
replace = True
|
|
||||||
logger.error("Preview checksum mismatch for %s", filepath.name)
|
|
||||||
if not future.done():
|
|
||||||
future.set_exception(
|
|
||||||
PreviewError(f"worker checksum mismatch for {filepath.name}")
|
|
||||||
)
|
|
||||||
except PreviewError as e:
|
|
||||||
if not future.done():
|
|
||||||
future.set_exception(e)
|
|
||||||
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
|
|
||||||
)
|
|
||||||
if not future.done():
|
|
||||||
future.set_exception(
|
|
||||||
PreviewError(
|
|
||||||
f"worker protocol failure for {filepath.name}: {e}"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
replace = True
|
|
||||||
logger.exception(
|
|
||||||
"Unexpected preview worker error for %s", filepath.name
|
|
||||||
)
|
|
||||||
if not future.done():
|
|
||||||
future.set_exception(
|
|
||||||
PreviewError(f"unexpected worker error for {filepath.name}")
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
if replace:
|
|
||||||
await self._replace_worker(worker)
|
|
||||||
elif worker.proc.returncode is None:
|
|
||||||
await self._idle.put(worker)
|
|
||||||
else:
|
|
||||||
await self._replace_worker(worker)
|
|
||||||
|
|
||||||
async def start(self) -> None:
|
|
||||||
workers = await asyncio.gather(
|
|
||||||
*(self._spawn_worker() for _ in range(self.size))
|
|
||||||
)
|
|
||||||
for worker in workers:
|
|
||||||
self._workers.add(worker)
|
|
||||||
await self._idle.put(worker)
|
|
||||||
for _ in range(self.size):
|
|
||||||
self._dispatchers.append(asyncio.create_task(self._dispatch_loop()))
|
|
||||||
|
|
||||||
async def run(
|
|
||||||
self,
|
|
||||||
filepath,
|
|
||||||
quality: int,
|
|
||||||
maxsize: int,
|
|
||||||
maxzoom: float,
|
|
||||||
data: bytes | None = None,
|
|
||||||
):
|
|
||||||
if self._closed:
|
|
||||||
raise PreviewError("preview worker pool closed")
|
|
||||||
loop = asyncio.get_running_loop()
|
|
||||||
future = loop.create_future()
|
|
||||||
self._seq += 1
|
|
||||||
await self._pending.put(
|
|
||||||
(
|
|
||||||
_preview_job_priority(filepath),
|
|
||||||
self._seq,
|
|
||||||
future,
|
|
||||||
(filepath, quality, maxsize, maxzoom, data),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return await future
|
|
||||||
|
|
||||||
async def close(self) -> None:
|
|
||||||
self._closed = True
|
|
||||||
for task in self._dispatchers:
|
|
||||||
task.cancel()
|
|
||||||
if self._dispatchers:
|
|
||||||
await asyncio.gather(*self._dispatchers, return_exceptions=True)
|
|
||||||
self._dispatchers.clear()
|
|
||||||
workers = list(self._workers)
|
|
||||||
self._workers.clear()
|
|
||||||
while not self._pending.empty():
|
|
||||||
try:
|
|
||||||
_priority, _seq, future, _args = self._pending.get_nowait()
|
|
||||||
except asyncio.QueueEmpty:
|
|
||||||
break
|
|
||||||
if not future.done():
|
|
||||||
future.set_exception(PreviewError("preview worker pool closed"))
|
|
||||||
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):
|
|
||||||
with contextlib.suppress(ProcessLookupError):
|
|
||||||
proc.kill()
|
|
||||||
await asyncio.gather(
|
|
||||||
*(proc.wait() for proc in list(_active_procs)), return_exceptions=True
|
|
||||||
)
|
|
||||||
_active_procs.clear()
|
|
||||||
|
|
||||||
|
|
||||||
@bp.on_request
|
@bp.on_request
|
||||||
@@ -415,178 +60,6 @@ async def verify_preview(request):
|
|||||||
await auth.verify(request)
|
await auth.verify(request)
|
||||||
|
|
||||||
|
|
||||||
class PreviewTimeoutError(Exception):
|
|
||||||
"""Raised when the preview subprocess exceeds PREVIEW_TIMEOUT."""
|
|
||||||
|
|
||||||
def __init__(self, message: str, *, backend: str | None = None):
|
|
||||||
super().__init__(message)
|
|
||||||
self.backend = backend
|
|
||||||
|
|
||||||
|
|
||||||
class PreviewError(Exception):
|
|
||||||
"""Raised when the preview subprocess exits with a non-zero status."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
message: str,
|
|
||||||
*,
|
|
||||||
stderr: str | None = None,
|
|
||||||
backend: str | None = None,
|
|
||||||
):
|
|
||||||
super().__init__(message)
|
|
||||||
self.stderr = stderr
|
|
||||||
self.backend = backend
|
|
||||||
|
|
||||||
|
|
||||||
# Max concurrent OnlyOffice conversion requests. OO has its own queue;
|
|
||||||
# we must not flood it. This is intentionally small.
|
|
||||||
OO_MAX_CONCURRENT = PREVIEW_WORKERS
|
|
||||||
|
|
||||||
|
|
||||||
class OOConversionManager:
|
|
||||||
"""Manages async OnlyOffice conversions with deduplication and concurrency limits."""
|
|
||||||
|
|
||||||
def __init__(self, max_concurrent: int = OO_MAX_CONCURRENT):
|
|
||||||
self._semaphore = asyncio.Semaphore(max_concurrent)
|
|
||||||
self._in_flight: dict[str, asyncio.Future[bytes]] = {}
|
|
||||||
self._tasks: set[asyncio.Task[None]] = set()
|
|
||||||
self._lock = asyncio.Lock()
|
|
||||||
|
|
||||||
async def convert(self, filepath: Path) -> bytes:
|
|
||||||
"""Return PNG bytes for *filepath*, deduplicating concurrent requests."""
|
|
||||||
stat = await asyncio.to_thread(filepath.stat)
|
|
||||||
key = f"{filepath}:{stat.st_mtime_ns}"
|
|
||||||
|
|
||||||
async with self._lock:
|
|
||||||
if key in self._in_flight:
|
|
||||||
future = self._in_flight[key]
|
|
||||||
else:
|
|
||||||
future = asyncio.get_running_loop().create_future()
|
|
||||||
self._in_flight[key] = future
|
|
||||||
task = asyncio.create_task(self._do_convert(filepath, key, future))
|
|
||||||
self._tasks.add(task)
|
|
||||||
task.add_done_callback(self._tasks.discard)
|
|
||||||
|
|
||||||
return await future
|
|
||||||
|
|
||||||
async def _do_convert(
|
|
||||||
self, filepath: Path, key: str, future: asyncio.Future[bytes]
|
|
||||||
) -> None:
|
|
||||||
try:
|
|
||||||
async with self._semaphore:
|
|
||||||
png_bytes = await onlyoffice.convert_to_png_async(
|
|
||||||
filepath, request_timeout=5.0
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
if not future.done():
|
|
||||||
future.set_exception(e)
|
|
||||||
async with self._lock:
|
|
||||||
self._in_flight.pop(key, None)
|
|
||||||
else:
|
|
||||||
if not future.done():
|
|
||||||
future.set_result(png_bytes)
|
|
||||||
async with self._lock:
|
|
||||||
self._in_flight.pop(key, None)
|
|
||||||
|
|
||||||
|
|
||||||
_oo_manager: OOConversionManager | None = None
|
|
||||||
|
|
||||||
|
|
||||||
def get_oo_manager() -> OOConversionManager:
|
|
||||||
"""Return the singleton OOConversionManager."""
|
|
||||||
global _oo_manager
|
|
||||||
if _oo_manager is None:
|
|
||||||
_oo_manager = OOConversionManager(max_concurrent=OO_MAX_CONCURRENT)
|
|
||||||
return _oo_manager
|
|
||||||
|
|
||||||
|
|
||||||
async def _generate_office_preview(
|
|
||||||
filepath: Path, quality: int, maxsize: int, maxzoom: float
|
|
||||||
) -> tuple[bytes | None, PreviewResponse | None]:
|
|
||||||
"""Generate a preview for an office file using OnlyOffice + worker AVIF conversion."""
|
|
||||||
manager = get_oo_manager()
|
|
||||||
t_oo_start = perf_counter()
|
|
||||||
png_bytes = await manager.convert(filepath)
|
|
||||||
t_oo_end = perf_counter()
|
|
||||||
|
|
||||||
img, resp = await _run_preview_process(
|
|
||||||
filepath, quality, maxsize, maxzoom, data=png_bytes
|
|
||||||
)
|
|
||||||
|
|
||||||
if resp is not None:
|
|
||||||
resp.backend = "onlyoffice+" + (resp.backend or "pyvips")
|
|
||||||
if resp.timings:
|
|
||||||
resp.timings = [round((t_oo_end - t_oo_start) * 1000, 1), *resp.timings]
|
|
||||||
return img, resp
|
|
||||||
|
|
||||||
|
|
||||||
async def _run_preview_process(
|
|
||||||
filepath, quality: int, maxsize: int, maxzoom: float, data: bytes | None = None
|
|
||||||
) -> 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, data)
|
|
||||||
|
|
||||||
|
|
||||||
def _onlyoffice_error_short_text(detail: str) -> str:
|
|
||||||
if detail.startswith("OnlyOffice conversion error:"):
|
|
||||||
code = detail.rsplit(":", 1)[-1].strip()
|
|
||||||
return {
|
|
||||||
"-8": "onlyoffice jwt error",
|
|
||||||
"-4": "onlyoffice input error",
|
|
||||||
"-2": "onlyoffice timeout error",
|
|
||||||
"-1": "onlyoffice unknown error",
|
|
||||||
}.get(code, f"onlyoffice {code} error")
|
|
||||||
if "OnlyOffice response did not contain FileUrl" in detail:
|
|
||||||
return "onlyoffice no-fileurl error"
|
|
||||||
return "onlyoffice error"
|
|
||||||
|
|
||||||
|
|
||||||
def _preview_job_priority(path) -> int:
|
|
||||||
"""Return priority for preview job (lower=higher priority).
|
|
||||||
|
|
||||||
Priority order: images (0) < video (1) < PDF (2) < office (3) < unknown (4)
|
|
||||||
"""
|
|
||||||
suffix = path.suffix.lower()
|
|
||||||
if suffix in DOC_PREVIEW_SUFFIXES:
|
|
||||||
return 2
|
|
||||||
if suffix in OFFICE_PREVIEW_SUFFIXES:
|
|
||||||
return 3
|
|
||||||
mime_type, _ = mimetypes.guess_type(path.name)
|
|
||||||
if mime_type and mime_type.startswith("image/"):
|
|
||||||
return 0
|
|
||||||
if mime_type and mime_type.startswith("video/"):
|
|
||||||
return 1
|
|
||||||
return 4
|
|
||||||
|
|
||||||
|
|
||||||
def _expected_preview_backend(path: Path) -> str:
|
|
||||||
"""Best-effort backend label used for timeout/access logging."""
|
|
||||||
suffix = path.suffix.lower()
|
|
||||||
if suffix in OFFICE_PREVIEW_SUFFIXES:
|
|
||||||
return "onlyoffice"
|
|
||||||
if suffix in DOC_PREVIEW_SUFFIXES:
|
|
||||||
return "pdf"
|
|
||||||
mime_type, _ = mimetypes.guess_type(path.name)
|
|
||||||
if mime_type and mime_type.startswith("video/"):
|
|
||||||
return "video"
|
|
||||||
if mime_type and mime_type.startswith("image/"):
|
|
||||||
return "pyvips"
|
|
||||||
return "preview"
|
|
||||||
|
|
||||||
|
|
||||||
def is_previewable_path(path) -> bool:
|
|
||||||
suffix = path.suffix.lower()
|
|
||||||
if suffix in DOC_PREVIEW_SUFFIXES or suffix in OFFICE_PREVIEW_SUFFIXES:
|
|
||||||
return True
|
|
||||||
mime_type, _ = mimetypes.guess_type(path.name)
|
|
||||||
if not mime_type:
|
|
||||||
return False
|
|
||||||
return mime_type.startswith(("image/", "video/"))
|
|
||||||
|
|
||||||
|
|
||||||
@bp.get("/<path:path>")
|
@bp.get("/<path:path>")
|
||||||
async def preview(req, path):
|
async def preview(req, path):
|
||||||
"""Preview a file"""
|
"""Preview a file"""
|
||||||
@@ -629,12 +102,12 @@ async def preview(req, path):
|
|||||||
try:
|
try:
|
||||||
if filepath.suffix.lower() in OFFICE_PREVIEW_SUFFIXES:
|
if filepath.suffix.lower() in OFFICE_PREVIEW_SUFFIXES:
|
||||||
img, preview_resp = await asyncio.wait_for(
|
img, preview_resp = await asyncio.wait_for(
|
||||||
_generate_office_preview(filepath, quality, maxsize, maxzoom),
|
generate_office_preview(filepath, quality, maxsize, maxzoom),
|
||||||
timeout=PREVIEW_TIMEOUT,
|
timeout=PREVIEW_TIMEOUT,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
img, preview_resp = await asyncio.wait_for(
|
img, preview_resp = await asyncio.wait_for(
|
||||||
_run_preview_process(filepath, quality, maxsize, maxzoom),
|
run_preview(filepath, quality, maxsize, maxzoom),
|
||||||
timeout=PREVIEW_TIMEOUT,
|
timeout=PREVIEW_TIMEOUT,
|
||||||
)
|
)
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
@@ -654,22 +127,36 @@ async def preview(req, path):
|
|||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
detail = str(e)
|
detail = str(e)
|
||||||
if detail.startswith("OnlyOffice"):
|
if detail.startswith("OnlyOffice"):
|
||||||
req.ctx.log_extra = _onlyoffice_error_short_text(detail)
|
req.ctx.log_extra = onlyoffice_error_short_text(detail)
|
||||||
return empty(503)
|
return empty(503)
|
||||||
raise
|
raise
|
||||||
|
except PreviewPoolClosedError:
|
||||||
|
# Server is shutting down; not an error, just a cancelled preview.
|
||||||
|
req.ctx.log_extra = "preview cancelled"
|
||||||
|
return empty(503)
|
||||||
except PreviewError as e:
|
except PreviewError as e:
|
||||||
if e.backend:
|
|
||||||
req.ctx.log_extra = e.backend
|
|
||||||
detail = str(e)
|
detail = str(e)
|
||||||
if detail == "preview worker error" and e.stderr:
|
if detail == "preview worker error" and e.stderr:
|
||||||
captured = e.stderr.strip()
|
captured = e.stderr.strip()
|
||||||
if captured:
|
if captured:
|
||||||
detail = captured.splitlines()[0]
|
detail = captured.splitlines()[0]
|
||||||
logger.error("%s preview: %s", filepath, detail)
|
# The worker already logged the failure (with traceback where the
|
||||||
|
# error occurred) — annotate the access log instead of re-logging,
|
||||||
|
# with a shortened reason. In dev mode, print the full error too.
|
||||||
|
backend = e.backend or _expected_preview_backend(filepath)
|
||||||
|
if req.app.debug:
|
||||||
|
full = detail
|
||||||
|
if e.stderr and e.stderr.strip() not in detail:
|
||||||
|
full = f"{detail}\n{e.stderr.strip()}"
|
||||||
|
logger.warning("[%s] preview failed: %s", backend, full.strip())
|
||||||
|
short = _shorten_error(detail)
|
||||||
|
req.ctx.log_extra = f"{backend}: {short}" if short else backend
|
||||||
return empty(422)
|
return empty(422)
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
|
# Server shutdown or client disconnect: the connection is being torn
|
||||||
|
# down, so responding is impossible — just annotate the access log.
|
||||||
req.ctx.log_extra = "preview cancelled"
|
req.ctx.log_extra = "preview cancelled"
|
||||||
return empty(503)
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Unhandled preview error for %s", filepath)
|
logger.exception("Unhandled preview error for %s", filepath)
|
||||||
return empty(500)
|
return empty(500)
|
||||||
|
|||||||
@@ -1,561 +0,0 @@
|
|||||||
"""Preview generation worker subprocess and synchronous preview engine.
|
|
||||||
|
|
||||||
Two modes are supported:
|
|
||||||
1) Legacy one-shot mode: argv has path/quality/maxsize/maxzoom.
|
|
||||||
2) Long-lived mode: read framed requests from stdin and write framed responses.
|
|
||||||
|
|
||||||
Framed request format (stdin):
|
|
||||||
(uint32 json size)(uint32 data size)(json)(binary data)
|
|
||||||
|
|
||||||
Framed response format (stdout):
|
|
||||||
(blake3(packet))(uint32 json size)(uint32 payload size)(json)(binary payload)
|
|
||||||
where packet = (uint32 json size)(uint32 payload size)(json)(binary payload).
|
|
||||||
"""
|
|
||||||
|
|
||||||
import contextlib
|
|
||||||
import gc
|
|
||||||
import io
|
|
||||||
import logging
|
|
||||||
import mimetypes
|
|
||||||
import shlex
|
|
||||||
import struct
|
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
import tempfile
|
|
||||||
from pathlib import Path
|
|
||||||
from time import perf_counter
|
|
||||||
|
|
||||||
import av
|
|
||||||
import fitz # PyMuPDF
|
|
||||||
import msgspec
|
|
||||||
import numpy as np
|
|
||||||
import pyvips
|
|
||||||
from blake3 import blake3
|
|
||||||
|
|
||||||
from cista import config
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
AVIF_FAST_EFFORT = 0
|
|
||||||
|
|
||||||
DOC_PREVIEW_SUFFIXES = {".pdf", ".xps", ".epub", ".mobi"}
|
|
||||||
|
|
||||||
OFFICE_PREVIEW_SUFFIXES = {
|
|
||||||
".doc",
|
|
||||||
".dot",
|
|
||||||
".docx",
|
|
||||||
".docm",
|
|
||||||
".dotx",
|
|
||||||
".dotm",
|
|
||||||
".rtf",
|
|
||||||
".odt",
|
|
||||||
".ott",
|
|
||||||
".txt",
|
|
||||||
".md",
|
|
||||||
".mhtml",
|
|
||||||
".mht",
|
|
||||||
".html",
|
|
||||||
".htm",
|
|
||||||
".xml",
|
|
||||||
".wps",
|
|
||||||
".wri",
|
|
||||||
# Spreadsheets
|
|
||||||
".xls",
|
|
||||||
".xlsx",
|
|
||||||
".xlsm",
|
|
||||||
".xlsb",
|
|
||||||
".xltx",
|
|
||||||
".xltm",
|
|
||||||
".ods",
|
|
||||||
".ots",
|
|
||||||
".csv",
|
|
||||||
# Presentations
|
|
||||||
".ppt",
|
|
||||||
".pptx",
|
|
||||||
".pptm",
|
|
||||||
".pps",
|
|
||||||
".ppsx",
|
|
||||||
".pot",
|
|
||||||
".potx",
|
|
||||||
".odp",
|
|
||||||
".otp",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
width: int | None = None
|
|
||||||
height: int | None = None
|
|
||||||
|
|
||||||
|
|
||||||
_enc = msgspec.json.Encoder()
|
|
||||||
_dec_req = msgspec.json.Decoder(PreviewRequest)
|
|
||||||
|
|
||||||
|
|
||||||
def _read_exactly(f, n: int) -> bytes:
|
|
||||||
buf = b""
|
|
||||||
while len(buf) < n:
|
|
||||||
chunk = f.read(n - len(buf))
|
|
||||||
if not chunk:
|
|
||||||
raise EOFError
|
|
||||||
buf += chunk
|
|
||||||
return buf
|
|
||||||
|
|
||||||
|
|
||||||
def _read_request() -> tuple[PreviewRequest, bytes] | None:
|
|
||||||
try:
|
|
||||||
header = _read_exactly(sys.stdin.buffer, 8)
|
|
||||||
except EOFError:
|
|
||||||
return None
|
|
||||||
json_size, data_size = struct.unpack("<II", header)
|
|
||||||
meta_raw = _read_exactly(sys.stdin.buffer, json_size)
|
|
||||||
data = b""
|
|
||||||
if data_size:
|
|
||||||
data = _read_exactly(sys.stdin.buffer, data_size)
|
|
||||||
req = _dec_req.decode(meta_raw)
|
|
||||||
return req, data
|
|
||||||
|
|
||||||
|
|
||||||
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 dispatch(path, quality, maxsize, maxzoom, data=None):
|
|
||||||
backend = "unknown"
|
|
||||||
try:
|
|
||||||
if data:
|
|
||||||
backend = "pyvips"
|
|
||||||
return process_image_buffer(
|
|
||||||
data, quality=quality, maxsize=maxsize, maxzoom=maxzoom
|
|
||||||
)
|
|
||||||
suffix = path.suffix.lower()
|
|
||||||
if suffix in DOC_PREVIEW_SUFFIXES:
|
|
||||||
backend = "pdf"
|
|
||||||
return process_pdf(path, quality=quality, maxsize=maxsize, maxzoom=maxzoom)
|
|
||||||
mime_type, _ = mimetypes.guess_type(path.name)
|
|
||||||
if mime_type and mime_type.startswith("video/"):
|
|
||||||
backend = "video"
|
|
||||||
return process_video(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:
|
|
||||||
return None, PreviewResponse(ok=False, backend=backend, error=str(e))
|
|
||||||
except Exception as e:
|
|
||||||
logger.exception("Preview dispatch failed for %s", path)
|
|
||||||
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):
|
|
||||||
return process_image_pyvips(path, maxsize=maxsize, quality=quality)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_image_dimensions(path: Path) -> tuple[int, int] | None:
|
|
||||||
"""Probe image dimensions.
|
|
||||||
|
|
||||||
pyvips can read the header of most formats (including HEIC) without
|
|
||||||
fully decoding the image.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
img = pyvips.Image.new_from_file(str(path))
|
|
||||||
img = img.autorot()
|
|
||||||
except pyvips.error.Error:
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return img.width, img.height
|
|
||||||
|
|
||||||
|
|
||||||
def _image_via_ffmpeg(path: Path, maxsize: int, quality: int) -> bytes:
|
|
||||||
"""Convert any image to AVIF using ffmpeg CLI.
|
|
||||||
|
|
||||||
ffmpeg handles HEIC tile assembly, EXIF rotation, HDR metadata and
|
|
||||||
ICC profile embedding automatically.
|
|
||||||
"""
|
|
||||||
dims = _get_image_dimensions(path)
|
|
||||||
crf = int(63 * (1 - quality / 100) ** 2)
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".avif", delete=False) as tmp_f:
|
|
||||||
tmp_path = tmp_f.name
|
|
||||||
cmd = [
|
|
||||||
"ffmpeg",
|
|
||||||
"-y",
|
|
||||||
"-i",
|
|
||||||
str(path),
|
|
||||||
"-frames:v",
|
|
||||||
"1",
|
|
||||||
"-c:v",
|
|
||||||
"av1",
|
|
||||||
"-crf",
|
|
||||||
str(crf),
|
|
||||||
"-cpu-used",
|
|
||||||
"8",
|
|
||||||
tmp_path,
|
|
||||||
]
|
|
||||||
if dims is not None:
|
|
||||||
w, h = dims
|
|
||||||
if max(w, h) > maxsize:
|
|
||||||
scale = min(maxsize / w, maxsize / h)
|
|
||||||
new_w = int(w * scale)
|
|
||||||
new_h = int(h * scale)
|
|
||||||
# insert -s <wxh> right after the input file
|
|
||||||
cmd.insert(4, "-s")
|
|
||||||
cmd.insert(5, f"{new_w}x{new_h}")
|
|
||||||
try:
|
|
||||||
try:
|
|
||||||
subprocess.run(cmd, capture_output=True, check=True, shell=False) # noqa: S603
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
shell_cmd = shlex.join(cmd)
|
|
||||||
stderr = (e.stderr or b"").decode(errors="replace").strip()
|
|
||||||
if stderr:
|
|
||||||
raise RuntimeError(
|
|
||||||
f"ffmpeg failed (exit {e.returncode}): {shell_cmd}\n{stderr}"
|
|
||||||
) from e
|
|
||||||
raise RuntimeError(
|
|
||||||
f"ffmpeg failed (exit {e.returncode}): {shell_cmd}"
|
|
||||||
) from e
|
|
||||||
with Path(tmp_path).open("rb") as f:
|
|
||||||
return f.read()
|
|
||||||
finally:
|
|
||||||
Path(tmp_path).unlink(missing_ok=True)
|
|
||||||
|
|
||||||
|
|
||||||
def process_image_pyvips(path, *, maxsize, quality):
|
|
||||||
t_start = perf_counter()
|
|
||||||
suffix = path.suffix.lower()
|
|
||||||
|
|
||||||
# HEIC/HEIF: ffmpeg handles tile assembly and HDR correctly;
|
|
||||||
# skip pyvips entirely.
|
|
||||||
if suffix in (".heic", ".heif"):
|
|
||||||
heic_dims = _get_image_dimensions(path)
|
|
||||||
width, height = heic_dims or (None, None)
|
|
||||||
ret = _image_via_ffmpeg(path, maxsize, quality)
|
|
||||||
t_end = perf_counter()
|
|
||||||
return ret, PreviewResponse(
|
|
||||||
ok=True,
|
|
||||||
mime="image/avif",
|
|
||||||
backend="ffmpeg",
|
|
||||||
timings=[round((t_end - t_start) * 1000, 1)],
|
|
||||||
width=width,
|
|
||||||
height=height,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Other image formats: pyvips first, ffmpeg fallback.
|
|
||||||
load_opts = {"access": "sequential"}
|
|
||||||
orig_w = orig_h = None
|
|
||||||
try:
|
|
||||||
img = pyvips.Image.new_from_file(str(path), **load_opts)
|
|
||||||
img = img.autorot()
|
|
||||||
orig_w, orig_h = img.width, img.height
|
|
||||||
scale = min(maxsize / img.width, maxsize / img.height, 1.0)
|
|
||||||
if scale < 1.0:
|
|
||||||
img = img.resize(scale)
|
|
||||||
ret = img.write_to_buffer(
|
|
||||||
".avif",
|
|
||||||
Q=quality,
|
|
||||||
effort=AVIF_FAST_EFFORT,
|
|
||||||
strip=True,
|
|
||||||
)
|
|
||||||
backend = "pyvips"
|
|
||||||
except pyvips.error.Error:
|
|
||||||
orig_w, orig_h = None, None
|
|
||||||
ret = _image_via_ffmpeg(path, maxsize, quality)
|
|
||||||
backend = "ffmpeg"
|
|
||||||
t_end = perf_counter()
|
|
||||||
|
|
||||||
return ret, PreviewResponse(
|
|
||||||
ok=True,
|
|
||||||
mime="image/avif",
|
|
||||||
backend=backend,
|
|
||||||
timings=[round((t_end - t_start) * 1000, 1)],
|
|
||||||
width=orig_w,
|
|
||||||
height=orig_h,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def process_image_buffer(data: bytes, *, quality, maxsize, maxzoom):
|
|
||||||
_ = maxzoom
|
|
||||||
t_start = perf_counter()
|
|
||||||
img = pyvips.Image.new_from_buffer(data, "")
|
|
||||||
img = img.autorot()
|
|
||||||
orig_w, orig_h = img.width, img.height
|
|
||||||
scale = min(maxsize / img.width, maxsize / img.height, 1.0)
|
|
||||||
if scale < 1.0:
|
|
||||||
img = img.resize(scale)
|
|
||||||
ret = img.write_to_buffer(
|
|
||||||
".avif",
|
|
||||||
Q=quality,
|
|
||||||
effort=AVIF_FAST_EFFORT,
|
|
||||||
strip=True,
|
|
||||||
)
|
|
||||||
t_end = perf_counter()
|
|
||||||
|
|
||||||
return ret, PreviewResponse(
|
|
||||||
ok=True,
|
|
||||||
mime="image/avif",
|
|
||||||
backend="pyvips",
|
|
||||||
timings=[round((t_end - t_start) * 1000, 1)],
|
|
||||||
width=orig_w,
|
|
||||||
height=orig_h,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def process_pdf(path, *, maxsize, maxzoom, quality, page_number=0):
|
|
||||||
t_load_start = perf_counter()
|
|
||||||
pdf = fitz.open(path)
|
|
||||||
page = pdf.load_page(page_number)
|
|
||||||
w, h = page.rect[2:4]
|
|
||||||
zoom = min(maxsize / w, maxsize / h, maxzoom)
|
|
||||||
mat = fitz.Matrix(zoom, zoom)
|
|
||||||
pix = page.get_pixmap(matrix=mat)
|
|
||||||
t_load_end = perf_counter()
|
|
||||||
|
|
||||||
t_save_start = perf_counter()
|
|
||||||
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()
|
|
||||||
|
|
||||||
return ret, PreviewResponse(
|
|
||||||
ok=True,
|
|
||||||
mime="image/avif",
|
|
||||||
backend=backend,
|
|
||||||
timings=[
|
|
||||||
round((t_load_end - t_load_start) * 1000, 1),
|
|
||||||
round((t_save_end - t_save_start) * 1000, 1),
|
|
||||||
],
|
|
||||||
width=round(w),
|
|
||||||
height=round(h),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def process_video(path, *, maxsize, quality):
|
|
||||||
frame = None
|
|
||||||
imgdata = io.BytesIO()
|
|
||||||
istream = ostream = icc = occ = frame = None
|
|
||||||
t_load_start = perf_counter()
|
|
||||||
# Initialize to avoid "possibly unbound" in static analysis when exceptions occur
|
|
||||||
t_load_end = t_load_start
|
|
||||||
t_save_start = t_load_start
|
|
||||||
t_save_end = t_load_start
|
|
||||||
with (
|
|
||||||
av.open(
|
|
||||||
str(path),
|
|
||||||
options={
|
|
||||||
"analyzeduration": "1000000", # 1 second (in microseconds)
|
|
||||||
"fflags": "fastseek",
|
|
||||||
},
|
|
||||||
) as icontainer,
|
|
||||||
av.open(imgdata, "w", format="avif") as ocontainer,
|
|
||||||
):
|
|
||||||
istream = icontainer.streams.video[0]
|
|
||||||
istream.codec_context.skip_frame = "NONKEY"
|
|
||||||
icontainer.seek((icontainer.duration or 0) // 8)
|
|
||||||
for frame in icontainer.decode(istream):
|
|
||||||
if frame.dts is not None:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise RuntimeError("No frames found in video")
|
|
||||||
|
|
||||||
# Resize frame to thumbnail size
|
|
||||||
# Capture display dimensions before resize (accounting for rotation)
|
|
||||||
disp_w = frame.width
|
|
||||||
disp_h = frame.height
|
|
||||||
if frame.rotation in (90, 270):
|
|
||||||
disp_w, disp_h = disp_h, disp_w
|
|
||||||
if frame.width > maxsize or frame.height > maxsize:
|
|
||||||
scale_factor = min(maxsize / frame.width, maxsize / frame.height)
|
|
||||||
new_width = int(frame.width * scale_factor)
|
|
||||||
new_height = int(frame.height * scale_factor)
|
|
||||||
frame = frame.reformat(width=new_width, height=new_height)
|
|
||||||
|
|
||||||
# Apply EXIF rotation if present
|
|
||||||
if frame.rotation:
|
|
||||||
# frame.rotation indicates clockwise rotation needed to display correctly
|
|
||||||
# np.rot90 rotates counter-clockwise, so we negate k
|
|
||||||
k = (frame.rotation // 90) % 4 # Convert to counter-clockwise rotations
|
|
||||||
if k == 2:
|
|
||||||
# 180° rotation can be done in YUV420p, preserving HDR
|
|
||||||
try:
|
|
||||||
fplanes = frame.to_ndarray()
|
|
||||||
# Split into Y, U, V planes of proper dimensions
|
|
||||||
planes = [
|
|
||||||
fplanes[: frame.height],
|
|
||||||
fplanes[
|
|
||||||
frame.height : frame.height + frame.height // 4
|
|
||||||
].reshape(frame.height // 2, frame.width // 2),
|
|
||||||
fplanes[frame.height + frame.height // 4 :].reshape(
|
|
||||||
frame.height // 2, frame.width // 2
|
|
||||||
),
|
|
||||||
]
|
|
||||||
# Rotate each plane by 180°
|
|
||||||
planes = [np.rot90(p, 2) for p in planes]
|
|
||||||
# Restore PyAV format
|
|
||||||
planes = np.hstack([p.flat for p in planes]).reshape(
|
|
||||||
-1, planes[0].shape[1]
|
|
||||||
)
|
|
||||||
frame = av.VideoFrame.from_ndarray(planes, format=frame.format.name)
|
|
||||||
del planes, fplanes
|
|
||||||
except Exception:
|
|
||||||
logger.exception("Error rotating video frame by 180°")
|
|
||||||
elif k in (1, 3):
|
|
||||||
# 90° or 270° rotation requires RGB conversion (loses HDR)
|
|
||||||
try:
|
|
||||||
rgb = frame.to_ndarray(format="rgb24")
|
|
||||||
rgb = np.rot90(rgb, k)
|
|
||||||
frame = av.VideoFrame.from_ndarray(rgb, format="rgb24")
|
|
||||||
frame = frame.reformat(
|
|
||||||
format="yuv420p"
|
|
||||||
) # Convert back for encoding
|
|
||||||
del rgb
|
|
||||||
except Exception:
|
|
||||||
logger.exception(
|
|
||||||
"Error rotating video frame by %s°", frame.rotation
|
|
||||||
)
|
|
||||||
|
|
||||||
# libsvtav1 rejects full-range JPEG-style YUV pixel formats such as
|
|
||||||
# yuvj420p, so normalize them before opening the encoder.
|
|
||||||
if frame.format.name.startswith("yuvj"):
|
|
||||||
frame = frame.reformat(format="yuv420p")
|
|
||||||
t_load_end = perf_counter()
|
|
||||||
|
|
||||||
t_save_start = perf_counter()
|
|
||||||
crf = str(int(63 * (1 - quality / 100) ** 2)) # Closely matching PIL quality-%
|
|
||||||
ostream = ocontainer.add_stream(
|
|
||||||
"av1",
|
|
||||||
options={
|
|
||||||
"crf": crf,
|
|
||||||
"usage": "realtime",
|
|
||||||
"cpu-used": "8",
|
|
||||||
"threads": "1",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if not isinstance(ostream, av.VideoStream):
|
|
||||||
raise TypeError("failed to initialize AV1 video stream")
|
|
||||||
ostream.width = frame.width
|
|
||||||
ostream.height = frame.height
|
|
||||||
ostream.pix_fmt = frame.format.name
|
|
||||||
icc = istream.codec_context
|
|
||||||
occ = ostream.codec_context
|
|
||||||
|
|
||||||
# Copy HDR metadata from input video stream
|
|
||||||
occ.color_primaries = icc.color_primaries
|
|
||||||
occ.color_trc = icc.color_trc
|
|
||||||
occ.colorspace = icc.colorspace
|
|
||||||
occ.color_range = icc.color_range
|
|
||||||
|
|
||||||
ocontainer.mux(ostream.encode(frame))
|
|
||||||
ocontainer.mux(ostream.encode(None)) # Flush the stream
|
|
||||||
t_save_end = perf_counter()
|
|
||||||
|
|
||||||
# Capture result before cleanup
|
|
||||||
ret = imgdata.getvalue()
|
|
||||||
resp = PreviewResponse(
|
|
||||||
ok=True,
|
|
||||||
mime="image/avif",
|
|
||||||
backend="video",
|
|
||||||
timings=[
|
|
||||||
round((t_load_end - t_load_start) * 1000, 1),
|
|
||||||
round((t_save_end - t_save_start) * 1000, 1),
|
|
||||||
],
|
|
||||||
width=disp_w,
|
|
||||||
height=disp_h,
|
|
||||||
)
|
|
||||||
del imgdata, istream, ostream, icc, occ, frame
|
|
||||||
gc.collect()
|
|
||||||
return ret, resp
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
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:
|
|
||||||
while True:
|
|
||||||
result = _read_request()
|
|
||||||
if result is None:
|
|
||||||
return
|
|
||||||
req, data = result
|
|
||||||
stderr_capture = io.StringIO()
|
|
||||||
handler = logging.StreamHandler(stderr_capture)
|
|
||||||
root_logger = logging.getLogger()
|
|
||||||
root_logger.addHandler(handler)
|
|
||||||
try:
|
|
||||||
with contextlib.redirect_stderr(stderr_capture):
|
|
||||||
result, resp = dispatch(
|
|
||||||
Path(req.path), req.quality, req.maxsize, req.maxzoom, data
|
|
||||||
)
|
|
||||||
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:
|
|
||||||
logger.exception("Preview worker error for %s", req.path)
|
|
||||||
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)
|
|
||||||
try:
|
|
||||||
config.load_config()
|
|
||||||
logger.warning(
|
|
||||||
"preview-worker config=%s master_secret=%s",
|
|
||||||
config.conffile,
|
|
||||||
config.config.secret,
|
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
logger.exception("preview-worker failed to load config at startup")
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
_run_once()
|
|
||||||
return
|
|
||||||
# Eagerly import heavy modules before signalling readiness so the parent
|
|
||||||
# does not hand us a request while we are still initialising.
|
|
||||||
sys.stdout.buffer.write(b"\x01")
|
|
||||||
sys.stdout.buffer.flush()
|
|
||||||
_run_loop()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
+28
-31
@@ -3,11 +3,13 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import unicodedata
|
|
||||||
from ipaddress import IPv6Address
|
from ipaddress import IPv6Address
|
||||||
|
|
||||||
from sanic.log import LOGGING_CONFIG_DEFAULTS
|
from sanic.log import LOGGING_CONFIG_DEFAULTS
|
||||||
|
|
||||||
|
from cista.util.logformat import EmojiFormatter as _EmojiFormatter
|
||||||
|
from cista.util.logformat import display_width as _display_width
|
||||||
|
|
||||||
logger = logging.getLogger("cista.access")
|
logger = logging.getLogger("cista.access")
|
||||||
|
|
||||||
|
|
||||||
@@ -132,14 +134,6 @@ def format_duration_ms(duration_ms: float) -> str:
|
|||||||
return f"{hours}h{minutes}m"
|
return f"{hours}h{minutes}m"
|
||||||
|
|
||||||
|
|
||||||
def _display_width(text: str) -> int:
|
|
||||||
return sum(
|
|
||||||
1 + (unicodedata.east_asian_width(c) in "FW")
|
|
||||||
for c in text
|
|
||||||
if unicodedata.category(c) != "Mn"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _format_left(label: str) -> str:
|
def _format_left(label: str) -> str:
|
||||||
return label[:19].ljust(19)
|
return label[:19].ljust(19)
|
||||||
|
|
||||||
@@ -156,6 +150,7 @@ def format_access_log(
|
|||||||
method: str,
|
method: str,
|
||||||
host: str,
|
host: str,
|
||||||
path: str,
|
path: str,
|
||||||
|
*,
|
||||||
duration_ms: float,
|
duration_ms: float,
|
||||||
extra: str | None = None,
|
extra: str | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
@@ -278,28 +273,6 @@ def configure_access_logging() -> None:
|
|||||||
logger.propagate = False
|
logger.propagate = False
|
||||||
|
|
||||||
|
|
||||||
_LEVEL_EMOJI = {
|
|
||||||
logging.DEBUG: "🔍",
|
|
||||||
logging.INFO: "ℹ️", # noqa: RUF001
|
|
||||||
logging.WARNING: "⚠️",
|
|
||||||
logging.ERROR: "🛑",
|
|
||||||
logging.CRITICAL: "🛑",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _format_level_prefix(levelno: int) -> str:
|
|
||||||
emoji = _LEVEL_EMOJI.get(levelno, "▪️")
|
|
||||||
prefix = f"{emoji} "
|
|
||||||
return prefix + (" " * max(0, 3 - _display_width(prefix)))
|
|
||||||
|
|
||||||
|
|
||||||
class _EmojiFormatter(logging.Formatter):
|
|
||||||
"""Compact formatter: emoji + message, no timestamp/level text/logger name."""
|
|
||||||
|
|
||||||
def format(self, record: logging.LogRecord) -> str:
|
|
||||||
return _format_level_prefix(record.levelno) + record.getMessage()
|
|
||||||
|
|
||||||
|
|
||||||
def configure_main_logging() -> None:
|
def configure_main_logging() -> None:
|
||||||
"""Replace Sanic's verbose 'Main yyyy-mm-dd INFO:' prefix with emoji-only format.
|
"""Replace Sanic's verbose 'Main yyyy-mm-dd INFO:' prefix with emoji-only format.
|
||||||
|
|
||||||
@@ -313,7 +286,31 @@ def configure_main_logging() -> None:
|
|||||||
LOGGING_CONFIG_DEFAULTS["formatters"]["generic"] = {
|
LOGGING_CONFIG_DEFAULTS["formatters"]["generic"] = {
|
||||||
"class": "cista.sanic_logging._EmojiFormatter",
|
"class": "cista.sanic_logging._EmojiFormatter",
|
||||||
}
|
}
|
||||||
|
# Sanic passes its "sanic.websockets" logger to websockets' ServerProtocol,
|
||||||
|
# so "connection closed" (websockets >= 17, INFO) and Sanic's own
|
||||||
|
# "Websocket timed out waiting for pong" (WARNING) both emit via
|
||||||
|
# sanic.websockets, not websockets.server. Raise it to ERROR so these
|
||||||
|
# routine disconnect messages are dropped while real errors still show.
|
||||||
|
# Patch the config defaults too, so the level survives Sanic's dictConfig.
|
||||||
|
LOGGING_CONFIG_DEFAULTS["loggers"]["sanic.websockets"]["level"] = "ERROR"
|
||||||
|
logging.getLogger("sanic.websockets").setLevel(logging.ERROR)
|
||||||
|
# Preview worker timeouts are already annotated in the access log extra;
|
||||||
|
# the pool's WARNING would otherwise fall to logging.lastResort, printing
|
||||||
|
# a bare message with no level prefix.
|
||||||
|
logging.getLogger("mediapreview.pool").setLevel(logging.ERROR)
|
||||||
# Also reformat any handlers already attached (covers the initial Sanic() call)
|
# Also reformat any handlers already attached (covers the initial Sanic() call)
|
||||||
for name in ("sanic.root", "sanic.error", "sanic.server", "sanic.websockets"):
|
for name in ("sanic.root", "sanic.error", "sanic.server", "sanic.websockets"):
|
||||||
for handler in logging.getLogger(name).handlers:
|
for handler in logging.getLogger(name).handlers:
|
||||||
handler.setFormatter(_EmojiFormatter())
|
handler.setFormatter(_EmojiFormatter())
|
||||||
|
|
||||||
|
|
||||||
|
def reset_sanic_log_levels() -> None:
|
||||||
|
"""Force Sanic's loggers back to INFO in debug/dev mode.
|
||||||
|
|
||||||
|
Debug mode enables DEBUG on sanic.root at runtime
|
||||||
|
(ApplicationState.set_mode calls logger.setLevel(DEBUG)), which unleashes
|
||||||
|
useless noise like the 'Error Page:' content-negotiation messages. Call
|
||||||
|
from before_server_start so the override lands after Sanic's own setup.
|
||||||
|
"""
|
||||||
|
for name in ("sanic.root", "sanic.error", "sanic.server"):
|
||||||
|
logging.getLogger(name).setLevel(logging.INFO)
|
||||||
|
|||||||
+11
-1
@@ -62,12 +62,18 @@ async def close_client():
|
|||||||
_client = None
|
_client = None
|
||||||
|
|
||||||
|
|
||||||
async def validate_sso_request(request, *, perm: str = "cista:login") -> dict | None:
|
async def validate_sso_request(
|
||||||
|
request, *, perm: str = "cista:login", renew: bool = True
|
||||||
|
) -> dict | None:
|
||||||
"""Validate an SSO request against the auth backend.
|
"""Validate an SSO request against the auth backend.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
request: The Sanic request object
|
request: The Sanic request object
|
||||||
perm: Permission to validate (default: cista:login, privileged also cista:admin)
|
perm: Permission to validate (default: cista:login, privileged also cista:admin)
|
||||||
|
renew: Whether to allow the auth backend to renew the session cookie.
|
||||||
|
Use ``False`` for WebSocket validation where Set-Cookie cannot be
|
||||||
|
forwarded to the client; this makes the request read-only and avoids
|
||||||
|
resetting the backend renewal timeout.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
User info dict if valid, None if validation fails with auth required response
|
User info dict if valid, None if validation fails with auth required response
|
||||||
@@ -88,12 +94,16 @@ async def validate_sso_request(request, *, perm: str = "cista:login") -> dict |
|
|||||||
headers["cookie"] = request.headers["cookie"]
|
headers["cookie"] = request.headers["cookie"]
|
||||||
if "authorization" in request.headers:
|
if "authorization" in request.headers:
|
||||||
headers["authorization"] = request.headers["authorization"]
|
headers["authorization"] = request.headers["authorization"]
|
||||||
|
if "user-agent" in request.headers:
|
||||||
|
headers["user-agent"] = request.headers["user-agent"]
|
||||||
headers["accept"] = "application/json"
|
headers["accept"] = "application/json"
|
||||||
headers["x-forwarded-for"] = request.client_ip
|
headers["x-forwarded-for"] = request.client_ip
|
||||||
headers["x-forwarded-host"] = request.host
|
headers["x-forwarded-host"] = request.host
|
||||||
headers["x-forwarded-proto"] = request.scheme
|
headers["x-forwarded-proto"] = request.scheme
|
||||||
|
|
||||||
url = f"{PASKIA_BACKEND_URL}/auth/api/validate?perm={perm}"
|
url = f"{PASKIA_BACKEND_URL}/auth/api/validate?perm={perm}"
|
||||||
|
if not renew:
|
||||||
|
url += "&renew=0"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = await client.post(
|
response = await client.post(
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import shutil
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
MIN_FREE_BYTES = 128 * 1024 * 1024
|
||||||
|
_CHECK_CACHE_TTL = 1.0
|
||||||
|
|
||||||
|
|
||||||
|
class InsufficientStorageError(Exception):
|
||||||
|
"""Raised when there is not enough disk space for an operation."""
|
||||||
|
|
||||||
|
|
||||||
|
_cache: dict[Path, tuple[float, int]] = {}
|
||||||
|
_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
|
def check_free_space(path: Path) -> None:
|
||||||
|
"""Raise InsufficientStorageError if free space on the filesystem containing *path*
|
||||||
|
|
||||||
|
is below MIN_FREE_BYTES. Results are cached per directory for 1 second.
|
||||||
|
"""
|
||||||
|
check_path = path.parent if path.parent.exists() else path
|
||||||
|
check_path = check_path.resolve()
|
||||||
|
|
||||||
|
now = time.monotonic()
|
||||||
|
with _lock:
|
||||||
|
ts, free = _cache.get(check_path, (0, 0))
|
||||||
|
if now - ts < _CHECK_CACHE_TTL:
|
||||||
|
if free < MIN_FREE_BYTES:
|
||||||
|
raise InsufficientStorageError(
|
||||||
|
f"Insufficient storage: {free} bytes free, "
|
||||||
|
f"need at least {MIN_FREE_BYTES} bytes"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
free = shutil.disk_usage(check_path).free
|
||||||
|
except OSError as e:
|
||||||
|
raise InsufficientStorageError(f"Cannot check disk usage: {e}") from e
|
||||||
|
|
||||||
|
with _lock:
|
||||||
|
_cache[check_path] = (now, free)
|
||||||
|
|
||||||
|
if free < MIN_FREE_BYTES:
|
||||||
|
raise InsufficientStorageError(
|
||||||
|
f"Insufficient storage: {free} bytes free, "
|
||||||
|
f"need at least {MIN_FREE_BYTES} bytes"
|
||||||
|
)
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
"""Shared log formatting helpers with no Sanic dependency.
|
||||||
|
|
||||||
|
Used by the main process (cista.sanic_logging) and by the preview worker
|
||||||
|
subprocess, which must not import Sanic.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import unicodedata
|
||||||
|
|
||||||
|
LEVEL_EMOJI = {
|
||||||
|
logging.DEBUG: "🔍",
|
||||||
|
logging.INFO: "ℹ️", # noqa: RUF001
|
||||||
|
logging.WARNING: "⚠️",
|
||||||
|
logging.ERROR: "🛑",
|
||||||
|
logging.CRITICAL: "🛑",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def display_width(text: str) -> int:
|
||||||
|
return sum(
|
||||||
|
1 + (unicodedata.east_asian_width(c) in "FW")
|
||||||
|
for c in text
|
||||||
|
if unicodedata.category(c) != "Mn"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def format_level_prefix(levelno: int) -> str:
|
||||||
|
emoji = LEVEL_EMOJI.get(levelno, "▪️")
|
||||||
|
prefix = f"{emoji} "
|
||||||
|
return prefix + (" " * max(0, 3 - display_width(prefix)))
|
||||||
|
|
||||||
|
|
||||||
|
class EmojiFormatter(logging.Formatter):
|
||||||
|
"""Compact formatter: emoji + message, no timestamp/level text/logger name."""
|
||||||
|
|
||||||
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
|
return format_level_prefix(record.levelno) + record.getMessage()
|
||||||
+10
-3
@@ -9,7 +9,6 @@ from os import stat_result
|
|||||||
from pathlib import Path, PurePosixPath
|
from pathlib import Path, PurePosixPath
|
||||||
from stat import S_ISDIR, S_ISREG
|
from stat import S_ISDIR, S_ISREG
|
||||||
|
|
||||||
import inotify.adapters
|
|
||||||
import msgspec
|
import msgspec
|
||||||
from natsort import humansorted, natsort_keygen, ns
|
from natsort import humansorted, natsort_keygen, ns
|
||||||
from sanic.log import logger
|
from sanic.log import logger
|
||||||
@@ -18,6 +17,11 @@ 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
|
||||||
|
|
||||||
|
try:
|
||||||
|
import inotify.adapters as inotify_adapters
|
||||||
|
except Exception:
|
||||||
|
inotify_adapters = None
|
||||||
|
|
||||||
# Platform-specific allocated size calculation
|
# Platform-specific allocated size calculation
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
import ctypes
|
import ctypes
|
||||||
@@ -665,10 +669,13 @@ DEBOUNCE_MAX = 0.1 # But no more than 100ms total
|
|||||||
|
|
||||||
def watcher(loop):
|
def watcher(loop):
|
||||||
"""Unified watcher thread handling inotify, websocket signals, and periodic scans."""
|
"""Unified watcher thread handling inotify, websocket signals, and periodic scans."""
|
||||||
use_inotify = sys.platform == "linux"
|
use_inotify = sys.platform == "linux" and inotify_adapters is not None
|
||||||
inotify_tree = None
|
inotify_tree = None
|
||||||
modified_flags = frozenset()
|
modified_flags = frozenset()
|
||||||
|
|
||||||
|
if sys.platform == "linux" and inotify_adapters is None:
|
||||||
|
logger.warning("inotify unavailable; falling back to periodic scanning")
|
||||||
|
|
||||||
if use_inotify:
|
if use_inotify:
|
||||||
modified_flags = frozenset(
|
modified_flags = frozenset(
|
||||||
(
|
(
|
||||||
@@ -684,7 +691,7 @@ def watcher(loop):
|
|||||||
|
|
||||||
while not stop_event.is_set():
|
while not stop_event.is_set():
|
||||||
if use_inotify:
|
if use_inotify:
|
||||||
inotify_tree = inotify.adapters.InotifyTree(rootpath.as_posix())
|
inotify_tree = inotify_adapters.InotifyTree(rootpath.as_posix())
|
||||||
|
|
||||||
# Initialize the tree from filesystem
|
# Initialize the tree from filesystem
|
||||||
try:
|
try:
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
services:
|
services:
|
||||||
onlyoffice:
|
onlyoffice:
|
||||||
build:
|
build:
|
||||||
context: ./docker/onlyoffice-converter-patch
|
context: ./mediapreview/mediapreview/docker
|
||||||
args:
|
args:
|
||||||
ONLYOFFICE_VERSION: "9.3.1"
|
ONLYOFFICE_VERSION: "9.3.1"
|
||||||
container_name: onlyoffice
|
container_name: onlyoffice
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
# Patched OnlyOffice Document Server with configurable converter worker count.
|
|
||||||
#
|
|
||||||
# The Community Edition hardcodes the document converter to 1 worker,
|
|
||||||
# which creates a severe bottleneck under concurrent load.
|
|
||||||
# This image patches the open-source license.js to spawn a configurable
|
|
||||||
# number of converter workers (default 8).
|
|
||||||
#
|
|
||||||
# Build:
|
|
||||||
# docker build -t onlyoffice-cista docker/onlyoffice-converter-patch
|
|
||||||
#
|
|
||||||
# Run:
|
|
||||||
# docker run -d -p 8988:80 \
|
|
||||||
# -e WORKERS=16 \
|
|
||||||
# -e JWT_SECRET=your-strong-secret \
|
|
||||||
# --name onlyoffice onlyoffice-cista
|
|
||||||
#
|
|
||||||
# JWT:
|
|
||||||
# Set JWT_SECRET to the same value you pass to Cista as ONLYOFFICE_JWT_SECRET.
|
|
||||||
# OnlyOffice will enable token validation automatically.
|
|
||||||
#
|
|
||||||
# The ONLYOFFICE_VERSION build arg lets you target a specific release.
|
|
||||||
|
|
||||||
ARG ONLYOFFICE_VERSION=9.3.1
|
|
||||||
|
|
||||||
FROM onlyoffice/documentserver:${ONLYOFFICE_VERSION}
|
|
||||||
|
|
||||||
# Prevent interactive apt prompts
|
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
|
||||||
|
|
||||||
# Install Node.js, npm, and git so we can run the FileConverter from source.
|
|
||||||
RUN apt-get update -qq && \
|
|
||||||
apt-get install -y -qq --no-install-recommends \
|
|
||||||
nodejs \
|
|
||||||
npm \
|
|
||||||
git \
|
|
||||||
ca-certificates && \
|
|
||||||
rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
# Clone the open-source server components (shallow, ~15 MB).
|
|
||||||
# The master branch is used because the Linux/web tags are not published
|
|
||||||
# in the server repo; the license.js file has been stable for years.
|
|
||||||
RUN git clone --depth 1 https://github.com/ONLYOFFICE/server.git /opt/oo-server
|
|
||||||
|
|
||||||
# Patch license.js so the converter worker count is read from an env var
|
|
||||||
# instead of being hardcoded to 1.
|
|
||||||
RUN sed -i \
|
|
||||||
's/count: 1,/count: parseInt(process.env.WORKERS, 10) || 8,/' \
|
|
||||||
/opt/oo-server/Common/sources/license.js
|
|
||||||
|
|
||||||
# Install npm dependencies for the modules the FileConverter touches.
|
|
||||||
# DocService deps are also needed because converter.js pulls in baseConnector.
|
|
||||||
RUN cd /opt/oo-server/Common && npm ci --no-audit --no-fund
|
|
||||||
RUN cd /opt/oo-server/FileConverter && npm ci --no-audit --no-fund
|
|
||||||
RUN cd /opt/oo-server/DocService && npm ci --no-audit --no-fund
|
|
||||||
|
|
||||||
# Back up the compiled pkg binary and replace it with our wrapper.
|
|
||||||
RUN mv /var/www/onlyoffice/documentserver/server/FileConverter/converter \
|
|
||||||
/var/www/onlyoffice/documentserver/server/FileConverter/converter.orig
|
|
||||||
|
|
||||||
COPY converter-wrapper.sh /var/www/onlyoffice/documentserver/server/FileConverter/converter
|
|
||||||
RUN chmod +x /var/www/onlyoffice/documentserver/server/FileConverter/converter
|
|
||||||
|
|
||||||
# Default worker count (override at runtime with -e WORKERS=16).
|
|
||||||
ENV WORKERS=8
|
|
||||||
|
|
||||||
# Use our custom entrypoint to persist the env var to a file that the
|
|
||||||
# non-root converter process (user=ds) can read.
|
|
||||||
COPY entrypoint.sh /app/ds/run-document-server-patched.sh
|
|
||||||
RUN chmod +x /app/ds/run-document-server-patched.sh
|
|
||||||
ENTRYPOINT ["/app/ds/run-document-server-patched.sh"]
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# Wrapper that runs the OnlyOffice FileConverter from patched Node.js source.
|
|
||||||
# Replaces the compiled pkg binary shipped with the Community Edition.
|
|
||||||
|
|
||||||
# The env var is not passed through supervisor to the 'ds' user, so we read
|
|
||||||
# it from a file written by the custom entrypoint.
|
|
||||||
if [ -z "${WORKERS}" ] && [ -r /tmp/oo-converter-workers.txt ]; then
|
|
||||||
export WORKERS=$(cat /tmp/oo-converter-workers.txt)
|
|
||||||
fi
|
|
||||||
|
|
||||||
cd /opt/oo-server/FileConverter || exit 1
|
|
||||||
|
|
||||||
export NODE_ENV=production-linux
|
|
||||||
export NODE_CONFIG_DIR=/etc/onlyoffice/documentserver
|
|
||||||
export NODE_DISABLE_COLORS=1
|
|
||||||
export APPLICATION_NAME=onlyoffice
|
|
||||||
export LD_LIBRARY_PATH=/var/www/onlyoffice/documentserver/server/FileConverter/bin
|
|
||||||
|
|
||||||
exec node sources/convertermaster.js "$@"
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# Custom entrypoint that persists WORKERS to a file readable by
|
|
||||||
# the non-root user that supervisor uses to run the converter.
|
|
||||||
|
|
||||||
echo "${WORKERS:-8}" > /tmp/oo-converter-workers.txt
|
|
||||||
chmod 644 /tmp/oo-converter-workers.txt
|
|
||||||
|
|
||||||
exec /app/ds/run-document-server.sh "$@"
|
|
||||||
@@ -6,9 +6,8 @@
|
|||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "run-p type-check \"build-only {@}\" --",
|
"build": "run-p type-check \"build-only {@}\" --",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
"test:unit": "vitest",
|
|
||||||
"build-only": "vite build",
|
"build-only": "vite build",
|
||||||
"type-check": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
|
"type-check": "vue-tsc --noEmit -p tsconfig.app.json --composite false",
|
||||||
"lint": "biome lint .",
|
"lint": "biome lint .",
|
||||||
"format": "biome format --write .",
|
"format": "biome format --write .",
|
||||||
"format:check": "biome format --check .",
|
"format:check": "biome format --check .",
|
||||||
@@ -18,8 +17,11 @@
|
|||||||
"node": ">=18.0.0"
|
"node": ">=18.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@codemirror/language-data": "^6.5.2",
|
||||||
|
"@codemirror/theme-one-dark": "^6.1.3",
|
||||||
"@imengyu/vue3-context-menu": "^1.5.3",
|
"@imengyu/vue3-context-menu": "^1.5.3",
|
||||||
"@vueuse/core": "^14.1.0",
|
"@vueuse/core": "^14.1.0",
|
||||||
|
"codemirror": "^6.0.2",
|
||||||
"esbuild": "^0.27.2",
|
"esbuild": "^0.27.2",
|
||||||
"lodash": "^4.17.23",
|
"lodash": "^4.17.23",
|
||||||
"lodash-es": "^4.17.23",
|
"lodash-es": "^4.17.23",
|
||||||
@@ -34,17 +36,13 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@biomejs/biome": "^1.9.4",
|
"@biomejs/biome": "^1.9.4",
|
||||||
"@tsconfig/node18": "^18.2.6",
|
"@tsconfig/node18": "^18.2.6",
|
||||||
"@types/jsdom": "^27.0.0",
|
|
||||||
"@types/lodash-es": "^4.17.12",
|
"@types/lodash-es": "^4.17.12",
|
||||||
"@types/node": "^25.1.0",
|
"@types/node": "^25.1.0",
|
||||||
"@vitejs/plugin-vue": "^6.0.3",
|
"@vitejs/plugin-vue": "^6.0.3",
|
||||||
"@vue/test-utils": "^2.4.6",
|
|
||||||
"@vue/tsconfig": "^0.8.1",
|
"@vue/tsconfig": "^0.8.1",
|
||||||
"jsdom": "^27.4.0",
|
|
||||||
"npm-run-all2": "^8.0.4",
|
"npm-run-all2": "^8.0.4",
|
||||||
"typescript": "~5.9.3",
|
"typescript": "~5.9.3",
|
||||||
"vite": "^7.3.1",
|
"vite": "^7.3.1",
|
||||||
"vitest": "^4.0.18",
|
|
||||||
"vue-tsc": "^3.2.4"
|
"vue-tsc": "^3.2.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+104
-11
@@ -11,11 +11,33 @@
|
|||||||
<AboutModal />
|
<AboutModal />
|
||||||
<AccessDeniedModal />
|
<AccessDeniedModal />
|
||||||
<header>
|
<header>
|
||||||
<HeaderMain ref="headerMain" :path="path.pathList" :query="path.query" />
|
<HeaderMain
|
||||||
<BreadCrumb :path="path.pathList" primary />
|
ref="headerMain"
|
||||||
|
:path="path.pathList"
|
||||||
|
:query="path.query"
|
||||||
|
:editor-mode="path.isEditorPath"
|
||||||
|
/>
|
||||||
|
<BreadCrumb
|
||||||
|
:path="path.breadcrumbPathList"
|
||||||
|
:links="path.breadcrumbLinks"
|
||||||
|
primary
|
||||||
|
/>
|
||||||
</header>
|
</header>
|
||||||
<main>
|
<main class="transition-wrapper">
|
||||||
<RouterView :path="path.pathList" :query="path.query" />
|
<Transition
|
||||||
|
:name="routeTransitionName"
|
||||||
|
@after-enter="store.transitionDirection = 'none'"
|
||||||
|
>
|
||||||
|
<div :key="routeViewKey" class="explorer-content">
|
||||||
|
<KeepAlive>
|
||||||
|
<component
|
||||||
|
:is="routeViewComponent"
|
||||||
|
:key="routeViewKey"
|
||||||
|
v-bind="routeViewProps"
|
||||||
|
/>
|
||||||
|
</KeepAlive>
|
||||||
|
</div>
|
||||||
|
</Transition>
|
||||||
</main>
|
</main>
|
||||||
<footer v-if="store.selected.size || store.uprogress.total || store.dprogress.total">
|
<footer v-if="store.selected.size || store.uprogress.total || store.dprogress.total">
|
||||||
<SelectionToolbar :path="path.pathList" />
|
<SelectionToolbar :path="path.pathList" />
|
||||||
@@ -27,10 +49,10 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import type HeaderMain from '@/components/HeaderMain.vue'
|
import type HeaderMain from '@/components/HeaderMain.vue'
|
||||||
import { loadSession, watchConnect, watchDisconnect } from '@/repositories/WS'
|
import { loadSession, watchConnect, watchDisconnect } from '@/repositories/WS'
|
||||||
|
import { getDocuments } from '@/stores/documentStore'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import type { ComputedRef } from 'vue'
|
import type { ComputedRef } from 'vue'
|
||||||
import { onMounted, onUnmounted, ref, watch } from 'vue'
|
import { onMounted, onUnmounted, ref, watch } from 'vue'
|
||||||
import { RouterView } from 'vue-router'
|
|
||||||
|
|
||||||
import Router from '@/router/index'
|
import Router from '@/router/index'
|
||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
@@ -41,23 +63,84 @@ import type SettingsModalVue from './components/SettingsModal.vue'
|
|||||||
import UserManagementModal from './components/UserManagementModal.vue'
|
import UserManagementModal from './components/UserManagementModal.vue'
|
||||||
import UserTokensModal from './components/UserTokensModal.vue'
|
import UserTokensModal from './components/UserTokensModal.vue'
|
||||||
import type { SortOrder } from './utils/docsort'
|
import type { SortOrder } from './utils/docsort'
|
||||||
|
import ExplorerView from './views/ExplorerView.vue'
|
||||||
|
import TextEditorView from './views/TextEditorView.vue'
|
||||||
|
|
||||||
interface Path {
|
interface Path {
|
||||||
path: string
|
path: string
|
||||||
|
canonicalPath: string
|
||||||
|
isEditorPath: boolean
|
||||||
pathList: string[]
|
pathList: string[]
|
||||||
|
breadcrumbPathList: string[]
|
||||||
|
breadcrumbLinks?: string[]
|
||||||
query: string
|
query: string
|
||||||
}
|
}
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
|
|
||||||
|
const getDocByPath = (fullPath: string) =>
|
||||||
|
getDocuments().find(
|
||||||
|
doc => (doc.loc ? `${doc.loc}/${doc.name}` : doc.name) === fullPath
|
||||||
|
)
|
||||||
|
|
||||||
const path: ComputedRef<Path> = computed(() => {
|
const path: ComputedRef<Path> = computed(() => {
|
||||||
const p = decodeURIComponent(Router.currentRoute.value.path).split('//')
|
const p = decodeURIComponent(Router.currentRoute.value.path).split('//')
|
||||||
const pathList = (p[0] ?? '').split('/').filter(value => value !== '')
|
const rawPath = p[0] ?? ''
|
||||||
|
const routePathList = rawPath.split('/').filter(value => value !== '')
|
||||||
const query = p.slice(1).join('//')
|
const query = p.slice(1).join('//')
|
||||||
|
const fullPath = routePathList.join('/')
|
||||||
|
// Access docVersion to make route mode reactive to tree updates
|
||||||
|
void store.docVersion
|
||||||
|
const doc = fullPath ? getDocByPath(fullPath) : null
|
||||||
|
const isEditorPath = !!(doc && !doc.dir && doc.text)
|
||||||
|
const canonicalBase = !fullPath ? '/' : doc?.dir ? `/${fullPath}/` : `/${fullPath}`
|
||||||
|
const canonicalPath = query
|
||||||
|
? `${rawPath}//${query}` // keep search URL shape untouched
|
||||||
|
: canonicalBase
|
||||||
|
const pathList = isEditorPath ? routePathList.slice(0, -1) : routePathList
|
||||||
|
const breadcrumbPathList = routePathList
|
||||||
|
const breadcrumbLinks = isEditorPath
|
||||||
|
? [
|
||||||
|
'/',
|
||||||
|
...routePathList
|
||||||
|
.slice(0, -1)
|
||||||
|
.map((_, index) => `/${routePathList.slice(0, index + 1).join('/')}/`),
|
||||||
|
`/${fullPath}`
|
||||||
|
]
|
||||||
|
: undefined
|
||||||
return {
|
return {
|
||||||
path: p[0] ?? '',
|
path: rawPath,
|
||||||
|
canonicalPath,
|
||||||
|
isEditorPath,
|
||||||
pathList,
|
pathList,
|
||||||
|
breadcrumbPathList,
|
||||||
|
breadcrumbLinks,
|
||||||
query
|
query
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
const routeTransitionName = computed(() => {
|
||||||
|
if (store.transitionDirection === 'forward') return 'slide-forward'
|
||||||
|
if (store.transitionDirection === 'backward') return 'slide-backward'
|
||||||
|
return ''
|
||||||
|
})
|
||||||
|
const routeViewComponent = computed(() =>
|
||||||
|
path.value.isEditorPath ? TextEditorView : ExplorerView
|
||||||
|
)
|
||||||
|
const routeViewKey = computed(() => {
|
||||||
|
return path.value.isEditorPath ? `editor:${path.value.path}` : 'explorer'
|
||||||
|
})
|
||||||
|
const routeViewProps = computed(() =>
|
||||||
|
path.value.isEditorPath ? {} : { path: path.value.pathList, query: path.value.query }
|
||||||
|
)
|
||||||
|
watch(
|
||||||
|
() => path.value.canonicalPath,
|
||||||
|
canonical => {
|
||||||
|
const current = decodeURIComponent(Router.currentRoute.value.path)
|
||||||
|
if (canonical && current !== canonical) {
|
||||||
|
Router.replace(canonical.replaceAll('?', '%3F').replaceAll('#', '%23'))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ immediate: true }
|
||||||
|
)
|
||||||
watch(
|
watch(
|
||||||
() => path.value.path,
|
() => path.value.path,
|
||||||
() => {
|
() => {
|
||||||
@@ -86,7 +169,9 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
|
|||||||
const fileExplorer = store.fileExplorer as any
|
const fileExplorer = store.fileExplorer as any
|
||||||
if (!fileExplorer) return
|
if (!fileExplorer) return
|
||||||
const c = fileExplorer.isCursor()
|
const c = fileExplorer.isCursor()
|
||||||
const input = (event.target as HTMLElement).tagName === 'INPUT'
|
const target = event.target as HTMLElement
|
||||||
|
const input =
|
||||||
|
['INPUT', 'TEXTAREA'].includes(target.tagName) || !!target.closest('.cm-editor')
|
||||||
const keyup = event.type === 'keyup'
|
const keyup = event.type === 'keyup'
|
||||||
|
|
||||||
// Always clear repeat timer on arrow keyup, even if focus moved to input
|
// Always clear repeat timer on arrow keyup, even if focus moved to input
|
||||||
@@ -142,11 +227,17 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
|
|||||||
// Paging/navigation key handling - fall through to bottom
|
// Paging/navigation 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 (
|
||||||
|
!path.value.isEditorPath &&
|
||||||
|
!input &&
|
||||||
|
!keyup &&
|
||||||
|
event.key === 'f' &&
|
||||||
|
(event.ctrlKey || event.metaKey)
|
||||||
|
) {
|
||||||
headerMain.value!.toggleSearchInput()
|
headerMain.value!.toggleSearchInput()
|
||||||
}
|
}
|
||||||
// Search also on / (UNIX style) - use code to support any keyboard layout
|
// Search also on / (UNIX style) - use code to support any keyboard layout
|
||||||
else if (!input && keyup && event.code === 'Slash') {
|
else if (!path.value.isEditorPath && !input && keyup && event.code === 'Slash') {
|
||||||
// Record the actual character for display (varies by keyboard layout)
|
// Record the actual character for display (varies by keyboard layout)
|
||||||
if (event.key.length === 1 && event.key !== store.prefs.searchHotkey) {
|
if (event.key.length === 1 && event.key !== store.prefs.searchHotkey) {
|
||||||
store.prefs.searchHotkey = event.key
|
store.prefs.searchHotkey = event.key
|
||||||
@@ -159,7 +250,9 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
|
|||||||
store.clearToast()
|
store.clearToast()
|
||||||
// Keep rename and other non-search inputs isolated from search behavior.
|
// Keep rename and other non-search inputs isolated from search behavior.
|
||||||
if (input && !searchInput) return
|
if (input && !searchInput) return
|
||||||
headerMain.value!.clearSearch(event)
|
if (!path.value.isEditorPath) {
|
||||||
|
headerMain.value!.clearSearch(event)
|
||||||
|
}
|
||||||
store.focusBreadcrumb()
|
store.focusBreadcrumb()
|
||||||
} else if (!input && keyup && event.key === 'Backspace') {
|
} else if (!input && keyup && event.key === 'Backspace') {
|
||||||
Router.back()
|
Router.back()
|
||||||
|
|||||||
@@ -57,6 +57,68 @@
|
|||||||
align-self: stretch;
|
align-self: stretch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Directory navigation slide transitions */
|
||||||
|
.transition-wrapper {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
grid-template-rows: 1fr;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.explorer-content {
|
||||||
|
grid-area: 1 / 1;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-forward-enter-active,
|
||||||
|
.slide-backward-enter-active {
|
||||||
|
z-index: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-forward-leave-active,
|
||||||
|
.slide-backward-leave-active {
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-forward-enter-active,
|
||||||
|
.slide-forward-leave-active,
|
||||||
|
.slide-backward-enter-active,
|
||||||
|
.slide-backward-leave-active {
|
||||||
|
transition: transform 0.22s cubic-bezier(0.32, 0.72, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-forward-enter-from {
|
||||||
|
transform: translate3d(100%, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-forward-enter-to {
|
||||||
|
transform: translate3d(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-forward-leave-from {
|
||||||
|
transform: translate3d(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-forward-leave-to {
|
||||||
|
transform: translate3d(-100%, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-backward-enter-from {
|
||||||
|
transform: translate3d(-100%, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-backward-enter-to {
|
||||||
|
transform: translate3d(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-backward-leave-from {
|
||||||
|
transform: translate3d(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slide-backward-leave-to {
|
||||||
|
transform: translate3d(100%, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
@media print {
|
@media print {
|
||||||
:root {
|
:root {
|
||||||
--primary-color: black;
|
--primary-color: black;
|
||||||
@@ -206,6 +268,8 @@ main {
|
|||||||
min-height: 0; /* Allow flex child to shrink below content size */
|
min-height: 0; /* Allow flex child to shrink below content size */
|
||||||
padding-bottom: 3em; /* convenience space on the bottom */
|
padding-bottom: 3em; /* convenience space on the bottom */
|
||||||
overflow-y: scroll;
|
overflow-y: scroll;
|
||||||
|
overflow-x: hidden;
|
||||||
|
position: relative;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M19.2 2.6H6.1V29h19.8V9.3l-6.7-6.7zm3 15c0 .2-.2.4-.4.4h-4.4v4.4c0 .2-.2.4-.4.4h-2.4c-.2 0-.4-.2-.4-.4V18H9.9c-.2 0-.4-.2-.4-.4v-2.4c0-.2.2-.4.4-.4h4.4v-4.4c0-.2.2-.4.4-.4H17c.2 0 .4.2.4.4v4.4h4.4c.2 0 .4.2.4.4v2.4z"/></svg>
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 28 28">
|
||||||
|
<path fill-rule="evenodd" d="M19.2 2.6H6.1V29h19.8V9.3l-6.7-6.7zM22.75 18.55c0 .2625-.175.4375-.4375.4375h-4.55v4.55c0 .2625-.175.4375-.4375.4375h-2.45c-.2625 0-.4375-.175-.4375-.4375v-4.55h-4.55c-.2625 0-.4375-.175-.4375-.4375V16.1c0-.2625.175-.4375.4375-.4375h4.55v-4.55c0-.2625.175-.4375.4375-.4375h2.45c.2625 0 .4375.175.4375.4375v4.55h4.55c.2625 0 .4375.175.4375.4375v2.45z" />
|
||||||
|
</svg>
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 293 B After Width: | Height: | Size: 452 B |
@@ -1 +1 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 512 512"><path d="M384 128h-69c24 16 46.5 44.5 53.5 64h15c32.5 0 64 32 64 64s-32.5 64-64 64h-96c-31.5 0-64-32-64-64 0-11.5 3.5-22.5 9-32H164c-2.5 10.5-4 21-4 32 0 64 63.5 128 127.5 128H384c64 0 128-64 128-128s-64-128-128-128zM143.5 320h-15c-32.5 0-64-32-64-64s32.5-64 64-64h96c31.5 0 64 32 64 64 0 11.5-3.5 22.5-9 32H348c2.5-10.5 4-21 4-32 0-64-63.5-128-127.5-128H128C64 128 0 192 0 256s64 128 128 128h69c-24-16-46.5-44.5-53.5-64z"/></svg>
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M384 128h-69c24 16 46.5 44.5 53.5 64h15c32.5 0 64 32 64 64s-32.5 64-64 64h-96c-31.5 0-64-32-64-64 0-11.5 3.5-22.5 9-32H164c-2.5 10.5-4 21-4 32 0 64 63.5 128 127.5 128H384c64 0 128-64 128-128s-64-128-128-128zM143.5 320h-15c-32.5 0-64-32-64-64s32.5-64 64-64h96c31.5 0 64 32 64 64 0 11.5-3.5 22.5-9 32H348c2.5-10.5 4-21 4-32 0-64-63.5-128-127.5-128H128C64 128 0 192 0 256s64 128 128 128h69c-24-16-46.5-44.5-53.5-64z"/></svg>
|
||||||
|
Before Width: | Height: | Size: 517 B After Width: | Height: | Size: 492 B |
@@ -8,7 +8,7 @@
|
|||||||
@focus=focusCurrent
|
@focus=focusCurrent
|
||||||
tabindex=0
|
tabindex=0
|
||||||
>
|
>
|
||||||
<a href="#/"
|
<a :href="`/#${urlAt(0)}`"
|
||||||
:ref="el => setLinkRef(0, el)"
|
:ref="el => setLinkRef(0, el)"
|
||||||
class="home"
|
class="home"
|
||||||
:class="{ current: !!isCurrent(0) }"
|
:class="{ current: !!isCurrent(0) }"
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
<CursorTooltip ref="homeTooltip" text="/">/</CursorTooltip>
|
<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="`/#${urlAt(index + 1)}`"
|
||||||
:class="{ current: !!isCurrent(index + 1) }"
|
:class="{ current: !!isCurrent(index + 1) }"
|
||||||
:aria-current="isCurrent(index + 1)"
|
:aria-current="isCurrent(index + 1)"
|
||||||
@click.prevent="navigate(index + 1)"
|
@click.prevent="navigate(index + 1)"
|
||||||
@@ -62,10 +62,17 @@ const setPathTooltipRef = (index: number, el: any) => {
|
|||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
path: Array<string>
|
path: Array<string>
|
||||||
|
links?: Array<string>
|
||||||
primary?: boolean
|
primary?: boolean
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const longest = ref<Array<string>>([])
|
const longest = ref<Array<string>>([])
|
||||||
|
const longestLinks = ref<Array<string>>(['/'])
|
||||||
|
|
||||||
|
const defaultLinks = (segments: Array<string>) => [
|
||||||
|
'/',
|
||||||
|
...segments.map((_, index) => `/${segments.slice(0, index + 1).join('/')}/`)
|
||||||
|
]
|
||||||
|
|
||||||
const isCurrent = (index: number) =>
|
const isCurrent = (index: number) =>
|
||||||
index == props.path.length ? 'location' : undefined
|
index == props.path.length ? 'location' : undefined
|
||||||
@@ -77,16 +84,22 @@ const focusCurrent = () => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const urlAt = (index: number) => {
|
||||||
|
const explicit = longestLinks.value[index]
|
||||||
|
return explicit ?? (index ? `/${longest.value.slice(0, index).join('/')}/` : '/')
|
||||||
|
}
|
||||||
|
|
||||||
const navigate = (index: number) => {
|
const navigate = (index: number) => {
|
||||||
const link = links[index]
|
const link = links[index]
|
||||||
if (!link) throw Error(`No link at index ${index} (path: ${props.path})`)
|
if (!link) throw Error(`No link at index ${index} (path: ${props.path})`)
|
||||||
const url = index ? `/${longest.value.slice(0, index).join('/')}/` : '/'
|
const url = urlAt(index)
|
||||||
const long = longest.value.length ? `/${longest.value.join('/')}/` : '/'
|
const long = longest.value.length ? `/${longest.value.join('/')}/` : '/'
|
||||||
const browser = decodeURIComponent(location.hash.slice(1).split('//')[0] ?? '')
|
const browser = decodeURIComponent(location.hash.slice(1).split('//')[0] ?? '')
|
||||||
const u = url.replaceAll('?', '%3F').replaceAll('#', '%23')
|
const u = url.replaceAll('?', '%3F').replaceAll('#', '%23')
|
||||||
// Clicking on current link clears the rest of the path and adds new history
|
// Clicking on current link clears the rest of the path and adds new history
|
||||||
if (isCurrent(index)) {
|
if (isCurrent(index)) {
|
||||||
longest.value.splice(index)
|
longest.value.splice(index)
|
||||||
|
longestLinks.value.splice(index + 1)
|
||||||
router.push(u)
|
router.push(u)
|
||||||
}
|
}
|
||||||
// Moving along breadcrumbs doesn't create new history
|
// Moving along breadcrumbs doesn't create new history
|
||||||
@@ -102,20 +115,26 @@ const move = (dir: number) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
|
const currentLinks = props.links ?? defaultLinks(props.path)
|
||||||
const longcut = longest.value.slice(0, props.path.length)
|
const longcut = longest.value.slice(0, props.path.length)
|
||||||
const same = longcut.every((value, index) => value === props.path[index])
|
const same = longcut.every((value, index) => value === props.path[index])
|
||||||
// Navigated out of previous path, reset longest to current
|
// Navigated out of previous path, reset longest to current
|
||||||
if (!same) longest.value = props.path
|
if (!same) {
|
||||||
else if (props.path.length > longcut.length) {
|
longest.value = props.path
|
||||||
|
longestLinks.value = currentLinks
|
||||||
|
} else if (props.path.length > longcut.length) {
|
||||||
longest.value = longcut.concat(props.path.slice(longcut.length))
|
longest.value = longcut.concat(props.path.slice(longcut.length))
|
||||||
|
longestLinks.value.splice(0, currentLinks.length, ...currentLinks)
|
||||||
} else {
|
} else {
|
||||||
// Prune deleted folders from longest
|
// Prune deleted folders from longest
|
||||||
for (let i = props.path.length; i < longest.value.length; ++i) {
|
for (let i = props.path.length; i < longest.value.length; ++i) {
|
||||||
if (!exists(longest.value.slice(0, i + 1))) {
|
if (!exists(longest.value.slice(0, i + 1))) {
|
||||||
longest.value = longest.value.slice(0, i)
|
longest.value = longest.value.slice(0, i)
|
||||||
|
longestLinks.value = longestLinks.value.slice(0, i + 1)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
longestLinks.value.splice(0, currentLinks.length, ...currentLinks)
|
||||||
}
|
}
|
||||||
// If needed, focus primary navigation to new location
|
// If needed, focus primary navigation to new location
|
||||||
if (props.primary)
|
if (props.primary)
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
</defs>
|
</defs>
|
||||||
|
|
||||||
<g :filter="isExpanded ? 'url(#pieShadow)' : 'none'">
|
<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="showOtherCategory ? 'url(#otherGradient)' : freeColor" :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="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(#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="midRadius" :cx="pieCx" :cy="pieCy" fill="transparent" stroke="url(#highlightOverlay)" :stroke-width="ringWidth" />
|
||||||
@@ -38,12 +38,12 @@
|
|||||||
<g ref="labelsRef" class="pie-labels">
|
<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="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="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>
|
<text v-if="showOtherCategory" :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>
|
<defs>
|
||||||
<path :id="storageLabelPath.id" :d="storageLabelPath.d" fill="none" />
|
<path :id="storageLabelPath.id" :d="storageLabelPath.d" fill="none" />
|
||||||
<path :id="freeLabelPath.id" :d="freeLabelPath.d" fill="none" />
|
<path :id="freeLabelPath.id" :d="freeLabelPath.d" fill="none" />
|
||||||
<path :id="otherLabelPath.id" :d="otherLabelPath.d" fill="none" />
|
<path v-if="showOtherCategory" :id="otherLabelPath.id" :d="otherLabelPath.d" fill="none" />
|
||||||
</defs>
|
</defs>
|
||||||
|
|
||||||
<text class="pie-label-sub" fill="#93e">
|
<text class="pie-label-sub" fill="#93e">
|
||||||
@@ -52,7 +52,7 @@
|
|||||||
<text class="pie-label-sub" :fill="freeColor">
|
<text class="pie-label-sub" :fill="freeColor">
|
||||||
<textPath :href="'#' + freeLabelPath.id" startOffset="50%" text-anchor="middle" dominant-baseline="middle">free</textPath>
|
<textPath :href="'#' + freeLabelPath.id" startOffset="50%" text-anchor="middle" dominant-baseline="middle">free</textPath>
|
||||||
</text>
|
</text>
|
||||||
<text class="pie-label-sub" fill="#d9f">
|
<text v-if="showOtherCategory" class="pie-label-sub" fill="#d9f">
|
||||||
<textPath :href="'#' + otherLabelPath.id" startOffset="50%" text-anchor="middle" dominant-baseline="middle">other</textPath>
|
<textPath :href="'#' + otherLabelPath.id" startOffset="50%" text-anchor="middle" dominant-baseline="middle">other</textPath>
|
||||||
</text>
|
</text>
|
||||||
</g>
|
</g>
|
||||||
@@ -98,18 +98,30 @@ const truncateLabel = (name: string, maxLen = 10): string => {
|
|||||||
return name.slice(0, maxLen - 1) + '…'
|
return name.slice(0, maxLen - 1) + '…'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const otherBytes = computed(() => Math.max(0, store.space.used - store.space.allocated))
|
||||||
|
const showOtherCategory = computed(() => {
|
||||||
|
const s = store.space
|
||||||
|
return !!s.disk && otherBytes.value / s.disk >= 0.01
|
||||||
|
})
|
||||||
|
const freeSliceBytes = computed(() =>
|
||||||
|
showOtherCategory.value
|
||||||
|
? store.space.free
|
||||||
|
: Math.max(0, store.space.disk - store.space.allocated)
|
||||||
|
)
|
||||||
|
|
||||||
// Calculate max label length based on angular gap to neighbor labels
|
// Calculate max label length based on angular gap to neighbor labels
|
||||||
const storageMaxLen = computed(() => {
|
const storageMaxLen = computed(() => {
|
||||||
const s = store.space
|
const s = store.space
|
||||||
if (!s.disk) return 10
|
if (!s.disk) return 10
|
||||||
// Sector spans in degrees
|
// Sector spans in degrees
|
||||||
const storageSpan = (s.allocated / s.disk) * 360
|
const storageSpan = (s.allocated / s.disk) * 360
|
||||||
const freeSpan = (s.free / s.disk) * 360
|
const freeSpan = (freeSliceBytes.value / s.disk) * 360
|
||||||
const otherSpan = ((s.used - s.allocated) / s.disk) * 360
|
const otherSpan = (otherBytes.value / s.disk) * 360
|
||||||
// Angular gap from storage label midpoint to neighbor label midpoints
|
// Angular gap from storage label midpoint to neighbor label midpoints
|
||||||
const gapToFree = (storageSpan + freeSpan) / 2
|
const gapToFree = (storageSpan + freeSpan) / 2
|
||||||
const gapToOther = (storageSpan + otherSpan) / 2
|
const minGap = showOtherCategory.value
|
||||||
const minGap = Math.min(gapToFree, gapToOther)
|
? Math.min(gapToFree, (storageSpan + otherSpan) / 2)
|
||||||
|
: gapToFree
|
||||||
// Allow longer names when there's sufficient gap to both neighbors
|
// Allow longer names when there's sufficient gap to both neighbors
|
||||||
if (minGap > 70) return 18
|
if (minGap > 70) return 18
|
||||||
if (minGap > 55) return 14
|
if (minGap > 55) return 14
|
||||||
@@ -143,7 +155,7 @@ const pieStorageDash = computed(() => {
|
|||||||
const pieFreeDash = computed(() => {
|
const pieFreeDash = computed(() => {
|
||||||
const s = store.space
|
const s = store.space
|
||||||
if (!s.disk) return `0 ${CIRC}`
|
if (!s.disk) return `0 ${CIRC}`
|
||||||
return `${(s.free / s.disk) * CIRC} ${CIRC}`
|
return `${(freeSliceBytes.value / s.disk) * CIRC} ${CIRC}`
|
||||||
})
|
})
|
||||||
|
|
||||||
const pieFreeOffsetVal = computed(() => {
|
const pieFreeOffsetVal = computed(() => {
|
||||||
@@ -179,8 +191,8 @@ const sectorInfo = computed(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const storagePct = s.allocated / s.disk
|
const storagePct = s.allocated / s.disk
|
||||||
const freePct = s.free / s.disk
|
const freePct = freeSliceBytes.value / s.disk
|
||||||
const otherPct = (s.used - s.allocated) / s.disk
|
const otherPct = showOtherCategory.value ? otherBytes.value / s.disk : 0
|
||||||
|
|
||||||
const storageAngle = storagePct * 180 // midpoint of storage sector
|
const storageAngle = storagePct * 180 // midpoint of storage sector
|
||||||
const freeStart = storagePct * 360
|
const freeStart = storagePct * 360
|
||||||
@@ -198,7 +210,7 @@ const sectorInfo = computed(() => {
|
|||||||
const rawAngles = computed(() => ({
|
const rawAngles = computed(() => ({
|
||||||
storage: sectorInfo.value.storage.angle,
|
storage: sectorInfo.value.storage.angle,
|
||||||
free: sectorInfo.value.free.angle,
|
free: sectorInfo.value.free.angle,
|
||||||
other: sectorInfo.value.other.angle
|
...(showOtherCategory.value ? { other: sectorInfo.value.other.angle } : {})
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const getSizeRotation = (angle: number) => (angle < 180 ? angle - 90 : angle + 90)
|
const getSizeRotation = (angle: number) => (angle < 180 ? angle - 90 : angle + 90)
|
||||||
@@ -219,7 +231,7 @@ const otherInnerPos = computed(() =>
|
|||||||
const labelLengths = computed(() => ({
|
const labelLengths = computed(() => ({
|
||||||
storage: storageName.value.length,
|
storage: storageName.value.length,
|
||||||
free: 4,
|
free: 4,
|
||||||
other: 5
|
...(showOtherCategory.value ? { other: 5 } : {})
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const getGapForPair = (len1: number, len2: number) => {
|
const getGapForPair = (len1: number, len2: number) => {
|
||||||
@@ -232,7 +244,9 @@ const adjustedLabelAngles = computed(() => {
|
|||||||
const labels = [
|
const labels = [
|
||||||
{ id: 'storage', angle: angles.storage, len: lens.storage },
|
{ id: 'storage', angle: angles.storage, len: lens.storage },
|
||||||
{ id: 'free', angle: angles.free, len: lens.free },
|
{ id: 'free', angle: angles.free, len: lens.free },
|
||||||
{ id: 'other', angle: angles.other, len: lens.other }
|
...(showOtherCategory.value
|
||||||
|
? [{ id: 'other', angle: angles.other!, len: lens.other! }]
|
||||||
|
: [])
|
||||||
]
|
]
|
||||||
labels.sort((a, b) => a.angle - b.angle)
|
labels.sort((a, b) => a.angle - b.angle)
|
||||||
|
|
||||||
@@ -283,7 +297,11 @@ const freeLabelPath = computed(() =>
|
|||||||
createArcPath(adjustedLabelAngles.value.free!, 'free', 4)
|
createArcPath(adjustedLabelAngles.value.free!, 'free', 4)
|
||||||
)
|
)
|
||||||
const otherLabelPath = computed(() =>
|
const otherLabelPath = computed(() =>
|
||||||
createArcPath(adjustedLabelAngles.value.other!, 'other', 5)
|
createArcPath(
|
||||||
|
adjustedLabelAngles.value.other ?? sectorInfo.value.other.angle,
|
||||||
|
'other',
|
||||||
|
5
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleClick = () => (isExpanded.value ? collapse() : expand())
|
const handleClick = () => (isExpanded.value ? collapse() : expand())
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<div v-if="!props.path || documents.length === 0" class="empty-container">
|
<div v-if="showEmpty" class="empty-container">
|
||||||
<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>
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
import { Cog } from '@/assets/svg'
|
import { Cog } from '@/assets/svg'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import { exists } from '@/utils/fileutil'
|
import { exists } from '@/utils/fileutil'
|
||||||
|
import { computed } from 'vue'
|
||||||
|
|
||||||
const cog = Cog
|
const cog = Cog
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
@@ -21,9 +22,29 @@ const props = defineProps<{
|
|||||||
path: string[]
|
path: string[]
|
||||||
documents: Document[]
|
documents: Document[]
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
|
const showEmpty = computed(() => {
|
||||||
|
const loc = props.path.join('/')
|
||||||
|
const hasVisibleGhost = store.ghosts.some(g => {
|
||||||
|
const full = g.loc ? `${g.loc}/${g.name}` : g.name
|
||||||
|
return g.loc === loc && !store.hiddenPaths.has(full)
|
||||||
|
})
|
||||||
|
|
||||||
|
return !props.path || (props.documents.length === 0 && !hasVisibleGhost)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
.empty-container {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
height: 100%;
|
||||||
|
font-size: 2rem;
|
||||||
|
text-shadow: 0 0 .3rem #000, 0 0 2rem #0008;
|
||||||
|
color: var(--accent-color);
|
||||||
|
}
|
||||||
@keyframes rotate {
|
@keyframes rotate {
|
||||||
0% { transform: rotate(0deg); }
|
0% { transform: rotate(0deg); }
|
||||||
100% { transform: rotate(360deg); }
|
100% { transform: rotate(360deg); }
|
||||||
|
|||||||
@@ -1,74 +1,77 @@
|
|||||||
<template>
|
<template>
|
||||||
<table v-if="props.documents.length || editing">
|
<div class="file-explorer">
|
||||||
<thead>
|
<table v-if="props.documents.length || editing">
|
||||||
<tr>
|
<thead>
|
||||||
<th class="selection">
|
<tr>
|
||||||
<input type="checkbox" tabindex="-1" v-model="allSelected" :indeterminate="selectionIndeterminate">
|
<th class="selection">
|
||||||
</th>
|
<input type="checkbox" tabindex="-1" v-model="allSelected" :indeterminate="selectionIndeterminate">
|
||||||
<th class="sortcolumn" :class="{ sortactive: store.sortOrder === 'name' }" @click="store.toggleSort('name')">Name</th>
|
</th>
|
||||||
<th class="sortcolumn modified right" :class="{ sortactive: store.sortOrder === 'modified' }" @click="store.toggleSort('modified')">Modified</th>
|
<th class="sortcolumn" :class="{ sortactive: store.sortOrder === 'name' }" @click="store.toggleSort('name')">Name</th>
|
||||||
<th class="sortcolumn size right" :class="{ sortactive: store.sortOrder === 'size' }" @click="store.toggleSort('size')">Size</th>
|
<th class="sortcolumn modified right" :class="{ sortactive: store.sortOrder === 'modified' }" @click="store.toggleSort('modified')">Modified</th>
|
||||||
<th class="menu"></th>
|
<th class="sortcolumn size right" :class="{ sortactive: store.sortOrder === 'size' }" @click="store.toggleSort('size')">Size</th>
|
||||||
</tr>
|
<th class="menu"></th>
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<tr v-if="editing?.key === 'new'" class="folder">
|
|
||||||
<td class="selection"></td>
|
|
||||||
<td class="name">
|
|
||||||
<FileRenameInput :doc="editing" :rename="mkdir" :exit="() => {editing = null}" />
|
|
||||||
</td>
|
|
||||||
<FileModified :doc=editing :now=nowkey />
|
|
||||||
<FileSize :doc=editing />
|
|
||||||
<td class="menu"></td>
|
|
||||||
</tr>
|
|
||||||
<template v-for="(doc, index) in documents" :key="doc.key">
|
|
||||||
<tr class="folder-change" v-if="showFolderBreadcrumb(index)">
|
|
||||||
<th colspan="5"><BreadCrumb :path="doc.loc ? doc.loc.split('/') : []" /></th>
|
|
||||||
</tr>
|
</tr>
|
||||||
|
</thead>
|
||||||
<tr
|
<tbody>
|
||||||
:id="`file-${doc.key}`"
|
<tr v-if="editing?.key === 'new'" :class="editing.dir ? 'folder' : 'file'">
|
||||||
:class="{ file: !doc.dir, folder: doc.dir, cursor: store.cursor === doc.key, ghost: doc.ghost }"
|
<td class="selection"></td>
|
||||||
@click="store.cursor = store.cursor === doc.key ? '' : doc.key"
|
|
||||||
@contextmenu.prevent="contextMenu($event, doc)"
|
|
||||||
>
|
|
||||||
<td class="selection" @click.up.stop="store.cursor = store.cursor === doc.key ? doc.key : ''">
|
|
||||||
<input
|
|
||||||
type="checkbox"
|
|
||||||
tabindex="-1"
|
|
||||||
:checked="store.selected.has(doc.key)"
|
|
||||||
@change="
|
|
||||||
($event.target as HTMLInputElement).checked
|
|
||||||
? store.selected.add(doc.key)
|
|
||||||
: store.selected.delete(doc.key)
|
|
||||||
"
|
|
||||||
/>
|
|
||||||
</td>
|
|
||||||
<td class="name">
|
<td class="name">
|
||||||
<template v-if="editing === doc">
|
<FileRenameInput :doc="editing" :rename="createItem" :exit="exitEditing" />
|
||||||
<FileRenameInput :doc="doc" :rename="rename" :exit="() => {editing = null}" />
|
|
||||||
</template>
|
|
||||||
<template v-else>
|
|
||||||
<a :href=doc.url tabindex=-1 @contextmenu.stop @focus.stop="store.cursor = doc.key">
|
|
||||||
{{ doc.name }}
|
|
||||||
</a>
|
|
||||||
<button tabindex=-1 v-if="store.cursor == doc.key" class="rename-button" @click="() => (editing = doc)">🖊️</button>
|
|
||||||
</template>
|
|
||||||
</td>
|
|
||||||
<FileModified :doc=doc :now=nowkey />
|
|
||||||
<FileSize :doc=doc />
|
|
||||||
<td class="menu">
|
|
||||||
<button tabindex=-1 @click.stop="contextMenu($event, doc)">⋮</button>
|
|
||||||
</td>
|
</td>
|
||||||
|
<FileModified :doc=editing :now=nowkey />
|
||||||
|
<FileSize :doc=editing />
|
||||||
|
<td class="menu"></td>
|
||||||
</tr>
|
</tr>
|
||||||
</template>
|
<template v-for="(doc, index) in documents" :key="doc.key">
|
||||||
<tr class="summary" v-if="props.documents.length > 1">
|
<tr class="folder-change" v-if="showFolderBreadcrumb(index)">
|
||||||
<td colspan="3" class="right">{{props.documents.length}} items</td>
|
<th colspan="5"><BreadCrumb :path="doc.loc ? doc.loc.split('/') : []" /></th>
|
||||||
<td class="size right">{{ formatSize(props.documents.reduce((a, b) => a + b.size, 0)) }}</td>
|
</tr>
|
||||||
<td class="menu"></td>
|
|
||||||
</tr>
|
<tr
|
||||||
</tbody>
|
:id="`file-${doc.key}`"
|
||||||
</table>
|
:class="{ file: !doc.dir, folder: doc.dir, cursor: store.cursor === doc.key, ghost: doc.ghost }"
|
||||||
|
@click="store.cursor = store.cursor === doc.key ? '' : doc.key"
|
||||||
|
@contextmenu.prevent="contextMenu($event, doc)"
|
||||||
|
>
|
||||||
|
<td class="selection" @click.up.stop="store.cursor = store.cursor === doc.key ? doc.key : ''">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
tabindex="-1"
|
||||||
|
:checked="store.selected.has(doc.key)"
|
||||||
|
@change="
|
||||||
|
($event.target as HTMLInputElement).checked
|
||||||
|
? store.selected.add(doc.key)
|
||||||
|
: store.selected.delete(doc.key)
|
||||||
|
"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td class="name">
|
||||||
|
<template v-if="editing === doc">
|
||||||
|
<FileRenameInput :doc="doc" :rename="rename" :exit="exitEditing" />
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
<a :href="doc.text ? doc.editurl : doc.url" tabindex=-1 @contextmenu.stop @focus.stop="store.cursor = doc.key">
|
||||||
|
{{ doc.name }}
|
||||||
|
</a>
|
||||||
|
<button tabindex=-1 class="rename-button" @click="() => (editing = doc)">🖊️</button>
|
||||||
|
</template>
|
||||||
|
</td>
|
||||||
|
<FileModified :doc=doc :now=nowkey />
|
||||||
|
<FileSize :doc=doc />
|
||||||
|
<td class="menu">
|
||||||
|
<button tabindex=-1 @click.stop="contextMenu($event, doc)">⋮</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</template>
|
||||||
|
<tr class="summary" v-if="props.documents.length > 1">
|
||||||
|
<td colspan="3" class="right">{{props.documents.length}} items</td>
|
||||||
|
<td class="size right">{{ formatSize(props.documents.reduce((a, b) => a + b.size, 0)) }}</td>
|
||||||
|
<td class="menu"></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<EmptyFolder v-else :documents="documents" :path="props.path" />
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
@@ -81,11 +84,13 @@ import ContextMenu from '@imengyu/vue3-context-menu'
|
|||||||
import {
|
import {
|
||||||
computed,
|
computed,
|
||||||
nextTick,
|
nextTick,
|
||||||
|
onActivated,
|
||||||
|
onDeactivated,
|
||||||
onMounted,
|
onMounted,
|
||||||
onUnmounted,
|
onUnmounted,
|
||||||
ref,
|
ref,
|
||||||
shallowRef,
|
shallowRef,
|
||||||
watchEffect
|
watch
|
||||||
} from 'vue'
|
} from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import FileRenameInput from './FileRenameInput.vue'
|
import FileRenameInput from './FileRenameInput.vue'
|
||||||
@@ -189,6 +194,9 @@ const pageMove = (direction: 1 | -1, ev: KeyboardEvent) => {
|
|||||||
|
|
||||||
// File rename
|
// File rename
|
||||||
const editing = shallowRef<Doc | null>(null)
|
const editing = shallowRef<Doc | null>(null)
|
||||||
|
const exitEditing = () => {
|
||||||
|
editing.value = null
|
||||||
|
}
|
||||||
const rename = async (doc: Doc, newName: string) => {
|
const rename = async (doc: Doc, newName: string) => {
|
||||||
const oldName = doc.name
|
const oldName = doc.name
|
||||||
doc.name = newName // We should get an update from watch but this is quicker
|
doc.name = newName // We should get an update from watch but this is quicker
|
||||||
@@ -206,8 +214,20 @@ const rename = async (doc: Doc, newName: string) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
defineExpose({
|
defineExpose({
|
||||||
|
newFile() {
|
||||||
|
const now = Math.floor(Date.now() / 1000)
|
||||||
|
editing.value = new Doc({
|
||||||
|
loc: loc.value,
|
||||||
|
key: 'new',
|
||||||
|
name: 'New File.txt',
|
||||||
|
dir: false,
|
||||||
|
mtime: now,
|
||||||
|
size: 0,
|
||||||
|
allocated: 0
|
||||||
|
})
|
||||||
|
store.cursor = editing.value.key
|
||||||
|
},
|
||||||
newFolder() {
|
newFolder() {
|
||||||
console.log('New folder')
|
|
||||||
const now = Math.floor(Date.now() / 1000)
|
const now = Math.floor(Date.now() / 1000)
|
||||||
editing.value = new Doc({
|
editing.value = new Doc({
|
||||||
loc: loc.value,
|
loc: loc.value,
|
||||||
@@ -231,7 +251,7 @@ defineExpose({
|
|||||||
const docs = props.documents
|
const docs = props.documents
|
||||||
if (docs.length > 0) {
|
if (docs.length > 0) {
|
||||||
store.cursor = docs[0]!.key
|
store.cursor = docs[0]!.key
|
||||||
// Also focus the element directly (watchEffect won't trigger if cursor unchanged)
|
// Also focus the element directly (post-flush watcher won't trigger if cursor unchanged)
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
const a = document.querySelector(
|
const a = document.querySelector(
|
||||||
`#file-${store.cursor} .name a`
|
`#file-${store.cursor} .name a`
|
||||||
@@ -317,22 +337,41 @@ const focusBreadcrumb = () => {
|
|||||||
const keyboardFollowScroll = createKeyboardFollowScroll()
|
const keyboardFollowScroll = createKeyboardFollowScroll()
|
||||||
const markKeyboardFollow = keyboardFollowScroll.markKeyboardFollow
|
const markKeyboardFollow = keyboardFollowScroll.markKeyboardFollow
|
||||||
const keepCursorVisibleSmooth = keyboardFollowScroll.keepVisible
|
const keepCursorVisibleSmooth = keyboardFollowScroll.keepVisible
|
||||||
watchEffect(() => {
|
// Deactivated (KeepAlive-cached) instances stay alive with frozen, potentially
|
||||||
if (store.cursor && store.cursor !== editing.value?.key) editing.value = null
|
// stale props - their watchers must not react to global store changes.
|
||||||
if (editing.value) store.cursor = editing.value?.key
|
let isActive = true
|
||||||
if (store.cursor) {
|
watch(
|
||||||
const a = document.querySelector(
|
() => store.cursor,
|
||||||
`#file-${store.cursor} .name a`
|
cursor => {
|
||||||
) as HTMLAnchorElement | null
|
if (!isActive) return
|
||||||
if (a) a.focus({ preventScroll: true })
|
if (cursor && editing.value && cursor !== editing.value.key) {
|
||||||
|
exitEditing()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
watchEffect(() => {
|
watch(
|
||||||
if (!props.documents.length && store.cursor && !store.query) {
|
() => store.cursor,
|
||||||
store.cursor = ''
|
cursor => {
|
||||||
focusBreadcrumb()
|
if (!isActive) return
|
||||||
|
if (cursor && !editing.value) {
|
||||||
|
const a = document.querySelector(
|
||||||
|
`#file-${cursor} .name a`
|
||||||
|
) as HTMLAnchorElement | null
|
||||||
|
if (a) a.focus({ preventScroll: true })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ flush: 'post' }
|
||||||
|
)
|
||||||
|
watch(
|
||||||
|
() => [props.documents.length, store.cursor, store.query, editing.value] as const,
|
||||||
|
([len, cursor, query, editingDoc]) => {
|
||||||
|
if (!isActive) return
|
||||||
|
if (!len && cursor && !query && !editingDoc) {
|
||||||
|
store.cursor = ''
|
||||||
|
focusBreadcrumb()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
let nowkey = ref(0)
|
let nowkey = ref(0)
|
||||||
let modifiedTimer: any = null
|
let modifiedTimer: any = null
|
||||||
const updateModified = () => {
|
const updateModified = () => {
|
||||||
@@ -346,23 +385,48 @@ onMounted(() => {
|
|||||||
active.focus({ preventScroll: true })
|
active.focus({ preventScroll: true })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
onActivated(() => {
|
||||||
|
isActive = true
|
||||||
|
})
|
||||||
|
onDeactivated(() => {
|
||||||
|
isActive = false
|
||||||
|
if (editing.value) exitEditing()
|
||||||
|
})
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
keyboardFollowScroll.cancel()
|
keyboardFollowScroll.cancel()
|
||||||
clearInterval(modifiedTimer)
|
clearInterval(modifiedTimer)
|
||||||
})
|
})
|
||||||
const mkdir = async (doc: Doc, name: string) => {
|
const editRoute = (path: string) =>
|
||||||
|
'/' +
|
||||||
|
path
|
||||||
|
.split('/')
|
||||||
|
.map(part => encodeURIComponent(part))
|
||||||
|
.join('/')
|
||||||
|
|
||||||
|
const createItem = async (doc: Doc, name: string) => {
|
||||||
doc.name = name
|
doc.name = name
|
||||||
doc.key = crypto.randomUUID()
|
doc.key = crypto.randomUUID()
|
||||||
store.addGhost(doc)
|
store.addGhost(doc)
|
||||||
editing.value = null
|
store.cursor = doc.key
|
||||||
|
exitEditing()
|
||||||
const path = doc.loc ? `${doc.loc}/${name}` : name
|
const path = doc.loc ? `${doc.loc}/${name}` : name
|
||||||
try {
|
try {
|
||||||
const res = await apiFetch(filesUrl(path), { method: 'MKCOL' })
|
const res = doc.dir
|
||||||
|
? await apiFetch(filesUrl(path), { method: 'MKCOL' })
|
||||||
|
: await apiFetch(filesUrl(path), {
|
||||||
|
method: 'PUT',
|
||||||
|
body: '',
|
||||||
|
headers: { 'Content-Type': 'text/plain; charset=utf-8' }
|
||||||
|
})
|
||||||
if (!res.ok) throw new Error(await parseErrorMessage(res))
|
if (!res.ok) throw new Error(await parseErrorMessage(res))
|
||||||
router.push(doc.urlrouter)
|
if (doc.dir) {
|
||||||
|
router.push(doc.urlrouter)
|
||||||
|
} else {
|
||||||
|
router.push(editRoute(path))
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Mkdir failed', err)
|
console.error('Create failed', err)
|
||||||
store.showToast(err instanceof Error ? err.message : 'Mkdir failed')
|
store.showToast(err instanceof Error ? err.message : 'Create failed')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const showFolderBreadcrumb = (i: number) => {
|
const showFolderBreadcrumb = (i: number) => {
|
||||||
@@ -496,9 +560,14 @@ const contextMenu = (ev: MouseEvent, doc: Doc) => {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
.file-explorer {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
table {
|
table {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
table-layout: fixed;
|
table-layout: fixed;
|
||||||
|
height: auto;
|
||||||
}
|
}
|
||||||
thead tr {
|
thead tr {
|
||||||
position: sticky;
|
position: sticky;
|
||||||
@@ -559,6 +628,12 @@ table td {
|
|||||||
.name .rename-button {
|
.name .rename-button {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
right: 0;
|
right: 0;
|
||||||
|
opacity: 0;
|
||||||
|
visibility: hidden;
|
||||||
|
}
|
||||||
|
tbody tr:hover .name .rename-button {
|
||||||
|
opacity: 1;
|
||||||
|
visibility: visible;
|
||||||
animation: appear calc(5 * var(--transition-time)) linear;
|
animation: appear calc(5 * var(--transition-time)) linear;
|
||||||
}
|
}
|
||||||
@keyframes appear {
|
@keyframes appear {
|
||||||
@@ -629,12 +704,6 @@ tbody .selection input {
|
|||||||
content: '📁';
|
content: '📁';
|
||||||
font-size: 1.5rem;
|
font-size: 1.5rem;
|
||||||
}
|
}
|
||||||
.empty-container {
|
|
||||||
padding-top: 3rem;
|
|
||||||
text-align: center;
|
|
||||||
font-size: 3rem;
|
|
||||||
color: var(--accent-color);
|
|
||||||
}
|
|
||||||
.folder-change {
|
.folder-change {
|
||||||
margin-left: -.5rem;
|
margin-left: -.5rem;
|
||||||
}
|
}
|
||||||
@@ -645,4 +714,3 @@ tbody .selection input {
|
|||||||
color: #888;
|
color: #888;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@/stores/main
|
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ input#FileRenameInput {
|
|||||||
padding: .75em;
|
padding: .75em;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
width: auto;
|
width: auto;
|
||||||
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div v-if="props.documents.length || editing" class="gallery" ref="gallery">
|
<div v-if="props.documents.length || editing" class="gallery" ref="gallery">
|
||||||
<GalleryFigure v-if="editing?.key === 'new'" :doc="editing" :key=editing.key :editing="{rename: mkdir, exit}" />
|
<GalleryFigure v-if="editing?.key === 'new'" :doc="editing" :key=editing.key :editing="{rename: createItem, exit}" />
|
||||||
<template v-for="(doc, index) in documents" :key=doc.key>
|
<template v-for="(doc, index) in documents" :key=doc.key>
|
||||||
<BreadCrumb v-if="showFolderBreadcrumb(index)" :path="doc.loc ? doc.loc.split('/') : []" class="folder-indicator"/>
|
<BreadCrumb v-if="showFolderBreadcrumb(index)" :path="doc.loc ? doc.loc.split('/') : []" class="folder-indicator"/>
|
||||||
<GalleryFigure
|
<GalleryFigure
|
||||||
@@ -8,11 +8,12 @@
|
|||||||
:editing="editing === doc ? {rename, exit} : null"
|
:editing="editing === doc ? {rename, exit} : null"
|
||||||
:style="{ '--gallery-figure-height': rowHeightsByKey[doc.key] ?? '15em' }"
|
:style="{ '--gallery-figure-height': rowHeightsByKey[doc.key] ?? '15em' }"
|
||||||
@menu="contextMenu($event, doc)"
|
@menu="contextMenu($event, doc)"
|
||||||
@rename="editing = doc; store.cursor = doc.key"
|
@rename="onFigureRename(doc)"
|
||||||
:class="{ 'folder-start': showFolderBreadcrumb(index) }"
|
:class="{ 'folder-start': showFolderBreadcrumb(index) }"
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
|
<EmptyFolder v-else :documents="documents" :path="props.path" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
@@ -25,12 +26,13 @@ import ContextMenu from '@imengyu/vue3-context-menu'
|
|||||||
import {
|
import {
|
||||||
computed,
|
computed,
|
||||||
nextTick,
|
nextTick,
|
||||||
|
onActivated,
|
||||||
|
onDeactivated,
|
||||||
onMounted,
|
onMounted,
|
||||||
onUnmounted,
|
onUnmounted,
|
||||||
ref,
|
ref,
|
||||||
shallowRef,
|
shallowRef,
|
||||||
watch,
|
watch
|
||||||
watchEffect
|
|
||||||
} from 'vue'
|
} from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
|
|
||||||
@@ -62,6 +64,10 @@ const editing = shallowRef<Doc | null>(null)
|
|||||||
const exit = () => {
|
const exit = () => {
|
||||||
editing.value = null
|
editing.value = null
|
||||||
}
|
}
|
||||||
|
const onFigureRename = (doc: Doc) => {
|
||||||
|
editing.value = doc
|
||||||
|
store.cursor = doc.key
|
||||||
|
}
|
||||||
const rename = async (doc: Doc, newName: string) => {
|
const rename = async (doc: Doc, newName: string) => {
|
||||||
const oldName = doc.name
|
const oldName = doc.name
|
||||||
doc.name = newName // We should get an update from watch but this is quicker
|
doc.name = newName // We should get an update from watch but this is quicker
|
||||||
@@ -170,6 +176,7 @@ const onImgLoad = (e: Event) => {
|
|||||||
}
|
}
|
||||||
const updateColumns = () => {
|
const updateColumns = () => {
|
||||||
if (!gallery.value) return
|
if (!gallery.value) return
|
||||||
|
if (gallery.value.getBoundingClientRect().width <= 0) return
|
||||||
const style = getComputedStyle(gallery.value)
|
const style = getComputedStyle(gallery.value)
|
||||||
const templates = style.gridTemplateColumns
|
const templates = style.gridTemplateColumns
|
||||||
.split(' ')
|
.split(' ')
|
||||||
@@ -260,6 +267,19 @@ const pageMove = (direction: 1 | -1, ev: KeyboardEvent) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defineExpose({
|
defineExpose({
|
||||||
|
newFile() {
|
||||||
|
const now = Math.floor(Date.now() / 1000)
|
||||||
|
editing.value = new Doc({
|
||||||
|
loc: loc.value,
|
||||||
|
key: 'new',
|
||||||
|
name: 'New File.txt',
|
||||||
|
dir: false,
|
||||||
|
mtime: now,
|
||||||
|
size: 0,
|
||||||
|
allocated: 0
|
||||||
|
})
|
||||||
|
store.cursor = editing.value.key
|
||||||
|
},
|
||||||
newFolder() {
|
newFolder() {
|
||||||
const now = Math.floor(Date.now() / 1000)
|
const now = Math.floor(Date.now() / 1000)
|
||||||
editing.value = new Doc({
|
editing.value = new Doc({
|
||||||
@@ -288,7 +308,7 @@ defineExpose({
|
|||||||
const docs = props.documents
|
const docs = props.documents
|
||||||
if (docs.length > 0) {
|
if (docs.length > 0) {
|
||||||
store.cursor = docs[0]!.key
|
store.cursor = docs[0]!.key
|
||||||
// Also focus the element directly (watchEffect won't trigger if cursor unchanged)
|
// Also focus the element directly (post-flush watcher won't trigger if cursor unchanged)
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
const a = document.querySelector(
|
const a = document.querySelector(
|
||||||
`#file-${store.cursor}`
|
`#file-${store.cursor}`
|
||||||
@@ -380,25 +400,55 @@ const focusBreadcrumb = () => {
|
|||||||
const keyboardFollowScroll = createKeyboardFollowScroll()
|
const keyboardFollowScroll = createKeyboardFollowScroll()
|
||||||
const markKeyboardFollow = keyboardFollowScroll.markKeyboardFollow
|
const markKeyboardFollow = keyboardFollowScroll.markKeyboardFollow
|
||||||
const keepCursorVisibleSmooth = keyboardFollowScroll.keepVisible
|
const keepCursorVisibleSmooth = keyboardFollowScroll.keepVisible
|
||||||
watchEffect(() => {
|
// Deactivated (KeepAlive-cached) instances stay alive with frozen, potentially
|
||||||
if (store.cursor && store.cursor !== editing.value?.key) editing.value = null
|
// stale props - their watchers must not react to global store changes.
|
||||||
if (editing.value) store.cursor = editing.value.key
|
let isActive = true
|
||||||
if (store.cursor && !editing.value) {
|
watch(
|
||||||
const a = document.querySelector(
|
() => store.cursor,
|
||||||
`#file-${store.cursor}`
|
cursor => {
|
||||||
) as HTMLAnchorElement | null
|
if (!isActive) return
|
||||||
if (a) {
|
if (cursor && editing.value && cursor !== editing.value.key) {
|
||||||
a.focus({ preventScroll: true })
|
exit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
watchEffect(() => {
|
watch(
|
||||||
if (!props.documents.length && store.cursor && !store.query) {
|
() => store.cursor,
|
||||||
store.cursor = ''
|
cursor => {
|
||||||
focusBreadcrumb()
|
if (!isActive) return
|
||||||
|
if (cursor && !editing.value) {
|
||||||
|
const a = document.querySelector(`#file-${cursor}`) as HTMLAnchorElement | null
|
||||||
|
if (a) {
|
||||||
|
a.focus({ preventScroll: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ flush: 'post' }
|
||||||
|
)
|
||||||
|
watch(
|
||||||
|
() => [props.documents.length, store.cursor, store.query, editing.value] as const,
|
||||||
|
([len, cursor, query, editingDoc]) => {
|
||||||
|
if (!isActive) return
|
||||||
|
if (!len && cursor && !query && !editingDoc) {
|
||||||
|
store.cursor = ''
|
||||||
|
focusBreadcrumb()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
let resizeObserver: ResizeObserver | null = null
|
let resizeObserver: ResizeObserver | null = null
|
||||||
|
const attachGalleryObservers = () => {
|
||||||
|
if (!gallery.value || resizeObserver) return
|
||||||
|
resizeObserver = new ResizeObserver(updateColumns)
|
||||||
|
resizeObserver.observe(gallery.value)
|
||||||
|
gallery.value.addEventListener('load', onImgLoad, { capture: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
const detachGalleryObservers = () => {
|
||||||
|
resizeObserver?.disconnect()
|
||||||
|
resizeObserver = null
|
||||||
|
gallery.value?.removeEventListener('load', onImgLoad, { capture: true })
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
const active = document.querySelector('.cursor') as HTMLElement | null
|
const active = document.querySelector('.cursor') as HTMLElement | null
|
||||||
if (active) {
|
if (active) {
|
||||||
@@ -406,33 +456,58 @@ onMounted(() => {
|
|||||||
}
|
}
|
||||||
updateColumns()
|
updateColumns()
|
||||||
seedFromDocs()
|
seedFromDocs()
|
||||||
if (gallery.value) {
|
attachGalleryObservers()
|
||||||
resizeObserver = new ResizeObserver(updateColumns)
|
})
|
||||||
resizeObserver.observe(gallery.value)
|
onActivated(() => {
|
||||||
gallery.value.addEventListener('load', onImgLoad, { capture: true })
|
isActive = true
|
||||||
}
|
nextTick(() => {
|
||||||
|
updateColumns()
|
||||||
|
attachGalleryObservers()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
onDeactivated(() => {
|
||||||
|
isActive = false
|
||||||
|
detachGalleryObservers()
|
||||||
|
if (editing.value) exit()
|
||||||
})
|
})
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
keyboardFollowScroll.cancel()
|
keyboardFollowScroll.cancel()
|
||||||
resizeObserver?.disconnect()
|
detachGalleryObservers()
|
||||||
gallery.value?.removeEventListener('load', onImgLoad, { capture: true })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Re-seed aspect ratios whenever docs update (e.g., ar patch from server)
|
// Re-seed aspect ratios whenever docs update (e.g., ar patch from server)
|
||||||
watch(() => props.documents, seedFromDocs)
|
watch(() => props.documents, seedFromDocs)
|
||||||
const mkdir = async (doc: Doc, name: string) => {
|
const editRoute = (path: string) =>
|
||||||
|
'/' +
|
||||||
|
path
|
||||||
|
.split('/')
|
||||||
|
.map(part => encodeURIComponent(part))
|
||||||
|
.join('/')
|
||||||
|
|
||||||
|
const createItem = async (doc: Doc, name: string) => {
|
||||||
doc.name = name
|
doc.name = name
|
||||||
doc.key = crypto.randomUUID()
|
doc.key = crypto.randomUUID()
|
||||||
store.addGhost(doc)
|
store.addGhost(doc)
|
||||||
editing.value = null
|
store.cursor = doc.key
|
||||||
|
exit()
|
||||||
const path = doc.loc ? `${doc.loc}/${name}` : name
|
const path = doc.loc ? `${doc.loc}/${name}` : name
|
||||||
try {
|
try {
|
||||||
const res = await apiFetch(filesUrl(path), { method: 'MKCOL' })
|
const res = doc.dir
|
||||||
|
? await apiFetch(filesUrl(path), { method: 'MKCOL' })
|
||||||
|
: await apiFetch(filesUrl(path), {
|
||||||
|
method: 'PUT',
|
||||||
|
body: '',
|
||||||
|
headers: { 'Content-Type': 'text/plain; charset=utf-8' }
|
||||||
|
})
|
||||||
if (!res.ok) throw new Error(await parseErrorMessage(res))
|
if (!res.ok) throw new Error(await parseErrorMessage(res))
|
||||||
router.push(doc.urlrouter)
|
if (doc.dir) {
|
||||||
|
router.push(doc.urlrouter)
|
||||||
|
} else {
|
||||||
|
router.push(editRoute(path))
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Mkdir failed', err)
|
console.error('Create failed', err)
|
||||||
store.showToast(err instanceof Error ? err.message : 'Mkdir failed')
|
store.showToast(err instanceof Error ? err.message : 'Create failed')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const showFolderBreadcrumb = (i: number) => {
|
const showFolderBreadcrumb = (i: number) => {
|
||||||
@@ -562,7 +637,8 @@ const contextMenu = (ev: MouseEvent, doc: Doc) => {
|
|||||||
display: grid;
|
display: grid;
|
||||||
gap: .5em;
|
gap: .5em;
|
||||||
grid-template-columns: repeat(auto-fill, minmax(15em, 1fr));
|
grid-template-columns: repeat(auto-fill, minmax(15em, 1fr));
|
||||||
align-items: end;
|
align-items: start;
|
||||||
|
align-content: start;
|
||||||
}
|
}
|
||||||
.folder-indicator {
|
.folder-indicator {
|
||||||
grid-column: 1 / -1;
|
grid-column: 1 / -1;
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
<span class="filename">{{ snap.displayName }}<SparseIndicator :doc="doc" class="after-name" /></span>
|
<span class="filename">{{ snap.displayName }}<SparseIndicator :doc="doc" class="after-name" /></span>
|
||||||
<span v-if="snap.ext" class="file-ext">.{{ snap.ext }}</span>
|
<span v-if="snap.ext" class="file-ext">.{{ snap.ext }}</span>
|
||||||
</span>
|
</span>
|
||||||
<button class="rename-btn" @click="$emit('rename')" title="Rename">✏️</button>
|
<button class="rename-btn" @click="emit('rename')" title="Rename">✏️</button>
|
||||||
</div>
|
</div>
|
||||||
<div class=namespacer></div>
|
<div class=namespacer></div>
|
||||||
</template>
|
</template>
|
||||||
@@ -49,10 +49,12 @@ import { Doc } from '@/repositories/Document'
|
|||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import { formatSize } from '@/utils'
|
import { formatSize } from '@/utils'
|
||||||
import { computed, ref } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
import CursorTooltip from './CursorTooltip.vue'
|
import CursorTooltip from './CursorTooltip.vue'
|
||||||
import SparseIndicator from './SparseIndicator.vue'
|
import SparseIndicator from './SparseIndicator.vue'
|
||||||
|
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
|
const router = useRouter()
|
||||||
type EditingProp = {
|
type EditingProp = {
|
||||||
rename: (doc: Doc, newName: string) => void
|
rename: (doc: Doc, newName: string) => void
|
||||||
exit: () => void
|
exit: () => void
|
||||||
@@ -62,6 +64,10 @@ const props = defineProps<{
|
|||||||
doc: Doc
|
doc: Doc
|
||||||
editing?: EditingProp
|
editing?: EditingProp
|
||||||
}>()
|
}>()
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(e: 'rename'): void
|
||||||
|
(e: 'menu', ev: MouseEvent): void
|
||||||
|
}>()
|
||||||
const m = ref<typeof MediaPreview | null>(null)
|
const m = ref<typeof MediaPreview | null>(null)
|
||||||
const tooltip = ref<InstanceType<typeof CursorTooltip> | null>(null)
|
const tooltip = ref<InstanceType<typeof CursorTooltip> | null>(null)
|
||||||
|
|
||||||
@@ -87,7 +93,12 @@ const snap = computed(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const onclick = (ev: Event) => {
|
const onclick = (ev: Event) => {
|
||||||
if (m.value!.play()) ev.preventDefault()
|
if (m.value!.play()) {
|
||||||
|
ev.preventDefault()
|
||||||
|
} else if (props.doc.text) {
|
||||||
|
ev.preventDefault()
|
||||||
|
router.push(props.doc.editurl.replace('/#', ''))
|
||||||
|
}
|
||||||
store.cursor = props.doc.key
|
store.cursor = props.doc.key
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -211,9 +222,9 @@ figcaption {
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
figcaption input[type='checkbox'] {
|
figcaption input[type='checkbox'] {
|
||||||
width: 1.5em;
|
width: 1.1em;
|
||||||
height: 1.5em;
|
height: 1.1em;
|
||||||
margin: .25em 0 .25em .25em;
|
margin: .25em .4em .25em .35em;
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
transition: opacity var(--transition-time) ease-in-out;
|
transition: opacity var(--transition-time) ease-in-out;
|
||||||
|
|||||||
@@ -1,30 +1,43 @@
|
|||||||
<template>
|
<template>
|
||||||
<nav class="headermain buttons">
|
<nav class="headermain buttons">
|
||||||
<UploadButton :path="props.path" />
|
<template v-if="!props.editorMode">
|
||||||
<SvgButton
|
<UploadButton :path="props.path" />
|
||||||
name="create-folder"
|
<SvgButton
|
||||||
tooltip="New folder"
|
name="create-file"
|
||||||
@click="() => { store.fileExplorer!.newFolder() }"
|
tooltip="New file"
|
||||||
/>
|
@click="() => { store.fileExplorer!.newFile() }"
|
||||||
<div class="smallgap"></div>
|
|
||||||
<SvgButton name="eye" @click="store.prefs.gallery = !store.prefs.gallery" tooltip="Details/Gallery" />
|
|
||||||
<div class="search-group">
|
|
||||||
<SvgButton name="find" tabindex="-1" @click="focusSearch" tooltip="Search" />
|
|
||||||
<input
|
|
||||||
ref="search"
|
|
||||||
type="search"
|
|
||||||
:value="query"
|
|
||||||
@input="updateSearch"
|
|
||||||
@keydown.escape="clearSearch"
|
|
||||||
/>
|
/>
|
||||||
<span v-if="!query" class="search-hint" @click="focusSearch">{{ store.prefs.searchHotkey }}</span>
|
<SvgButton
|
||||||
</div>
|
name="create-folder"
|
||||||
<div v-if="showSortHints" class="sort-hints">
|
tooltip="New folder"
|
||||||
|
@click="() => { store.fileExplorer!.newFolder() }"
|
||||||
|
/>
|
||||||
|
<div class="smallgap"></div>
|
||||||
|
<SvgButton name="eye" @click="store.prefs.gallery = !store.prefs.gallery" tooltip="Details/Gallery" />
|
||||||
|
<div class="search-group">
|
||||||
|
<SvgButton name="find" tabindex="-1" @click="focusSearch" tooltip="Search" />
|
||||||
|
<input
|
||||||
|
ref="search"
|
||||||
|
type="search"
|
||||||
|
:value="query"
|
||||||
|
@input="updateSearch"
|
||||||
|
@keydown.escape="clearSearch"
|
||||||
|
/>
|
||||||
|
<span v-if="!query" class="search-hint" @click="focusSearch">{{ store.prefs.searchHotkey }}</span>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<div v-if="!props.editorMode && showSortHints" class="sort-hints">
|
||||||
<span class="sort-label">Order</span>
|
<span class="sort-label">Order</span>
|
||||||
<span class="keycap">1</span>
|
<span class="keycap">1</span>
|
||||||
<span class="keycap">2</span>
|
<span class="keycap">2</span>
|
||||||
<span class="keycap">3</span>
|
<span class="keycap">3</span>
|
||||||
</div>
|
</div>
|
||||||
|
<SvgButton
|
||||||
|
v-if="props.editorMode"
|
||||||
|
name="disk"
|
||||||
|
tooltip="Save (Ctrl/Cmd+S)"
|
||||||
|
@click="store.editorSave?.()"
|
||||||
|
/>
|
||||||
<div class="spacer smallgap"></div>
|
<div class="spacer smallgap"></div>
|
||||||
<DiskSpace v-if="store.space.disk" />
|
<DiskSpace v-if="store.space.disk" />
|
||||||
<SvgButton name="cog" @click="settingsMenu" />
|
<SvgButton name="cog" @click="settingsMenu" />
|
||||||
@@ -49,6 +62,7 @@ const textInputFocused = ref(false)
|
|||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
path: Array<string>
|
path: Array<string>
|
||||||
query: string
|
query: string
|
||||||
|
editorMode?: boolean
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const isInputElement = (el: Element | null): boolean => {
|
const isInputElement = (el: Element | null): boolean => {
|
||||||
|
|||||||
@@ -15,11 +15,11 @@
|
|||||||
</div>
|
</div>
|
||||||
<span class="select-size">{{ selectionDisplay.size }}</span>
|
<span class="select-size">{{ selectionDisplay.size }}</span>
|
||||||
<DownloadButton />
|
<DownloadButton />
|
||||||
<button
|
<SvgButton
|
||||||
class="action-button"
|
name="link"
|
||||||
title="Copy share link (Alt-click for read/write)"
|
tooltip="Copy share link (Alt-click for read/write)"
|
||||||
@click="copyShareLink"
|
@click="copyShareLink"
|
||||||
>share</button>
|
/>
|
||||||
<SvgButton name="copy" tooltip="Copy here" @click="op('cp', dst)" />
|
<SvgButton name="copy" tooltip="Copy here" @click="op('cp', dst)" />
|
||||||
<SvgButton name="paste" tooltip="Move here" @click="op('mv', dst)" />
|
<SvgButton name="paste" tooltip="Move here" @click="op('mv', dst)" />
|
||||||
<SvgButton name="trash" tooltip="Delete ⚠️" @click="op('rm')" />
|
<SvgButton name="trash" tooltip="Delete ⚠️" @click="op('rm')" />
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
@mouseenter="unselectTooltip?.startHover"
|
@mouseenter="unselectTooltip?.startHover"
|
||||||
@mousemove="unselectTooltip?.updatePosition"
|
@mousemove="unselectTooltip?.updatePosition"
|
||||||
@mouseleave="unselectTooltip?.endHover"
|
@mouseleave="unselectTooltip?.endHover"
|
||||||
>✖ selection</button>
|
>✖ deselect</button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
import { Doc } from '@/repositories/Document'
|
import { Doc } from '@/repositories/Document'
|
||||||
import { getDocuments } from '@/stores/documentStore'
|
import { getDocuments } from '@/stores/documentStore'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import { collator } from '@/utils'
|
import { collator, formatSize } from '@/utils'
|
||||||
import { onMounted, onUnmounted, ref } from 'vue'
|
import { onMounted, onUnmounted, ref } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
|
|
||||||
@@ -44,6 +44,7 @@ type InflightBlock = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const UPLOAD_BLOCK_SIZE = 16 << 20 // 16 MiB
|
const UPLOAD_BLOCK_SIZE = 16 << 20 // 16 MiB
|
||||||
|
const UPLOAD_MARGIN_BYTES = 512 * 1024 * 1024 // 512 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[]
|
||||||
@@ -116,6 +117,28 @@ const uploadCloudFiles = (files: CloudFile[]) => {
|
|||||||
}
|
}
|
||||||
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))
|
||||||
|
|
||||||
|
// Space check: reject the whole batch if there isn't enough free space.
|
||||||
|
const batchTotal = files.reduce((sum, f) => sum + f.file.size, 0)
|
||||||
|
const allDocs = getDocuments()
|
||||||
|
const docByPath = new Map<string, Doc>()
|
||||||
|
for (const d of allDocs) {
|
||||||
|
const path = d.loc ? `${d.loc}/${d.name}` : d.name
|
||||||
|
docByPath.set(path, d)
|
||||||
|
}
|
||||||
|
let overwriteSize = 0
|
||||||
|
for (const f of files) {
|
||||||
|
const existing = docByPath.get(f.cloudName)
|
||||||
|
if (existing && !existing.dir) overwriteSize += existing.size
|
||||||
|
}
|
||||||
|
const netNeed = batchTotal - overwriteSize
|
||||||
|
if (store.space.free < netNeed + UPLOAD_MARGIN_BYTES) {
|
||||||
|
store.showToast(
|
||||||
|
`Not enough free space (need ${formatSize(netNeed + UPLOAD_MARGIN_BYTES)}, have ${formatSize(store.space.free)})`
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Optimistic update: ghost folders and files
|
// Optimistic update: ghost folders and files
|
||||||
const now = Math.floor(Date.now() / 1000)
|
const now = Math.floor(Date.now() / 1000)
|
||||||
const docs = getDocuments()
|
const docs = getDocuments()
|
||||||
|
|||||||
@@ -89,6 +89,14 @@ export class Doc {
|
|||||||
get print(): boolean {
|
get print(): boolean {
|
||||||
return (FILE_TYPES.print as readonly string[]).includes(this.ext)
|
return (FILE_TYPES.print as readonly string[]).includes(this.ext)
|
||||||
}
|
}
|
||||||
|
get text(): boolean {
|
||||||
|
return (FILE_TYPES.text as readonly string[]).includes(this.ext)
|
||||||
|
}
|
||||||
|
get editurl(): string {
|
||||||
|
if (!this.text) return ''
|
||||||
|
const p = this.loc ? `${this.loc}/${this.name}` : this.name
|
||||||
|
return '/#/' + p.replaceAll('?', '%3F').replaceAll('#', '%23')
|
||||||
|
}
|
||||||
get complete(): boolean {
|
get complete(): boolean {
|
||||||
return !this.ghost && (this.dir || this.size <= this.allocated)
|
return !this.ghost && (this.dir || this.size <= this.allocated)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
|
import { useMainStore } from '@/stores/main'
|
||||||
import ExplorerView from '@/views/ExplorerView.vue'
|
import ExplorerView from '@/views/ExplorerView.vue'
|
||||||
import { createRouter, createWebHashHistory } from 'vue-router'
|
import { createRouter, createWebHashHistory } from 'vue-router'
|
||||||
|
|
||||||
|
function getPathDepth(path: string): number {
|
||||||
|
const pathPart = decodeURIComponent(path).split('//')[0] ?? ''
|
||||||
|
return pathPart.split('/').filter(Boolean).length
|
||||||
|
}
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHashHistory(import.meta.env.BASE_URL),
|
history: createWebHashHistory(import.meta.env.BASE_URL),
|
||||||
routes: [
|
routes: [
|
||||||
@@ -12,4 +18,17 @@ const router = createRouter({
|
|||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.beforeEach((to, from) => {
|
||||||
|
const store = useMainStore()
|
||||||
|
const toDepth = getPathDepth(to.path)
|
||||||
|
const fromDepth = getPathDepth(from.path)
|
||||||
|
if (toDepth > fromDepth) {
|
||||||
|
store.transitionDirection = 'forward'
|
||||||
|
} else if (toDepth < fromDepth) {
|
||||||
|
store.transitionDirection = 'backward'
|
||||||
|
} else {
|
||||||
|
store.transitionDirection = 'none'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|||||||
@@ -98,6 +98,8 @@ export const useMainStore = defineStore('main', {
|
|||||||
privileged: false as boolean,
|
privileged: false as boolean,
|
||||||
isLoggedIn: false as boolean
|
isLoggedIn: false as boolean
|
||||||
},
|
},
|
||||||
|
transitionDirection: 'none' as 'forward' | 'backward' | 'none',
|
||||||
|
editorSave: null as null | (() => void),
|
||||||
space: {
|
space: {
|
||||||
disk: 0,
|
disk: 0,
|
||||||
free: 0,
|
free: 0,
|
||||||
@@ -326,6 +328,7 @@ export const useMainStore = defineStore('main', {
|
|||||||
this.connected = false
|
this.connected = false
|
||||||
this.dialog = ''
|
this.dialog = ''
|
||||||
this.cursor = ''
|
this.cursor = ''
|
||||||
|
this.editorSave = null
|
||||||
},
|
},
|
||||||
async logout() {
|
async logout() {
|
||||||
console.log('Logout')
|
console.log('Logout')
|
||||||
|
|||||||
@@ -5,10 +5,18 @@ export const exists = (path: string[]) => {
|
|||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
// Access docVersion to make this reactive
|
// Access docVersion to make this reactive
|
||||||
void store.docVersion
|
void store.docVersion
|
||||||
|
if (path.length === 0) return true
|
||||||
const p = path.join('/')
|
const p = path.join('/')
|
||||||
return getDocuments().some(
|
const hidden = store.hiddenPaths
|
||||||
doc => (doc.loc ? `${doc.loc}/${doc.name}` : doc.name) === p
|
const inDocs = getDocuments().some(doc => {
|
||||||
)
|
const full = doc.loc ? `${doc.loc}/${doc.name}` : doc.name
|
||||||
|
return full === p && !hidden.has(full)
|
||||||
|
})
|
||||||
|
if (inDocs) return true
|
||||||
|
return store.ghosts.some(g => {
|
||||||
|
const full = g.loc ? `${g.loc}/${g.name}` : g.name
|
||||||
|
return full === p && !hidden.has(full)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Strip file extension intelligently (handles .tar.gz, name.with.dots.pdf, etc.) */
|
/** Strip file extension intelligently (handles .tar.gz, name.with.dots.pdf, etc.) */
|
||||||
|
|||||||
@@ -77,7 +77,65 @@ export const FILE_TYPES = {
|
|||||||
imageBrowser: ['avif', 'gif', 'jpg', 'jpeg', 'png', 'webp', 'svg'],
|
imageBrowser: ['avif', 'gif', 'jpg', 'jpeg', 'png', 'webp', 'svg'],
|
||||||
// Images that require server-side preview (browsers cannot display them natively)
|
// Images that require server-side preview (browsers cannot display them natively)
|
||||||
image: ['bmp', 'heic', 'heif', 'ico', 'tif', 'tiff'],
|
image: ['bmp', 'heic', 'heif', 'ico', 'tif', 'tiff'],
|
||||||
print: ['epub', 'mobi', 'pdf']
|
print: ['epub', 'mobi', 'pdf'],
|
||||||
|
text: [
|
||||||
|
'txt',
|
||||||
|
'md',
|
||||||
|
'json',
|
||||||
|
'xml',
|
||||||
|
'yaml',
|
||||||
|
'yml',
|
||||||
|
'toml',
|
||||||
|
'ini',
|
||||||
|
'conf',
|
||||||
|
'config',
|
||||||
|
'cfg',
|
||||||
|
'log',
|
||||||
|
'csv',
|
||||||
|
'tsv',
|
||||||
|
'py',
|
||||||
|
'js',
|
||||||
|
'ts',
|
||||||
|
'jsx',
|
||||||
|
'tsx',
|
||||||
|
'html',
|
||||||
|
'htm',
|
||||||
|
'css',
|
||||||
|
'scss',
|
||||||
|
'sass',
|
||||||
|
'less',
|
||||||
|
'vue',
|
||||||
|
'php',
|
||||||
|
'rb',
|
||||||
|
'go',
|
||||||
|
'rs',
|
||||||
|
'java',
|
||||||
|
'c',
|
||||||
|
'cpp',
|
||||||
|
'h',
|
||||||
|
'hpp',
|
||||||
|
'cs',
|
||||||
|
'swift',
|
||||||
|
'kt',
|
||||||
|
'sh',
|
||||||
|
'bash',
|
||||||
|
'zsh',
|
||||||
|
'fish',
|
||||||
|
'ps1',
|
||||||
|
'bat',
|
||||||
|
'cmd',
|
||||||
|
'sql',
|
||||||
|
'lua',
|
||||||
|
'r',
|
||||||
|
'pl',
|
||||||
|
'dockerfile',
|
||||||
|
'makefile',
|
||||||
|
'gitignore',
|
||||||
|
'gitattributes',
|
||||||
|
'env',
|
||||||
|
'diff',
|
||||||
|
'patch'
|
||||||
|
]
|
||||||
} as const
|
} as const
|
||||||
|
|
||||||
export type FileCategory = keyof typeof FILE_TYPES
|
export type FileCategory = keyof typeof FILE_TYPES
|
||||||
|
|||||||
@@ -1,29 +1,32 @@
|
|||||||
<template>
|
<template>
|
||||||
<Gallery
|
<div class="transition-wrapper">
|
||||||
v-if="store.prefs.gallery"
|
<Transition
|
||||||
ref="fileExplorer"
|
:name="transitionName"
|
||||||
:key="`gallery-${folderPath}`"
|
@after-enter="onAfterEnter"
|
||||||
:path="props.path"
|
>
|
||||||
:documents="documents"
|
<KeepAlive>
|
||||||
/>
|
<component
|
||||||
<FileExplorer
|
:is="store.prefs.gallery ? Gallery : FileExplorer"
|
||||||
v-else
|
:key="cacheKey"
|
||||||
ref="fileExplorer"
|
ref="fileExplorer"
|
||||||
:key="`explorer-${folderPath}`"
|
class="explorer-content"
|
||||||
:path="props.path"
|
:path="props.path"
|
||||||
:documents="documents"
|
:documents="documents"
|
||||||
/>
|
/>
|
||||||
|
</KeepAlive>
|
||||||
|
</Transition>
|
||||||
|
</div>
|
||||||
<div v-if="store.searchLoading" class="search-loading">Searching...</div>
|
<div v-if="store.searchLoading" class="search-loading">Searching...</div>
|
||||||
<EmptyFolder :documents=documents :path=props.path />
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import FileExplorer from '@/components/FileExplorer.vue'
|
import FileExplorer from '@/components/FileExplorer.vue'
|
||||||
|
import Gallery from '@/components/Gallery.vue'
|
||||||
import { getDocuments } from '@/stores/documentStore'
|
import { getDocuments } from '@/stores/documentStore'
|
||||||
import { useMainStore } from '@/stores/main'
|
import { useMainStore } from '@/stores/main'
|
||||||
import { collator } from '@/utils'
|
import { collator } from '@/utils'
|
||||||
import { sorted, sortedGrouped } from '@/utils/docsort'
|
import { sorted, sortedGrouped } from '@/utils/docsort'
|
||||||
import { computed, ref, watch, watchEffect } from 'vue'
|
import { computed, nextTick, ref, watch, watchEffect } from 'vue'
|
||||||
|
|
||||||
const store = useMainStore()
|
const store = useMainStore()
|
||||||
const fileExplorer = ref()
|
const fileExplorer = ref()
|
||||||
@@ -34,6 +37,31 @@ 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('/'))
|
||||||
|
const cacheKey = computed(
|
||||||
|
() => `${store.prefs.gallery ? 'gallery' : 'list'}:${folderPath.value}`
|
||||||
|
)
|
||||||
|
|
||||||
|
const transitionName = computed(() => {
|
||||||
|
if (store.transitionDirection === 'forward') return 'slide-forward'
|
||||||
|
if (store.transitionDirection === 'backward') return 'slide-backward'
|
||||||
|
return ''
|
||||||
|
})
|
||||||
|
|
||||||
|
const folderScrollTop = new Map<string, number>()
|
||||||
|
const scrollKey = (path: string) => path || '/'
|
||||||
|
const getMainScroller = () => document.querySelector('main') as HTMLElement | null
|
||||||
|
|
||||||
|
const restoreScroll = (path: string) => {
|
||||||
|
const scroller = getMainScroller()
|
||||||
|
if (!scroller) return
|
||||||
|
const top = folderScrollTop.get(scrollKey(path)) ?? 0
|
||||||
|
scroller.scrollTop = top
|
||||||
|
}
|
||||||
|
|
||||||
|
const onAfterEnter = () => {
|
||||||
|
store.transitionDirection = 'none'
|
||||||
|
restoreScroll(folderPath.value)
|
||||||
|
}
|
||||||
|
|
||||||
// Handle route-based search changes (back/forward navigation, direct URL)
|
// Handle route-based search changes (back/forward navigation, direct URL)
|
||||||
// Skip if store.query already matches (means we triggered this via typing)
|
// Skip if store.query already matches (means we triggered this via typing)
|
||||||
@@ -87,6 +115,19 @@ watchEffect(() => {
|
|||||||
store.fileExplorer = fileExplorer.value
|
store.fileExplorer = fileExplorer.value
|
||||||
})
|
})
|
||||||
|
|
||||||
|
watch(
|
||||||
|
folderPath,
|
||||||
|
async (path, oldPath) => {
|
||||||
|
const scroller = getMainScroller()
|
||||||
|
if (scroller && oldPath !== undefined) {
|
||||||
|
folderScrollTop.set(scrollKey(oldPath), scroller.scrollTop)
|
||||||
|
}
|
||||||
|
await nextTick()
|
||||||
|
requestAnimationFrame(() => restoreScroll(path))
|
||||||
|
},
|
||||||
|
{ immediate: true }
|
||||||
|
)
|
||||||
|
|
||||||
// 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(
|
watch(
|
||||||
[() => props.path.join('/'), () => store.documentCount],
|
[() => props.path.join('/'), () => store.documentCount],
|
||||||
@@ -100,16 +141,6 @@ watch(
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.empty-container {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
height: 100%;
|
|
||||||
font-size: 2rem;
|
|
||||||
text-shadow: 0 0 .3rem #000, 0 0 2rem #0008;
|
|
||||||
color: var(--accent-color);
|
|
||||||
}
|
|
||||||
.search-loading {
|
.search-loading {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
bottom: 1rem;
|
bottom: 1rem;
|
||||||
|
|||||||
@@ -0,0 +1,255 @@
|
|||||||
|
<template>
|
||||||
|
<div class="text-editor">
|
||||||
|
<div class="editor-body">
|
||||||
|
<div v-if="loading" class="status">Loading…</div>
|
||||||
|
<div v-else-if="error" class="status error">{{ error }}</div>
|
||||||
|
<div v-else ref="editorHost" class="editor-host"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { apiFetch } from '@/repositories/Client'
|
||||||
|
import { useMainStore } from '@/stores/main'
|
||||||
|
import { indentWithTab } from '@codemirror/commands'
|
||||||
|
import { LanguageDescription } from '@codemirror/language'
|
||||||
|
import { languages } from '@codemirror/language-data'
|
||||||
|
import { Compartment, EditorState } from '@codemirror/state'
|
||||||
|
import { oneDark } from '@codemirror/theme-one-dark'
|
||||||
|
import { EditorView, keymap } from '@codemirror/view'
|
||||||
|
import { basicSetup } from 'codemirror'
|
||||||
|
import {
|
||||||
|
computed,
|
||||||
|
nextTick,
|
||||||
|
onActivated,
|
||||||
|
onDeactivated,
|
||||||
|
onMounted,
|
||||||
|
onUnmounted,
|
||||||
|
ref
|
||||||
|
} from 'vue'
|
||||||
|
import { useRoute } from 'vue-router'
|
||||||
|
|
||||||
|
const route = useRoute()
|
||||||
|
const store = useMainStore()
|
||||||
|
|
||||||
|
const MAX_SIZE = 1024 * 1024 // 1 MiB
|
||||||
|
|
||||||
|
const filePath = computed(() => {
|
||||||
|
const raw = decodeURIComponent(route.path).split('//')[0] ?? ''
|
||||||
|
return raw.replace(/^\//, '').replace(/\/$/, '')
|
||||||
|
})
|
||||||
|
const filename = computed(() => filePath.value.split('/').pop() || '')
|
||||||
|
|
||||||
|
const filesUrl = computed(() => {
|
||||||
|
return (
|
||||||
|
'/files/' +
|
||||||
|
filePath.value
|
||||||
|
.split('/')
|
||||||
|
.map(part => encodeURIComponent(part))
|
||||||
|
.join('/')
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const content = ref('')
|
||||||
|
const original = ref('')
|
||||||
|
const loading = ref(true)
|
||||||
|
const saving = ref(false)
|
||||||
|
const error = ref('')
|
||||||
|
const editorHost = ref<HTMLDivElement | null>(null)
|
||||||
|
let editorView: EditorView | null = null
|
||||||
|
const languageCompartment = new Compartment()
|
||||||
|
|
||||||
|
const dirty = computed(() => content.value !== original.value)
|
||||||
|
|
||||||
|
const beforeUnload = (event: BeforeUnloadEvent) => {
|
||||||
|
if (!dirty.value) return
|
||||||
|
event.preventDefault()
|
||||||
|
event.returnValue = ''
|
||||||
|
}
|
||||||
|
|
||||||
|
let beforeUnloadActive = false
|
||||||
|
|
||||||
|
const activateEditorBindings = () => {
|
||||||
|
store.editorSave = save
|
||||||
|
if (!beforeUnloadActive) {
|
||||||
|
window.addEventListener('beforeunload', beforeUnload)
|
||||||
|
beforeUnloadActive = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const deactivateEditorBindings = () => {
|
||||||
|
if (store.editorSave === save) {
|
||||||
|
store.editorSave = null
|
||||||
|
}
|
||||||
|
if (beforeUnloadActive) {
|
||||||
|
window.removeEventListener('beforeunload', beforeUnload)
|
||||||
|
beforeUnloadActive = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const detectLanguage = async () => {
|
||||||
|
const language = LanguageDescription.matchFilename(languages, filename.value)
|
||||||
|
if (!language) return []
|
||||||
|
try {
|
||||||
|
return [await language.load()]
|
||||||
|
} catch {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const initEditor = async (text: string) => {
|
||||||
|
if (!editorHost.value) return
|
||||||
|
const languageExtensions = await detectLanguage()
|
||||||
|
const state = EditorState.create({
|
||||||
|
doc: text,
|
||||||
|
extensions: [
|
||||||
|
basicSetup,
|
||||||
|
oneDark,
|
||||||
|
languageCompartment.of(languageExtensions),
|
||||||
|
EditorView.updateListener.of(update => {
|
||||||
|
if (update.docChanged) {
|
||||||
|
content.value = update.state.doc.toString()
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
keymap.of([
|
||||||
|
{
|
||||||
|
key: 'Mod-s',
|
||||||
|
run: () => {
|
||||||
|
void save()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
indentWithTab
|
||||||
|
])
|
||||||
|
]
|
||||||
|
})
|
||||||
|
editorView = new EditorView({ state, parent: editorHost.value })
|
||||||
|
editorView.focus()
|
||||||
|
}
|
||||||
|
|
||||||
|
const save = async () => {
|
||||||
|
if (saving.value || loading.value) return
|
||||||
|
saving.value = true
|
||||||
|
try {
|
||||||
|
const res = await apiFetch(filesUrl.value, {
|
||||||
|
method: 'PUT',
|
||||||
|
body: content.value,
|
||||||
|
headers: { 'Content-Type': 'text/plain; charset=utf-8' }
|
||||||
|
})
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json().catch(() => ({}))
|
||||||
|
throw new Error(data.message || data.detail || `${res.status} ${res.statusText}`)
|
||||||
|
}
|
||||||
|
original.value = content.value
|
||||||
|
store.showToast(`Saved ${filename.value}`)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Save failed', err)
|
||||||
|
store.showToast(err instanceof Error ? err.message : 'Save failed')
|
||||||
|
} finally {
|
||||||
|
saving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
activateEditorBindings()
|
||||||
|
loading.value = true
|
||||||
|
error.value = ''
|
||||||
|
try {
|
||||||
|
const res = await fetch(filesUrl.value, { method: 'HEAD' })
|
||||||
|
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
|
||||||
|
const size = Number(res.headers.get('content-length') || '0')
|
||||||
|
if (size > MAX_SIZE) {
|
||||||
|
throw new Error(
|
||||||
|
`File is too large to edit (${(size / 1024 / 1024).toFixed(1)} MB)`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
const textRes = await fetch(filesUrl.value)
|
||||||
|
if (!textRes.ok) throw new Error(`${textRes.status} ${textRes.statusText}`)
|
||||||
|
const text = await textRes.text()
|
||||||
|
content.value = text
|
||||||
|
original.value = text
|
||||||
|
loading.value = false
|
||||||
|
await nextTick()
|
||||||
|
await initEditor(text)
|
||||||
|
} catch (err) {
|
||||||
|
error.value = err instanceof Error ? err.message : 'Failed to load file'
|
||||||
|
} finally {
|
||||||
|
if (loading.value) loading.value = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
onActivated(() => {
|
||||||
|
activateEditorBindings()
|
||||||
|
})
|
||||||
|
|
||||||
|
onDeactivated(() => {
|
||||||
|
deactivateEditorBindings()
|
||||||
|
})
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
deactivateEditorBindings()
|
||||||
|
editorView?.destroy()
|
||||||
|
editorView = null
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.text-editor {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
height: 100%;
|
||||||
|
background: #1a1a1a;
|
||||||
|
color: #ddd;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
.editor-body {
|
||||||
|
flex: 1;
|
||||||
|
min-height: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
.editor-host {
|
||||||
|
flex: 1;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
.editor-host :deep(.cm-editor) {
|
||||||
|
flex: 1;
|
||||||
|
height: 100%;
|
||||||
|
border: none;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
.editor-host :deep(.cm-scroller) {
|
||||||
|
font-family: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
|
||||||
|
font-size: 0.875rem;
|
||||||
|
line-height: 1.5;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
.editor-host :deep(.cm-content) {
|
||||||
|
padding: 1rem;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
.editor-host :deep(.cm-selectionBackground) {
|
||||||
|
background: var(--soft-color, #146) !important;
|
||||||
|
}
|
||||||
|
.editor-host :deep(.cm-focused .cm-selectionBackground) {
|
||||||
|
background: var(--soft-color, #146) !important;
|
||||||
|
}
|
||||||
|
.editor-host :deep(.cm-content ::selection) {
|
||||||
|
background: var(--soft-color, #146);
|
||||||
|
}
|
||||||
|
.editor-host :deep(.cm-content, .cm-gutter) {
|
||||||
|
font-family: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
|
||||||
|
}
|
||||||
|
.editor-host :deep(.cm-line, .cm-gutters, .cm-gutterElement) {
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
.status {
|
||||||
|
padding: 2rem;
|
||||||
|
text-align: center;
|
||||||
|
font-size: 1rem;
|
||||||
|
color: #888;
|
||||||
|
}
|
||||||
|
.status.error {
|
||||||
|
color: #f55;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -6,9 +6,6 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "./tsconfig.app.json"
|
"path": "./tsconfig.app.json"
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "./tsconfig.vitest.json"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,6 @@
|
|||||||
{
|
{
|
||||||
"extends": "@tsconfig/node18/tsconfig.json",
|
"extends": "@tsconfig/node18/tsconfig.json",
|
||||||
"include": [
|
"include": ["vite.config.*"],
|
||||||
"vite.config.*",
|
|
||||||
"vitest.config.*",
|
|
||||||
"cypress.config.*",
|
|
||||||
"nightwatch.conf.*",
|
|
||||||
"playwright.config.*"
|
|
||||||
],
|
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"composite": true,
|
"composite": true,
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "./tsconfig.app.json",
|
|
||||||
"exclude": [],
|
|
||||||
"compilerOptions": {
|
|
||||||
"composite": true,
|
|
||||||
"types": ["node", "jsdom"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+4
-2
@@ -33,6 +33,7 @@ dependencies = [
|
|||||||
"html5tagger>=1.3.0",
|
"html5tagger>=1.3.0",
|
||||||
"httpx>=0.28.0",
|
"httpx>=0.28.0",
|
||||||
"inotify>=0.2.12",
|
"inotify>=0.2.12",
|
||||||
|
"mediapreview[standard]",
|
||||||
"msgspec>=0.19.0",
|
"msgspec>=0.19.0",
|
||||||
"natsort>=8.4.0",
|
"natsort>=8.4.0",
|
||||||
"numpy>=2.3.2",
|
"numpy>=2.3.2",
|
||||||
@@ -77,7 +78,7 @@ docs = [
|
|||||||
source = "vcs"
|
source = "vcs"
|
||||||
|
|
||||||
[tool.hatch.build]
|
[tool.hatch.build]
|
||||||
artifacts = ["cista/frontend-build", "cista/docker"]
|
artifacts = ["cista/frontend-build"]
|
||||||
targets.sdist.hooks.custom.path = "scripts/fastapi-vue/buildhook.py"
|
targets.sdist.hooks.custom.path = "scripts/fastapi-vue/buildhook.py"
|
||||||
targets.sdist.include = [
|
targets.sdist.include = [
|
||||||
"/cista",
|
"/cista",
|
||||||
@@ -132,6 +133,7 @@ ignore = [
|
|||||||
"ANN205", # legacy codebase: no full runtime annotation coverage yet
|
"ANN205", # legacy codebase: no full runtime annotation coverage yet
|
||||||
"BLE001", # broad catch remains in boundary/proxy/error-handling paths
|
"BLE001", # broad catch remains in boundary/proxy/error-handling paths
|
||||||
"C901", # legacy complexity; keep other correctness rules enabled
|
"C901", # legacy complexity; keep other correctness rules enabled
|
||||||
|
"CPY", # copyright notices not wanted in this codebase
|
||||||
"D100", # legacy docs not yet standardized
|
"D100", # legacy docs not yet standardized
|
||||||
"D101", # legacy docs not yet standardized
|
"D101", # legacy docs not yet standardized
|
||||||
"D102", # legacy docs not yet standardized
|
"D102", # legacy docs not yet standardized
|
||||||
@@ -160,7 +162,7 @@ ignore = [
|
|||||||
"TRY003", # exception-message strictness too noisy on legacy handlers
|
"TRY003", # exception-message strictness too noisy on legacy handlers
|
||||||
]
|
]
|
||||||
isort.known-first-party = ["cista"]
|
isort.known-first-party = ["cista"]
|
||||||
per-file-ignores."tests/*" = ["S", "ANN", "D", "INP", "PLR2004", "ARG001"]
|
per-file-ignores."tests/*" = ["S", "ANN", "D", "INP", "PLR2004", "ARG001", "SLF001"]
|
||||||
per-file-ignores."scripts/*" = ["T20"]
|
per-file-ignores."scripts/*" = ["T20"]
|
||||||
|
|
||||||
[dependency-groups]
|
[dependency-groups]
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import errno
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import NamedTuple
|
||||||
|
from unittest.mock import patch
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import pytest_asyncio
|
||||||
|
from sanic import Sanic
|
||||||
|
|
||||||
|
from cista import config, watching
|
||||||
|
from cista.api import fileserver
|
||||||
|
from cista.fileserver import bp as fileserver_bp
|
||||||
|
|
||||||
|
|
||||||
|
class Usage(NamedTuple):
|
||||||
|
total: int
|
||||||
|
used: int
|
||||||
|
free: int
|
||||||
|
|
||||||
|
|
||||||
|
def _low_disk_usage(*args, **kwargs):
|
||||||
|
return Usage(total=1000, used=900, free=10)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def setup_storage(tmp_path: Path):
|
||||||
|
config.config = config.Config(path=tmp_path, listen=":0", public=True)
|
||||||
|
watching.state.root = []
|
||||||
|
watching.rootpath = tmp_path
|
||||||
|
yield tmp_path
|
||||||
|
watching.state.root = []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest_asyncio.fixture()
|
||||||
|
async def client(setup_storage: Path):
|
||||||
|
app = Sanic(f"disk-space-test-{uuid4().hex}", strict_slashes=True)
|
||||||
|
app.router.ALLOWED_METHODS = (
|
||||||
|
*app.router.ALLOWED_METHODS,
|
||||||
|
"MKCOL",
|
||||||
|
"MOVE",
|
||||||
|
"COPY",
|
||||||
|
"PROPFIND",
|
||||||
|
)
|
||||||
|
app.blueprint(fileserver_bp)
|
||||||
|
await fileserver.start()
|
||||||
|
yield app.asgi_client
|
||||||
|
await fileserver.stop()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_upload_rejected_when_disk_low(client):
|
||||||
|
with patch("cista.util.diskspace.shutil.disk_usage", side_effect=_low_disk_usage):
|
||||||
|
_, res = await client.put("/files/test.txt", data=b"hello world")
|
||||||
|
assert res.status_code == 507
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_upload_rejected_on_enospc(client):
|
||||||
|
with patch(
|
||||||
|
"cista.fileio.os.write",
|
||||||
|
side_effect=OSError(errno.ENOSPC, "No space left on device"),
|
||||||
|
):
|
||||||
|
_, res = await client.put("/files/test.txt", data=b"hello world")
|
||||||
|
assert res.status_code == 507
|
||||||
@@ -115,6 +115,12 @@ def setup_storage(tmp_path: Path):
|
|||||||
mode="rw",
|
mode="rw",
|
||||||
share_paths=["docs"],
|
share_paths=["docs"],
|
||||||
)
|
)
|
||||||
|
share_anon = config.Token(
|
||||||
|
key="share_anon_123",
|
||||||
|
kind="share",
|
||||||
|
mode="ro",
|
||||||
|
share_paths=["docs"],
|
||||||
|
)
|
||||||
config.config = config.Config(
|
config.config = config.Config(
|
||||||
path=tmp_path,
|
path=tmp_path,
|
||||||
listen=":0",
|
listen=":0",
|
||||||
@@ -124,6 +130,7 @@ def setup_storage(tmp_path: Path):
|
|||||||
"test_token_123": token,
|
"test_token_123": token,
|
||||||
"share_ro_123": share_ro,
|
"share_ro_123": share_ro,
|
||||||
"share_rw_123": share_rw,
|
"share_rw_123": share_rw,
|
||||||
|
"share_anon_123": share_anon,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
watching.state.root = []
|
watching.state.root = []
|
||||||
@@ -285,3 +292,20 @@ async def test_share_token_rw_allows_writes_in_scope_only(client):
|
|||||||
"/files/secret.txt", headers=_basic_auth("token", "share_rw_123")
|
"/files/secret.txt", headers=_basic_auth("token", "share_rw_123")
|
||||||
)
|
)
|
||||||
assert res.status_code == 404
|
assert res.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_anonymous_share_token_requires_public_mode(client):
|
||||||
|
config.config.public = True
|
||||||
|
|
||||||
|
_, res = await client.get(
|
||||||
|
"/files/docs/a.txt", headers=_basic_auth("token", "share_anon_123")
|
||||||
|
)
|
||||||
|
assert res.status_code == 200
|
||||||
|
assert res.body == b"A"
|
||||||
|
|
||||||
|
config.config.public = False
|
||||||
|
_, res = await client.get(
|
||||||
|
"/files/docs/a.txt", headers=_basic_auth("token", "share_anon_123")
|
||||||
|
)
|
||||||
|
assert res.status_code == 401
|
||||||
|
|||||||
@@ -215,3 +215,18 @@ async def test_create_share_token(client):
|
|||||||
share_tokens = [t for t in res.json["tokens"] if t.get("kind") == "share"]
|
share_tokens = [t for t in res.json["tokens"] if t.get("kind") == "share"]
|
||||||
assert len(share_tokens) == 1
|
assert len(share_tokens) == 1
|
||||||
assert share_tokens[0]["mode"] == "ro"
|
assert share_tokens[0]["mode"] == "ro"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_create_share_token_public_anonymous(client):
|
||||||
|
config.config = msgspec.structs.replace(config.config, public=True)
|
||||||
|
|
||||||
|
_, res = await client.post(
|
||||||
|
"/api/share-tokens",
|
||||||
|
json={"paths": ["hello.txt"], "mode": "ro", "name": "public-share"},
|
||||||
|
)
|
||||||
|
assert res.status_code == 200
|
||||||
|
data = res.json
|
||||||
|
assert data["kind"] == "share"
|
||||||
|
assert data["username"] == ""
|
||||||
|
assert data["sso_user_id"] == ""
|
||||||
|
|||||||
Reference in New Issue
Block a user