Compare commits

..
Author SHA1 Message Date
+11 45a14bf2f2 Beta/v2.1.0 (#3046)
Signed-off-by: MichaIng <micha@dietpi.com>
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Mattijs Bliek <mattijs@mattijsbliek.com>
Co-authored-by: GammelSami <39372285+GammelSami@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: yzxcj797 <54314860+yzxcj797@users.noreply.github.com>
Co-authored-by: yzxcj797 <yzxcj797@users.noreply.github.com>
Co-authored-by: DireMunchkin <dire.munchkin@proton.me>
Co-authored-by: Chris <39135018+Chri6s@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aditya Raj Singh <github@adityaraj.com>
Co-authored-by: tbsagdic <86909795+tbsagdic@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Ethan Lee Qi Yang <pluscompanyhqDEPT@gmail.com>
Co-authored-by: Aditya Raj Singh <aditya@bncw.in>
Co-authored-by: MichaIng <micha@dietpi.com>
Co-authored-by: FandacqkaCorp <david@davidyloreto.com>
Co-authored-by: FandacqkaCorp <275647464+FandacqkaCorp@users.noreply.github.com>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: connorjfarrell <connorjfarrell@users.noreply.github.com>
2026-09-30 13:04:20 -05:00
e13f7075dc Beta/v2.0.9 (#3020)
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: FandacqkaCorp <david@davidyloreto.com>
Co-authored-by: FandacqkaCorp <275647464+FandacqkaCorp@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2026-09-25 19:33:57 -05:00
480 changed files with 38644 additions and 9135 deletions

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
root = true
[*]
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf
[*.go]
indent_size = 4
[{Makefile,makefile}]
indent_style = tab
+41
View File
@@ -35,3 +35,44 @@ jobs:
context: .
file: ./_docker/Dockerfile.playwright-general
push: false
create_beta_release:
needs: [test_playwright]
if: startsWith(github.event.head_commit.message, 'Beta/v')
name: Create Beta Release
runs-on: ubuntu-24.04-arm
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ secrets.PAT }}
fetch-depth: 0
- name: Extract version from commit message
id: extract_version
shell: bash
run: |
tag_name=$(echo "${{ github.event.head_commit.message }}" | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+')-beta
if [ -z "$tag_name" ] || [ "$tag_name" = "-beta" ]; then
echo "Could not extract version from: ${{ github.event.head_commit.message }}"
exit 1
fi
echo "tag_name=$tag_name" >> $GITHUB_OUTPUT
- name: Tag release commit on main
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
TAG="${{ steps.extract_version.outputs.tag_name }}"
git tag -f "$TAG" "${{ github.sha }}"
git push origin "refs/tags/$TAG" --force
- name: Create Release
uses: softprops/action-gh-release@v2
with:
target_commitish: ${{ github.sha }}
token: ${{ secrets.PAT }}
tag_name: ${{ steps.extract_version.outputs.tag_name }}
prerelease: false
make_latest: false
draft: false
generate_release_notes: true
overwrite_existing: true
name: ${{ steps.extract_version.outputs.tag_name }}
+8 -2
View File
@@ -43,7 +43,13 @@ jobs:
with:
node-version-file: frontend/.nvmrc
- working-directory: frontend
run: npm i && npm run lint
run: npm i
- name: Lint frontend
working-directory: frontend
run: npm run lint
- name: Lint frontend CSS
working-directory: frontend
run: npm run lint:css
test-frontend:
runs-on: ubuntu-24.04-arm
steps:
@@ -79,4 +85,4 @@ jobs:
with:
node-version-file: frontend/.nvmrc
- working-directory: frontend
run: npm i && npm run icons:check
run: npm i && npm run icons:check
+101 -9
View File
@@ -2,6 +2,97 @@
All notable changes to this project will be documented in this file. For commit guidelines, please refer to [Standard Version](https://github.com/conventional-changelog/standard-version).
## v2.1.0
**Security**:
- [Moderate] Conflicting upload responses no longer synchronously drain the request body before returning HTTP 409; the server now closes the body and marks the connection for closure, preventing denial-of-service from clients that never send EOF (CWE-400).
- Share download links no longer create links with token, instead they link to the UI prompting for password before download. If a direct download is required, the `/api/share/direct` api exists and documented by swagger. (#2888)
**New Features**:
- Added Storage Quotas
- configurable by folder, source, user, and share.
- Administrators can create, edit, view, and delete folder quotas through the interface.
- Quota information now appears in source views, share details, sidebar progress bars, and folder prompts.
- Enhancements to the editor (#2714):
- Added a floating button to open a resizable split view with live editing for markdown files, the scrolling is synced in both sides and scrolling in single-view modes (the viewer and editor in non-split) now also tries to sync with each other so you don't lose context easily.
- Added a toolbar: This toolbar is present on all the files.
- JSON files have a button to toggle between formatted and minified versions (#2854) (#2566).
- Markdown files have a richer toolbar with lots of quick actions, for example you can now browse, navigate and insert images more easily from your current source.
- Added a prompt to configure some editor settings in UI, that prompt is accessible via the toolbar in the three-dots menu, there you can configure things like:
- Word wrap, keybinds, autocompletion, scrollbar, etc.
- The markdown viewer now supports rendering LaTeX Math and Chemistry formulas.
- Improved and fixed some styles that weren't following the app light/dark theme (#2949).
- Proxy auth group support: `groupsClaim`, `adminGroup`, and `userGroups` now control role-based access when the proxy sends a group/role header (#2755). Admin is determined by `adminGroup` only.
- Moved `auth.adminUsername` and `auth.adminPassword` to `auth.methods.password` (legacy top-level keys are migrated automatically on load).
- Support for `.elrc` (word-by-word lyrics), `.vtt`, `.srt` sidecar files for lyrics in audio files in the media player. (#2838)
- Added setting to configure the placement of the prompts close button in profile settings (#2853) (#2595).
- Added "New file templates". You can now add and configure pre-defined filenames + extension for the creation of new files in the context menu on profile settings! (#2881) (#1239).
- sidebar links can be bulk edited as yaml (#1963) (settings > user management)
- admins can customize more defaults:
- share creation defaults and enforcements (#1692) (#2434) (#2279) (#2812) (settings > share management)
- sidebar link defaults and enforcements (#2561) (settings > user management)
- tool defaults and enforcement (settings > user management)
- Add cli init CLI command (#2957)
- Sort header in file listings inside prompts like move/copy/quick jump, is now sticky. Also added a button to show a search input and filter the items of the current listing (#3028).
- Quick jump now scrolls and highlights the current item you are previewing (#3028).
- When hovering `date` and `name` in the listings, will show up the formatted/raw date (the inverse of what you have configured to display) and the full filename (#3028) (#2623).
- User groups can now be managed from the WebUI under `User Management` -> `Groups` and when editing a user (#2983) (#3040).
**Notes**:
- An Admin can remove a `defautlEnabled` source for a user and it will remove removed until an admin adds it back.
- Enhancements to cross-source copy/move permissions and behavior.
- Improved resume upload behavior.
- Play/pause on videos in mobile now is toggled by the button in the middle rather than the whole container (#2828).
- improved video thumbnail generation speed and efficiency.
- Added icons based on extension to the upload prompt and new file/folder/rename prompts (#2881).
- Profile settings show minimal options by default. Full advanced profile settings can be shown by default by enabling `account.showAdvancedProfile`.
- sidebar links/navigation is button group toggle for clearer visibility
- Pop-up preview has 200ms debounce delay so it doesn't flash from moving the cursor across files quickly.
- Added risc-v to official releases
- Polished a bit some styles across various places of the UI (#3019) (#3028).
- Moved `Access Management` under `User Management` in the UI (#3040).
- improved developer experience with native vue webhook reloads.
- improved ffmpeg handling and concurrency via library upgrades
**Bug Fixes**:
- Undo in a fresh opened file on the editor was setting the file empty (#2714)
- Added some missing styles in the markdown viewer (#2714)
- Recaptcha not working (#1925) (#2861)
- Fix disk-usage overstatement on virtiofs bind mounts (#2894)
- Support non-ASCII share passwords (#2933)
- Fall back to buffered copies when FUSE rejects fast paths (#2938)
- Preserve deleted sidebar links across restarts (#2935)
- scope padding to listing view (#2934)
- Hide the Replace option on upload/create conflict prompts when the user lacks modify permission (or when a public share disallows replacements), so create-only users are not offered an action that the server rejects (#2837)
- Preserve Ctrl-click file selection when keyboard state is stale (#2958) (#2923)
- Avoid false stalls during parallel transfers (#2950) (#2948) thanks @gudcks0305
- Cap source usage-bar percentage at 100% when indexed size exceeds partition total (#2761) (#2238)
- On Linux, source partition totals sum distinct filesystems mounted under the source root (nested mounts) so usage bars match indexed content (#2761)
- Context menu stuck when opened from a listing in a prompt like move/copy or search (#3028).
## v2.0.9
**Security**:
- Fresh install with default `admin`/`admin` startup generates a random initial password and logs it once (#2977). Updated documentation to follow.
- OIDC login binds OAuth `state` to an HttpOnly cookie (crypto-random) and rejects tampered callbacks; post-login redirects must be same-app relative paths.
- Login, logout, and session-expiry redirects reject open-redirect targets (`//`, `/\\`, off-site URLs); signup sends credentials in a JSON body instead of query parameters.
**Notes**:
- Webdav always shows hidden files, ignores user preference. (#3004)
- SQLite shared cache (`cache=shared`) removed from the application database connection: its table-lock conflicts bypass `busy_timeout` and fail immediately. WAL mode already allows concurrent readers.
**Bugfixes**:
- Fixed intermittent blank page / blocked inline SPA script after CSP hardening (#2995)
- ffmpeg unresponsive lock issue (#2996)
- fixed token migration lockout regression from v2.0.8-beta (#2999)
- Public shares with OnlyOffice disabled (`enableOnlyOffice=false`) now reject `/office/config` and `/office/callback` requests server-side instead of only hiding the editor in the UI
- Session/API token registrations now record the JWT expiry; expired mappings are pruned at startup, and tokens expired beyond the same 2-minute grace window used for session rotation no longer resolve a user identity on public share routes
- Startup now fails when the configured/env auth signing key differs from the key persisted in the application database, instead of warning and minting tokens with divergent keys
- OnlyOffice document downloads now re-validate redirect targets against the configured document-server host, closing an SSRF gap where a redirect could send the fetch to an internal address
- OnlyOffice "closed with changes" callbacks now keep the document key until the save succeeds, so a failed save can be retried by the document server instead of being rejected as an unknown session
- External JWT (JwtAuth) requests now reuse an existing valid session cookie instead of minting and registering a new session token on every request
- SQLite index and application databases configure `busy_timeout` on every pooled connection, and busy/locked detection now uses driver result codes; index batch writes and maintenance no longer report success when the DB stays busy, and index cache reads return busy errors instead of empty results so listing can fall back to the filesystem.
## v2.0.8
**Security**:
@@ -28,7 +119,7 @@ All notable changes to this project will be documented in this file. For commit
- Add cli init CLI command (#2957)
**Notes**:
- Pop-up preview has 200ms debounce delay so it doesn't flash from moving the cursor across files quickly.
- Pop-up preview has 200ms debounce delay so it doesn't flash from moving the cursor across files quickly.
- Added risc-v to official releases
**Bugfixes**:
@@ -108,6 +199,7 @@ All notable changes to this project will be documented in this file. For commit
- External subtitles fail to load on public video shares due to authenticated subtitle endpoint (#2822) (#2827)
- OnlyOffice is inaccessible on password-protected shares (#2811)
- FFmpeg 9.0 incorrectly detected as below minimum 5.0.0 on Windows (#2820) -- thanks @yzxcj797
- Queue buttons not clickable in the desktop panel in audio files.
## v2.0.1
@@ -181,7 +273,7 @@ This version represents the most significant change to date. It **requires** bot
- opt-in feature to send deployment analytics to filebrowser quantum developer servers
- anonymized with a viewer so users can see what info would be sent.
- if opt-in, every month a snapshot of your deployment config would be sent to developer servers
- this will help me know what features are being used and what versions everyone is on over time. I will also provide a public dashboard with this information in the future.
- this will help me know what features are being used and what versions everyone is on over time. I will also provide a public dashboard with this information in the future.
- WebDAV now supports set modification time via the `X-OC-Mtime` header for clients that support it (#2626). See [WebDAV docs](https://filebrowserquantum.com/en/docs/features/webdav/).
- Copy operations now preserve their original modification times (#2642) (#2647):
- WebUI preserves both, files and directories.
@@ -361,14 +453,14 @@ This version represents the most significant change to date. It **requires** bot
- Improved style of drag and drop into listing view (#2407)
- Edit Sidebar links has new "show tools in sidebar" toggle and all users have this enabled by default. can be disabled via for new users `userDefaults.sidebar.showTools: false`
- Update user defaults ordering (#1140)
- Save view modes and sizes into local storage instead of db (#2301)
- Save view modes and sizes into local storage instead of db (#2301)
**BugFixes**:
- Blue overlay when using gestures in video files on mobile (#2360)
- Playback queue wasn't updating when changing of folder (#2360)
- Added missing `UserDefaults.listing.showCopyPath` option to config file (#2364)
- Delete prompt thumbnail didn't worked when using it with the `delete` shortcut. (#2407)
- Searching icons in the Icon picker prompt wasn't working properly (#2407)
- Searching icons in the Icon picker prompt wasn't working properly (#2407)
- Drag and drop style state was stuck until page reload (#2407)
- Navigate close settings shows "something went wrong" (#2047)
@@ -514,7 +606,7 @@ This version represents the most significant change to date. It **requires** bot
- Cannot edit shared file in OnlyOffice #2143
- PWA install button disappeared (#2086)
- Deleting a root folder was possible #2128
- PUT resource api errors if action against a folder (#2153)
- PUT resource api errors if action against a folder (#2153)
- LDAP authentication issue if a password caontains @ symbol (#2154)
- Share banner seems to be not working for custom urls (#2120)
@@ -548,7 +640,7 @@ Note: a potentially breaking change for docker users: \the default user is now "
- New Sidebar Features
- Sidebar tree navigation (#2006) (#350)
- Source usage to be customized to show os-reported values rather than calculated. This can be changed per source by editing the source link in the sidebar. (#1266) (#982)
- Archive/Unarchive actions in UI (#1252) (#335) (#1569)
- Archive/Unarchive actions in UI (#1252) (#335) (#1569)
- new api to archive/unarchive files on the server
- requires `create` user permissions
- archiving actions respect `server.maxArchiveSize`
@@ -711,7 +803,7 @@ Note: a potentially breaking change for docker users: \the default user is now "
## v1.2.1-beta
**New Features**:
- Global disable onlyoffice editor via "*" file option to disable all files for a specific user. (#1533)
- Global disable onlyoffice editor via "*" file option to disable all files for a specific user. (#1533)
**Notes**:
- upgraded imaging package and improved thumbnail generation performance. (#1797) (#1850)
@@ -814,7 +906,7 @@ Sorry for the delay -- a lot of effort went into this release, specifically abou
- warning for low free space below 20GB
- warning if read/write latency is slow
- fatal for any errors reading or writing to the directory
- links to official docs for more info
- links to official docs for more info
- access rule changes:
- denied folders won't show up in parent directory listing view (#1684)
- tools will respect access rules
@@ -849,7 +941,7 @@ Sorry for the delay -- a lot of effort went into this release, specifically abou
**BugFixes**:
- better index status updates, fixing delays #1649
- fixed long load times for listings with media info due sequential processing of files.
- fixed long load times for listings with media info due sequential processing of files.
- downloaded files always included `utf-8` in filename #1671
- custom sidebar links allow external links like `https://google.com`
- html title not populated correctly for links #1676
+9 -9
View File
@@ -86,7 +86,7 @@ See the [Official Docs](https://filebrowserquantum.com/). Contributions are welc
## Comparison Chart
Application Name | <img width="48" alt="" src="https://github.com/user-attachments/assets/c40b22c9-33da-47b7-bc4c-ce69bb5cc174" > Quantum | <img width="48" alt="" src="https://github.com/filebrowser/filebrowser/blob/master/frontend/public/img/logo.svg" > Filebrowser | <img width="48" alt="" src="https://github.com/mickael-kerjean/filestash/blob/master/public/assets/logo/app_icon.png?raw=true" > Filestash | <img width="48" alt="" src="https://avatars.githubusercontent.com/u/19211038?s=200&v=4" > Nextcloud | <img width="48" alt="" src="https://cdn.iconscout.com/icon/free/png-256/free-google-drive-logo-icon-svg-download-png-2476481.png" > Google_Drive | <img width="48" alt="" src="https://avatars.githubusercontent.com/u/6422152?v=4" > FileRun
--- | --- | --- | --- | --- | --- | --- |
Filesystem support | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ |
Filesystem support | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
Linux | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
Windows | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ |
Mac | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ |
@@ -95,7 +95,7 @@ Has Stable Release? | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
S3 support | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ |
webdav support | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ |
FTP support | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ |
Dedicated docs site? | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
Dedicated docs site? | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Multiple sources at once | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ |
Docker image size | 180 MB (with ffmpeg) | 31 MB | 240 MB (main image) | 250 MB | ❌ | > 2 GB |
Min. Memory Requirements | 512 MB | 128 MB | 128 MB (main image) | 512 MB | ❌ | 512 MB |
@@ -109,25 +109,25 @@ Content-aware search? | ❌ | ❌ | ✅ | configurable | ✅ | ✅ |
Custom job support | :construction: | ✅ | ✅ | ✅ | ❌ | ✅ |
Multiple users | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Single sign-on support | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
LDAP sign-on support | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ |
LDAP sign-on support | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Long-live API key support | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
API documentation page | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ |
Mobile App | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |
API documentation page | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Mobile App | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ |
open source? | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
tags support | :construction: | ❌ | ✅ | ✅ | ❌ | ✅ |
shareable web links? | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Event-based notifications | :construction: | ❌ | ✅ | ❌ | ❌ | ✅ |
Metrics | :construction: | ❌ | ✅ | ❌ | ❌ | ❌ |
file space quotas | :construction: | ❌ | ✅ | ❌ | ✅ | ✅ |
Metrics | :construction: | ❌ | ✅ | ✅ | ❌ | ❌ |
file space quotas | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ |
text-based files editor | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Office file support | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Office file previews | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ |
Themes | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ |
Themes | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
Branding support | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ |
activity log | ✅ | ❌ | ✅ | ✅ | ✅ | ✅
Comments support | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ |
trash support | :construction: | ❌ | ✅ | ✅ | ✅ | ✅ |
Starred/pinned files | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ |
Starred/pinned files | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Chromecast support | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
Share collections of files | :construction: | ❌ | ❌ | ❌ | ❌ | ✅ |
Can archive selected files | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
+1 -1
View File
@@ -1,4 +1,4 @@
FROM gtstef/ffmpeg:9.0-decode AS ffmpeg
FROM gtstef/ffmpeg:9.0.1-decode AS ffmpeg
FROM golang:alpine AS base
ARG VERSION
ARG REVISION
+1 -1
View File
@@ -4,4 +4,4 @@ WORKDIR /app/frontend
COPY frontend/package-lock.json /tmp/package-lock.json
RUN PW_VERSION=$(node -p "require('/tmp/package-lock.json').packages['node_modules/@playwright/test'].version") \
&& npm i "@playwright/test@${PW_VERSION}" oauth2-mock-server @types/node
RUN npx playwright install --with-deps chromium
RUN npx playwright install --with-deps chromium-headless-shell
+1
View File
@@ -15,4 +15,5 @@ COPY [ "./backend/internal/web/embed/", "./internal/web/dist/"]
# Run tests
ENV FILEBROWSER_PLAYWRIGHT_TEST=true
ENV FILEBROWSER_ADMIN_PASSWORD=playwright-password
RUN ./filebrowser & sleep 2 && cd ../frontend && npx playwright test
+3
View File
@@ -5,6 +5,9 @@ http:
frontend:
<<: *frontend-main
auth:
<<: *auth-main
userDefaults:
preview:
image: true
@@ -21,4 +21,7 @@ server-main: &server-main
defaultEnabled: true
rules:
- folderPath: "/"
viewable: true
viewable: true
auth-main: &auth-main
adminPassword: "playwright-password"
+2 -1
View File
@@ -4,7 +4,8 @@ http:
<<: *http-main
frontend:
<<: *frontend-main
auth:
<<: *auth-main
userDefaults:
preview:
image: true
@@ -21,4 +21,7 @@ server-main: &server-main
defaultEnabled: true
rules:
- folderPath: "/"
viewable: true
viewable: true
auth-main: &auth-main
adminPassword: "playwright-password"
@@ -1,6 +1,9 @@
http:
port: 8080
baseURL: "/files/"
auth:
adminPassword: "playwright-password"
server:
logging:
- levels: "info|error|warning|debug"
@@ -10,3 +13,6 @@ server:
name: "playwright"
config:
defaultEnabled: true
userDefaults:
account:
showAdvancedProfile: true
+1
View File
@@ -41,6 +41,7 @@ frontend:
title: "Playwright"
auth:
adminPassword: "playwright-password"
methods:
password:
signup: true
+3
View File
@@ -16,6 +16,9 @@ server:
private: true
defaultEnabled: true
auth:
adminPassword: "playwright-password"
frontend:
name: "Graham's Filebrowser"
disableDefaultLinks: true
+1
View File
@@ -0,0 +1 @@
FILEBROWSER_DEVMODE=true
+10 -4
View File
@@ -1,16 +1,22 @@
root = "."
tmp_dir = "tmp"
env_files = [".air.env"]
[build]
pre_cmd = ["go run ./scripts/setup-gofitz-cgo"]
cmd = "go build -o ./tmp/filebrowser --tags=mupdf --ldflags=\"-w -s -X 'github.com/gtsteffaniak/filebrowser/backend/internal/version.CommitSHA=commitSHA' -X 'github.com/gtsteffaniak/filebrowser/backend/internal/version.Version=version'\" ."
cmd = "bash ./scripts/air-build.sh"
entrypoint = "./tmp/filebrowser"
full_bin = "FILEBROWSER_DEVMODE=true CGO_ENABLED=1 ./tmp/filebrowser -c test_config.yaml"
args_bin = ["-c", "test_config.yaml"]
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["tmp", "vendor", "internal/web/dist", "internal/web/embed", "internal/preview/testdata", "internal/preview/thumbnails", "internal/preview/heic", "swagger/docs"]
exclude_dir = ["tmp", "vendor", "internal/web/dist", "internal/web/embed", "internal/preview/testdata", "internal/preview/thumbnails", "internal/preview/heic", "swagger/docs", "internal/preview/gofitzinclude"]
poll = true
poll_interval = 1000
log = "air.log"
delay = 1000 # ms
[build.windows]
cmd = "bash ./scripts/air-build.sh"
entrypoint = "./tmp/filebrowser.exe"
[log]
time = true
-22
View File
@@ -1,22 +0,0 @@
root = "."
tmp_dir = "tmp"
[build]
pre_cmd = ["go run ./scripts/setup-gofitz-cgo"]
cmd = "go build -o ./tmp/filebrowser.exe --tags=mupdf --ldflags=\"-w -s -X 'github.com/gtsteffaniak/filebrowser/backend/internal/version.CommitSHA=commitSHA' -X 'github.com/gtsteffaniak/filebrowser/backend/internal/version.Version=version'\" ."
entrypoint = "./tmp/filebrowser.exe"
full_bin = "FILEBROWSER_DEVMODE=true CGO_ENABLED=1 ./tmp/filebrowser.exe -c test_config.yaml"
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["tmp", "vendor", "internal\\web\\dist", "internal\\web\\embed", "internal\\preview\\testdata", "internal\\preview\\thumbnails", "internal\\preview\\heic", "swagger\\docs"]
exclude_unchanged = true
follow_symlink = false
poll = true
poll_interval = 1000
log = "air.log"
delay = 1000 # ms
[log]
time = true
[misc]
clean_on_exit = false
+5 -5
View File
@@ -28,7 +28,7 @@ require (
github.com/OpenPeeDeeP/depguard/v2 v2.2.1 // indirect
github.com/air-verse/air v1.67.4 // indirect
github.com/alecthomas/chroma/v2 v2.27.0 // indirect
github.com/alecthomas/go-check-sumtype v0.4.0 // indirect
github.com/alecthomas/go-check-sumtype v0.5.1-0.20260828200218-ae6904d28606 // indirect
github.com/alexkohler/nakedret/v2 v2.0.6 // indirect
github.com/alexkohler/prealloc v1.1.0 // indirect
github.com/alfatraining/structtag v1.0.0 // indirect
@@ -54,7 +54,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/charithe/durationcheck v0.0.11 // indirect
github.com/charmbracelet/colorprofile v0.4.3 // indirect
github.com/charmbracelet/ultraviolet v0.0.0-20260910203606-6c9e17dc7a16 // indirect
github.com/charmbracelet/ultraviolet v0.0.0-20260922123528-4e49372c11f9 // indirect
github.com/charmbracelet/x/ansi v0.11.8 // indirect
github.com/charmbracelet/x/term v0.2.2 // indirect
github.com/charmbracelet/x/termios v0.1.1 // indirect
@@ -105,7 +105,7 @@ require (
github.com/golangci/dupl v0.0.0-20260401084720-c99c5cf5c202 // indirect
github.com/golangci/go-printf-func-name v0.1.1 // indirect
github.com/golangci/gofmt v0.0.0-20260820135601-e84e05053792 // indirect
github.com/golangci/golangci-lint/v2 v2.13.2 // indirect
github.com/golangci/golangci-lint/v2 v2.14.0 // indirect
github.com/golangci/golines v0.15.0 // indirect
github.com/golangci/misspell v0.8.0 // indirect
github.com/golangci/plugin-module-register v0.1.2 // indirect
@@ -150,7 +150,7 @@ require (
github.com/mattn/go-colorable v0.1.15 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/mattn/go-runewidth v0.0.30 // indirect
github.com/mgechev/revive v1.16.0 // indirect
github.com/mgechev/revive v1.17.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moricho/tparallel v0.3.2 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
@@ -200,7 +200,7 @@ require (
github.com/swaggo/swag v1.16.6 // indirect
github.com/tdewolff/parse/v2 v2.8.16 // indirect
github.com/tetafro/godot v1.5.6 // indirect
github.com/timakin/bodyclose v0.0.0-20260723120731-857993a2939c // indirect
github.com/timakin/bodyclose v0.0.0-20260923065923-98ded9ab4ef2 // indirect
github.com/timonwong/loggercheck v0.12.0 // indirect
github.com/tomarrell/wrapcheck/v2 v2.12.0 // indirect
github.com/tommy-muehle/go-mnd/v2 v2.5.1 // indirect
+10
View File
@@ -58,6 +58,8 @@ github.com/alecthomas/chroma/v2 v2.27.0 h1:FodwmyOBgJULFYmDqibcp9pvfDLWdtPRh9v/r
github.com/alecthomas/chroma/v2 v2.27.0/go.mod h1:NjJ3ciIgrqBNeIkWZ4e46nseoLDslxU1LmfCoL+wcY8=
github.com/alecthomas/go-check-sumtype v0.4.0 h1:gd4mE4QsA62sXlnFIs8GUv8/0xH3DOUqTkYQR1l2v28=
github.com/alecthomas/go-check-sumtype v0.4.0/go.mod h1:aYYllI8IXNjo7ZYUzI2+wyLRECMg3zo+8CZj9JTBAWU=
github.com/alecthomas/go-check-sumtype v0.5.1-0.20260828200218-ae6904d28606 h1:dXPJ+uOJLWb8rjlAuLX7JPiRJtZKw/53Ol+WxdDJ1Eo=
github.com/alecthomas/go-check-sumtype v0.5.1-0.20260828200218-ae6904d28606/go.mod h1:ZH31IJyNpxpStxP2ayCcPQz+3T2G/OnxIQqe4LWvSDw=
github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs=
github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/alexkohler/nakedret/v2 v2.0.6 h1:ME3Qef1/KIKr3kWX3nti3hhgNxw6aqN5pZmQiFSsuzQ=
@@ -140,6 +142,8 @@ github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex
github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=
github.com/charmbracelet/ultraviolet v0.0.0-20260910203606-6c9e17dc7a16 h1:/hSCZO1puetbGd2JikHKN0nAcb2mFevzxJElFoduhaw=
github.com/charmbracelet/ultraviolet v0.0.0-20260910203606-6c9e17dc7a16/go.mod h1:JFvmQPZh3uAu7PpSZU91bcR0BrIy24HhW+0EsW1uGxE=
github.com/charmbracelet/ultraviolet v0.0.0-20260922123528-4e49372c11f9 h1:ZGHc3eaN2TIhkpwRibtr8N9jLQJOY1rh6GEByRoRmp4=
github.com/charmbracelet/ultraviolet v0.0.0-20260922123528-4e49372c11f9/go.mod h1:D9SVXVpAsD1pP6xR6BiERbe4+dUU6EW9US1TFrNutGA=
github.com/charmbracelet/x/ansi v0.11.8 h1:JMFwp0CgDC2+jcOB162HH5k7I3FVbgFSMMYg7dSPBQQ=
github.com/charmbracelet/x/ansi v0.11.8/go.mod h1:ZNN+3mXny/516oTQPLMPIBeSINvNJJQ8uQXDgbeJxY0=
github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
@@ -292,6 +296,8 @@ github.com/golangci/gofmt v0.0.0-20260820135601-e84e05053792 h1:WL8YKrt3UbOBqSRU
github.com/golangci/gofmt v0.0.0-20260820135601-e84e05053792/go.mod h1:te5hX0dW4C5r6YbXs+6ysNr8Q5UTmdIqGbb+mlFiYmA=
github.com/golangci/golangci-lint/v2 v2.13.2 h1:bCyq3E4vo9qwzifjpzJqYndEt7Ncva80qkk8G2B4TXU=
github.com/golangci/golangci-lint/v2 v2.13.2/go.mod h1:5xaMd1kAxV7GSBPEyngw8gnnjoRocqW9UVKdBR6627w=
github.com/golangci/golangci-lint/v2 v2.14.0 h1:ot8QffRa4LzAAEgtvNYrVs9esxQ7xoTwoAv6uhp4ngA=
github.com/golangci/golangci-lint/v2 v2.14.0/go.mod h1:amHimCzNDP1GfVno3hIiNssjvqXmuW+CPZAHOdR4cIc=
github.com/golangci/golines v0.15.0 h1:Qnph25g8Y1c5fdo1X7GaRDGgnMHgnxh4Gk4VfPTtRx0=
github.com/golangci/golines v0.15.0/go.mod h1:AZjXd23tbHMpowhtnGlj9KCNsysj72aeZVVHnVcZx10=
github.com/golangci/misspell v0.8.0 h1:qvxQhiE2/5z+BVRo1kwYA8yGz+lOlu5Jfvtx2b04Jbg=
@@ -416,6 +422,8 @@ github.com/mattn/go-runewidth v0.0.30 h1:+KUuiDA4fF0R1p5FeueHefjDm+GIM+kWfFnDjyb
github.com/mattn/go-runewidth v0.0.30/go.mod h1:3qAiGCV4Koz/yuveO58qUefmUTRm8r0IGEXZ9jeHp/8=
github.com/mgechev/revive v1.16.0 h1:9OZVO8xOPUwIYynplHXv7liMllPQ/BTNZLp07sM/Im0=
github.com/mgechev/revive v1.16.0/go.mod h1:nT854jMmvQsS/qqxEaMPM4pJSUpxhcPoHM788ZxDvdQ=
github.com/mgechev/revive v1.17.0 h1:hwTTVC3svSv9X2FA2hiYT17Uws+lfxDWhPxzBZZCREg=
github.com/mgechev/revive v1.17.0/go.mod h1:uiVNUF8ruVh4WHm/aPs7kKEeuo+m6oO3tuOfniyzP/g=
github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
@@ -569,6 +577,8 @@ github.com/tetratelabs/wazero v1.12.0 h1:DuWcpNu/FzgEXgGBDp8J1Spc+CWOvvtvVyjKlaZ
github.com/tetratelabs/wazero v1.12.0/go.mod h1:LvKtzl2RqO4gyF27BiXU+nKAjcV8f38U+kP/q2vgxh0=
github.com/timakin/bodyclose v0.0.0-20260723120731-857993a2939c h1:d/FUP8GD4QiY/rdFzZmBu3KqL9eBNVzMu7W/5pGkaMM=
github.com/timakin/bodyclose v0.0.0-20260723120731-857993a2939c/go.mod h1:sDHLK7rb/59v/ZxZ7KtymgcoxuUMxjXq8gtu9VMOK8M=
github.com/timakin/bodyclose v0.0.0-20260923065923-98ded9ab4ef2 h1:HECVafB2AKntWWRfsiQ4gdunLx5WOYQzFdCsxqOrzp8=
github.com/timakin/bodyclose v0.0.0-20260923065923-98ded9ab4ef2/go.mod h1:JLoPVtROtNgJ99yXTIxuGNttvmJa85tkJAO6lr05qBA=
github.com/timonwong/loggercheck v0.12.0 h1:7Cb9XGLJZSWYNjgL459DIhpSiEHupD+MLQ6vaN2Pl3g=
github.com/timonwong/loggercheck v0.12.0/go.mod h1:Wa6Y+SJHQR01mViZlLLY5GAZVOWOLNMbkXddMjFWQCs=
github.com/tomarrell/wrapcheck/v2 v2.12.0 h1:H/qQ1aNWz/eeIhxKAFvkfIA+N7YDvq6TWVFL27Of9is=
+2 -2
View File
@@ -251,8 +251,8 @@ func createConfig(configpath string, noInput bool) error {
fmt.Printf("Error: '%s' is not a valid path. Please enter a path to a file ending in .sqlite", config.Server.DatabaseV2.Path)
}
config.Frontend.Name = askQuestion(reader, "What should the application brand name be?", "FileBrowser Quantum")
config.Auth.AdminUsername = askQuestion(reader, "What should the default admin username be?", "admin")
config.Auth.AdminPassword = askQuestion(reader, "What should the default admin password be?", "admin")
config.Auth.Methods.PasswordAuth.AdminUsername = askQuestion(reader, "What should the default admin username be?", "admin")
config.Auth.Methods.PasswordAuth.AdminPassword = askQuestion(reader, "What should the default admin password be?", "admin")
modifyDefault := askYesNoQuestion(reader, "Should a new user be able to modify content by default?", "no")
config.Server.Sources[0].Config.DefaultPermissions = settings.NormalizeSourceFilePermissions(users.SourceFilePermissions{
+12
View File
@@ -0,0 +1,12 @@
package cmd
import (
"os"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/testutil"
)
func TestMain(m *testing.M) {
os.Exit(testutil.Main(m))
}
+31 -2
View File
@@ -296,8 +296,9 @@ func migrateUsers(oldDB *storm.DB, sqlStore *sqldb.SQLStore) error {
users.MigrateToSourcePermissions(user)
}
normalizeUserTokensBeforeSQLite(user)
promoteLegacyApiKeysBeforeSQLite(user)
updateTokens(user)
user.Version = users.ProfileStorageVersion
user.Version = users.NewestUserVersion
if newScopesCount > oldScopesCount {
promoted++
logger.Infof(" user %q: legacy database had %d scopes, SQLite now has %d",
@@ -347,6 +348,34 @@ func normalizeUserTokensBeforeSQLite(user *users.User) {
user.Tokens = normalized
}
// promoteLegacyApiKeysBeforeSQLite merges Bolt apiKeys entries into Tokens so
// they survive the SQLite migration and the hashed-tokens backfill (strict
// auth ignores BelongsTo and requires a hash row per raw JWT).
func promoteLegacyApiKeysBeforeSQLite(user *users.User) {
if len(user.ApiKeys) == 0 {
return
}
if user.Tokens == nil {
user.Tokens = make(map[string]users.AuthToken)
}
for name, token := range user.ApiKeys {
if token.Name == "" {
token.Name = name
}
if token.Token == "" {
token.Token = token.Key
}
if token.Token == "" {
continue
}
if _, exists := user.Tokens[token.Name]; exists {
continue
}
token.Permissions = users.SanitizeTokenPermissions(token.Permissions)
users.StoreToken(user.Tokens, token)
}
}
// migrateShares migrates all shares from BoltDB to SQLite.
//
// Legacy Bolt "Link" / "Share" JSON (flat CommonShare + Link fields):
@@ -476,7 +505,7 @@ func migrateAccessRules(oldDB *storm.DB, sqlStore *sqldb.SQLStore) error {
logger.Warningf(" skipping hashed token: invalid user id 0")
continue
}
err := sqlStore.SaveHashedToken(tokenHash, uint64(userID), false)
err := sqlStore.SaveHashedToken(tokenHash, uint64(userID), false, 0)
if err != nil {
return fmt.Errorf("failed to save hashed token: %w", err)
}
+86
View File
@@ -2,6 +2,7 @@ package cmd
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
@@ -72,3 +73,88 @@ func TestMigrateAdminTokensRoundTripSQLite(t *testing.T) {
os.Stdout.Write(out)
os.Stdout.Write([]byte("\n"))
}
func TestPromoteLegacyApiKeysBeforeSQLite(t *testing.T) {
user := &users.User{
FrontendUser: users.FrontendUser{Username: "legacy-tokens"},
Tokens: map[string]users.AuthToken{},
}
user.ApiKeys = map[string]users.AuthToken{
"legacy-sync": {Key: "raw-jwt-1"},
"empty": {},
}
user.Tokens["obsidian"] = users.AuthToken{Name: "obsidian", Token: "raw-jwt-existing"}
promoteLegacyApiKeysBeforeSQLite(user)
// Existing Tokens entries win; empty raw tokens are skipped.
if got := user.Tokens["obsidian"].Token; got != "raw-jwt-existing" {
t.Fatalf("existing token must win, got %q", got)
}
if got := user.Tokens["legacy-sync"].Token; got != "raw-jwt-1" {
t.Fatalf("promoted token must carry raw JWT, got %q", got)
}
if _, ok := user.Tokens["raw-jwt-1"]; !ok {
t.Fatal("expected raw-JWT alias key after promotion")
}
if _, ok := user.Tokens["empty"]; ok {
t.Fatal("token without raw material must be skipped")
}
}
func TestUpdateTokenHashBackfillSuccessStampsVersion(t *testing.T) {
user := &users.User{FrontendUser: users.FrontendUser{Username: "no-tokens"}}
user.Version = users.ProfileStorageVersion
changed, failed := updateTokenHashBackfill(user)
if failed {
t.Fatal("tokenless user must not fail backfill")
}
if !changed {
t.Fatal("expected version bump to count as changed")
}
if user.Version != users.TokenHashBackfillVersion {
t.Fatalf("version = %d, want %d", user.Version, users.TokenHashBackfillVersion)
}
}
func TestUpdateTokenHashBackfillFailureSkipsBump(t *testing.T) {
// Force token hash registration to fail; the version must stay below
// newest so the backfill retries on next startup.
orig := addApiToken
addApiToken = func(string, uint64) error {
return errors.New("simulated token registration failure")
}
t.Cleanup(func() { addApiToken = orig })
user := &users.User{FrontendUser: users.FrontendUser{Username: "backfill-retry"}}
user.Version = users.ProfileStorageVersion
user.ApiKeys = map[string]users.AuthToken{
"legacy": {Key: "raw-jwt-retry"},
}
_, failed := updateTokenHashBackfill(user)
if !failed {
t.Fatal("expected failure when token registration fails")
}
if user.Version != users.ProfileStorageVersion {
t.Fatalf("failed backfill must not bump version, got %d", user.Version)
}
if bumpToNewestVersion(user, failed) {
t.Fatal("catch-all must not bump past a failed migration")
}
}
func TestBumpToNewestVersion(t *testing.T) {
user := &users.User{FrontendUser: users.FrontendUser{Username: "stale"}}
user.Version = users.ProfileStorageVersion
if !bumpToNewestVersion(user, false) {
t.Fatal("expected bump for stale version without failure")
}
if user.Version != users.NewestUserVersion {
t.Fatalf("version = %d, want %d", user.Version, users.NewestUserVersion)
}
if bumpToNewestVersion(user, false) {
t.Fatal("must not bump when already newest")
}
}
+2 -2
View File
@@ -200,9 +200,9 @@ func rootCMD(ctx context.Context, serverConfig *settings.Server, a *app.App, shu
if err != nil {
logger.Fatalf("Failed to create sub filesystem: %v", err)
}
fileutils.InitAssetFS(subAssets, true)
fileutils.InitAssetFS(subAssets, true, false)
} else {
fileutils.InitAssetFS(nil, false)
fileutils.InitAssetFS(nil, false, settings.Env.IsDevMode)
}
// Start preview service
+58 -13
View File
@@ -16,6 +16,10 @@ import (
var createBackup = false
// addApiToken registers a raw API token's hash with access storage. It is a
// variable so tests can inject a failing registration deterministically.
var addApiToken = state.AddApiToken
func validateUserInfo(newDB bool) {
// update source info for users if names/sources/paths might have changed
usersList, err := state.GetAllUsers()
@@ -55,20 +59,22 @@ func validateUserInfo(newDB bool) {
if normalizeApiTokenPermissions(user) {
changedFields = append(changedFields, "tokens")
}
tokenBackfillChanged, tokenBackfillFailed := updateTokenHashBackfill(user)
if tokenBackfillChanged && !tokenBackfillFailed {
changedFields = append(changedFields, "tokens", "version")
} else if tokenBackfillChanged {
changedFields = append(changedFields, "tokens")
}
if state.ApplyEnforcedSyncToUser(user) {
changedFields = append(changedFields, settings.UserJSONFieldsForEnforcedSync()...)
}
if state.ApplyEnforcedSourcePermissionsSyncToUser(user) {
changedFields = append(changedFields, "backendScopes", "backendSourcePermissions")
}
if user.Version < users.ProfileStorageVersion {
user.Version = users.ProfileStorageVersion
if bumpToNewestVersion(user, tokenBackfillFailed) {
changedFields = append(changedFields, "version")
}
adminUser := settings.Config.Auth.AdminUsername
if adminUser == "" {
adminUser = "admin"
}
adminUser := settings.PasswordAdminUsername()
if user.Username == adminUser && user.Permissions.Admin {
adminPerms := settings.AdminPerms()
if user.Permissions.Share != adminPerms.Share || user.Permissions.Api != adminPerms.Api {
@@ -78,10 +84,10 @@ func validateUserInfo(newDB bool) {
changedFields = append(changedFields, "permissions")
}
}
if user.Username == adminUser && settings.Config.Auth.AdminPassword != "" && user.LoginMethod == users.LoginMethodPassword {
if user.Username == adminUser && settings.ConfigTriggersAdminPasswordReset() && user.LoginMethod == users.LoginMethodPassword {
logger.Info("Resetting admin user to default username and password.")
user.Permissions = settings.AdminPerms()
user.Password = settings.Config.Auth.AdminPassword
user.Password = settings.PasswordAdminPassword()
user.LoginMethod = users.LoginMethodPassword
changedFields = append(changedFields, "permissions", "password", "loginMethod")
changePass = true
@@ -129,7 +135,7 @@ func dedupeFields(fields []string) []string {
}
func updateUserScopes(user *users.User) bool {
newScopes := settings.MergeDefaultEnabledBackendScopes(user.BackendScopes)
newScopes := settings.MergeDefaultEnabledBackendScopes(user.BackendScopes, user.DeclinedDefaultSources)
changed := !reflect.DeepEqual(user.BackendScopes, newScopes)
user.BackendScopes = newScopes
return changed
@@ -162,7 +168,7 @@ func updateShowFirstLogin(user *users.User) bool {
// func to convert legacy user with perm key to permissions
func updatePermissions(user *users.User) bool {
if user.Version >= 1 {
if user.Version >= users.PermMigrationVersion {
return false
}
updateUser := true
@@ -208,7 +214,7 @@ func updatePermissions(user *users.User) bool {
user.Permissions.Delete = true
updateUser = true
}
user.Version = 2
user.Version = users.PermMigrationVersion
if updateUser {
createBackup = true
}
@@ -248,7 +254,7 @@ func updateSidebarLinks(user *users.User, scopesChanged bool) bool {
}
func updateTokens(user *users.User) bool {
if user.Version >= 2 {
if user.Version >= users.TokenMigrationVersion {
return false
}
if user.ApiKeys != nil {
@@ -259,7 +265,7 @@ func updateTokens(user *users.User) bool {
users.StoreToken(user.Tokens, token)
}
}
user.Version = 2
user.Version = users.TokenMigrationVersion
return true
}
@@ -281,3 +287,42 @@ func normalizeApiTokenPermissions(user *users.User) bool {
}
return changed
}
// updateTokenHashBackfill promotes legacy ApiKeys into Tokens and registers a
// hashed_tokens row for every stored raw JWT so strict server-side auth (which
// ignores BelongsTo) accepts pre-2.0.8 tokens. Runs once per user via
// TokenHashBackfillVersion. It reports (changed, failed): when hash
// registration fails the version is left alone so callers skip the newest
// bump and the backfill retries on next startup.
func updateTokenHashBackfill(user *users.User) (bool, bool) {
if user == nil || user.Version >= users.TokenHashBackfillVersion {
return false, false
}
before := len(user.Tokens)
promoteLegacyApiKeysBeforeSQLite(user)
changed := len(user.Tokens) != before
hashesRegistered := true
for _, raw := range state.CollectStoredRawTokens(user) {
if err := addApiToken(raw, user.ID); err != nil {
logger.Errorf("could not register token hash for user %s: %v", user.Username, err)
hashesRegistered = false
}
}
if hashesRegistered {
user.Version = users.TokenHashBackfillVersion
createBackup = true
return true, false
}
return changed, true
}
// bumpToNewestVersion stamps the user at NewestUserVersion. It is skipped when
// a migration failed so the failed migration retries on next startup instead
// of being buried by the catch-all bump.
func bumpToNewestVersion(user *users.User, migrationFailed bool) bool {
if migrationFailed || user == nil || user.Version >= users.NewestUserVersion {
return false
}
user.Version = users.NewestUserVersion
return true
}
+1
View File
@@ -53,6 +53,7 @@ userDefaults:
lockPassword: false
disableSettings: false
disableUpdateNotifications: false
showAdvancedProfile: false
permissions:
api: false
admin: false
+14 -15
View File
@@ -9,17 +9,17 @@ require (
github.com/coreos/go-systemd/v22 v22.7.0
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8
github.com/dsoprea/go-exif/v3 v3.0.1
github.com/evanoberholster/imagemeta v1.0.0
github.com/evanoberholster/imagemeta v1.1.0
github.com/gen2brain/go-fitz v1.28.2
github.com/go-ldap/ldap/v3 v3.4.14
github.com/go-playground/validator/v10 v10.30.4
github.com/go-webauthn/webauthn v0.18.1
github.com/go-playground/validator/v10 v10.30.5
github.com/go-webauthn/webauthn v0.18.2
github.com/goccy/go-yaml v1.19.2
github.com/golang-jwt/jwt/v4 v4.5.2
github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0
github.com/gtsteffaniak/go-cache v1.1.0
github.com/gtsteffaniak/go-ffmpeg v1.0.0
github.com/gtsteffaniak/go-ffmpeg v1.2.0
github.com/gtsteffaniak/go-logger v1.1.0
github.com/kovidgoyal/imaging v1.8.23
github.com/mattn/go-sqlite3 v1.14.52
@@ -38,7 +38,7 @@ require (
golang.org/x/time v0.16.0
golang.org/x/tools v0.50.0
gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.59.0
modernc.org/sqlite v1.60.1
)
require (
@@ -48,14 +48,14 @@ require (
github.com/dsoprea/go-logging v0.0.0-20200710184922-b02d349568dd // indirect
github.com/dsoprea/go-utility/v2 v2.0.0-20221003172846-a3e1774ef349 // indirect
github.com/dustin/go-humanize v1.1.0 // indirect
github.com/ebitengine/purego v0.11.0 // indirect
github.com/ebitengine/purego v0.11.1 // indirect
github.com/fxamacker/cbor/v2 v2.9.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.15 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.8 // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-jose/go-jose/v4 v4.1.5 // indirect
github.com/go-openapi/jsonpointer v1.0.1 // indirect
github.com/go-openapi/jsonreference v1.0.2 // indirect
github.com/go-openapi/jsonpointer v1.0.2 // indirect
github.com/go-openapi/jsonreference v1.0.3 // indirect
github.com/go-openapi/spec v1.0.1 // indirect
github.com/go-openapi/swag/conv v0.29.2 // indirect
github.com/go-openapi/swag/jsonutils v0.29.2 // indirect
@@ -64,12 +64,12 @@ require (
github.com/go-openapi/swag/stringutils v0.29.2 // indirect
github.com/go-openapi/swag/typeutils v0.29.2 // indirect
github.com/go-openapi/swag/yamlutils v0.29.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/locales v0.14.2 // indirect
github.com/go-playground/universal-translator v0.18.2 // indirect
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
github.com/go-webauthn/x v0.3.1 // indirect
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
github.com/golang/geo v0.0.0-20260818125358-b200a1149890 // indirect
github.com/golang/geo v0.0.0-20260928092222-7d12f68cfadb // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-tpm v0.9.8 // indirect
@@ -78,14 +78,14 @@ require (
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.5.0 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/ncruces/go-strftime v1.1.0 // indirect
github.com/philhofer/fwd v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rogpeppe/go-internal v1.16.0 // indirect
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd // indirect
github.com/swaggo/files v1.0.1 // indirect
github.com/tinylib/msgp v1.6.4 // indirect
github.com/tinylib/msgp v1.6.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.etcd.io/bbolt v1.5.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
@@ -95,7 +95,7 @@ require (
google.golang.org/protobuf v1.36.12 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
modernc.org/libc v1.76.0 // indirect
modernc.org/libc v1.77.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.12.1 // indirect
)
@@ -103,5 +103,4 @@ require (
exclude (
github.com/alecthomas/go-check-sumtype v0.5.0
github.com/gtsteffaniak/go-ffmpeg v1.1.0
github.com/gtsteffaniak/go-ffmpeg v1.2.0
)
+32 -32
View File
@@ -47,10 +47,10 @@ github.com/dsoprea/go-utility/v2 v2.0.0-20221003172846-a3e1774ef349 h1:DilThiXje
github.com/dsoprea/go-utility/v2 v2.0.0-20221003172846-a3e1774ef349/go.mod h1:4GC5sXji84i/p+irqghpPFZBF8tRN/Q7+700G0/DLe8=
github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg=
github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI=
github.com/ebitengine/purego v0.11.0 h1:jhp/D+Nyv7UUW8HAcmcjt2N2rYrYi9m3SL21k0Ua/NI=
github.com/ebitengine/purego v0.11.0/go.mod h1:DCHPP08djqhNSoTfImcnHYQRZmd0qhakvrozqaEYhGQ=
github.com/evanoberholster/imagemeta v1.0.0 h1:FlSihlsjkWIoS83wmgPOP3CbAxlRpxM8jQGZgLu+MWQ=
github.com/evanoberholster/imagemeta v1.0.0/go.mod h1:2ITZJjD1ygzWIhepvn8coRvpaFcnDNk3E6hx6v7z8DU=
github.com/ebitengine/purego v0.11.1 h1:2zpWRSQNVKN4eKsKO9eM1ILDgWfYMY9GwqRmK6XeQ/0=
github.com/ebitengine/purego v0.11.1/go.mod h1:DCHPP08djqhNSoTfImcnHYQRZmd0qhakvrozqaEYhGQ=
github.com/evanoberholster/imagemeta v1.1.0 h1:0iBtgVxLORZTZAlKxRcNodBmjMfHCSFwUh8Asmi83oI=
github.com/evanoberholster/imagemeta v1.1.0/go.mod h1:nmf6le+qSyoRAXgp2psPbtfkxcEgf/AZGgai9pnN8d0=
github.com/fxamacker/cbor/v2 v2.9.4 h1:xwjVlxEMR3S605oUlgBjKLTTeGFciYPGYCtF/35LKGo=
github.com/fxamacker/cbor/v2 v2.9.4/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
github.com/gabriel-vasile/mimetype v1.4.15 h1:05iP/CYtZ/w455R/KZM6rZ5ieAdh99UPtd+d3YzLmaI=
@@ -69,10 +69,10 @@ github.com/go-jose/go-jose/v4 v4.1.5 h1:RjgjO2LOtWOJKUC5wpwY9LR3B3vwVAz6JS2YHfYU
github.com/go-jose/go-jose/v4 v4.1.5/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
github.com/go-ldap/ldap/v3 v3.4.14 h1:D6PYdEgsaVzsXyr6w/yDC06Ria4uUhWm+Rb+er8lfAs=
github.com/go-ldap/ldap/v3 v3.4.14/go.mod h1:S4eJUMUNjDkE0ZJtIZdybwyb03sGGLW6gxXT1Hs8VKA=
github.com/go-openapi/jsonpointer v1.0.1 h1:2KxywRmNwJkT/FMBa3iRNHEaAxSJvjqoufQZy3au1Mg=
github.com/go-openapi/jsonpointer v1.0.1/go.mod h1:wI7ZYsFmbIi9nBXOZqgDaS/bqOchRGZjqxFli7FBYxY=
github.com/go-openapi/jsonreference v1.0.2 h1:oS4et8FOf3p3UQxEo4Xt0esijmBUM+F259Xl72OSZsc=
github.com/go-openapi/jsonreference v1.0.2/go.mod h1:TbUNSOo+fcorZjFaNoiSDSoaNnnZtqJtLGR1PuvE/Cs=
github.com/go-openapi/jsonpointer v1.0.2 h1:NbW7xT2b+l7x+JefiPGDsZWsrLk1ogrz4AHK1TTzMt8=
github.com/go-openapi/jsonpointer v1.0.2/go.mod h1:N0rI4MbNKD7xAFTiHtB+5Nhrg2j/wwYg7m7Fmb4/jk8=
github.com/go-openapi/jsonreference v1.0.3 h1:5rDbq+JsjCv6/nUDDJt83nLZoADI4w03uvHXer+4aYQ=
github.com/go-openapi/jsonreference v1.0.3/go.mod h1:OJSlMXHysgH272D9z2tiy2MGe5A9vQLHaCPb6KbYdE4=
github.com/go-openapi/spec v1.0.1 h1:lj2vdGpNDcVgwRc6qXdw6qt/KQpCtSa9tnUH6vpDPDk=
github.com/go-openapi/spec v1.0.1/go.mod h1:M//GWQGtDUAjnP37gE6fInLgaczB+FatoipV3H1fYw8=
github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM=
@@ -94,20 +94,20 @@ github.com/go-openapi/swag/yamlutils v0.29.2 h1:IFKFFeDnuIwzfsuWRQU+rI8iL3g4XyAY
github.com/go-openapi/swag/yamlutils v0.29.2/go.mod h1:7MGqtcrK73sxQ4ceiyIf8qiXS/s09JLMdR5esK5euYQ=
github.com/go-openapi/testify/enable/yaml/v2 v2.7.0 h1:wPW6YRgx3+SID1yUy/Xwa17L8kFEaEKod2VRbJDZNUs=
github.com/go-openapi/testify/enable/yaml/v2 v2.7.0/go.mod h1:mI1M88etYbc3PhgHsWQK2kwvNwW5aGFqMPbmib+SGIs=
github.com/go-openapi/testify/v2 v2.7.0 h1:bycOreEj6wfBvijg3YFogZ/sFjTCDmQnwSodSzHa3X8=
github.com/go-openapi/testify/v2 v2.7.0/go.mod h1:SgsVHtfooshd0tublTtJ50FPKhujf47YRqauXXOUxfw=
github.com/go-openapi/testify/v2 v2.8.0 h1:19QDx5b57p8KjO2E4sgs/woL4Akp+FGLRlVm2xn0Rho=
github.com/go-openapi/testify/v2 v2.8.0/go.mod h1:4gUN8jC+RqOE2qjW/vDmfyTvl5BCht4qL7oca0odrak=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.30.4 h1:9Rcod2ZPO6mOEG6b4GqyoHE/H6//Ze0RuhOo1hT1x0w=
github.com/go-playground/validator/v10 v10.30.4/go.mod h1:numpT+RPLE91R9oYWMY/R9zRgJBewr3IXHko4OISPpk=
github.com/go-playground/locales v0.14.2 h1:d8UmcrM6Nip0hfGZKLGpAvZH37XB4TS0xzK9B56YNCY=
github.com/go-playground/locales v0.14.2/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.2 h1:LCsMLC9RzmbUMNUPVYD15dmcjwYAJhmX8mPZRW4rAVU=
github.com/go-playground/universal-translator v0.18.2/go.mod h1:67VZIMp5lQpDWlnStOct22q1bkdJGJqHghbOtmkawxk=
github.com/go-playground/validator/v10 v10.30.5 h1:YyCXvVShZbs2Sm3Mb53eNOlhRXctSOzW5QJAouCTZL4=
github.com/go-playground/validator/v10 v10.30.5/go.mod h1:wEqiaov48pXX1kjhc3Da8y0M0Dtg/BK7gurFBLgwFrQ=
github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro=
github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/go-webauthn/webauthn v0.18.1 h1:KaQw6M+ODLvxHwddyeo6zFhhmicfLup/BClhigB6A+0=
github.com/go-webauthn/webauthn v0.18.1/go.mod h1:s4rZTQnKHWxIh6G3yEGqlxvtiLceA1jigll8FpqSgQ8=
github.com/go-webauthn/webauthn v0.18.2 h1:0BeftmEHU7i3Dv0VFwBtidy/ba37Vcdjvqst9EYu8Sk=
github.com/go-webauthn/webauthn v0.18.2/go.mod h1:hEXaOuLxvZ3zG9miZe3ehlyeVso9AtklXG+kTn36k+A=
github.com/go-webauthn/x v0.3.1 h1:1ff37z3XfmTTomkhlURgGizLIDyOvPgTt2t9nlzKLRo=
github.com/go-webauthn/x v0.3.1/go.mod h1:ZInxAynYXfBPvvm5gzKZ7geBlL23K71xASMgohHl/Rg=
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
@@ -119,8 +119,8 @@ github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArs
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
github.com/golang/geo v0.0.0-20200319012246-673a6f80352d/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
github.com/golang/geo v0.0.0-20260818125358-b200a1149890 h1:m+G0ip1+N4CF0ex34SeojAon6htIIBwvzsyXNx1fGWg=
github.com/golang/geo v0.0.0-20260818125358-b200a1149890/go.mod h1:Mymr9kRGDc64JPr03TSZmuIBODZ3KyswLzm1xL0HFA8=
github.com/golang/geo v0.0.0-20260928092222-7d12f68cfadb h1:z//sB+BhCYNvUrk19E8aWqNx+BBom8p/uD/nh95N/5U=
github.com/golang/geo v0.0.0-20260928092222-7d12f68cfadb/go.mod h1:Mymr9kRGDc64JPr03TSZmuIBODZ3KyswLzm1xL0HFA8=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
@@ -143,8 +143,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gtsteffaniak/go-cache v1.1.0 h1:ni/2cJpCxpGmaMZd92iTDIGOy+yUcMcxQuW7bSKhkCE=
github.com/gtsteffaniak/go-cache v1.1.0/go.mod h1:rHUZvf4huhMX/bnNK4/oQ3sNAE2fYC2jFpAWZiBPWac=
github.com/gtsteffaniak/go-ffmpeg v1.0.0 h1:dUWvlZmWnkiujDtIMLGwrpfSAOnM+IOtk93HXF6HR0U=
github.com/gtsteffaniak/go-ffmpeg v1.0.0/go.mod h1:FefhoD+KZzHWqBaUpntcKp6lXtUzdFW00F74axnENUQ=
github.com/gtsteffaniak/go-ffmpeg v1.2.0 h1:WSFfRL5xVUpEXBNI4D3f2Uz8yxbc3OEqTtjp4Z9yPUo=
github.com/gtsteffaniak/go-ffmpeg v1.2.0/go.mod h1:FefhoD+KZzHWqBaUpntcKp6lXtUzdFW00F74axnENUQ=
github.com/gtsteffaniak/go-logger v1.1.0 h1:baFds+iibuLLNpNLwygz32bDqdsunI6dGWcdxwMIVKI=
github.com/gtsteffaniak/go-logger v1.1.0/go.mod h1:rapapTju4D7bwt6h4JOdZNUXDjnp0WkTBd6TN5yWglI=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
@@ -187,8 +187,8 @@ github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsRe
github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A=
github.com/mattn/go-sqlite3 v1.14.52 h1:wVbm2Qnf4OXkqhBTSPuCRZDRnxfbVrrmiCEroVdog8U=
github.com/mattn/go-sqlite3 v1.14.52/go.mod h1:6JTjA44L93a0QCyJef5YvlPoKXntQPjzWv5gtm9sB6w=
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk=
github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g=
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
@@ -217,8 +217,8 @@ github.com/swaggo/http-swagger v1.3.4 h1:q7t/XLx0n15H1Q9/tk3Y9L4n210XzJF5WtnDX64
github.com/swaggo/http-swagger v1.3.4/go.mod h1:9dAh0unqMBAlbp1uE2Uc2mQTxNMU/ha4UbucIg1MFkQ=
github.com/swaggo/swag v1.16.6 h1:qBNcx53ZaX+M5dxVyTrgQ0PJ/ACK+NzhwcbieTt+9yI=
github.com/swaggo/swag v1.16.6/go.mod h1:ngP2etMK5a0P3QBizic5MEwpRmluJZPHjXcMoj4Xesg=
github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
github.com/tinylib/msgp v1.6.5 h1:iAH6XTP7BpzErjBZlujJQXxA+GmRi2YWs6M4KdLtNbE=
github.com/tinylib/msgp v1.6.5/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
@@ -313,8 +313,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/cc/v4 v4.29.7 h1:q+NXGJ0bK3b4TXFYQQVr9pYETGnmwFWkrUzJnMya/Tg=
modernc.org/cc/v4 v4.29.7/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=
modernc.org/ccgo/v4 v4.35.2 h1:JPAIttQRHdY7aRdr04+iTW7Sx+6OSZcmKJ0OZl/tNaA=
modernc.org/ccgo/v4 v4.35.2/go.mod h1:9sddcpn4NuDAFGtBPa2Dk3NHfnQfcoKveCC5crwWp8I=
modernc.org/ccgo/v4 v4.36.1 h1:ZNIUZAryN0UgnJwtyxrdEzcFc3yD4Cu4AzjfPXsLsIE=
modernc.org/ccgo/v4 v4.36.1/go.mod h1:rrtGc2QkS239nYb/mQNuBMyjq3/y3ZXWbBjPoV3wqzA=
modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM=
modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU=
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
@@ -323,8 +323,8 @@ modernc.org/gc/v3 v3.1.5 h1:21ldfPfRYE31Tb7B3mwAK8gy1AxP4+dKjrOQPfqakoc=
modernc.org/gc/v3 v3.1.5/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
modernc.org/libc v1.76.0 h1:eaJHMv2zn5oXT6IPXPwxAMVpzmQzSDsCdKcNl1ZpaRg=
modernc.org/libc v1.76.0/go.mod h1:2h0dedmVSE8qH2DrxzYDXbQaxLMl0XNg8Z7/HJRdk2M=
modernc.org/libc v1.77.1 h1:Ct8j47QtiZ1Enj2DtFXQtUqrPCAjdCmPjtCuvrYQ0Hs=
modernc.org/libc v1.77.1/go.mod h1:87/pZ4L6nD1zqW4nItuS12YO7hN1igAah34xjnQo/W0=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.12.1 h1:nFMiWrpStgZczNl6XI9GnIk/rWhYIyHGUaR04pGbp9g=
@@ -333,8 +333,8 @@ modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg=
modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
modernc.org/sqlite v1.59.0 h1:X1es1GpqBlS/5T+vbM4HLUdaa8OtQx468DF2vrx+38A=
modernc.org/sqlite v1.59.0/go.mod h1:+paeT2A3iPRHkQDwG7oA6Tk0zQd5woMEI8q7orfry8k=
modernc.org/sqlite v1.60.1 h1:/blz53O951KWFOso4QQvEs/Fq6cDBKLtMVrYNSeJVKw=
modernc.org/sqlite v1.60.1/go.mod h1:1dIoEagfDE72QytD5scH1lxARtaUgKgHC/NuApA27r0=
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
+1
View File
@@ -0,0 +1 @@
../../frontend/dist
+74
View File
@@ -7,6 +7,7 @@ import (
"time"
activitydb "github.com/gtsteffaniak/filebrowser/backend/internal/database/activity"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/quota"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
)
@@ -287,6 +288,79 @@ func RecordAccessDelete(r *http.Request, actor *Actor, source, path string, chan
})
}
func RecordQuotaCreate(r *http.Request, actor *Actor, source, path string, changes []activitydb.FieldChange) {
RecordQuotaMutation(r, actor, activitydb.EventQuotaCreate, source, path, changes)
}
func RecordQuotaUpdate(r *http.Request, actor *Actor, source, path string, changes []activitydb.FieldChange) {
RecordQuotaMutation(r, actor, activitydb.EventQuotaUpdate, source, path, changes)
}
func RecordQuotaDelete(r *http.Request, actor *Actor, source, path string, changes []activitydb.FieldChange) {
RecordQuotaMutation(r, actor, activitydb.EventQuotaDelete, source, path, changes)
}
func RecordQuotaMutation(r *http.Request, actor *Actor, eventType activitydb.EventType, source, path string, changes []activitydb.FieldChange) {
RecordUser(r, actor, activitydb.Entry{
EventType: eventType,
Source: source,
Path: path,
Details: activitydb.Details{
Source: source,
Path: path,
Changes: changes,
},
})
}
func quotaFolderChanges(q quota.FolderQuota) []activitydb.FieldChange {
changes := []activitydb.FieldChange{
{Field: "limitBytes", To: strconv.FormatInt(q.LimitBytes, 10)},
{Field: "meter", To: q.Meter},
}
if q.UserID > 0 {
changes = append(changes, activitydb.FieldChange{
Field: "userId",
To: strconv.FormatUint(q.UserID, 10),
})
}
return changes
}
func QuotaFolderCreateChanges(q quota.FolderQuota) []activitydb.FieldChange {
return quotaFolderChanges(q)
}
func QuotaFolderDeleteChanges(q quota.FolderQuota) []activitydb.FieldChange {
return quotaFolderChanges(q)
}
func QuotaFolderUpdateChanges(before, after quota.FolderQuota) []activitydb.FieldChange {
var changes []activitydb.FieldChange
if before.LimitBytes != after.LimitBytes {
changes = append(changes, activitydb.FieldChange{
Field: "limitBytes",
From: strconv.FormatInt(before.LimitBytes, 10),
To: strconv.FormatInt(after.LimitBytes, 10),
})
}
if before.Meter != after.Meter {
changes = append(changes, activitydb.FieldChange{
Field: "meter",
From: before.Meter,
To: after.Meter,
})
}
if before.UserID != after.UserID {
changes = append(changes, activitydb.FieldChange{
Field: "userId",
From: strconv.FormatUint(before.UserID, 10),
To: strconv.FormatUint(after.UserID, 10),
})
}
return changes
}
func AccessRuleCreateChanges(allow bool, ruleCategory, value string) []activitydb.FieldChange {
ruleType := "deny"
if allow {
+87 -96
View File
@@ -1,13 +1,11 @@
package files
import (
"bufio"
"context"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
@@ -25,8 +23,6 @@ import (
"github.com/gtsteffaniak/go-logger/logger"
)
var reDuration = regexp.MustCompile(`^\[(?:(\d{1,2}):)?(\d{1,2}):(\d{1,2})\.(\d+)\](.*)`)
// CheckPermissionsFunc allows tests to override CheckPermissions behavior
var CheckPermissionsFunc = func(opts utils.FileOptions, user *users.User) (string, string, error) {
return checkPermissionsImpl(opts, user, svc())
@@ -50,7 +46,7 @@ func addMetadataToChildren(response *iteminfo.ExtendedFileInfo, opts utils.FileO
wg.Go(func() {
fullPath := response.RealPath + "/" + fileItem.Name
if isAudio {
if err := extractAudioMetadata(context.Background(), fileItem, fullPath, opts.AlbumArt, opts.Metadata, false, sharedFFmpegService); err != nil {
if err := extractAudioMetadata(context.Background(), fileItem, fullPath, opts.AlbumArt, opts.Metadata, sharedFFmpegService); err != nil {
logger.Debugf("failed to extract metadata for file: %s, error: %v", fileItem.Name, err)
}
} else {
@@ -393,7 +389,7 @@ func processContent(info *iteminfo.ExtendedFileInfo, idx *indexing.Index, opts u
extItem := &iteminfo.ExtendedItemInfo{
ItemInfo: info.ItemInfo,
}
err := extractAudioMetadata(context.Background(), extItem, info.RealPath, opts.AlbumArt || opts.Content, opts.Metadata || opts.Content, false, nil)
err := extractAudioMetadata(context.Background(), extItem, info.RealPath, opts.AlbumArt || opts.Content, opts.Metadata || opts.Content, nil)
if err != nil {
logger.Debugf("failed to extract audio metadata for file: "+info.RealPath, info.Name, err)
} else {
@@ -431,57 +427,10 @@ func generateOfficeId(realPath string) string {
return key
}
// Parses raw LRC text into a slice of Lyric.
// Lines with [hh:mm:ss.xx] (timestamps) will be parsed to ms for synced lyrics
// The ones without timestamp are kept as unsynced (timestamp 0).
func parseLRC(raw string) []iteminfo.Lyric {
var lines []iteminfo.Lyric
scanner := bufio.NewScanner(strings.NewReader(raw))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
matches := reDuration.FindStringSubmatch(line)
if len(matches) == 6 {
// Hours default to 0 (if not present in the timestamps)
h := 0
if matches[1] != "" {
h, _ = strconv.Atoi(matches[1])
}
min, _ := strconv.Atoi(matches[2])
sec, _ := strconv.Atoi(matches[3])
msStr := matches[4]
// Normalise to 3 digits, some songs have more than 3 digits in the milliseconds
switch {
case len(msStr) < 3:
msStr += strings.Repeat("0", 3-len(msStr))
case len(msStr) > 3:
msStr = msStr[:3]
}
ms, _ := strconv.Atoi(msStr)
timestamp := int64((h*3600+min*60+sec)*1000 + ms)
text := strings.TrimSpace(matches[5])
lines = append(lines, iteminfo.Lyric{
Text: text,
Timestamp: timestamp,
})
} else {
// Line without a valid timestamp – treat as unsynced (timestamp 0)
lines = append(lines, iteminfo.Lyric{
Text: line,
Timestamp: 0,
})
}
}
return lines
}
// extractAudioMetadata extracts metadata from an audio file using dhowden/tag
// and optionally extracts duration using the ffmpeg service with concurrency control
// If ffmpegService is nil, a new service will be created (for backward compatibility)
func extractAudioMetadata(ctx context.Context, item *iteminfo.ExtendedItemInfo, realPath string, getArt bool, getDuration bool, getLyrics bool, ffmpegService *ffmpeg.FFmpegService) error {
func extractAudioMetadata(ctx context.Context, item *iteminfo.ExtendedItemInfo, realPath string, getArt bool, getDuration bool, ffmpegService *ffmpeg.FFmpegService) error {
file, err := os.Open(realPath)
if err != nil {
return err
@@ -518,28 +467,7 @@ func extractAudioMetadata(ctx context.Context, item *iteminfo.ExtendedItemInfo,
item.Metadata.Track = track
// Check if lyrics are available without extract them
if rawLyrics := m.Lyrics(); rawLyrics != "" {
item.Metadata.HasLyrics = true
} else {
// Check for sidecar .lrc file
dir := filepath.Dir(realPath)
base := filepath.Base(realPath)
ext := filepath.Ext(base)
nameWithoutExt := strings.TrimSuffix(base, ext)
lrcPath := filepath.Join(dir, nameWithoutExt+".lrc")
if _, err := os.Stat(lrcPath); err == nil {
item.Metadata.HasLyrics = true
}
}
if getLyrics {
lyrics, err := ExtractLyrics(realPath)
if err != nil {
logger.Debugf("failed to extract lyrics for %s: %v", realPath, err)
} else if len(lyrics) > 0 {
item.Metadata.Lyrics = lyrics
}
}
item.Metadata.HasLyrics = hasLyricsSidecar(realPath) || m.Lyrics() != ""
// Extract duration ONLY if explicitly requested using the ffmpeg VideoService
// This respects concurrency limits and gracefully handles missing ffmpeg
@@ -572,35 +500,91 @@ func extractAudioMetadata(ctx context.Context, item *iteminfo.ExtendedItemInfo,
return nil
}
// Returns lyrics from an audio file (from embedded tags or from a .lrc file with the same name).
func ExtractLyrics(realPath string) ([]iteminfo.Lyric, error) {
var lyricsSidecarExts = []string{".elrc", ".lrc", ".srt", ".vtt"}
func lyricsSidecar(dir, nameWithoutExt, ext string) (path string, ok bool) {
sidecarPath := filepath.Join(dir, nameWithoutExt+ext)
info, err := os.Lstat(sidecarPath)
if err != nil {
return "", false
}
if !info.Mode().IsRegular() {
return "", false
}
if info.Size() > 15*1024*1024 { // 15MB
return "", false
}
return sidecarPath, true
}
func findLyricsSidecar(realPath string) (data []byte, ext string, ok bool) {
dir := filepath.Dir(realPath)
base := filepath.Base(realPath)
nameWithoutExt := strings.TrimSuffix(base, filepath.Ext(base))
for _, ext = range lyricsSidecarExts {
sidecarPath, candOk := lyricsSidecar(dir, nameWithoutExt, ext)
if !candOk {
continue
}
data, err := os.ReadFile(sidecarPath)
if err != nil {
continue
}
if strings.TrimSpace(string(data)) == "" {
continue
}
return data, strings.TrimPrefix(ext, "."), true
}
return nil, "", false
}
func hasLyricsSidecar(realPath string) bool {
dir := filepath.Dir(realPath)
base := filepath.Base(realPath)
nameWithoutExt := strings.TrimSuffix(base, filepath.Ext(base))
for _, ext := range lyricsSidecarExts {
sidecarPath, ok := lyricsSidecar(dir, nameWithoutExt, ext)
if !ok {
continue
}
f, err := os.Open(sidecarPath)
if err != nil {
continue
}
buf := make([]byte, 4096)
for {
n, readErr := f.Read(buf)
if n > 0 && strings.TrimSpace(string(buf[:n])) != "" {
f.Close()
return true
}
if readErr != nil {
break
}
}
f.Close()
}
return false
}
// Returns raw lyrics from an audio file (from embedded tags or from sidecar files with the same name).
func ExtractLyrics(realPath string) (string, string, error) {
if data, ext, ok := findLyricsSidecar(realPath); ok {
return string(data), ext, nil
}
file, err := os.Open(realPath)
if err != nil {
return nil, err
return "", "", err
}
defer file.Close()
m, err := tag.ReadFrom(file)
if err != nil {
return nil, err
return "", "", err
}
var lyrics []iteminfo.Lyric
if raw := m.Lyrics(); raw != "" {
lyrics = parseLRC(raw)
return raw, "embedded", nil
}
if len(lyrics) == 0 {
// Check for sidecar .lrc file
dir := filepath.Dir(realPath)
base := filepath.Base(realPath)
ext := filepath.Ext(base)
nameWithoutExt := strings.TrimSuffix(base, ext)
lrcPath := filepath.Join(dir, nameWithoutExt+".lrc")
if data, err := os.ReadFile(lrcPath); err == nil {
lyrics = parseLRC(string(data))
}
}
return lyrics, nil
return "", "", nil
}
// extractVideoMetadata extracts duration and codec info from video files using ffprobe.
@@ -961,7 +945,14 @@ func WriteDirectory(opts utils.FileOptions) error {
return nil
}
// WriteFileFunc is a variable that can be mocked in tests
var WriteFileFunc = writeFile
func WriteFile(source, path string, in io.Reader) error {
return WriteFileFunc(source, path, in)
}
func writeFile(source, path string, in io.Reader) error {
idx := indexing.GetIndex(source)
if idx == nil {
return fmt.Errorf("could not get index: %v ", source)
@@ -447,6 +447,7 @@ var types = map[string]string{
".vtt": "text/subtitle-vtt",
".ssa": "text/subtitle-ssa",
".lrc": "text/lyrics",
".elrc": "text/lyrics",
".sbv": "text/plain",
".ssi": "text/x-server-parsed-html",
".ssm": "application/streamingmedia",
@@ -21,11 +21,13 @@ func (d dirFS) Open(name string) (fs.File, error) {
// InitAssetFS initializes the asset filesystem for the application
// This should be called once during startup before http or preview services start
func InitAssetFS(embeddedAssets fs.FS, useEmbedded bool) {
func InitAssetFS(embeddedAssets fs.FS, useEmbedded bool, devMode bool) {
if useEmbedded {
assetFs = embeddedAssets
} else if devMode {
// Dev mode: serve frontend source assets (templates, fonts, icons)
assetFs = dirFS{Dir: http.Dir("../frontend/public")}
} else {
// Dev mode: Serve files from internal/web/dist directory
assetFs = dirFS{Dir: http.Dir("internal/web/dist")}
}
}
+55 -27
View File
@@ -56,48 +56,62 @@ func unixModeToFileMode(u uint32) os.FileMode {
}
// MoveFile moves a file from src to dst.
// By default, the rename system call is used. If src and dst point to different volumes,
// the file copy is used as a fallback.
// By default, the rename system call is used. If src and dst are on different volumes,
// CopyFile is used as a fallback and the source is removed synchronously on success.
func MoveFile(src, dst string) error {
err := os.Rename(src, dst)
if err == nil {
return nil
}
// fallback
err = CopyFile(src, dst)
if err != nil {
logger.Errorf("CopyFile failed %v", err)
return err
}
go func() {
err = os.RemoveAll(src)
if err != nil {
logger.Errorf("os.Remove failed %v", err)
if err := os.Rename(src, dst); err != nil {
if !isCrossDeviceRenameError(err) {
return err
}
}()
if err := CopyFile(src, dst); err != nil {
return err
}
return os.RemoveAll(src)
}
return nil
}
func preserveFileMode(path string, mode os.FileMode) error {
return os.Chmod(path, mode.Perm())
}
// CopyFile copies a file or directory from source to dest and returns an error if any.
func CopyFile(source, dest string) error {
// Check if the source exists and whether it's a file or directory.
info, err := os.Stat(source)
info, err := os.Lstat(source)
if err != nil {
return err
}
if info.Mode()&os.ModeSymlink != 0 {
return copySymlink(source, dest)
}
if info.IsDir() {
// If the source is a directory, copy it recursively.
return copyDirectory(source, dest)
}
// If the source is a file, copy the file.
return copySingleFile(source, dest)
}
func copySymlink(source, dest string) error {
target, err := os.Readlink(source)
if err != nil {
return err
}
destDir := filepath.Dir(dest)
if err = os.MkdirAll(destDir, EffectiveDirPerm()); err != nil {
return err
}
tmpDir, err := os.MkdirTemp(destDir, ".fb-link-*")
if err != nil {
return err
}
defer func() { _ = os.RemoveAll(tmpDir) }()
tmpPath := filepath.Join(tmpDir, "link")
if err := os.Symlink(target, tmpPath); err != nil {
return err
}
return os.Rename(tmpPath, dest)
}
// copySingleFile handles copying a single file.
func copySingleFile(source, dest string) error {
// Get source file info to preserve permissions
@@ -161,7 +175,7 @@ func copyFileContents(dst io.Writer, src io.Reader) error {
// copyDirectory handles copying directories recursively.
func copyDirectory(source, dest string) error {
srcInfo, err := os.Stat(source)
srcInfo, err := os.Lstat(source)
if err != nil {
return err
}
@@ -170,6 +184,9 @@ func copyDirectory(source, dest string) error {
if err != nil {
return err
}
if err = preserveFileMode(dest, srcInfo.Mode()); err != nil {
logger.Debugf("Could not set directory permissions for %s: %v", dest, err)
}
// Read the contents of the source directory.
entries, err := os.ReadDir(source)
@@ -182,6 +199,17 @@ func copyDirectory(source, dest string) error {
srcPath := filepath.Join(source, entry.Name())
destPath := filepath.Join(dest, entry.Name())
if entry.Type()&os.ModeSymlink != 0 {
linkTarget := ""
linkTarget, err = os.Readlink(srcPath)
if err != nil {
return err
}
if err = os.Symlink(linkTarget, destPath); err != nil {
return err
}
continue
}
if entry.IsDir() {
// Recursively copy subdirectories.
err = copyDirectory(srcPath, destPath)
@@ -139,6 +139,223 @@ func TestCopyFilePreservesModTime(t *testing.T) {
}
}
// moveViaCrossDeviceFallback mirrors MoveFile's EXDEV fallback (CopyFile + RemoveAll).
func moveViaCrossDeviceFallback(t *testing.T, src, dst string) {
t.Helper()
if err := CopyFile(src, dst); err != nil {
t.Fatal(err)
}
if err := os.RemoveAll(src); err != nil {
t.Fatal(err)
}
}
func TestMoveFileDoesNotFallbackOnNonCrossDeviceError(t *testing.T) {
srcDir := t.TempDir()
src := filepath.Join(srcDir, "file.txt")
if err := os.WriteFile(src, []byte("data"), 0o644); err != nil {
t.Fatal(err)
}
dst := filepath.Join(srcDir, "existing-dir")
if err := os.Mkdir(dst, 0o755); err != nil {
t.Fatal(err)
}
err := MoveFile(src, dst)
if err == nil {
t.Fatal("expected rename error")
}
if _, statErr := os.Stat(src); statErr != nil {
t.Fatalf("source file should remain after rename failure: %v", statErr)
}
entries, err := os.ReadDir(dst)
if err != nil {
t.Fatal(err)
}
if len(entries) != 0 {
t.Fatalf("destination directory should be unchanged, got %d entries", len(entries))
}
}
func TestMoveFileCrossDevicePreservesFilePermissions(t *testing.T) {
srcDir := t.TempDir()
dstDir := t.TempDir()
src := filepath.Join(srcDir, "secret.txt")
if err := os.WriteFile(src, []byte("data"), 0o640); err != nil {
t.Fatal(err)
}
dst := filepath.Join(dstDir, "secret.txt")
moveViaCrossDeviceFallback(t, src, dst)
got, err := os.Stat(dst)
if err != nil {
t.Fatal(err)
}
if got.Mode().Perm() != 0o640 {
t.Fatalf("dst perm = %#o, want 0640", got.Mode().Perm())
}
if _, err := os.Stat(src); !os.IsNotExist(err) {
t.Fatalf("src should be removed, stat err = %v", err)
}
}
func TestMoveFileCrossDeviceMovesDirectory(t *testing.T) {
srcDir := t.TempDir()
dstParent := t.TempDir()
nested := filepath.Join(srcDir, "nested.txt")
if err := os.WriteFile(nested, []byte("nested"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chmod(srcDir, 0o755); err != nil {
t.Fatal(err)
}
dst := filepath.Join(dstParent, "moved-dir")
moveViaCrossDeviceFallback(t, srcDir, dst)
movedNested := filepath.Join(dst, "nested.txt")
if _, err := os.Stat(movedNested); err != nil {
t.Fatalf("expected nested file at destination: %v", err)
}
got, err := os.Stat(dst)
if err != nil {
t.Fatal(err)
}
if got.Mode().Perm() != 0o755 {
t.Fatalf("dst dir perm = %#o, want 0755", got.Mode().Perm())
}
if _, err := os.Stat(srcDir); !os.IsNotExist(err) {
t.Fatalf("source directory should be removed, stat err = %v", err)
}
}
func TestMoveFileCrossDevicePreservesSymlinkToFile(t *testing.T) {
targetDir := t.TempDir()
target := filepath.Join(targetDir, "target.txt")
if err := os.WriteFile(target, []byte("data"), 0o644); err != nil {
t.Fatal(err)
}
srcDir := t.TempDir()
dstDir := t.TempDir()
link := filepath.Join(srcDir, "link")
if err := os.Symlink(target, link); err != nil {
t.Fatal(err)
}
dst := filepath.Join(dstDir, "link")
moveViaCrossDeviceFallback(t, link, dst)
gotTarget, err := os.Readlink(dst)
if err != nil {
t.Fatal(err)
}
if gotTarget != target {
t.Fatalf("link target = %q, want %q", gotTarget, target)
}
info, err := os.Lstat(dst)
if err != nil {
t.Fatal(err)
}
if info.Mode()&os.ModeSymlink == 0 {
t.Fatal("expected destination to remain a symlink")
}
if _, err := os.Stat(link); !os.IsNotExist(err) {
t.Fatalf("source symlink should be removed, stat err = %v", err)
}
}
func TestMoveFileCrossDevicePreservesSymlinkToDirectory(t *testing.T) {
targetDir := t.TempDir()
if err := os.Mkdir(filepath.Join(targetDir, "inside"), 0o755); err != nil {
t.Fatal(err)
}
srcDir := t.TempDir()
dstDir := t.TempDir()
link := filepath.Join(srcDir, "dirlink")
if err := os.Symlink(targetDir, link); err != nil {
t.Fatal(err)
}
dst := filepath.Join(dstDir, "dirlink")
moveViaCrossDeviceFallback(t, link, dst)
gotTarget, err := os.Readlink(dst)
if err != nil {
t.Fatal(err)
}
if gotTarget != targetDir {
t.Fatalf("link target = %q, want %q", gotTarget, targetDir)
}
info, err := os.Lstat(dst)
if err != nil {
t.Fatal(err)
}
if info.Mode()&os.ModeSymlink == 0 {
t.Fatal("expected destination to remain a symlink")
}
}
func TestMoveFileCrossDeviceMovesDirectoryWithNestedSymlink(t *testing.T) {
target := filepath.Join(t.TempDir(), "target.txt")
if err := os.WriteFile(target, []byte("linked"), 0o644); err != nil {
t.Fatal(err)
}
srcDir := t.TempDir()
if err := os.Symlink(target, filepath.Join(srcDir, "nested-link")); err != nil {
t.Fatal(err)
}
dstParent := t.TempDir()
dst := filepath.Join(dstParent, "moved-dir")
moveViaCrossDeviceFallback(t, srcDir, dst)
movedLink := filepath.Join(dst, "nested-link")
gotTarget, err := os.Readlink(movedLink)
if err != nil {
t.Fatal(err)
}
if gotTarget != target {
t.Fatalf("nested link target = %q, want %q", gotTarget, target)
}
info, err := os.Lstat(movedLink)
if err != nil {
t.Fatal(err)
}
if info.Mode()&os.ModeSymlink == 0 {
t.Fatal("expected nested entry to remain a symlink")
}
}
func TestCopySymlinkReplacesExistingDestination(t *testing.T) {
target := filepath.Join(t.TempDir(), "target.txt")
if err := os.WriteFile(target, []byte("data"), 0o644); err != nil {
t.Fatal(err)
}
workDir := t.TempDir()
src := filepath.Join(workDir, "link")
if err := os.Symlink(target, src); err != nil {
t.Fatal(err)
}
dst := filepath.Join(workDir, "existing")
if err := os.WriteFile(dst, []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
if err := copySymlink(src, dst); err != nil {
t.Fatal(err)
}
gotTarget, err := os.Readlink(dst)
if err != nil {
t.Fatal(err)
}
if gotTarget != target {
t.Fatalf("link target = %q, want %q", gotTarget, target)
}
info, err := os.Lstat(dst)
if err != nil {
t.Fatal(err)
}
if info.Mode()&os.ModeSymlink == 0 {
t.Fatal("expected destination to be a symlink")
}
}
func TestCopyFileContentsFallsBackAfterCopyFileRangeBadFileDescriptor(t *testing.T) {
const content = "complete file contents"
src := struct{ io.Reader }{bytes.NewBufferString(content)}
@@ -0,0 +1,17 @@
//go:build !windows
package fileutils
import (
"errors"
"os"
"syscall"
)
func isCrossDeviceRenameError(err error) bool {
if errors.Is(err, syscall.EXDEV) {
return true
}
var linkErr *os.LinkError
return errors.As(err, &linkErr) && errors.Is(linkErr.Err, syscall.EXDEV)
}
@@ -0,0 +1,18 @@
//go:build windows
package fileutils
import (
"errors"
"os"
"syscall"
)
func isCrossDeviceRenameError(err error) bool {
const errNotSameDevice = syscall.Errno(17)
if errors.Is(err, errNotSameDevice) {
return true
}
var linkErr *os.LinkError
return errors.As(err, &linkErr) && errors.Is(linkErr.Err, errNotSameDevice)
}
+12
View File
@@ -0,0 +1,12 @@
package analytics
import (
"os"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/testutil"
)
func TestMain(m *testing.M) {
os.Exit(testutil.Main(m))
}
+12
View File
@@ -0,0 +1,12 @@
package auth
import (
"os"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/testutil"
)
func TestMain(m *testing.M) {
os.Exit(testutil.Main(m))
}
+1 -4
View File
@@ -15,10 +15,7 @@ type NoAuth struct{}
// AuthenticateNoAuth authenticates as the configured admin user with no credentials required.
func AuthenticateNoAuth(r *http.Request, user *users.Storage) (*users.User, error) {
admin := settings.Config.Auth.AdminUsername
if admin == "" {
admin = "admin"
}
admin := settings.PasswordAdminUsername()
id, err := users.ResolveUsernameToID(admin)
if err != nil {
return nil, err
+31
View File
@@ -1,8 +1,10 @@
package auth
import (
"encoding/json"
"net/http"
"os"
"strings"
"github.com/gtsteffaniak/filebrowser/backend/internal/errors"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
@@ -38,3 +40,32 @@ func AuthenticateProxy(r *http.Request, usr *users.Storage, headerName string) (
func (a ProxyAuth) Auth(r *http.Request, usr *users.Storage) (*users.User, error) {
return AuthenticateProxy(r, usr, a.Header)
}
// ExtractGroupsFromHeader reads group/role values from a proxy header named by groupsClaim.
// Returns present=false when the header name is empty or the header is missing/blank.
func ExtractGroupsFromHeader(r *http.Request, headerName string) ([]string, bool) {
if headerName == "" {
return nil, false
}
headerVal := strings.TrimSpace(r.Header.Get(headerName))
if headerVal == "" {
return nil, false
}
if strings.HasPrefix(headerVal, "[") {
var groups []string
if err := json.Unmarshal([]byte(headerVal), &groups); err == nil {
return groups, true
}
}
groups := ExtractGroupsFromClaims(map[string]interface{}{headerName: headerVal}, headerName)
if len(groups) == 1 && strings.Contains(groups[0], ",") {
parts := strings.Split(groups[0], ",")
groups = make([]string, 0, len(parts))
for _, part := range parts {
if part = strings.TrimSpace(part); part != "" {
groups = append(groups, part)
}
}
}
return groups, true
}
@@ -0,0 +1,81 @@
package auth_test
import (
"net/http"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/auth"
)
func TestExtractGroupsFromHeader_SingleValue(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Cosmos-Role", "2")
groups, present := auth.ExtractGroupsFromHeader(req, "x-cosmos-role")
if !present {
t.Fatal("expected groups to be present")
}
if len(groups) != 1 || groups[0] != "2" {
t.Fatalf("groups = %#v, want [\"2\"]", groups)
}
}
func TestExtractGroupsFromHeader_CommaSeparated(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Groups", "1, 2")
groups, present := auth.ExtractGroupsFromHeader(req, "X-Groups")
if !present {
t.Fatal("expected groups to be present")
}
if len(groups) != 2 || groups[0] != "1" || groups[1] != "2" {
t.Fatalf("groups = %#v, want [\"1\", \"2\"]", groups)
}
}
func TestExtractGroupsFromHeader_JSONArray(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Groups", `["admin","users"]`)
groups, present := auth.ExtractGroupsFromHeader(req, "X-Groups")
if !present {
t.Fatal("expected groups to be present")
}
if len(groups) != 2 || groups[0] != "admin" || groups[1] != "users" {
t.Fatalf("groups = %#v, want [\"admin\", \"users\"]", groups)
}
}
func TestExtractGroupsFromHeader_Missing(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
if err != nil {
t.Fatal(err)
}
groups, present := auth.ExtractGroupsFromHeader(req, "X-Groups")
if present {
t.Fatalf("expected groups to be absent, got %#v", groups)
}
}
func TestExtractGroupsFromHeader_EmptyHeaderName(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Groups", "admin")
groups, present := auth.ExtractGroupsFromHeader(req, "")
if present {
t.Fatalf("expected groups to be absent, got %#v", groups)
}
}
+288 -59
View File
@@ -1,6 +1,7 @@
package access
import (
"encoding/json"
"fmt"
"maps"
"path/filepath"
@@ -10,10 +11,11 @@ import (
"sync"
"time"
"github.com/gtsteffaniak/filebrowser/backend/internal/errors"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
"github.com/gtsteffaniak/filebrowser/backend/internal/utils"
"github.com/golang-jwt/jwt/v4"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
"github.com/gtsteffaniak/filebrowser/backend/internal/errors"
"github.com/gtsteffaniak/filebrowser/backend/internal/utils"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
"github.com/gtsteffaniak/go-cache/cache"
"github.com/gtsteffaniak/go-logger/logger"
)
@@ -65,11 +67,37 @@ type GroupMap map[string]StringSet
type HashedTokenInfo struct {
UserID uint64
IsSession bool
// ExpiresAt is the token's exp claim as unix time; 0 means no expiry (e.g.
// non-JWT strings or rows persisted before expiry tracking).
ExpiresAt int64
}
// sessionTokenRetireGrace is how long a rotated session token stays usable after
// renewal. In-flight requests still carrying the previous cookie must not 401.
const sessionTokenRetireGrace = 2 * time.Minute
// BearerTokenGrace is how long a bearer token may still resolve after session
// rotation or after its JWT exp (e.g. share-ACL identity). In-flight requests
// still carrying the previous cookie must not 401 during rotation.
const BearerTokenGrace = 2 * time.Minute
// TokenExpiryUnix extracts the exp claim of a bearer JWT without verifying the
// signature; the value is only used for expiry bookkeeping and tokens are
// verified on the auth path. Returns 0 for non-JWT strings or tokens without
// an expiry claim.
func TokenExpiryUnix(tokenString string) int64 {
claims := jwt.MapClaims{}
if _, _, err := jwt.NewParser().ParseUnverified(tokenString, claims); err != nil {
return 0
}
exp, ok := claims["exp"].(float64)
if !ok {
return 0
}
return int64(exp)
}
// TokenExpiredPastGrace reports whether a token expiry is older than the
// resolution grace window. expiresAt == 0 (unknown/none) never expires.
func TokenExpiredPastGrace(expiresAt int64, now time.Time) bool {
return expiresAt != 0 && now.After(time.Unix(expiresAt, 0).Add(BearerTokenGrace))
}
// Storage manages access rules and group membership.
type Storage struct {
@@ -82,17 +110,30 @@ type Storage struct {
sqlStore SQLPersister // SQL store for persistence
}
// RuleKey identifies one access_rules row.
type RuleKey struct {
Source string
Path string
}
// RuleUpsert is an access_rules row to insert or replace.
type RuleUpsert struct {
RuleKey
Rule *AccessRule
}
// SQLPersister interface for SQL persistence operations
type SQLPersister interface {
SaveAccessRule(source, path string, rule *AccessRule) error
DeleteAccessRule(source, path string) error
SaveGroup(name string, members StringSet) error
DeleteGroup(name string) error
DeleteGroupWithRules(name string, save []RuleUpsert, remove []RuleKey) error
SaveRevokedToken(tokenHash string, revokedAt int64) error
PersistImmediateTokenRevocation(tokenHash string) error
PersistTokenRetirement(tokenHash string, revokedAt int64, pruned []string) error
DeleteRevokedToken(tokenHash string) error
SaveHashedToken(tokenHash string, userID uint64, isSession bool) error
SaveHashedToken(tokenHash string, userID uint64, isSession bool, expiresAt int64) error
DeleteHashedToken(tokenHash string) error
DeleteHashedTokensByUserID(userID uint64) error
}
@@ -109,30 +150,35 @@ func (s *Storage) Flush() error {
// persistGroupSQLNL upserts or deletes one groups row to match in-memory state.
// Caller must hold s.mux. If the group is absent from s.Groups, it is deleted from SQL.
func (s *Storage) persistGroupSQLNL(groupname string) {
func (s *Storage) persistGroupSQLNL(groupname string) error {
if s.sqlStore == nil {
return
return nil
}
members, ok := s.Groups[groupname]
if !ok {
if err := s.sqlStore.DeleteGroup(groupname); err != nil {
logger.Errorf("failed to delete group %q from sql: %v", groupname, err)
return fmt.Errorf("failed to delete group %q from sql: %w", groupname, err)
}
return
return nil
}
if err := s.sqlStore.SaveGroup(groupname, members); err != nil {
logger.Errorf("failed to save group %q: %v", groupname, err)
return fmt.Errorf("failed to save group %q: %w", groupname, err)
}
return nil
}
// ensureGroupExistsNL creates an empty in-memory group if missing and write-through persists it.
// Caller must hold s.mux.
func (s *Storage) ensureGroupExistsNL(groupname string) {
func (s *Storage) ensureGroupExistsNL(groupname string) error {
if _, ok := s.Groups[groupname]; ok {
return
return nil
}
s.Groups[groupname] = make(StringSet)
s.persistGroupSQLNL(groupname)
if err := s.persistGroupSQLNL(groupname); err != nil {
delete(s.Groups, groupname)
return err
}
return nil
}
// NewStorage creates a new Storage instance.
@@ -280,7 +326,9 @@ func (s *Storage) AllowUser(sourcePath string, indexPath utils.IndexPath, userna
func (s *Storage) DenyGroup(sourcePath string, indexPath utils.IndexPath, groupname string) error {
s.mux.Lock()
defer s.mux.Unlock()
s.ensureGroupExistsNL(groupname)
if err := s.ensureGroupExistsNL(groupname); err != nil {
return err
}
rule := s.getOrCreateRuleNL(sourcePath, indexPath)
if _, ok := rule.Deny.Groups[groupname]; ok {
return errors.ErrExist
@@ -296,7 +344,9 @@ func (s *Storage) DenyGroup(sourcePath string, indexPath utils.IndexPath, groupn
func (s *Storage) AllowGroup(sourcePath string, indexPath utils.IndexPath, groupname string) error {
s.mux.Lock()
defer s.mux.Unlock()
s.ensureGroupExistsNL(groupname)
if err := s.ensureGroupExistsNL(groupname); err != nil {
return err
}
rule := s.getOrCreateRuleNL(sourcePath, indexPath)
if _, ok := rule.Allow.Groups[groupname]; ok {
return errors.ErrExist
@@ -546,14 +596,22 @@ func (s *Storage) GetAllRules(sourcePath string) (map[string]FrontendAccessRule,
func (s *Storage) AddUserToGroup(group, username string) error {
s.mux.Lock()
defer s.mux.Unlock()
createdGroup := false
if _, ok := s.Groups[group]; !ok {
s.Groups[group] = make(StringSet)
createdGroup = true
}
if _, ok := s.Groups[group][username]; ok {
return nil
}
s.Groups[group][username] = struct{}{}
s.persistGroupSQLNL(group)
if err := s.persistGroupSQLNL(group); err != nil {
delete(s.Groups[group], username)
if createdGroup {
delete(s.Groups, group)
}
return err
}
s.clearAllCaches()
return nil
}
@@ -570,6 +628,103 @@ func (s *Storage) GetAllGroups() []string {
return groups
}
// GetGroupMembers returns every group with its sorted member usernames.
func (s *Storage) GetGroupMembers() map[string][]string {
s.mux.RLock()
defer s.mux.RUnlock()
result := make(map[string][]string, len(s.Groups))
for group, members := range s.Groups {
names := make([]string, 0, len(members))
for name := range members {
names = append(names, name)
}
sort.Strings(names)
result[group] = names
}
return result
}
// SetGroupMembers creates the group if needed and replaces its membership.
func (s *Storage) SetGroupMembers(group string, usernames []string) error {
s.mux.Lock()
defer s.mux.Unlock()
members := make(StringSet, len(usernames))
for _, name := range usernames {
if name != "" {
members[name] = struct{}{}
}
}
previous, existed := s.Groups[group]
s.Groups[group] = members
if err := s.persistGroupSQLNL(group); err != nil {
// Keep memory and SQL consistent: undo the in-memory change.
if existed {
s.Groups[group] = previous
} else {
delete(s.Groups, group)
}
return err
}
s.clearAllCaches()
return nil
}
// DeleteGroup removes a group and every access rule entry that references it.
// The group row and all affected rule rows are written in one SQL transaction, so a
// failure stores nothing. On failure the in-memory groups and rules are restored and the
// error is returned. The lock is held throughout, so no other change can interleave.
func (s *Storage) DeleteGroup(group string) error {
s.mux.Lock()
defer s.mux.Unlock()
previousMembers, existed := s.Groups[group]
// Snapshot the rules so they can be restored if the transaction fails.
rulesSnapshot, err := json.Marshal(s.AllRules)
if err != nil {
return fmt.Errorf("failed to snapshot access rules: %w", err)
}
delete(s.Groups, group)
touched := s.removeAllRulesForGroupNL(group)
if !existed && len(touched) == 0 {
return nil
}
if err := s.persistGroupDeleteNL(group, touched); err != nil {
if existed {
s.Groups[group] = previousMembers
}
restored := make(SourceRuleMap)
if jsonErr := json.Unmarshal(rulesSnapshot, &restored); jsonErr != nil {
logger.Errorf("failed to restore access rules after group delete error: %v", jsonErr)
} else {
s.AllRules = restored
}
return err
}
s.clearAllCaches()
return nil
}
// persistGroupDeleteNL writes a group deletion and the resulting rule row changes to SQL
// in one transaction. Caller must hold s.mux.
func (s *Storage) persistGroupDeleteNL(group string, touched []RuleKey) error {
if s.sqlStore == nil {
return nil
}
var save []RuleUpsert
var remove []RuleKey
for _, key := range touched {
var rule *AccessRule
if bySrc, ok := s.AllRules[key.Source]; ok {
rule = bySrc[key.Path]
}
if accessRuleHasPayload(rule) {
save = append(save, RuleUpsert{RuleKey: key, Rule: rule})
} else {
remove = append(remove, key)
}
}
return s.sqlStore.DeleteGroupWithRules(group, save, remove)
}
// GetUserGroups returns all groups for a specific user.
func (s *Storage) GetUserGroups(username string) []string {
s.mux.RLock()
@@ -589,7 +744,6 @@ func (s *Storage) GetUserGroups(username string) []string {
func (s *Storage) SyncUserGroups(username string, newGroups []string) error {
s.mux.Lock()
defer s.mux.Unlock()
affected := make(map[string]struct{})
// Create a set of new groups for efficient lookup
newGroupsSet := make(StringSet, len(newGroups))
@@ -600,30 +754,62 @@ func (s *Storage) SyncUserGroups(username string, newGroups []string) error {
newGroupsSet[g] = struct{}{}
}
// Iterate over all existing groups to find the user's current memberships
affected := make(map[string]struct{})
for group, members := range s.Groups {
_, userIsInGroup := members[username]
_, groupIsInNewSet := newGroupsSet[group]
// If user is in a group that is not in their new set of groups, remove them.
if userIsInGroup && !groupIsInNewSet {
delete(s.Groups[group], username)
affected[group] = struct{}{}
}
}
for group := range newGroupsSet {
if _, ok := s.Groups[group]; !ok {
affected[group] = struct{}{}
continue
}
if _, ok := s.Groups[group][username]; !ok {
affected[group] = struct{}{}
}
}
// Add user to new groups
before := make(map[string]StringSet, len(affected))
for group := range affected {
before[group] = cloneStringSet(s.Groups[group])
}
for group, members := range s.Groups {
_, userIsInGroup := members[username]
_, groupIsInNewSet := newGroupsSet[group]
if userIsInGroup && !groupIsInNewSet {
delete(s.Groups[group], username)
}
}
for group := range newGroupsSet {
if _, ok := s.Groups[group]; !ok {
s.Groups[group] = make(StringSet)
}
if _, ok := s.Groups[group][username]; !ok {
s.Groups[group][username] = struct{}{}
affected[group] = struct{}{}
}
s.Groups[group][username] = struct{}{}
}
for group := range affected {
s.persistGroupSQLNL(group)
affectedGroups := slices.Sorted(maps.Keys(affected))
var persisted []string
for _, group := range affectedGroups {
if err := s.persistGroupSQLNL(group); err != nil {
for _, g := range persisted {
if restoreErr := s.restoreGroupSQLNL(g, before[g]); restoreErr != nil {
logger.Errorf("failed to restore group %q in sql after sync failure: %v", g, restoreErr)
}
}
for g, snap := range before {
if len(snap) == 0 {
delete(s.Groups, g)
} else {
s.Groups[g] = snap
}
}
return err
}
persisted = append(persisted, group)
}
if len(affected) > 0 {
s.clearAllCaches()
@@ -631,6 +817,31 @@ func (s *Storage) SyncUserGroups(username string, newGroups []string) error {
return nil
}
func (s *Storage) restoreGroupSQLNL(groupname string, members StringSet) error {
if s.sqlStore == nil {
return nil
}
if len(members) == 0 {
if err := s.sqlStore.DeleteGroup(groupname); err != nil {
return fmt.Errorf("failed to delete group %q from sql: %w", groupname, err)
}
return nil
}
if err := s.sqlStore.SaveGroup(groupname, members); err != nil {
return fmt.Errorf("failed to save group %q: %w", groupname, err)
}
return nil
}
func cloneStringSet(src StringSet) StringSet {
if len(src) == 0 {
return nil
}
dst := make(StringSet, len(src))
maps.Copy(dst, src)
return dst
}
// RemoveUserFromGroup removes a username from a group.
func (s *Storage) RemoveUserFromGroup(group, username string) error {
s.mux.Lock()
@@ -643,7 +854,10 @@ func (s *Storage) RemoveUserFromGroup(group, username string) error {
return nil
}
delete(members, username)
s.persistGroupSQLNL(group)
if err := s.persistGroupSQLNL(group); err != nil {
s.Groups[group][username] = struct{}{}
return err
}
s.clearAllCaches()
return nil
}
@@ -841,44 +1055,46 @@ func (s *Storage) RemoveAllRulesForUser(username string) error {
func (s *Storage) RemoveAllRulesForGroup(groupname string) error {
s.mux.Lock()
defer s.mux.Unlock()
changed := false
dirty := make(map[string]map[string]struct{})
touch := func(sp, ip string) {
if dirty[sp] == nil {
dirty[sp] = make(map[string]struct{})
touched := s.removeAllRulesForGroupNL(groupname)
if len(touched) > 0 {
s.clearAllCaches()
for _, key := range touched {
s.persistRuleSQLNL(key.Source, key.Path)
}
dirty[sp][ip] = struct{}{}
}
return nil
}
// removeAllRulesForGroupNL removes a group from every allow and deny list in memory and
// returns the rules it touched (rules left empty are deleted). It does not persist; the
// caller must hold s.mux and save the returned rules.
func (s *Storage) removeAllRulesForGroupNL(groupname string) []RuleKey {
var touched []RuleKey
for sourcePath, rulesBySource := range s.AllRules {
for indexPath, rule := range rulesBySource {
touchedRule := false
if _, exists := rule.Allow.Groups[groupname]; exists {
delete(rule.Allow.Groups, groupname)
touch(sourcePath, indexPath)
changed = true
touchedRule = true
}
if _, exists := rule.Deny.Groups[groupname]; exists {
delete(rule.Deny.Groups, groupname)
touch(sourcePath, indexPath)
changed = true
touchedRule = true
}
if len(rule.Allow.Users) == 0 && len(rule.Allow.Groups) == 0 && len(rule.Deny.Users) == 0 && len(rule.Deny.Groups) == 0 {
// A rule left with only DenyAll must stay, otherwise removing the group would open the path.
if !accessRuleHasPayload(rule) {
delete(s.AllRules[sourcePath], indexPath)
if len(s.AllRules[sourcePath]) == 0 {
delete(s.AllRules, sourcePath)
}
touch(sourcePath, indexPath)
touchedRule = true
}
if touchedRule {
touched = append(touched, RuleKey{Source: sourcePath, Path: indexPath})
}
}
}
if changed {
s.clearAllCaches()
for sp, paths := range dirty {
for ip := range paths {
s.persistRuleSQLNL(sp, ip)
}
}
}
return nil
return touched
}
// GetRulesForUser returns all rules for a specific user for a given sourcePath.
@@ -1338,7 +1554,7 @@ func (s *Storage) RevokeToken(tokenString string) error {
}
// RetireToken schedules a rotated session token for revocation after
// sessionTokenRetireGrace. The owner mapping is kept during the grace window so
// BearerTokenGrace. The owner mapping is kept during the grace window so
// in-flight requests still carrying the previous cookie stay authenticated.
func (s *Storage) RetireToken(tokenString string) error {
tokenHash := utils.HashSHA256(tokenString)
@@ -1415,7 +1631,7 @@ func (rb *tokenRetireRollback) capture(s *Storage, tokenHash string, now time.Ti
rb.prunedRevoked = make(map[string]int64)
rb.prunedHashed = make(map[string]HashedTokenInfo)
for hash, revokedAt := range revokedAfter {
if revokedAt != 0 && now.Sub(time.Unix(revokedAt, 0)) < sessionTokenRetireGrace {
if revokedAt != 0 && now.Sub(time.Unix(revokedAt, 0)) < BearerTokenGrace {
continue
}
if at, ok := s.RevokedTokens[hash]; ok {
@@ -1448,7 +1664,7 @@ func (rb *tokenRetireRollback) apply(s *Storage) {
func (s *Storage) pruneRevocationsNL(now time.Time) []string {
var pruned []string
for hash, revokedAt := range s.RevokedTokens {
if revokedAt == 0 || now.Sub(time.Unix(revokedAt, 0)) >= sessionTokenRetireGrace {
if revokedAt == 0 || now.Sub(time.Unix(revokedAt, 0)) >= BearerTokenGrace {
delete(s.RevokedTokens, hash)
delete(s.HashedTokens, hash)
pruned = append(pruned, hash)
@@ -1470,7 +1686,7 @@ func (s *Storage) IsTokenRevoked(tokenString string) bool {
if revokedAt == 0 {
return true
}
return time.Since(time.Unix(revokedAt, 0)) >= sessionTokenRetireGrace
return time.Since(time.Unix(revokedAt, 0)) >= BearerTokenGrace
}
// AddApiToken maps a named API token string hash to an owner user id.
@@ -1484,13 +1700,19 @@ func (s *Storage) AddSessionToken(tokenString string, userID uint64) error {
}
func (s *Storage) addHashedToken(tokenString string, userID uint64, isSession bool) error {
expiresAt := TokenExpiryUnix(tokenString)
// Tokens already past the expiry grace window are never registered; expired
// mappings are pruned at startup load instead of on each registration.
if TokenExpiredPastGrace(expiresAt, time.Now()) {
return nil
}
tokenHash := utils.HashSHA256(tokenString)
s.mux.Lock()
s.HashedTokens[tokenHash] = HashedTokenInfo{UserID: userID, IsSession: isSession}
s.HashedTokens[tokenHash] = HashedTokenInfo{UserID: userID, IsSession: isSession, ExpiresAt: expiresAt}
sqlStore := s.sqlStore
s.mux.Unlock()
if sqlStore != nil {
return sqlStore.SaveHashedToken(tokenHash, userID, isSession)
return sqlStore.SaveHashedToken(tokenHash, userID, isSession, expiresAt)
}
return nil
}
@@ -1502,12 +1724,19 @@ func (s *Storage) GetUserIDFromToken(tokenString string) (uint64, bool) {
}
// GetHashedTokenInfo retrieves owner/type metadata for a given token string.
// Tokens past their expiry grace window resolve as unknown.
func (s *Storage) GetHashedTokenInfo(tokenString string) (HashedTokenInfo, bool) {
s.mux.RLock()
defer s.mux.RUnlock()
tokenHash := utils.HashSHA256(tokenString)
info, exists := s.HashedTokens[tokenHash]
return info, exists
if !exists {
return info, false
}
if TokenExpiredPastGrace(info.ExpiresAt, time.Now()) {
return HashedTokenInfo{}, false
}
return info, true
}
// GetRevokedTokens returns a copy of the revocation map (hash → revocation unix time).
@@ -1,6 +1,7 @@
package access_test
import (
stderrors "errors"
"os"
"path/filepath"
"strings"
@@ -1851,3 +1852,200 @@ func TestAddUserToGroup_WriteThrough(t *testing.T) {
t.Fatal("bob should be removed from editors in SQL")
}
}
func TestSetGroupMembers_ReplacesMembership(t *testing.T) {
setupTestSources()
s, _, sqlStore := createTestStorageWithSQL(t)
if err := s.SetGroupMembers("editors", []string{"alice", "bob", ""}); err != nil {
t.Fatalf("SetGroupMembers: %v", err)
}
if err := s.SetGroupMembers("editors", []string{"bob", "carol"}); err != nil {
t.Fatalf("SetGroupMembers replace: %v", err)
}
got := s.GetGroupMembers()["editors"]
if len(got) != 2 || got[0] != "bob" || got[1] != "carol" {
t.Fatalf("unexpected members: %v", got)
}
sqlGroups, err := sqlStore.GetAllGroups()
if err != nil {
t.Fatalf("GetAllGroups: %v", err)
}
if _, ok := sqlGroups["editors"]["alice"]; ok {
t.Fatal("alice should have been removed in SQL")
}
if _, ok := sqlGroups["editors"]["carol"]; !ok {
t.Fatal("carol should be persisted in SQL")
}
}
func TestDeleteGroup_RemovesGroupAndRules(t *testing.T) {
setupTestSources()
s, _, sqlStore := createTestStorageWithSQL(t)
if err := s.SetGroupMembers("acme", []string{"alice"}); err != nil {
t.Fatalf("SetGroupMembers: %v", err)
}
if err := s.AllowGroup("mnt/storage", idxPath("/tenant"), "acme"); err != nil {
t.Fatalf("AllowGroup: %v", err)
}
if err := s.DeleteGroup("acme"); err != nil {
t.Fatalf("DeleteGroup: %v", err)
}
if _, ok := s.GetGroupMembers()["acme"]; ok {
t.Fatal("acme should be gone from memory")
}
sqlGroups, err := sqlStore.GetAllGroups()
if err != nil {
t.Fatalf("GetAllGroups: %v", err)
}
if _, ok := sqlGroups["acme"]; ok {
t.Fatal("acme should be gone from SQL")
}
if rules := s.GetRulesForGroup("mnt/storage", "acme"); len(rules) != 0 {
t.Fatalf("expected no rules for deleted group, got %v", rules)
}
// Deleting a missing group is a no-op.
if err := s.DeleteGroup("acme"); err != nil {
t.Fatalf("DeleteGroup missing: %v", err)
}
}
// failingGroupDeletePersister wraps a real store but fails the transactional group delete.
type failingGroupDeletePersister struct {
access.SQLPersister
}
func (failingGroupDeletePersister) DeleteGroupWithRules(string, []access.RuleUpsert, []access.RuleKey) error {
return stderrors.New("simulated sql failure")
}
func TestDeleteGroup_FailureLeavesGroupAndRulesIntact(t *testing.T) {
setupTestSources()
s, _, sqlStore := createTestStorageWithSQL(t)
if err := s.SetGroupMembers("acme", []string{"alice"}); err != nil {
t.Fatalf("SetGroupMembers: %v", err)
}
if err := s.AllowGroup("mnt/storage", idxPath("/tenant"), "acme"); err != nil {
t.Fatalf("AllowGroup: %v", err)
}
if err := s.DenyGroup("mnt/storage", idxPath("/private"), "acme"); err != nil {
t.Fatalf("DenyGroup: %v", err)
}
rulesBefore := len(s.GetRulesForGroup("mnt/storage", "acme"))
if rulesBefore != 2 {
t.Fatalf("expected 2 rules before delete, got %d", rulesBefore)
}
s.SetSQLStore(failingGroupDeletePersister{sqlStore})
if err := s.DeleteGroup("acme"); err == nil {
t.Fatal("expected DeleteGroup to return the SQL error")
}
if got := s.GetGroupMembers()["acme"]; len(got) != 1 || got[0] != "alice" {
t.Fatalf("group should be restored after a failed delete, got %v", got)
}
if got := len(s.GetRulesForGroup("mnt/storage", "acme")); got != rulesBefore {
t.Fatalf("rules should be restored after a failed delete: want %d, got %d", rulesBefore, got)
}
// SQL was never changed.
sqlGroups, err := sqlStore.GetAllGroups()
if err != nil {
t.Fatal(err)
}
if _, ok := sqlGroups["acme"]; !ok {
t.Fatal("group row should still be in SQL")
}
sqlRules, err := sqlStore.GetAllAccessRules()
if err != nil {
t.Fatal(err)
}
if len(sqlRules["mnt/storage"]) != 2 {
t.Fatalf("rule rows should still be in SQL, got %v", sqlRules["mnt/storage"])
}
}
func TestDeleteGroup_RemovesGroupAndRuleRowsFromSQL(t *testing.T) {
setupTestSources()
s, _, sqlStore := createTestStorageWithSQL(t)
if err := s.SetGroupMembers("acme", []string{"alice"}); err != nil {
t.Fatalf("SetGroupMembers: %v", err)
}
if err := s.AllowGroup("mnt/storage", idxPath("/tenant"), "acme"); err != nil {
t.Fatalf("AllowGroup: %v", err)
}
if err := s.AllowUser("mnt/storage", idxPath("/tenant"), "alice"); err != nil {
t.Fatalf("AllowUser: %v", err)
}
if err := s.DeleteGroup("acme"); err != nil {
t.Fatalf("DeleteGroup: %v", err)
}
sqlGroups, err := sqlStore.GetAllGroups()
if err != nil {
t.Fatal(err)
}
if _, ok := sqlGroups["acme"]; ok {
t.Fatal("group row should be gone from SQL")
}
sqlRules, err := sqlStore.GetAllAccessRules()
if err != nil {
t.Fatal(err)
}
for _, rule := range sqlRules["mnt/storage"] {
if _, has := rule.Allow.Groups["acme"]; has {
t.Fatal("no stored rule should still reference the deleted group")
}
}
// The rule that also allowed a user must survive with only the group removed.
if len(sqlRules["mnt/storage"]) != 1 {
t.Fatalf("expected the user rule to remain, got %v", sqlRules["mnt/storage"])
}
}
func TestDeleteGroup_KeepsDenyAllRule(t *testing.T) {
setupTestSources()
s, _, sqlStore := createTestStorageWithSQL(t)
// "Deny everyone except group acme": a DenyAll rule whose only allow entry is the group.
if err := s.SetGroupMembers("acme", []string{"alice"}); err != nil {
t.Fatalf("SetGroupMembers: %v", err)
}
if err := s.DenyAll("mnt/storage", idxPath("/tenant")); err != nil {
t.Fatalf("DenyAll: %v", err)
}
if err := s.AllowGroup("mnt/storage", idxPath("/tenant"), "acme"); err != nil {
t.Fatalf("AllowGroup: %v", err)
}
if !s.Permitted("mnt/storage", idxPath("/tenant"), "alice") {
t.Fatal("alice should be permitted through the group before it is deleted")
}
if err := s.DeleteGroup("acme"); err != nil {
t.Fatalf("DeleteGroup: %v", err)
}
// Deleting the group must not remove the deny-all rule and open the path to everyone.
if s.Permitted("mnt/storage", idxPath("/tenant"), "alice") || s.Permitted("mnt/storage", idxPath("/tenant"), "bob") {
t.Fatal("path must stay denied for everyone after the allowed group is deleted")
}
rules := s.GetRulesForGroup("mnt/storage", "acme")
if len(rules) != 0 {
t.Fatalf("no rule should reference the deleted group, got %v", rules)
}
sqlRules, err := sqlStore.GetAllAccessRules()
if err != nil {
t.Fatal(err)
}
stored := 0
for _, r := range sqlRules["mnt/storage"] {
if r.DenyAll {
stored++
}
}
if stored != 1 {
t.Fatalf("expected the deny-all rule to remain stored in SQL, found %d", stored)
}
}
@@ -0,0 +1,174 @@
package access_test
import (
"errors"
"maps"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/access"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
)
type stubGroupSQL struct {
saveErr error
deleteErr error
}
func (s *stubGroupSQL) SaveAccessRule(string, string, *access.AccessRule) error { return nil }
func (s *stubGroupSQL) DeleteAccessRule(string, string) error { return nil }
func (s *stubGroupSQL) SaveGroup(string, access.StringSet) error {
return s.saveErr
}
func (s *stubGroupSQL) DeleteGroup(string) error { return s.deleteErr }
func (s *stubGroupSQL) DeleteGroupWithRules(string, []access.RuleUpsert, []access.RuleKey) error {
return s.deleteErr
}
func (s *stubGroupSQL) SaveRevokedToken(string, int64) error {
return nil
}
func (s *stubGroupSQL) PersistImmediateTokenRevocation(string) error { return nil }
func (s *stubGroupSQL) PersistTokenRetirement(string, int64, []string) error { return nil }
func (s *stubGroupSQL) DeleteRevokedToken(string) error { return nil }
func (s *stubGroupSQL) SaveHashedToken(string, uint64, bool, int64) error { return nil }
func (s *stubGroupSQL) DeleteHashedToken(string) error { return nil }
func (s *stubGroupSQL) DeleteHashedTokensByUserID(uint64) error {
return nil
}
type recordingGroupSQL struct {
groups map[string]access.StringSet
saveCalls int
failOn int
}
func (s *recordingGroupSQL) SaveAccessRule(string, string, *access.AccessRule) error { return nil }
func (s *recordingGroupSQL) DeleteAccessRule(string, string) error { return nil }
func (s *recordingGroupSQL) SaveGroup(name string, members access.StringSet) error {
if s.groups == nil {
s.groups = make(map[string]access.StringSet)
}
s.saveCalls++
if s.failOn > 0 && s.saveCalls == s.failOn {
return errors.New("save failed")
}
s.groups[name] = cloneStringSet(members)
return nil
}
func (s *recordingGroupSQL) DeleteGroup(name string) error {
if s.groups == nil {
s.groups = make(map[string]access.StringSet)
}
delete(s.groups, name)
return nil
}
func (s *recordingGroupSQL) DeleteGroupWithRules(name string, _ []access.RuleUpsert, _ []access.RuleKey) error {
return s.DeleteGroup(name)
}
func (s *recordingGroupSQL) SaveRevokedToken(string, int64) error { return nil }
func (s *recordingGroupSQL) PersistImmediateTokenRevocation(string) error { return nil }
func (s *recordingGroupSQL) PersistTokenRetirement(string, int64, []string) error { return nil }
func (s *recordingGroupSQL) DeleteRevokedToken(string) error { return nil }
func (s *recordingGroupSQL) SaveHashedToken(string, uint64, bool, int64) error { return nil }
func (s *recordingGroupSQL) DeleteHashedToken(string) error { return nil }
func (s *recordingGroupSQL) DeleteHashedTokensByUserID(uint64) error { return nil }
func cloneStringSet(src access.StringSet) access.StringSet {
if len(src) == 0 {
return nil
}
dst := make(access.StringSet, len(src))
maps.Copy(dst, src)
return dst
}
func TestAddUserToGroup_RollsBackOnSQLFailure(t *testing.T) {
userStore := users.NewStorage(nil)
store := access.NewStorage(userStore)
store.SetSQLStore(&stubGroupSQL{saveErr: errors.New("save failed")})
err := store.AddUserToGroup("editors", "alice")
if err == nil {
t.Fatal("expected SQL save error")
}
groups := store.GetUserGroups("alice")
if len(groups) != 0 {
t.Fatalf("expected in-memory rollback, alice groups = %#v", groups)
}
}
func TestRemoveUserFromGroup_RollsBackOnSQLFailure(t *testing.T) {
userStore := users.NewStorage(nil)
store := access.NewStorage(userStore)
if err := store.AddUserToGroup("editors", "alice"); err != nil {
t.Fatalf("seed group: %v", err)
}
store.SetSQLStore(&stubGroupSQL{saveErr: errors.New("save failed")})
err := store.RemoveUserFromGroup("editors", "alice")
if err == nil {
t.Fatal("expected SQL save error")
}
groups := store.GetUserGroups("alice")
if len(groups) != 1 || groups[0] != "editors" {
t.Fatalf("expected in-memory rollback, alice groups = %#v", groups)
}
}
func TestAllowGroup_ReturnsSQLSaveError(t *testing.T) {
setupTestSources()
store := access.NewStorage(users.NewStorage(nil))
store.SetSQLStore(&stubGroupSQL{saveErr: errors.New("save failed")})
err := store.AllowGroup("mnt/storage", idxPath("/tenant"), "acme")
if err == nil {
t.Fatal("expected SQL save error from AllowGroup")
}
}
func TestEnsureGroupExistsNL_RollsBackOnSQLFailure(t *testing.T) {
setupTestSources()
store := access.NewStorage(users.NewStorage(nil))
store.SetSQLStore(&stubGroupSQL{saveErr: errors.New("save failed")})
err := store.AllowGroup("mnt/storage", idxPath("/tenant"), "newgroup")
if err == nil {
t.Fatal("expected SQL save error")
}
if groups := store.GetAllGroups(); len(groups) != 0 {
t.Fatalf("expected no in-memory group after failed ensure, got %#v", groups)
}
}
func TestSyncUserGroups_RollsBackOnPartialPersistFailure(t *testing.T) {
userStore := users.NewStorage(nil)
store := access.NewStorage(userStore)
store.Groups["groupA"] = access.StringSet{"alice": {}}
rec := &recordingGroupSQL{
failOn: 2,
groups: map[string]access.StringSet{
"groupA": {"alice": {}},
},
}
store.SetSQLStore(rec)
err := store.SyncUserGroups("alice", []string{"groupB"})
if err == nil {
t.Fatal("expected partial persist failure")
}
groups := store.GetUserGroups("alice")
if len(groups) != 1 || groups[0] != "groupA" {
t.Fatalf("expected in-memory rollback to groupA only, got %#v", groups)
}
if _, ok := store.Groups["groupB"]; ok {
t.Fatal("groupB should not remain in memory after rollback")
}
if rec.saveCalls != 3 {
t.Fatalf("saveCalls = %d, want 3 (groupA persist, groupB fail, groupA restore)", rec.saveCalls)
}
if _, ok := rec.groups["groupA"]["alice"]; !ok {
t.Fatalf("expected SQL rollback for groupA, got %#v", rec.groups["groupA"])
}
if _, ok := rec.groups["groupB"]; ok {
t.Fatalf("expected SQL groupB to be absent after rollback, got %#v", rec.groups["groupB"])
}
}
@@ -0,0 +1,12 @@
package access
import (
"os"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/testutil"
)
func TestMain(m *testing.M) {
os.Exit(testutil.Main(m))
}
+40 -18
View File
@@ -5,6 +5,7 @@ import (
"testing"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/access"
"github.com/gtsteffaniak/filebrowser/backend/internal/utils"
)
@@ -15,7 +16,10 @@ func (failingRevokePersister) SaveAccessRule(string, string, *access.AccessRule)
func (failingRevokePersister) DeleteAccessRule(string, string) error { return nil }
func (failingRevokePersister) SaveGroup(string, access.StringSet) error { return nil }
func (failingRevokePersister) DeleteGroup(string) error { return nil }
func (failingRevokePersister) SaveRevokedToken(string, int64) error { return nil }
func (failingRevokePersister) DeleteGroupWithRules(string, []access.RuleUpsert, []access.RuleKey) error {
return nil
}
func (failingRevokePersister) SaveRevokedToken(string, int64) error { return nil }
func (failingRevokePersister) PersistImmediateTokenRevocation(string) error {
return errors.New("simulated revocation persistence failure")
}
@@ -23,7 +27,7 @@ func (failingRevokePersister) PersistTokenRetirement(string, int64, []string) er
return errors.New("simulated retirement persistence failure")
}
func (failingRevokePersister) DeleteRevokedToken(string) error { return nil }
func (failingRevokePersister) SaveHashedToken(string, uint64, bool) error { return nil }
func (failingRevokePersister) SaveHashedToken(string, uint64, bool, int64) error { return nil }
func (failingRevokePersister) DeleteHashedToken(string) error { return nil }
func (failingRevokePersister) DeleteHashedTokensByUserID(uint64) error { return nil }
@@ -109,25 +113,43 @@ func TestRevokeTokenIsImmediate(t *testing.T) {
}
}
func TestRetireTokenHonorsGraceWindow(t *testing.T) {
// signedTestJWT mints a bare HS256 JWT carrying only an exp claim. The registry
// only reads claims unverified, so any key works.
func signedTestJWT(t *testing.T, exp time.Time) string {
t.Helper()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{"exp": exp.Unix()})
tokenString, err := token.SignedString([]byte("test-key"))
if err != nil {
t.Fatal(err)
}
return tokenString
}
func TestExpiredTokenResolvesWithinGrace(t *testing.T) {
store, _ := createTestStorage(t)
if err := store.AddSessionToken("tok", 1); err != nil {
token := signedTestJWT(t, time.Now().Add(-time.Minute))
if err := store.AddSessionToken(token, 7); err != nil {
t.Fatal(err)
}
if err := store.RetireToken("tok"); err != nil {
t.Fatal(err)
info, ok := store.GetHashedTokenInfo(token)
if !ok || info.UserID != 7 || !info.IsSession {
t.Fatalf("recently expired token must still resolve within grace, got %+v ok=%v", info, ok)
}
if store.IsTokenRevoked("tok") {
t.Fatal("retired token must remain valid during the grace window")
}
if _, ok := store.GetHashedTokenInfo("tok"); !ok {
t.Fatal("retired token mapping must remain during the grace window")
}
// Simulate the grace window elapsing.
store.RevokedTokens[utils.HashSHA256("tok")] = time.Now().Add(-3 * time.Minute).Unix()
if !store.IsTokenRevoked("tok") {
t.Fatal("retired token must be revoked once the grace window elapses")
if info.ExpiresAt == 0 {
t.Fatal("expiry must be recorded from the token's exp claim")
}
}
func TestExpiredTokenPastGraceNeverRegistered(t *testing.T) {
store, _ := createTestStorage(t)
token := signedTestJWT(t, time.Now().Add(-access.BearerTokenGrace-time.Minute))
if err := store.AddSessionToken(token, 7); err != nil {
t.Fatal(err)
}
if _, ok := store.GetHashedTokenInfo(token); ok {
t.Fatal("token expired past grace must not resolve")
}
if _, ok := store.HashedTokens[utils.HashSHA256(token)]; ok {
t.Fatal("token expired past grace must not be registered")
}
}
@@ -24,6 +24,9 @@ const (
EventAccessUpdate EventType = "accessUpdate"
EventAccessCreate EventType = "accessCreate"
EventAccessDelete EventType = "accessDelete"
EventQuotaCreate EventType = "quotaCreate"
EventQuotaUpdate EventType = "quotaUpdate"
EventQuotaDelete EventType = "quotaDelete"
EventLogin EventType = "login"
EventLogout EventType = "logout"
EventSignup EventType = "signup"
@@ -54,6 +57,9 @@ var AllEventTypes = []EventType{
EventAccessUpdate,
EventAccessCreate,
EventAccessDelete,
EventQuotaCreate,
EventQuotaUpdate,
EventQuotaDelete,
EventLogin,
EventLogout,
EventSignup,
@@ -84,6 +90,13 @@ var AccessEventTypes = []EventType{
EventAccessDelete,
}
// QuotaEventTypes are folder quota mutations (scope=quotas).
var QuotaEventTypes = []EventType{
EventQuotaCreate,
EventQuotaUpdate,
EventQuotaDelete,
}
// ShareEventTypes are share lifecycle events (scope=shares). Share downloads are
// EventDownload rows with a non-empty details.shareHash.
var ShareEventTypes = []EventType{
@@ -108,13 +121,15 @@ func ResolveScopeEventTypes(scope string, explicit []EventType) ([]EventType, er
allowed = FileEventTypes
case "access":
allowed = AccessEventTypes
case "quotas":
allowed = QuotaEventTypes
case "shares":
if len(explicit) == 0 {
return nil, nil
}
allowed = ShareScopeEventTypes
default:
return nil, fmt.Errorf("scope must be all, files, access, or shares")
return nil, fmt.Errorf("scope must be all, files, access, quotas, or shares")
}
if len(explicit) == 0 {
return allowed, nil
@@ -144,6 +159,7 @@ func (e EventType) Valid() bool {
EventArchive, EventUnarchive,
EventShareCreate, EventShareUpdate, EventShareDelete,
EventUserCreate, EventUserUpdate, EventUserDelete, EventAccessUpdate, EventAccessCreate, EventAccessDelete,
EventQuotaCreate, EventQuotaUpdate, EventQuotaDelete,
EventLogin, EventLogout, EventSignup,
EventPasskeyRegister, EventPasskeyDelete,
EventTokenCreate, EventTokenDelete,
@@ -44,6 +44,18 @@ func TestResolveScopeEventTypes(t *testing.T) {
t.Fatalf("access default missing %q in %v", et, got)
}
}
got, err = ResolveScopeEventTypes("quotas", nil)
if err != nil {
t.Fatal(err)
}
if len(got) != len(QuotaEventTypes) {
t.Fatalf("quotas default: got %d types, want %d", len(got), len(QuotaEventTypes))
}
for _, et := range QuotaEventTypes {
if !containsEventType(got, et) {
t.Fatalf("quotas default missing %q in %v", et, got)
}
}
}
func containsEventType(list []EventType, target EventType) bool {
+91
View File
@@ -0,0 +1,91 @@
package quota
import (
"fmt"
"strings"
"github.com/gtsteffaniak/filebrowser/backend/pkg/indexing"
)
// MeasurementStatusReady means enforcement uses the configured meter.
const MeasurementStatusReady = "ready"
// MeasurementStatusAccountedFallback means index meter configured but accounted counter used.
const MeasurementStatusAccountedFallback = "accounted_fallback"
// IsIndexMeter reports whether meter counts usage from the search index.
func IsIndexMeter(meter string) bool {
return meter == MeterIndexSize || meter == MeterIndexScope
}
// IndexingEnabled reports whether folder sizes are available from the index on this source.
func IndexingEnabled(sourceName string) bool {
idx := indexing.GetIndex(sourceName)
if idx == nil {
return false
}
return !idx.Config.ResolvedRules.IndexingDisabled
}
// IndexSizeAvailable reports whether GetFolderSize succeeds for the quota root path.
func IndexSizeAvailable(sourceName, indexPath string) bool {
idx := indexing.GetIndex(sourceName)
if idx == nil || idx.Config.ResolvedRules.IndexingDisabled {
return false
}
path := normalizeMeterPath(indexPath)
_, ok := idx.GetFolderSizeForIndexPath(path)
return ok
}
// EffectiveMeter resolves runtime meter: accounted when indexing off, configured index when size known, else accounted fallback.
func EffectiveMeter(configuredMeter, sourceName, quotaRootPath string) string {
if configuredMeter == MeterAccounted || !IsIndexMeter(configuredMeter) {
return MeterAccounted
}
if !IndexingEnabled(sourceName) {
return MeterAccounted
}
if IndexSizeAvailable(sourceName, quotaRootPath) {
return configuredMeter
}
return MeterAccounted
}
// MeasurementStatus compares configured vs effective meter for API snapshots.
func MeasurementStatus(configuredMeter, effectiveMeter string) string {
if IsIndexMeter(configuredMeter) && effectiveMeter == MeterAccounted {
return MeasurementStatusAccountedFallback
}
return MeasurementStatusReady
}
// ValidateConfiguredMeter rejects index meters when indexing is disabled on the source.
func ValidateConfiguredMeter(sourceName, meter string) error {
meter = strings.TrimSpace(meter)
if meter == "" {
return nil
}
if meter == MeterAccounted {
return nil
}
if IsIndexMeter(meter) {
if !IndexingEnabled(sourceName) {
return fmt.Errorf("index meter not allowed when indexing is disabled on source %q", sourceName)
}
return nil
}
return fmt.Errorf("invalid quota meter %q", meter)
}
func normalizeMeterPath(path string) string {
path = strings.TrimSpace(path)
path = strings.TrimSuffix(path, "/")
if path == "" {
return "/"
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return path
}
@@ -0,0 +1,116 @@
package quota_test
import (
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/quota"
"github.com/gtsteffaniak/filebrowser/backend/pkg/indexing"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
)
func setupMeterTestSource(t *testing.T, name, path string, indexingDisabled bool) {
t.Helper()
prevSourceMap := settings.Config.Server.SourceMap
prevNameToSource := settings.Config.Server.NameToSource
t.Cleanup(func() {
indexing.ClearTestIndices()
settings.Config.Server.SourceMap = prevSourceMap
settings.Config.Server.NameToSource = prevNameToSource
})
indexing.SetTestIndex(name, path)
idx := indexing.GetIndex(name)
if idx == nil {
t.Fatal("expected test index")
}
idx.Config.ResolvedRules.IndexingDisabled = indexingDisabled
settings.Config.Server.SourceMap = map[string]*settings.Source{
path: {Path: path, Name: name},
}
settings.Config.Server.NameToSource = map[string]*settings.Source{
name: settings.Config.Server.SourceMap[path],
}
}
func TestValidateConfiguredMeter_IndexingDisabled(t *testing.T) {
root := t.TempDir()
name := "meter-src"
setupMeterTestSource(t, name, root, true)
if err := quota.ValidateConfiguredMeter(name, quota.MeterIndexSize); err == nil {
t.Fatal("expected error for index_size when indexing disabled")
}
if err := quota.ValidateConfiguredMeter(name, quota.MeterIndexScope); err == nil {
t.Fatal("expected error for index_scope when indexing disabled")
}
if err := quota.ValidateConfiguredMeter(name, quota.MeterAccounted); err != nil {
t.Fatalf("accounted should be allowed: %v", err)
}
}
func TestEffectiveMeter_IndexingDisabledForcesAccounted(t *testing.T) {
root := t.TempDir()
name := "meter-src2"
setupMeterTestSource(t, name, root, true)
got := quota.EffectiveMeter(quota.MeterIndexSize, name, "/")
if got != quota.MeterAccounted {
t.Fatalf("expected accounted, got %q", got)
}
}
func TestEffectiveMeter_ExplicitAccountedWhenIndexOn(t *testing.T) {
root := t.TempDir()
name := "meter-src3"
setupMeterTestSource(t, name, root, false)
idx := indexing.GetIndex(name)
idx.SetFolderSize("/", 1024)
got := quota.EffectiveMeter(quota.MeterAccounted, name, "/")
if got != quota.MeterAccounted {
t.Fatalf("expected accounted, got %q", got)
}
}
func TestEffectiveMeter_IndexWhenSizeAvailable(t *testing.T) {
root := t.TempDir()
name := "meter-src4"
setupMeterTestSource(t, name, root, false)
idx := indexing.GetIndex(name)
idx.SetFolderSize("/", 2048)
got := quota.EffectiveMeter(quota.MeterIndexSize, name, "/")
if got != quota.MeterIndexSize {
t.Fatalf("expected index_size, got %q", got)
}
status := quota.MeasurementStatus(quota.MeterIndexSize, got)
if status != quota.MeasurementStatusReady {
t.Fatalf("expected ready, got %q", status)
}
}
func TestEffectiveMeter_IndexPathTrailingSlash(t *testing.T) {
root := t.TempDir()
name := "meter-src6"
setupMeterTestSource(t, name, root, false)
idx := indexing.GetIndex(name)
idx.SetFolderSize("/projects/", 4096)
got := quota.EffectiveMeter(quota.MeterIndexSize, name, "/projects")
if got != quota.MeterIndexSize {
t.Fatalf("expected index_size for path without trailing slash, got %q", got)
}
}
func TestEffectiveMeter_RuntimeFallback(t *testing.T) {
root := t.TempDir()
name := "meter-src5"
setupMeterTestSource(t, name, root, false)
got := quota.EffectiveMeter(quota.MeterIndexScope, name, "/missing")
if got != quota.MeterAccounted {
t.Fatalf("expected accounted fallback, got %q", got)
}
status := quota.MeasurementStatus(quota.MeterIndexScope, got)
if status != quota.MeasurementStatusAccountedFallback {
t.Fatalf("expected accounted_fallback, got %q", status)
}
}
+43
View File
@@ -0,0 +1,43 @@
package quota
// Meter names for scope and folder quotas.
const (
MeterIndexScope = "index_scope"
MeterIndexSize = "index_size"
MeterAccounted = "accounted"
)
// FolderQuota is a path-bound storage cap with configurable meter.
type FolderQuota struct {
ID string `json:"id"`
Source string `json:"source"` // backend source path
Path string `json:"path"` // index path within source
UserID uint64 `json:"userId,omitempty"` // 0 = all users
LimitBytes int64 `json:"limitBytes"`
Meter string `json:"meter,omitempty"` // index_size | index_scope (indexed size) | accounted (tracked usage)
}
// Counter holds accounted usage for a quota_id.
type Counter struct {
QuotaID string `json:"quotaId"`
UsedBytes int64 `json:"usedBytes"`
ReservedBytes int64 `json:"reservedBytes"`
}
// Snapshot is a read model for API and admission checks.
type Snapshot struct {
QuotaID string `json:"quotaId"`
Kind string `json:"quotaKind"` // folder, scope, share
LimitBytes int64 `json:"limitBytes"`
UsedBytes int64 `json:"usedBytes"`
ReservedBytes int64 `json:"reservedBytes"`
Meter string `json:"meter,omitempty"` // effective meter (backward compatible)
ConfiguredMeter string `json:"configuredMeter,omitempty"`
EffectiveMeter string `json:"effectiveMeter,omitempty"`
MeasurementStatus string `json:"measurementStatus,omitempty"` // ready, accounted_fallback
}
// ShareQuotaID returns the counter key for a share quota.
func ShareQuotaID(hash string) string {
return "share:" + hash
}
+19 -2
View File
@@ -48,6 +48,7 @@ type ShareLimits struct {
PerUserDownloadLimit bool `json:"perUserDownloadLimit,omitempty"`
ExtractEmbeddedSubtitles bool `json:"extractEmbeddedSubtitles,omitempty"`
DownloadsLimit int `json:"downloadsLimit,omitempty"`
QuotaLimitBytes int64 `json:"quotaLimitBytes,omitempty"`
HideFileExt string `json:"hideFileExt,omitempty"` // show hidden files based on extensions in shares
Banner string `json:"banner,omitempty"`
SourceName string `json:"source,omitempty"` // source display name for API; backend path is Share.SourcePath
@@ -85,8 +86,11 @@ type ShareEditable struct {
type ShareFrontend struct {
ShareEditable
ShareColumns
Username string `json:"username,omitempty"`
PathExists bool `json:"pathExists,omitempty"`
Username string `json:"username,omitempty"`
PathExists bool `json:"pathExists,omitempty"`
QuotaUsedBytes int64 `json:"quotaUsedBytes,omitempty"`
QuotaReservedBytes int64 `json:"quotaReservedBytes,omitempty"`
QuotaAvailableBytes int64 `json:"quotaAvailableBytes,omitempty"`
}
// SharePostBody is POST /api/share JSON. Plaintext password is hashed to Share.PasswordHash before persist.
@@ -98,6 +102,19 @@ type SharePostBody struct {
Path string `json:"path,omitempty"`
}
// EditableFromShare returns the client-editable subset of a persisted share.
// ShareExpiryInput is omitted because expiration is stored in ShareColumns.Expire;
// callers merging partial updates must preserve Expire when expires/unit are absent.
func EditableFromShare(s *Share) ShareEditable {
if s == nil {
return ShareEditable{}
}
return ShareEditable{
FrontendShareInfo: s.FrontendShareInfo,
ShareLimits: s.ShareLimits,
}
}
// ApplyPostBodyUpdate copies client-editable fields onto link.
// Caller must preserve path, sourcePath, pinnedItems, version, download counters, and secrets.
func ApplyPostBodyUpdate(link *Share, req *SharePostBody, expire int64) {
+46 -93
View File
@@ -9,6 +9,7 @@ import (
"strings"
"time"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/sqlitebusy"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
"github.com/gtsteffaniak/filebrowser/backend/pkg/indexing/iteminfo"
"github.com/gtsteffaniak/go-logger/logger"
@@ -291,11 +292,12 @@ func (db *IndexDB) InsertItem(source, path string, info *iteminfo.FileInfo) erro
time.Now().Unix(),
)
if err != nil {
if !isBusyError(err) && !isTransactionError(err) {
if !sqlitebusy.IsBusyOrLocked(err) {
logger.Errorf("InsertItem failed for source=%s path=%s: %v", source, path, err)
}
return sqlitebusy.Wrap(err)
}
return err
return nil
}
// dbTransientOpAttempts is how many times to run a bulk DB op that may hit SQLITE_BUSY before giving up.
@@ -304,9 +306,7 @@ const dbTransientOpAttempts = 2
const dbTransientRetryBackoff = 50 * time.Millisecond
// BulkInsertItems inserts multiple items in a single transaction for a specific source.
// Database errors (busy/locked) are treated as soft failures - the filesystem is the source of truth.
// Returns nil on success or soft failure (busy/locked), error only on hard failures.
// Transient busy/locked errors retry once before giving up (see dbTransientOpAttempts).
// Transient busy/locked errors retry before giving up (see dbTransientOpAttempts); persistent busy returns an error.
func (db *IndexDB) BulkInsertItems(source string, items []*iteminfo.FileInfo) error {
if len(items) == 0 {
return nil
@@ -332,8 +332,10 @@ func (db *IndexDB) BulkInsertItems(source string, items []*iteminfo.FileInfo) er
continue
}
if transient {
logger.Warningf("[DB_TX] BulkInsertItems: still failing after %d attempts (%d items, %v) — batch dropped",
logger.Warningf("[DB_TX] BulkInsertItems: still failing after %d attempts (%d items, %v)",
dbTransientOpAttempts, len(items), time.Since(startTime))
return fmt.Errorf("bulk insert failed after %d attempts (%d items): %w",
dbTransientOpAttempts, len(items), sqlitebusy.ErrBusy)
}
return nil
}
@@ -345,7 +347,7 @@ func (db *IndexDB) BulkInsertItems(source string, items []*iteminfo.FileInfo) er
func (db *IndexDB) attemptBulkInsert(source string, items []*iteminfo.FileInfo) (success bool, transient bool, err error) {
tx, err := db.BeginTransaction()
if err != nil {
if isBusyError(err) || isTransactionError(err) {
if sqlitebusy.IsBusyOrLocked(err) {
db.mu.Unlock()
logger.Debugf("[DB_TX] BulkInsertItems: BeginTransaction failed (DB busy/locked): %v", err)
return false, true, nil
@@ -377,7 +379,7 @@ func (db *IndexDB) attemptBulkInsert(source string, items []*iteminfo.FileInfo)
last_updated = excluded.last_updated
`)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
if sqlitebusy.IsBusyOrLocked(err) {
logger.Debugf("[DB_TX] BulkInsertItems: Prepare busy/locked: %v", err)
return false, true, nil
}
@@ -402,7 +404,7 @@ func (db *IndexDB) attemptBulkInsert(source string, items []*iteminfo.FileInfo)
nowUnix,
)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
if sqlitebusy.IsBusyOrLocked(err) {
logger.Debugf("[DB_TX] BulkInsertItems: Exec busy/locked during insert: %v", err)
return false, true, nil
}
@@ -412,7 +414,7 @@ func (db *IndexDB) attemptBulkInsert(source string, items []*iteminfo.FileInfo)
}
if err := tx.Commit(); err != nil {
if isBusyError(err) || isTransactionError(err) {
if sqlitebusy.IsBusyOrLocked(err) {
logger.Debugf("[DB_TX] BulkInsertItems: Commit busy/locked: %v", err)
return false, true, nil
}
@@ -422,30 +424,8 @@ func (db *IndexDB) attemptBulkInsert(source string, items []*iteminfo.FileInfo)
return true, false, nil
}
// isBusyError checks if an error is SQLITE_BUSY (error code 5)
func isBusyError(err error) bool {
if err == nil {
return false
}
errStr := err.Error()
// SQLITE_BUSY is error code 5, and modernc.org/sqlite returns it as "database is locked (5)"
return strings.Contains(errStr, "database is locked") || strings.Contains(errStr, "SQLITE_BUSY") || strings.Contains(errStr, "(5)")
}
// isTransactionError checks if an error is related to nested transactions (error code 1)
func isTransactionError(err error) bool {
if err == nil {
return false
}
errStr := err.Error()
// SQLITE_ERROR for transaction issues is error code 1
return strings.Contains(errStr, "cannot start a transaction within a transaction") ||
strings.Contains(errStr, "cannot commit") ||
strings.Contains(errStr, "(1)")
}
// GetItem retrieves a single item by path for a specific source.
// Returns nil on database busy/lock errors (non-fatal).
// Busy/locked errors are returned so callers can fall back to the filesystem.
func (db *IndexDB) GetItem(source, path string) (*iteminfo.FileInfo, error) {
query := `
SELECT path, name, size, mod_time, type, is_dir, is_hidden, has_preview
@@ -454,19 +434,13 @@ func (db *IndexDB) GetItem(source, path string) (*iteminfo.FileInfo, error) {
row := db.QueryRow(query, source, path)
item, err := scanItem(row)
if err != nil {
// Soft failure: DB is busy or locked, return nil
// Caller will handle missing data by fetching from filesystem
if isBusyError(err) || isTransactionError(err) {
return nil, nil
}
return nil, err
return nil, sqlitebusy.Wrap(err)
}
return item, nil
}
// GetItemsByPaths retrieves multiple items by their paths in a single query for a specific source.
// This is more efficient than calling GetItem multiple times.
// Returns empty map on database busy/lock errors (non-fatal).
func (db *IndexDB) GetItemsByPaths(source string, paths []string) (map[string]*iteminfo.FileInfo, error) {
if len(paths) == 0 {
return make(map[string]*iteminfo.FileInfo), nil
@@ -488,12 +462,7 @@ func (db *IndexDB) GetItemsByPaths(source string, paths []string) (map[string]*i
rows, err := db.Query(query, args...)
if err != nil {
// Soft failure: DB is busy or locked, return empty map
// Caller will handle missing data (e.g., skip size updates, fetch from filesystem)
if isBusyError(err) || isTransactionError(err) {
return make(map[string]*iteminfo.FileInfo), nil
}
return nil, err
return nil, sqlitebusy.Wrap(err)
}
defer rows.Close()
@@ -519,9 +488,9 @@ func (db *IndexDB) GetDirectoryChildren(source, dirPath string) ([]*iteminfo.Fil
rows, err := db.Query(query, source, dirPath)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
logger.Warningf("[DB_TX] GetDirectoryChildren: DB busy/locked, skipping query")
return []*iteminfo.FileInfo{}, nil
if sqlitebusy.IsBusyOrLocked(err) {
logger.Warningf("[DB_TX] GetDirectoryChildren: DB busy/locked: %v", err)
return nil, sqlitebusy.Wrap(err)
}
logger.Errorf("GetDirectoryChildren: Query failed for source=%s, parent_path=%s, error=%v", source, dirPath, err)
return nil, err
@@ -671,9 +640,9 @@ func (db *IndexDB) DeleteStaleFolders(source string, pathPrefix string, scanStar
}
if err != nil {
if isBusyError(err) || isTransactionError(err) {
logger.Debugf("[DB_MAINTENANCE] DeleteStaleEntriesQuick Phase 1: DB busy, skipping cleanup")
return totalDeleted, nil
if sqlitebusy.IsBusyOrLocked(err) {
logger.Debugf("[DB_MAINTENANCE] DeleteStaleEntriesQuick Phase 1: DB busy: %v", err)
return totalDeleted, sqlitebusy.Wrap(err)
}
return totalDeleted, err
}
@@ -738,9 +707,9 @@ func (db *IndexDB) DeleteStaleFilesInDirs(source string, updatedDirs []string, s
result, err := db.Exec(query, queryArgs...)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
logger.Debugf("[DB_MAINTENANCE] DeleteStaleFilesInDirs: DB busy, skipping batch")
break // Skip this batch and continue with next
if sqlitebusy.IsBusyOrLocked(err) {
logger.Debugf("[DB_MAINTENANCE] DeleteStaleFilesInDirs: DB busy: %v", err)
return totalDeleted, sqlitebusy.Wrap(err)
}
return totalDeleted, err
}
@@ -802,9 +771,9 @@ func (db *IndexDB) DeleteStaleEntries(source string, pathPrefix string, scanStar
}
if err != nil {
if isBusyError(err) || isTransactionError(err) {
logger.Debugf("[DB_MAINTENANCE] DeleteStaleEntries: DB busy, skipping cleanup")
return totalDeleted, nil
if sqlitebusy.IsBusyOrLocked(err) {
logger.Debugf("[DB_MAINTENANCE] DeleteStaleEntries: DB busy: %v", err)
return totalDeleted, sqlitebusy.Wrap(err)
}
return totalDeleted, err
}
@@ -832,9 +801,9 @@ func (db *IndexDB) DeleteStaleItemsOlderThan(olderThan time.Duration) (int, erro
result, err := db.Exec(query, cutoffTime)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
logger.Debugf("[DB_MAINTENANCE] DeleteStaleItemsOlderThan: DB busy, skipping cleanup")
return 0, nil
if sqlitebusy.IsBusyOrLocked(err) {
logger.Debugf("[DB_MAINTENANCE] DeleteStaleItemsOlderThan: DB busy: %v", err)
return 0, sqlitebusy.Wrap(err)
}
return 0, err
}
@@ -859,8 +828,8 @@ func (db *IndexDB) GetRecursiveCount(source string, pathPrefix string) (dirs uin
var dirCount, fileCount sql.NullInt64
err = db.QueryRow(query, source, pathPrefix, nextPrefix).Scan(&dirCount, &fileCount)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
return 0, 0, nil
if sqlitebusy.IsBusyOrLocked(err) {
return 0, 0, sqlitebusy.Wrap(err)
}
return 0, 0, err
}
@@ -883,8 +852,8 @@ func (db *IndexDB) GetDirectFileCount(source string, pathPrefix string) (files u
var fileCount sql.NullInt64
err = db.QueryRow(query, source, pathPrefix).Scan(&fileCount)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
return 0, nil
if sqlitebusy.IsBusyOrLocked(err) {
return 0, sqlitebusy.Wrap(err)
}
return 0, err
}
@@ -920,9 +889,8 @@ func (db *IndexDB) GetTypeGroupsForSize(source string, size int64, pathPrefix st
rows, err := db.Query(query, args...)
if err != nil {
// Soft failure: DB is busy or locked, return empty results
if isBusyError(err) || isTransactionError(err) {
return []string{}, nil
if sqlitebusy.IsBusyOrLocked(err) {
return nil, sqlitebusy.Wrap(err)
}
return nil, err
}
@@ -976,9 +944,8 @@ func (db *IndexDB) GetFilesForMultipleSizes(source string, sizes []int64, pathPr
rows, err := db.Query(query, args...)
if err != nil {
// Soft failure: DB is busy or locked, return empty results
if isBusyError(err) || isTransactionError(err) {
return make(map[int64][]*iteminfo.FileInfo), nil
if sqlitebusy.IsBusyOrLocked(err) {
return nil, sqlitebusy.Wrap(err)
}
return nil, err
}
@@ -1008,10 +975,7 @@ func (db *IndexDB) GetAllDirectories(source string) ([]string, error) {
rows, err := db.Query(query, source)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
return []string{}, nil
}
return nil, err
return nil, sqlitebusy.Wrap(err)
}
defer rows.Close()
@@ -1071,16 +1035,17 @@ func (db *IndexDB) UpdateFolderSizesIfChanged(source string, pathSizes map[strin
for attempt := 1; attempt <= dbTransientOpAttempts; attempt++ {
result, err := db.Exec(query, args...)
if err != nil {
if isBusyError(err) || isTransactionError(err) {
if sqlitebusy.IsBusyOrLocked(err) {
if attempt < dbTransientOpAttempts {
logger.Warningf("[DB_TX] UpdateFolderSizesIfChanged: busy/locked (attempt %d/%d), retrying after %v: %v",
attempt, dbTransientOpAttempts, dbTransientRetryBackoff, err)
time.Sleep(dbTransientRetryBackoff)
continue
}
logger.Warningf("[DB_TX] UpdateFolderSizesIfChanged: still busy after %d attempts (%d paths, %v) — skipped",
logger.Warningf("[DB_TX] UpdateFolderSizesIfChanged: still busy after %d attempts (%d paths, %v)",
dbTransientOpAttempts, len(pathSizes), time.Since(startTime))
return 0, nil
return 0, fmt.Errorf("update folder sizes failed after %d attempts (%d paths): %w",
dbTransientOpAttempts, len(pathSizes), sqlitebusy.ErrBusy)
}
return 0, err
}
@@ -1148,11 +1113,7 @@ func (db *IndexDB) GetFilesBySizeAndType(source string, size int64, fileType str
rows, err := db.Query(query, args...)
if err != nil {
// Soft failure: DB is busy or locked, return empty results
if isBusyError(err) || isTransactionError(err) {
return []*iteminfo.FileInfo{}, nil
}
return nil, err
return nil, sqlitebusy.Wrap(err)
}
defer rows.Close()
@@ -1194,11 +1155,7 @@ func (db *IndexDB) GetFilesBySize(source string, size int64, pathPrefix string)
rows, err := db.Query(query, args...)
if err != nil {
// Soft failure: DB is busy or locked, return empty slice
if isBusyError(err) || isTransactionError(err) {
return []*iteminfo.FileInfo{}, nil
}
return nil, err
return nil, sqlitebusy.Wrap(err)
}
defer rows.Close()
@@ -1244,11 +1201,7 @@ func (db *IndexDB) GetSizeGroupsForDuplicates(source string, minSize int64, path
rows, err := db.Query(query, args...)
if err != nil {
// Soft failure: DB is busy or locked, return empty results
if isBusyError(err) || isTransactionError(err) {
return []int64{}, make(map[int64]int), nil
}
return nil, nil, err
return nil, nil, sqlitebusy.Wrap(err)
}
defer rows.Close()
+5 -3
View File
@@ -9,6 +9,7 @@ import (
"time"
"github.com/gtsteffaniak/filebrowser/backend/internal/adapters/fs/fileutils"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/sqlitebusy"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
"github.com/gtsteffaniak/go-logger/logger"
// SQLite driver is imported in driver_cgo.go or driver_nocgo.go based on build tags
@@ -225,9 +226,10 @@ func NewTempDB(id string, config ...*TempDBConfig) (*TempDB, error) {
tmpFile.Close()
}
// Open SQLite database with basic connection string
// Driver is selected at compile time: "sqlite3" (CGO) or "sqlite" (pure Go)
db, err := sql.Open(SqliteDriver, tmpPath)
// Open SQLite database with basic connection string.
// Driver is selected at compile time: "sqlite3" (CGO) or "sqlite" (pure Go).
// busy_timeout is set in the DSN so every pooled connection honors it.
db, err := sql.Open(SqliteDriver, sqlitebusy.WithBusyTimeout(tmpPath))
if err != nil {
os.Remove(tmpPath)
return nil, fmt.Errorf("failed to open SQLite database: %w", err)
+37
View File
@@ -101,6 +101,43 @@ func (s *SQLStore) SaveGroup(name string, members access.StringSet) error {
return nil
}
// DeleteGroupWithRules deletes a group and applies access rule row changes in a single
// transaction. If any statement fails nothing is written. A group row that is already
// absent is not an error, so the call is safe to retry.
func (s *SQLStore) DeleteGroupWithRules(name string, save []access.RuleUpsert, remove []access.RuleKey) error {
tx, err := s.BeginTx()
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
}
rollback := func(cause error) error {
if rbErr := tx.Rollback(); rbErr != nil {
return fmt.Errorf("%w (rollback also failed: %v)", cause, rbErr)
}
return cause
}
for _, u := range save {
ruleDataJSON, err := json.Marshal(u.Rule)
if err != nil {
return rollback(fmt.Errorf("failed to marshal rule data: %w", err))
}
if _, err := tx.Exec(`INSERT OR REPLACE INTO access_rules (source, path, rule_data) VALUES (?, ?, ?)`, u.Source, u.Path, ruleDataJSON); err != nil {
return rollback(fmt.Errorf("failed to save access rule: %w", err))
}
}
for _, k := range remove {
if _, err := tx.Exec(`DELETE FROM access_rules WHERE source = ? AND path = ?`, k.Source, k.Path); err != nil {
return rollback(fmt.Errorf("failed to delete access rule: %w", err))
}
}
if _, err := tx.Exec(`DELETE FROM groups WHERE name = ?`, name); err != nil {
return rollback(fmt.Errorf("failed to delete group: %w", err))
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("failed to commit group delete: %w", err)
}
return nil
}
// DeleteGroup deletes a group by name
func (s *SQLStore) DeleteGroup(name string) error {
query := `DELETE FROM groups WHERE name = ?`
@@ -0,0 +1,98 @@
package sqldb
import (
"path/filepath"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/access"
)
// newGroupTestStore opens a fresh SQLStore in a temp dir.
func newGroupTestStore(t *testing.T) *SQLStore {
t.Helper()
store, _, err := NewSQLStore(filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatalf("failed to create SQL store: %v", err)
}
t.Cleanup(func() { store.Close() })
return store
}
// groupRule returns a rule that allows the given group.
func groupRule(group string) *access.AccessRule {
return &access.AccessRule{Allow: access.RuleSet{
Users: access.StringSet{},
Groups: access.StringSet{group: {}},
}}
}
func TestDeleteGroupWithRules_CommitsAllChanges(t *testing.T) {
store := newGroupTestStore(t)
keep := access.RuleKey{Source: "src", Path: "/keep"}
drop := access.RuleKey{Source: "src", Path: "/drop"}
if err := store.SaveGroup("acme", access.StringSet{"alice": {}}); err != nil {
t.Fatal(err)
}
if err := store.SaveAccessRule(keep.Source, keep.Path, groupRule("acme")); err != nil {
t.Fatal(err)
}
if err := store.SaveAccessRule(drop.Source, drop.Path, groupRule("acme")); err != nil {
t.Fatal(err)
}
// keep is rewritten (now allows "other"), drop is removed, the group row is deleted.
err := store.DeleteGroupWithRules("acme", []access.RuleUpsert{{RuleKey: keep, Rule: groupRule("other")}}, []access.RuleKey{drop})
if err != nil {
t.Fatalf("DeleteGroupWithRules: %v", err)
}
groups, err := store.GetAllGroups()
if err != nil {
t.Fatal(err)
}
if _, ok := groups["acme"]; ok {
t.Fatal("group row should be deleted")
}
rules, err := store.GetAllAccessRules()
if err != nil {
t.Fatal(err)
}
if _, ok := rules["src"]["/drop"]; ok {
t.Fatal("dropped rule row should be deleted")
}
kept, ok := rules["src"]["/keep"]
if !ok {
t.Fatal("kept rule row should still exist")
}
if _, has := kept.Allow.Groups["other"]; !has {
t.Fatalf("kept rule should have been rewritten, got %+v", kept.Allow.Groups)
}
// Deleting again (group row already absent) is not an error.
if err := store.DeleteGroupWithRules("acme", nil, nil); err != nil {
t.Fatalf("repeat delete should succeed: %v", err)
}
}
func TestDeleteGroupWithRules_RollsBackOnFailure(t *testing.T) {
store := newGroupTestStore(t)
if err := store.SaveGroup("acme", access.StringSet{"alice": {}}); err != nil {
t.Fatal(err)
}
// Make the rule statements fail so the transaction has to roll back.
if _, err := store.db.Exec(`DROP TABLE access_rules`); err != nil {
t.Fatal(err)
}
err := store.DeleteGroupWithRules("acme", []access.RuleUpsert{{RuleKey: access.RuleKey{Source: "src", Path: "/x"}, Rule: groupRule("other")}}, nil)
if err == nil {
t.Fatal("expected an error when the rule write fails")
}
groups, gErr := store.GetAllGroups()
if gErr != nil {
t.Fatal(gErr)
}
if _, ok := groups["acme"]; !ok {
t.Fatal("group row must survive a failed transaction")
}
}
@@ -0,0 +1,12 @@
package sqldb
import (
"os"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/testutil"
)
func TestMain(m *testing.M) {
os.Exit(testutil.Main(m))
}
+91 -2
View File
@@ -6,7 +6,7 @@ import (
)
// currentSchemaVersion is the SQLite schema marker for this codebase.
const currentSchemaVersion = 3
const currentSchemaVersion = 4
// Schema creates all tables for the SQLite database
func createSchema(db *sql.DB) error {
@@ -78,7 +78,8 @@ func createSchema(db *sql.DB) error {
CREATE TABLE IF NOT EXISTS hashed_tokens (
token_hash TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
is_session INTEGER NOT NULL DEFAULT 0
is_session INTEGER NOT NULL DEFAULT 0,
expires_at INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_hashed_tokens_user_id ON hashed_tokens(user_id);
@@ -123,6 +124,28 @@ func createSchema(db *sql.DB) error {
CREATE INDEX IF NOT EXISTS idx_activity_user_created ON activity_log(user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_activity_event_created ON activity_log(event_type, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_activity_user_event_created ON activity_log(user_id, event_type, created_at DESC);
-- Folder quotas (path-bound caps; meter: index_size | accounted)
CREATE TABLE IF NOT EXISTS quotas (
id TEXT PRIMARY KEY NOT NULL,
source TEXT NOT NULL,
path TEXT NOT NULL,
user_id TEXT,
limit_bytes INTEGER NOT NULL DEFAULT 0,
meter TEXT NOT NULL DEFAULT 'index_size',
created_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_quotas_source ON quotas(source);
CREATE INDEX IF NOT EXISTS idx_quotas_source_path ON quotas(source, path);
-- Accounted usage counters (scope, share, optional folder accounted in future)
CREATE TABLE IF NOT EXISTS quota_counters (
quota_id TEXT PRIMARY KEY NOT NULL,
used_bytes INTEGER NOT NULL DEFAULT 0,
reserved_bytes INTEGER NOT NULL DEFAULT 0,
version INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL
);
`
_, err := db.Exec(schema)
@@ -183,6 +206,9 @@ func runMigrations(db *sql.DB, fromVersion int) error {
case 1:
// Canonical schema is createSchema + Bolt import; no step migrations.
case 2:
if err := migrateV2(db); err != nil {
return err
}
if err := normalizeLegacyShareTokens(db); err != nil {
return err
}
@@ -190,6 +216,10 @@ func runMigrations(db *sql.DB, fromVersion int) error {
if err := addHashedTokenSessionColumn(db); err != nil {
return err
}
case 4:
if err := addHashedTokenExpiryColumn(db); err != nil {
return err
}
default:
return fmt.Errorf("unknown schema version: %d", v)
}
@@ -204,6 +234,48 @@ func runMigrations(db *sql.DB, fromVersion int) error {
return nil
}
func migrateV2(db *sql.DB) error {
schema := `
CREATE TABLE IF NOT EXISTS quotas (
id TEXT PRIMARY KEY NOT NULL,
source TEXT NOT NULL,
path TEXT NOT NULL,
user_id TEXT,
limit_bytes INTEGER NOT NULL DEFAULT 0,
meter TEXT NOT NULL DEFAULT 'index_size',
created_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_quotas_source ON quotas(source);
CREATE INDEX IF NOT EXISTS idx_quotas_source_path ON quotas(source, path);
CREATE TABLE IF NOT EXISTS quota_counters (
quota_id TEXT PRIMARY KEY NOT NULL,
used_bytes INTEGER NOT NULL DEFAULT 0,
reserved_bytes INTEGER NOT NULL DEFAULT 0,
version INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL
);
`
if _, err := db.Exec(schema); err != nil {
return fmt.Errorf("migrate v2: %w", err)
}
return migrateV2AddMeterColumn(db)
}
func migrateV2AddMeterColumn(db *sql.DB) error {
var count int
if err := db.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('quotas') WHERE name = 'meter'`).Scan(&count); err != nil {
return fmt.Errorf("migrate v2 meter column check: %w", err)
}
if count > 0 {
return nil
}
if _, err := db.Exec(`ALTER TABLE quotas ADD COLUMN meter TEXT NOT NULL DEFAULT 'index_size'`); err != nil {
return fmt.Errorf("migrate v2 add meter column: %w", err)
}
return nil
}
// addHashedTokenSessionColumn adds the is_session flag to pre-existing
// hashed_tokens tables. Existing rows are treated as non-session (API) tokens.
func addHashedTokenSessionColumn(db *sql.DB) error {
@@ -220,6 +292,23 @@ func addHashedTokenSessionColumn(db *sql.DB) error {
return nil
}
// addHashedTokenExpiryColumn adds the expires_at timestamp to pre-existing
// hashed_tokens tables. Existing rows get 0 (unknown expiry) and are left
// untouched; expiry is recorded for newly registered tokens.
func addHashedTokenExpiryColumn(db *sql.DB) error {
hasColumn, err := tableHasColumn(db, "hashed_tokens", "expires_at")
if err != nil {
return err
}
if hasColumn {
return nil
}
if _, err := db.Exec(`ALTER TABLE hashed_tokens ADD COLUMN expires_at INTEGER NOT NULL DEFAULT 0`); err != nil {
return fmt.Errorf("failed to add hashed_tokens.expires_at: %w", err)
}
return nil
}
// tableHasColumn reports whether table/column exists in the current schema.
func tableHasColumn(db *sql.DB, table, column string) (bool, error) {
rows, err := db.Query(fmt.Sprintf("PRAGMA table_info(%s)", table))
+221
View File
@@ -0,0 +1,221 @@
package sqldb
import (
"database/sql"
"fmt"
"strconv"
"strings"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/quota"
)
func scanQuotaUserID(s string, dest *uint64) error {
if s == "" {
*dest = 0
return nil
}
u, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return fmt.Errorf("parse quota user_id: %w", err)
}
*dest = u
return nil
}
func quotaUserIDDB(id uint64) string {
if id == 0 {
return ""
}
return strconv.FormatUint(id, 10)
}
// GetAllFolderQuotas loads every folder quota row.
func (s *SQLStore) GetAllFolderQuotas() ([]quota.FolderQuota, error) {
rows, err := s.db.Query(`SELECT id, source, path, user_id, limit_bytes, meter FROM quotas`)
if err != nil {
return nil, fmt.Errorf("query quotas: %w", err)
}
defer rows.Close()
var out []quota.FolderQuota
for rows.Next() {
var q quota.FolderQuota
var userIDStr string
if err := rows.Scan(&q.ID, &q.Source, &q.Path, &userIDStr, &q.LimitBytes, &q.Meter); err != nil {
return nil, fmt.Errorf("scan quota: %w", err)
}
if err := scanQuotaUserID(userIDStr, &q.UserID); err != nil {
return nil, err
}
out = append(out, q)
}
return out, rows.Err()
}
// GetFolderQuotaByID returns one folder quota.
func (s *SQLStore) GetFolderQuotaByID(id string) (*quota.FolderQuota, error) {
var q quota.FolderQuota
var userIDStr string
err := s.db.QueryRow(`SELECT id, source, path, user_id, limit_bytes, meter FROM quotas WHERE id = ?`, id).
Scan(&q.ID, &q.Source, &q.Path, &userIDStr, &q.LimitBytes, &q.Meter)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("quota not found")
}
if err != nil {
return nil, fmt.Errorf("get quota: %w", err)
}
if err := scanQuotaUserID(userIDStr, &q.UserID); err != nil {
return nil, err
}
return &q, nil
}
// GetFolderQuotasForSourcePath returns quotas for a source and optional path prefix.
func (s *SQLStore) GetFolderQuotasForSourcePath(source, path string) ([]quota.FolderQuota, error) {
path = strings.TrimSuffix(path, "/")
query := `SELECT id, source, path, user_id, limit_bytes, meter FROM quotas WHERE source = ?`
args := []interface{}{source}
if path != "" {
query += ` AND (path = ? OR path LIKE ?)`
args = append(args, path, path+"/%")
}
rows, err := s.db.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("query quotas for source: %w", err)
}
defer rows.Close()
var out []quota.FolderQuota
for rows.Next() {
var q quota.FolderQuota
var userIDStr string
if err := rows.Scan(&q.ID, &q.Source, &q.Path, &userIDStr, &q.LimitBytes, &q.Meter); err != nil {
return nil, fmt.Errorf("scan quota: %w", err)
}
if err := scanQuotaUserID(userIDStr, &q.UserID); err != nil {
return nil, err
}
out = append(out, q)
}
return out, rows.Err()
}
// SaveFolderQuota inserts or updates a folder quota.
func (s *SQLStore) SaveFolderQuota(q *quota.FolderQuota) error {
meter := q.Meter
if meter == "" {
meter = quota.MeterIndexSize
}
_, err := s.db.Exec(
`INSERT INTO quotas (id, source, path, user_id, limit_bytes, meter, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET source=excluded.source, path=excluded.path, user_id=excluded.user_id, limit_bytes=excluded.limit_bytes, meter=excluded.meter`,
q.ID, q.Source, q.Path, quotaUserIDDB(q.UserID), q.LimitBytes, meter, currentTimestamp(),
)
if err != nil {
return fmt.Errorf("save quota: %w", err)
}
return nil
}
// DeleteFolderQuota removes a folder quota and its counter.
func (s *SQLStore) DeleteFolderQuota(id string) error {
if _, err := s.db.Exec(`DELETE FROM quotas WHERE id = ?`, id); err != nil {
return fmt.Errorf("delete quota: %w", err)
}
if _, err := s.db.Exec(`DELETE FROM quota_counters WHERE quota_id = ?`, id); err != nil {
return fmt.Errorf("delete quota counter: %w", err)
}
return nil
}
// GetAllQuotaCounters loads all quota counters.
func (s *SQLStore) GetAllQuotaCounters() ([]quota.Counter, error) {
rows, err := s.db.Query(`SELECT quota_id, used_bytes, reserved_bytes FROM quota_counters`)
if err != nil {
return nil, fmt.Errorf("query quota counters: %w", err)
}
defer rows.Close()
var out []quota.Counter
for rows.Next() {
var c quota.Counter
if err := rows.Scan(&c.QuotaID, &c.UsedBytes, &c.ReservedBytes); err != nil {
return nil, fmt.Errorf("scan quota counter: %w", err)
}
out = append(out, c)
}
return out, rows.Err()
}
// GetQuotaCounter returns one counter row.
func (s *SQLStore) GetQuotaCounter(quotaID string) (*quota.Counter, error) {
var c quota.Counter
err := s.db.QueryRow(`SELECT quota_id, used_bytes, reserved_bytes FROM quota_counters WHERE quota_id = ?`, quotaID).
Scan(&c.QuotaID, &c.UsedBytes, &c.ReservedBytes)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("quota counter not found")
}
if err != nil {
return nil, fmt.Errorf("get quota counter: %w", err)
}
return &c, nil
}
// EnsureQuotaCounter creates a counter row if missing.
func (s *SQLStore) EnsureQuotaCounter(quotaID string) error {
_, err := s.db.Exec(
`INSERT INTO quota_counters (quota_id, used_bytes, reserved_bytes, version, updated_at)
VALUES (?, 0, 0, 0, ?)
ON CONFLICT(quota_id) DO NOTHING`,
quotaID, currentTimestamp(),
)
if err != nil {
return fmt.Errorf("ensure quota counter: %w", err)
}
return nil
}
// DeleteQuotaCounter removes a counter row.
func (s *SQLStore) DeleteQuotaCounter(quotaID string) error {
_, err := s.db.Exec(`DELETE FROM quota_counters WHERE quota_id = ?`, quotaID)
if err != nil {
return fmt.Errorf("delete quota counter: %w", err)
}
return nil
}
// BulkUpsertQuotaCounters persists dirty counters.
func (s *SQLStore) BulkUpsertQuotaCounters(counters []quota.Counter) error {
if len(counters) == 0 {
return nil
}
tx, err := s.db.Begin()
if err != nil {
return fmt.Errorf("begin quota counter tx: %w", err)
}
defer func() {
_ = tx.Rollback()
}()
stmt, err := tx.Prepare(
`INSERT INTO quota_counters (quota_id, used_bytes, reserved_bytes, version, updated_at)
VALUES (?, ?, ?, 0, ?)
ON CONFLICT(quota_id) DO UPDATE SET
used_bytes = excluded.used_bytes,
reserved_bytes = excluded.reserved_bytes,
updated_at = excluded.updated_at`,
)
if err != nil {
return fmt.Errorf("prepare quota counter upsert: %w", err)
}
defer stmt.Close()
now := currentTimestamp()
for _, c := range counters {
if _, err := stmt.Exec(c.QuotaID, c.UsedBytes, c.ReservedBytes, now); err != nil {
return fmt.Errorf("upsert quota counter %s: %w", c.QuotaID, err)
}
}
return tx.Commit()
}
+22 -6
View File
@@ -32,20 +32,36 @@ func (s *SQLStore) GetSetting(key string) ([]byte, error) {
// SaveSetting inserts or updates a setting
func (s *SQLStore) SaveSetting(key string, value interface{}) error {
query := `INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)`
if err := s.saveSettingQuery(s.db.Exec, query, key, value); err != nil {
return fmt.Errorf("failed to save setting: %w", err)
}
return nil
}
type settingExec func(query string, args ...interface{}) (sql.Result, error)
func (s *SQLStore) saveSettingQuery(exec settingExec, query, key string, value interface{}) error {
valueJSON, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("failed to marshal setting value: %w", err)
}
query := `INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)`
_, err = s.db.Exec(query, key, valueJSON)
if err != nil {
return fmt.Errorf("failed to save setting: %w", err)
if _, err := exec(query, key, valueJSON); err != nil {
return err
}
return nil
}
func (s *SQLStore) saveSettingInTx(tx *sql.Tx, key string, value interface{}) error {
query := `INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)`
return s.saveSettingQuery(tx.Exec, query, key, value)
}
// SaveSettingTx inserts or updates a setting within an existing transaction.
func (s *SQLStore) SaveSettingTx(tx *sql.Tx, key string, value interface{}) error {
return s.saveSettingInTx(tx, key, value)
}
// DeleteSetting deletes a setting by key
func (s *SQLStore) DeleteSetting(key string) error {
query := `DELETE FROM settings WHERE key = ?`
+8 -2
View File
@@ -7,6 +7,7 @@ import (
"path/filepath"
"time"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/sqlitebusy"
"github.com/gtsteffaniak/go-logger/logger"
// SQLite driver is imported in driver_cgo.go or driver_nocgo.go based on build tags
)
@@ -38,8 +39,13 @@ func NewSQLStoreWithOptions(dbPath string, opts NewSQLStoreOpts) (*SQLStore, boo
return nil, existingDb, fmt.Errorf("failed to create database directory: %w", err)
}
// Open SQLite database with the appropriate driver
db, err := sql.Open(SqliteDriver, fmt.Sprintf("file:%s?cache=shared&mode=rwc&_journal_mode=WAL", dbPath))
// Open SQLite database with the appropriate driver.
// busy_timeout is set in the DSN so every pooled connection honors it.
// Shared cache is intentionally not used: its table-lock conflicts surface as
// SQLITE_LOCKED_SHAREDCACHE, which busy_timeout does not wait on, and SQLite
// discourages the mode. WAL already provides concurrent readers.
dsn := fmt.Sprintf("file:%s?mode=rwc&_journal_mode=WAL", dbPath)
db, err := sql.Open(SqliteDriver, sqlitebusy.WithBusyTimeout(dsn))
if err != nil {
return nil, existingDb, fmt.Errorf("failed to open database: %w", err)
}
+9 -6
View File
@@ -41,6 +41,7 @@ type sqlExecutor interface {
type HashedTokenRecord struct {
UserID uint64
IsSession bool
ExpiresAt int64 // unix expiry from the token's exp claim; 0 = unknown/none
}
// PersistImmediateTokenRevocation records an immediate revocation and removes the
@@ -145,9 +146,10 @@ func (s *SQLStore) DeleteRevokedToken(tokenHash string) error {
// SaveHashedToken saves a token hash to owner user_id mapping (decimal text).
// isSession records that the bearer token is a FileBrowser session token rather
// than a named API token, so auth can reject non-session tokens without caps.
func (s *SQLStore) SaveHashedToken(tokenHash string, userID uint64, isSession bool) error {
query := `INSERT OR REPLACE INTO hashed_tokens (token_hash, user_id, is_session) VALUES (?, ?, ?)`
_, err := s.db.Exec(query, tokenHash, strconv.FormatUint(userID, 10), isSession)
// expiresAt is the token's exp claim as unix time; 0 means no expiry.
func (s *SQLStore) SaveHashedToken(tokenHash string, userID uint64, isSession bool, expiresAt int64) error {
query := `INSERT OR REPLACE INTO hashed_tokens (token_hash, user_id, is_session, expires_at) VALUES (?, ?, ?, ?)`
_, err := s.db.Exec(query, tokenHash, strconv.FormatUint(userID, 10), isSession, expiresAt)
if err != nil {
return fmt.Errorf("failed to save hashed token: %w", err)
}
@@ -170,7 +172,7 @@ func (s *SQLStore) GetUserIDByTokenHash(tokenHash string) (uint64, error) {
// GetAllHashedTokens retrieves all token hash → owner mappings.
func (s *SQLStore) GetAllHashedTokens() (map[string]HashedTokenRecord, error) {
query := `SELECT token_hash, user_id, is_session FROM hashed_tokens`
query := `SELECT token_hash, user_id, is_session, expires_at FROM hashed_tokens`
rows, err := s.db.Query(query)
if err != nil {
return nil, fmt.Errorf("failed to get hashed tokens: %w", err)
@@ -181,14 +183,15 @@ func (s *SQLStore) GetAllHashedTokens() (map[string]HashedTokenRecord, error) {
for rows.Next() {
var tokenHash, idStr string
var isSession bool
if err := rows.Scan(&tokenHash, &idStr, &isSession); err != nil {
var expiresAt int64
if err := rows.Scan(&tokenHash, &idStr, &isSession, &expiresAt); err != nil {
return nil, fmt.Errorf("failed to scan hashed token: %w", err)
}
uid, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid user_id in hashed_tokens: %w", err)
}
hashedTokens[tokenHash] = HashedTokenRecord{UserID: uid, IsSession: isSession}
hashedTokens[tokenHash] = HashedTokenRecord{UserID: uid, IsSession: isSession, ExpiresAt: expiresAt}
}
if err := rows.Err(); err != nil {
@@ -32,7 +32,7 @@ func TestMigrationAddsHashedTokenSessionColumn(t *testing.T) {
t.Fatalf("runMigrations: %v", err)
}
if err = store.SaveHashedToken("new-hash", 8, true); err != nil {
if err = store.SaveHashedToken("new-hash", 8, true, 12345); err != nil {
t.Fatalf("SaveHashedToken after migration: %v", err)
}
records, err := store.GetAllHashedTokens()
@@ -46,9 +46,15 @@ func TestMigrationAddsHashedTokenSessionColumn(t *testing.T) {
if legacy.IsSession {
t.Fatal("legacy rows must migrate as non-session tokens")
}
if legacy.ExpiresAt != 0 {
t.Fatalf("legacy rows must migrate with no expiry, got %d", legacy.ExpiresAt)
}
if !records["new-hash"].IsSession {
t.Fatal("session flag was not persisted")
}
if records["new-hash"].ExpiresAt != 12345 {
t.Fatalf("expires_at not persisted, got %d", records["new-hash"].ExpiresAt)
}
}
func TestGetSettingMissingReturnsSentinel(t *testing.T) {
@@ -0,0 +1,42 @@
package sqlitebusy
import (
"errors"
"fmt"
"strings"
)
// DefaultBusyTimeoutMs is how long SQLite waits on SQLITE_BUSY before failing an operation.
const DefaultBusyTimeoutMs = 5000
// ErrBusy is returned when an operation could not complete because the database was busy or locked.
var ErrBusy = errors.New("sqlite database is busy or locked")
// WithBusyTimeout appends the busy_timeout connection parameter to dsn so that
// every connection in the pool honors the timeout. Both supported drivers
// (mattn/go-sqlite3 and modernc.org/sqlite) apply the parameter at
// connection-open time, unlike a one-off PRAGMA which only configures the
// single connection that database/sql happens to pick.
func WithBusyTimeout(dsn string) string {
separator := "?"
if strings.Contains(dsn, "?") {
separator = "&"
}
return fmt.Sprintf("%s%s_busy_timeout=%d", dsn, separator, DefaultBusyTimeoutMs)
}
// IsBusyOrLocked reports whether err is SQLITE_BUSY or SQLITE_LOCKED from the
// configured SQLite driver, classified by the driver's result code rather than
// error text.
func IsBusyOrLocked(err error) bool {
return err != nil && isBusyOrLocked(err)
}
// Wrap returns err wrapped with ErrBusy when the underlying error is busy/locked.
// The original error is retained so callers can still inspect the driver result code.
func Wrap(err error) error {
if err == nil || !IsBusyOrLocked(err) {
return err
}
return fmt.Errorf("%w: %w", ErrBusy, err)
}
@@ -0,0 +1,199 @@
package sqlitebusy
import (
"context"
"database/sql"
"errors"
"path/filepath"
"testing"
)
// sqliteBusySnapshot is the extended result code returned when a WAL read
// transaction tries to upgrade to a write on a stale snapshot.
const sqliteBusySnapshot = 517
func TestIsBusyOrLocked(t *testing.T) {
if IsBusyOrLocked(nil) {
t.Fatal("expected nil not to be busy")
}
if IsBusyOrLocked(newGenericError(t)) {
t.Fatal("expected non-busy SQLITE_ERROR not to be classified as busy")
}
if !IsBusyOrLocked(newBusyError(t)) {
t.Fatal("expected SQLITE_BUSY to be classified as busy")
}
if !IsBusyOrLocked(newBusySnapshotError(t)) {
t.Fatal("expected SQLITE_BUSY_SNAPSHOT to be classified as busy")
}
}
func TestWrap(t *testing.T) {
for name, makeErr := range map[string]func(*testing.T) error{
"SQLITE_BUSY": newBusyError,
"SQLITE_BUSY_SNAPSHOT": newBusySnapshotError,
} {
t.Run(name, func(t *testing.T) {
wrapped := Wrap(makeErr(t))
if !errors.Is(wrapped, ErrBusy) {
t.Fatalf("expected ErrBusy, got %v", wrapped)
}
})
}
generic := newGenericError(t)
if err := Wrap(generic); errors.Is(err, ErrBusy) {
t.Fatalf("expected generic error to pass through unwrapped, got %v", err)
}
if err := Wrap(nil); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
// TestWithBusyTimeoutAppliesToEveryConnection verifies the DSN parameter is
// applied at connection-open time, so a second pooled connection does not
// bypass the timeout like a one-off PRAGMA would.
func TestWithBusyTimeoutAppliesToEveryConnection(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "pool.db")
db, err := sql.Open(testDriver, WithBusyTimeout("file:"+dbPath))
if err != nil {
t.Fatalf("open: %v", err)
}
defer db.Close()
db.SetMaxOpenConns(2)
ctx := context.Background()
conn1, err := db.Conn(ctx)
if err != nil {
t.Fatalf("conn1: %v", err)
}
defer conn1.Close()
conn2, err := db.Conn(ctx)
if err != nil {
t.Fatalf("conn2: %v", err)
}
defer conn2.Close()
for i, conn := range []*sql.Conn{conn1, conn2} {
var timeout int
if err := conn.QueryRowContext(ctx, "PRAGMA busy_timeout").Scan(&timeout); err != nil {
t.Fatalf("conn %d: read busy_timeout: %v", i+1, err)
}
if timeout != DefaultBusyTimeoutMs {
t.Fatalf("conn %d: busy_timeout = %d, want %d", i+1, timeout, DefaultBusyTimeoutMs)
}
}
}
// newGenericError returns a real SQLITE_ERROR (result code 1) from the driver.
func newGenericError(t *testing.T) error {
t.Helper()
db, err := sql.Open(testDriver, filepath.Join(t.TempDir(), "generic.db"))
if err != nil {
t.Fatalf("open: %v", err)
}
defer db.Close()
_, err = db.Exec("SELECT * FROM table_that_does_not_exist")
if err == nil {
t.Fatal("expected error from invalid query")
}
return err
}
// newBusyError returns a real SQLITE_BUSY (result code 5) from the driver by
// holding a write lock on one connection and writing from a second with a zero
// busy timeout.
func newBusyError(t *testing.T) error {
t.Helper()
dbPath := filepath.Join(t.TempDir(), "busy.db")
holder, err := sql.Open(testDriver, WithBusyTimeout("file:"+dbPath))
if err != nil {
t.Fatalf("open holder: %v", err)
}
defer holder.Close()
holder.SetMaxOpenConns(1)
if _, err = holder.Exec("CREATE TABLE t (id INTEGER)"); err != nil {
t.Fatalf("create table: %v", err)
}
tx, err := holder.Begin()
if err != nil {
t.Fatalf("begin: %v", err)
}
defer func() { _ = tx.Rollback() }()
if _, err = tx.Exec("INSERT INTO t VALUES (1)"); err != nil {
t.Fatalf("holder insert: %v", err)
}
contender, err := sql.Open(testDriver, "file:"+dbPath+"?_busy_timeout=0")
if err != nil {
t.Fatalf("open contender: %v", err)
}
defer contender.Close()
_, err = contender.Exec("INSERT INTO t VALUES (2)")
if err == nil {
t.Fatal("expected SQLITE_BUSY from contended write")
}
return err
}
// newBusySnapshotError returns a real extended-busy error (SQLITE_BUSY_SNAPSHOT)
// from the driver. In WAL mode, a transaction that has already taken a read
// snapshot cannot upgrade to a write after another connection committed —
// SQLite returns SQLITE_BUSY_SNAPSHOT instead of waiting on busy_timeout.
func newBusySnapshotError(t *testing.T) error {
t.Helper()
dbPath := filepath.Join(t.TempDir(), "snapshot.db")
dsn := "file:" + dbPath + "?_journal_mode=WAL&_busy_timeout=0"
db, err := sql.Open(testDriver, dsn)
if err != nil {
t.Fatalf("open: %v", err)
}
defer db.Close()
db.SetMaxOpenConns(2)
if _, err = db.Exec("CREATE TABLE t (id INTEGER); INSERT INTO t VALUES (1)"); err != nil {
t.Fatalf("setup: %v", err)
}
ctx := context.Background()
reader, err := db.Conn(ctx)
if err != nil {
t.Fatalf("reader conn: %v", err)
}
defer reader.Close()
tx, err := reader.BeginTx(ctx, nil)
if err != nil {
t.Fatalf("begin: %v", err)
}
defer func() { _ = tx.Rollback() }()
// Establish the read snapshot.
var count int
if err = tx.QueryRow("SELECT COUNT(*) FROM t").Scan(&count); err != nil {
t.Fatalf("snapshot read: %v", err)
}
// Commit a write from a different connection, invalidating the snapshot.
writer, err := db.Conn(ctx)
if err != nil {
t.Fatalf("writer conn: %v", err)
}
defer writer.Close()
if _, err = writer.ExecContext(ctx, "INSERT INTO t VALUES (2)"); err != nil {
t.Fatalf("writer insert: %v", err)
}
// Upgrading the stale snapshot to a write must fail with SQLITE_BUSY_SNAPSHOT.
_, err = tx.Exec("INSERT INTO t VALUES (3)")
if err == nil {
t.Fatal("expected SQLITE_BUSY_SNAPSHOT from write on stale snapshot")
}
if code := driverErrorCode(err); code != sqliteBusySnapshot {
t.Fatalf("expected extended code %d (SQLITE_BUSY_SNAPSHOT), got %d", sqliteBusySnapshot, code)
}
return err
}
@@ -0,0 +1,23 @@
//go:build cgosql
// +build cgosql
package sqlitebusy
import (
"errors"
sqlite3 "github.com/mattn/go-sqlite3"
)
const testDriver = "sqlite3"
// driverErrorCode returns the driver's extended result code for err, so tests
// can assert which busy/locked variant was produced. mattn reports the
// extended code in Error.ExtendedCode.
func driverErrorCode(err error) int {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return int(sqliteErr.ExtendedCode)
}
return -1
}
@@ -0,0 +1,23 @@
//go:build !cgosql
// +build !cgosql
package sqlitebusy
import (
"errors"
sqlite "modernc.org/sqlite"
)
const testDriver = "sqlite"
// driverErrorCode returns the driver's extended result code for err, so tests
// can assert which busy/locked variant was produced. modernc reports the
// (possibly extended) code via Error.Code.
func driverErrorCode(err error) int {
var sqliteErr *sqlite.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.Code()
}
return -1
}
@@ -0,0 +1,21 @@
//go:build cgosql
// +build cgosql
package sqlitebusy
import (
"errors"
sqlite3 "github.com/mattn/go-sqlite3"
)
// isBusyOrLocked reports whether err is a SQLITE_BUSY or SQLITE_LOCKED error
// from the CGO (mattn/go-sqlite3) driver. Error.Code is already the primary
// result code; extended codes are reported separately in Error.ExtendedCode.
func isBusyOrLocked(err error) bool {
var sqliteErr sqlite3.Error
if !errors.As(err, &sqliteErr) {
return false
}
return sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked
}
@@ -0,0 +1,28 @@
//go:build !cgosql
// +build !cgosql
package sqlitebusy
import (
"errors"
sqlite "modernc.org/sqlite"
sqlite3 "modernc.org/sqlite/lib"
)
// isBusyOrLocked reports whether err is a SQLITE_BUSY or SQLITE_LOCKED error
// from the pure-Go (modernc.org/sqlite) driver. The driver enables extended
// result codes, so Error.Code() may carry extra detail in the high bits (e.g.
// SQLITE_BUSY_SNAPSHOT); only the lowest byte holds the primary code.
func isBusyOrLocked(err error) bool {
var sqliteErr *sqlite.Error
if !errors.As(err, &sqliteErr) {
return false
}
switch sqliteErr.Code() & 0xff {
case sqlite3.SQLITE_BUSY, sqlite3.SQLITE_LOCKED:
return true
default:
return false
}
}
@@ -64,6 +64,7 @@ func APIScopesToBackend(apiScopes []FrontendScope) ([]BackendScope, error) {
Path: source.Path,
Scope: scope.Scope,
Permissions: frontendScopePermissions(scope),
Quota: scope.Quota,
})
}
return newScopes, nil
@@ -86,6 +87,7 @@ func (u *User) GetFrontendScopes() []FrontendScope {
Name: source.Name,
Scope: scope.Scope,
Permissions: &perms,
Quota: scope.Quota,
})
}
return newScopes
@@ -0,0 +1,164 @@
package users
import (
"encoding/json"
"testing"
)
func assertBoolMapEntries(t *testing.T, name string, got, want map[string]bool) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("%s length = %d, want %d (got=%v want=%v)", name, len(got), len(want), got, want)
}
for key, wantVal := range want {
gotVal, ok := got[key]
if !ok {
t.Fatalf("%s missing key %q", name, key)
}
if gotVal != wantVal {
t.Fatalf("%s[%q] = %v, want %v", name, key, gotVal, wantVal)
}
}
}
func TestFrontendUserEffectiveToolAccessJSON(t *testing.T) {
t.Parallel()
u := FrontendUser{
Username: "alice",
EffectiveToolAccess: map[string]bool{
"sizeViewer": true,
"fileWatcher": false,
"activityViewer": true,
"duplicateFinder": false,
},
}
encoded, err := json.Marshal(u)
if err != nil {
t.Fatal(err)
}
var payload map[string]json.RawMessage
if err := json.Unmarshal(encoded, &payload); err != nil {
t.Fatal(err)
}
if _, ok := payload["effectiveToolAccess"]; !ok {
t.Fatalf("expected effectiveToolAccess key in JSON, got keys: %v", mapKeys(payload))
}
var decoded FrontendUser
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatal(err)
}
assertBoolMapEntries(t, "effectiveToolAccess", decoded.EffectiveToolAccess, u.EffectiveToolAccess)
}
func TestToolAccessMapRoundTripValidIDs(t *testing.T) {
t.Parallel()
original := ToolAccessMap{
ToolSizeViewer: true,
ToolFileWatcher: false,
ToolActivityViewer: true,
ToolDuplicateFinder: false,
ToolAdvancedSearch: true,
ToolMaterialIconPicker: false,
}
encoded, err := json.Marshal(original)
if err != nil {
t.Fatal(err)
}
var payload map[string]bool
if err := json.Unmarshal(encoded, &payload); err != nil {
t.Fatal(err)
}
if len(payload) != len(original) {
t.Fatalf("toolAccess JSON length = %d, want %d", len(payload), len(original))
}
var decoded ToolAccessMap
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatal(err)
}
if len(decoded) != len(original) {
t.Fatalf("toolAccess length = %d, want %d", len(decoded), len(original))
}
for id, want := range original {
got, ok := decoded[id]
if !ok {
t.Fatalf("toolAccess missing key %q", id)
}
if got != want {
t.Fatalf("toolAccess[%q] = %v, want %v", id, got, want)
}
}
}
func TestToolAccessMapRejectsUnknownToolID(t *testing.T) {
t.Parallel()
var m ToolAccessMap
err := json.Unmarshal([]byte(`{"sizeViewer":true,"notATool":false}`), &m)
if err == nil {
t.Fatal("expected unknown tool id to be rejected")
}
}
func TestUserToolAccessJSONRoundTrip(t *testing.T) {
t.Parallel()
wantToolAccess := ToolAccessMap{
ToolSizeViewer: true,
ToolFileWatcher: false,
}
u := User{
FrontendUser: FrontendUser{
Username: "bob",
ToolAccess: wantToolAccess,
},
}
encoded, err := json.Marshal(u)
if err != nil {
t.Fatal(err)
}
var payload map[string]json.RawMessage
if err := json.Unmarshal(encoded, &payload); err != nil {
t.Fatal(err)
}
if _, ok := payload["toolAccess"]; !ok {
t.Fatalf("expected toolAccess key in JSON, got keys: %v", mapKeys(payload))
}
var decoded User
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatal(err)
}
if len(decoded.ToolAccess) != len(wantToolAccess) {
t.Fatalf("toolAccess length = %d, want %d", len(decoded.ToolAccess), len(wantToolAccess))
}
for id, want := range wantToolAccess {
got, ok := decoded.ToolAccess[id]
if !ok {
t.Fatalf("toolAccess missing key %q", id)
}
if got != want {
t.Fatalf("toolAccess[%q] = %v, want %v", id, got, want)
}
}
}
func mapKeys(m map[string]json.RawMessage) []string {
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
return keys
}
@@ -0,0 +1,91 @@
package users
import (
"encoding/json"
"fmt"
)
// ToolID identifies a built-in application tool (matches frontend route segments).
type ToolID string
const (
ToolSizeViewer ToolID = "sizeViewer"
ToolDuplicateFinder ToolID = "duplicateFinder"
ToolAdvancedSearch ToolID = "advancedSearch"
ToolMaterialIconPicker ToolID = "materialIconPicker"
ToolFileWatcher ToolID = "fileWatcher"
ToolActivityViewer ToolID = "activityViewer"
)
// AllToolIDs lists every defined tool in stable catalog order.
var AllToolIDs = []ToolID{
ToolSizeViewer,
ToolDuplicateFinder,
ToolAdvancedSearch,
ToolMaterialIconPicker,
ToolFileWatcher,
ToolActivityViewer,
}
// Valid reports whether id is a known tool constant.
func (id ToolID) Valid() bool {
switch id {
case ToolSizeViewer, ToolDuplicateFinder, ToolAdvancedSearch,
ToolMaterialIconPicker, ToolFileWatcher, ToolActivityViewer:
return true
default:
return false
}
}
// ParseToolID parses s into a ToolID when it names a catalog tool.
func ParseToolID(s string) (ToolID, error) {
id := ToolID(s)
if !id.Valid() {
return "", fmt.Errorf("unknown tool id %q", s)
}
return id, nil
}
// ToolAccessMap stores per-user tool grants keyed by ToolID.
// JSON uses string keys for API compatibility.
type ToolAccessMap map[ToolID]bool
// MarshalJSON encodes the map with string keys.
func (m ToolAccessMap) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
out := make(map[string]bool, len(m))
for id, granted := range m {
if id.Valid() {
out[string(id)] = granted
}
}
return json.Marshal(out)
}
// UnmarshalJSON decodes string-keyed JSON into typed ToolID keys.
func (m *ToolAccessMap) UnmarshalJSON(data []byte) error {
if m == nil {
return fmt.Errorf("ToolAccessMap: UnmarshalJSON on nil pointer")
}
if string(data) == "null" {
*m = nil
return nil
}
var raw map[string]bool
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
out := make(ToolAccessMap, len(raw))
for key, granted := range raw {
id, err := ParseToolID(key)
if err != nil {
return err
}
out[id] = granted
}
*m = out
return nil
}
+60 -29
View File
@@ -8,10 +8,27 @@ import (
)
// ProfileStorageVersion is the user version after nested profile JSON in user_data.
// It marks a storage-shape boundary (used by the persist layer), not the newest version.
const ProfileStorageVersion = 5
// NewestUserVersion is the single reference for the latest user version.
// Per-migration watermarks below keep their historical values; only this moves forward.
const NewestUserVersion = 6
// PermMigrationVersion is the user version after the legacy perm-key to permissions migration.
const PermMigrationVersion = 1
// TokenMigrationVersion is the user version after the legacy apiKeys to tokens migration.
const TokenMigrationVersion = 2
// TokenHashBackfillVersion is the user version that guarantees every stored API
// token (Tokens and legacy ApiKeys) has a SHA256(raw) → owner row in
// hashed_tokens. Strict server-side auth ignores BelongsTo and rejects any
// bearer JWT without a hash mapping, so this migration must run on startup.
const TokenHashBackfillVersion = 6
// CurrentUserMigrationVersion is persisted for newly created accounts and after legacy migrations finish.
const CurrentUserMigrationVersion = ProfileStorageVersion
const CurrentUserMigrationVersion = NewestUserVersion
type LoginMethod string
@@ -77,7 +94,7 @@ type AuthToken struct {
Token string `json:"token,omitempty"`
Name string `json:"name,omitempty"`
Username string `json:"username,omitempty"`
BelongsTo uint64 `json:"belongsTo,omitempty"` // legacy stored metadata only; identity is hashed_tokens lookup
BelongsTo uint64 `json:"belongsTo,omitempty"` // legacy stored metadata only, never trusted for auth; identity is always the hashed_tokens lookup
IssuedAt int64 `json:"issuedAt,omitempty"`
ExpiresAt int64 `json:"expiresAt,omitempty"`
Permissions Permissions `json:"Permissions,omitempty"`
@@ -89,11 +106,11 @@ type MinimalAuthToken struct {
}
type Permissions struct {
Api bool `json:"api"` // allow api access
Admin bool `json:"admin"` // allow admin access
Api bool `json:"api"` // allow api access
Admin bool `json:"admin"` // allow admin access
Modify bool `json:"modify,omitempty"` // deprecated: legacy user migration only; not used for API tokens
Share bool `json:"share"` // allow sharing files
Realtime bool `json:"realtime"` // allow realtime updates
Share bool `json:"share"` // allow sharing files
Realtime bool `json:"realtime"` // allow realtime updates
Delete bool `json:"delete,omitempty"` // deprecated: legacy user migration only; not used for API tokens
Create bool `json:"create,omitempty"` // deprecated: legacy user migration only; not used for API tokens
Download bool `json:"download,omitempty"` // deprecated: legacy user migration only; not used for API tokens
@@ -102,12 +119,12 @@ type Permissions struct {
// SourceFilePermissions holds per-source file operation permissions (v4+).
type SourceFilePermissions struct {
View bool `json:"view"`
Download bool `json:"download"`
Modify bool `json:"modify"`
Delete bool `json:"delete"`
Create bool `json:"create"`
Configured bool `json:"configured,omitempty"` // true when explicitly set (allows intentional deny-all)
View bool `json:"view"`
Download bool `json:"download"`
Modify bool `json:"modify"`
Delete bool `json:"delete"`
Create bool `json:"create"`
Configured bool `json:"configured,omitempty"` // true when explicitly set (allows intentional deny-all)
}
// MarkSourceFilePermissionsConfigured marks permissions as explicitly set (including deny-all).
@@ -156,16 +173,17 @@ type Preview struct {
// FrontendUser holds fields safe to return from user APIs (embedded on User).
type FrontendUser struct {
NonAdminEditable
DisableSettings bool `json:"disableSettings"`
Username string `json:"username"`
FrontendScopes []FrontendScope `json:"scopes"`
LockPassword bool `json:"lockPassword"`
Permissions Permissions `json:"permissions"` // global: admin, api, share, realtime
SourcePermissions map[string]SourceFilePermissions `json:"sourcePermissions,omitempty"` // deprecated: use scopes[].permissions
LoginMethod LoginMethod `json:"loginMethod"`
OtpEnabled bool `json:"otpEnabled"`
ShowFirstLogin bool `json:"showFirstLogin"`
Perm Permissions `json:"perm,omitzero"`
DisableSettings bool `json:"disableSettings"`
Username string `json:"username"`
FrontendScopes []FrontendScope `json:"scopes"`
LockPassword bool `json:"lockPassword"`
Permissions Permissions `json:"permissions"` // global: admin, api, share, realtime
SourcePermissions map[string]SourceFilePermissions `json:"sourcePermissions,omitempty"` // deprecated: use scopes[].permissions
LoginMethod LoginMethod `json:"loginMethod"`
OtpEnabled bool `json:"otpEnabled"`
ShowFirstLogin bool `json:"showFirstLogin"`
EffectiveToolAccess map[string]bool `json:"effectiveToolAccess,omitempty"`
Perm Permissions `json:"perm,omitzero"`
}
// PinnedItems maps source filesystem path -> index directory path -> pinned item names.
@@ -183,6 +201,7 @@ type User struct {
// BackendScopes is the authoritative, persisted access list (SourceScope.Name = backend source path).
// SQLite stores this inside user_data JSON under the key "scopes" (see sqldb.UserData).
BackendScopes []BackendScope `json:"backendScopes,omitempty"`
DeclinedDefaultSources []string `json:"declinedDefaultSources,omitempty"`
BackendSourcePermissions map[string]SourceFilePermissions `json:"backendSourcePermissions,omitempty"` // key = source path
Tokens map[string]AuthToken `json:"tokens,omitempty"`
TOTPSecret string `json:"totpSecret,omitempty"`
@@ -193,16 +212,24 @@ type User struct {
UserLegacyFields `json:",inline"`
}
type ScopeQuota struct {
ID string `json:"id"`
LimitBytes int64 `json:"limitBytes"` // 0 = unlimited
Meter string `json:"meter,omitempty"` // index_scope (indexed size) | accounted (tracked usage)
}
type FrontendScope struct {
Name string `json:"name"` // Bolt: filesystem path; JSON API: display name after prepForFrontend
Scope string `json:"scope"` // index path within that source
Name string `json:"name"` // Bolt: filesystem path; JSON API: display name after prepForFrontend
Scope string `json:"scope"` // index path within that source
Permissions *SourceFilePermissions `json:"permissions,omitempty"`
Quota *ScopeQuota `json:"quota,omitempty"`
}
type BackendScope struct {
Path string `json:"path"` // real path for the source
Scope string `json:"scope"` // index path within that source
Path string `json:"path"` // real path for the source
Scope string `json:"scope"` // index path within that source
Permissions SourceFilePermissions `json:"permissions,omitempty"`
Quota *ScopeQuota `json:"quota,omitempty"`
}
// json tags must match variable name with smaller case first letter
@@ -216,9 +243,10 @@ type NonAdminEditable struct {
StickySidebar bool `json:"stickySidebar"` // keep sidebar open when navigating
DarkMode bool `json:"darkMode"` // should dark mode be enabled
Password string `json:"password,omitempty"`
Locale string `json:"locale"` // language to use: eg. de, en, or fr
ViewMode string `json:"viewMode"` // view mode to use: eg. normal, list, grid, or compact
SingleClick bool `json:"singleClick"` // open directory on single click, also enables middle click to open in new tab
Locale string `json:"locale"` // language to use: eg. de, en, or fr
ViewMode string `json:"viewMode"` // view mode to use: eg. normal, list, grid, or compact
NewFileTemplate []string `json:"newFileTemplate"` // list of custom filenames that will be used as template for new files
SingleClick bool `json:"singleClick"` // open directory on single click, also enables middle click to open in new tab
Sorting Sorting `json:"sorting"`
ShowHidden bool `json:"showHidden"` // show hidden files in the UI. On windows this includes files starting with a dot and windows hidden files
HideFileExt string `json:"hideFileExt"` // space separated list of file extensions to hide in UI and API
@@ -238,11 +266,14 @@ type NonAdminEditable struct {
ShowToolsInSidebar bool `json:"showToolsInSidebar"` // when false, sidebar hides links with category "tool" (default: true)
DebugOffice bool `json:"debugOffice"` // debug onlyoffice editor
SidebarLinks []SidebarLink `json:"sidebarLinks"` // customizable sidebar links
ToolAccess ToolAccessMap `json:"toolAccess,omitempty"` // per-tool access overrides
HideFilesInTree bool `json:"hideFilesInTree"` // hide files in the sidebar tree navigation, when true, will show only directories.
DeleteAfterArchive bool `json:"deleteAfterArchive"` // delete source files after successful creation/extraction of archives
PreferEditorForMarkdown bool `json:"preferEditorForMarkdown"` // prefer editor first for markdown files instead of the Markdown Viewer
PromptRightCloseButton bool `json:"promptRightCloseButton"` // show the prompts close button on the right
ShowFirstLogin bool `json:"showFirstLogin"`
PasskeyCredentials []WebAuthnCredential `json:"passkeyCredentials,omitempty"`
ShowAdvancedProfile bool `json:"showAdvancedProfile"`
}
type FileLoading struct {
+1
View File
@@ -35,4 +35,5 @@ var (
ErrPasskeyExists = errors.New("passkey credential already exists")
ErrPasskeyNotEnabled = errors.New("passkey authentication is not enabled")
ErrPasskeyInvalidSession = errors.New("invalid or expired passkey session")
ErrQuotaExceeded = errors.New("quota exceeded")
)
-10
View File
@@ -69,11 +69,6 @@ func (s *Service) ProbeFile(ctx context.Context, mediaPath string) (*FileProbeIn
return nil, err
}
if err = s.Acquire(ctx); err != nil {
return nil, err
}
defer s.Release()
info, err := s.inner.ProbeStream(ctx, goffmpeg.ProbeStreamOptions{
URL: localPath,
StreamType: goffmpeg.StreamFile,
@@ -106,11 +101,6 @@ func (s *Service) GetMediaDuration(ctx context.Context, mediaPath string) (float
return duration, nil
}
if err = s.Acquire(ctx); err != nil {
return 0, err
}
defer s.Release()
duration, err := s.inner.GetMediaDuration(ctx, localPath)
if err != nil {
return 0, err
+16 -23
View File
@@ -27,7 +27,8 @@ var global *Service
// InitOptions configures startup initialization.
type InitOptions struct {
FFmpegPath string
MaxConcurrent int
MaxConcurrent int // deprecated: use Concurrency
Concurrency goffmpeg.Concurrency
CacheDir string
SkipHWTests bool
HardwareAcceleration bool
@@ -35,9 +36,6 @@ type InitOptions struct {
// Initialize creates the global ffmpeg service and runs capability detection.
func Initialize(ctx context.Context, opts InitOptions) error {
if opts.MaxConcurrent < 1 {
opts.MaxConcurrent = 4
}
if opts.CacheDir == "" {
opts.CacheDir = os.TempDir()
}
@@ -46,12 +44,20 @@ func Initialize(ctx context.Context, opts InitOptions) error {
logger.Info("Detecting ffmpeg hardware codec support...")
}
svc, err := goffmpeg.New(ctx, goffmpeg.Config{
cfg := goffmpeg.Config{
FFmpegPath: opts.FFmpegPath,
MaxConcurrent: opts.MaxConcurrent,
Concurrency: opts.Concurrency,
Logger: goffmpeg.NopLogger(),
SkipHWTests: opts.SkipHWTests,
})
}
if opts.Concurrency.MaxProbe == 0 && opts.Concurrency.MaxDecode == 0 && opts.Concurrency.MaxEncode == 0 {
if opts.MaxConcurrent < 1 {
opts.MaxConcurrent = 4
}
cfg.MaxConcurrent = opts.MaxConcurrent
}
svc, err := goffmpeg.New(ctx, cfg)
if err != nil {
global = nil
return err
@@ -107,20 +113,12 @@ func (s *Service) Release() {
}
// VideoPreview extracts a JPEG preview frame to w.
func (s *Service) VideoPreview(ctx context.Context, w io.Writer, videoPath string, percentageSeek int) error {
// go-ffmpeg Service methods acquire concurrency slots automatically; do not Acquire before calling.
func (s *Service) VideoPreview(ctx context.Context, w io.Writer, opts ops.PreviewOptions) error {
if s == nil || s.inner == nil {
return fmt.Errorf("ffmpeg service not available")
}
if err := s.Acquire(ctx); err != nil {
return err
}
defer s.Release()
return s.inner.VideoPreview(ctx, w, ops.PreviewOptions{
Input: videoPath,
SeekPercent: float64(percentageSeek),
Quality: 10,
})
return s.inner.VideoPreview(ctx, w, opts)
}
// ExtractSubtitle returns embedded subtitle content as WebVTT.
@@ -138,11 +136,6 @@ func (s *Service) ExtractSubtitle(ctx context.Context, videoPath string, streamI
return content, nil
}
if err = s.Acquire(ctx); err != nil {
return "", err
}
defer s.Release()
content, err := s.inner.ExtractSubtitle(ctx, videoPath, streamIndex)
if err != nil {
return "", err
@@ -0,0 +1,106 @@
package ffmpeg
import (
"context"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"time"
goffmpeg "github.com/gtsteffaniak/go-ffmpeg"
)
func requireFFmpegBinaries(t *testing.T) {
t.Helper()
if os.Getenv("GOFFMPEG_FFMPEG_PATH") != "" {
return
}
if _, err := exec.LookPath("ffmpeg"); err != nil {
t.Skip("ffmpeg not available")
}
if _, err := exec.LookPath("ffprobe"); err != nil {
t.Skip("ffprobe not available")
}
}
func testSampleMP4(t *testing.T) string {
t.Helper()
if p := os.Getenv("GOFFMPEG_SAMPLE_MP4"); p != "" {
return p
}
dir := t.TempDir()
out := filepath.Join(dir, "sample.mp4")
cmd := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error",
"-f", "lavfi", "-i", "color=c=blue:s=320x240:d=2",
"-c:v", "libx264", "-pix_fmt", "yuv420p", "-y", out)
if err := cmd.Run(); err != nil {
t.Skipf("could not generate sample mp4: %v", err)
}
return out
}
// TestGetMediaDurationConcurrentNoDeadlock ensures probe-tier concurrency does not deadlock.
// With MaxProbe=2, four concurrent GetMediaDuration calls queue and complete.
func TestGetMediaDurationConcurrentNoDeadlock(t *testing.T) {
requireFFmpegBinaries(t)
sample := testSampleMP4(t)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := Initialize(ctx, InitOptions{
Concurrency: goffmpeg.Concurrency{
MaxProbe: 2,
MaxDecode: 4,
MaxEncode: 2,
},
CacheDir: t.TempDir(),
SkipHWTests: true,
HardwareAcceleration: false,
})
if err != nil {
t.Fatalf("Initialize() error = %v", err)
}
t.Cleanup(func() { global = nil })
svc := Get()
if svc == nil {
t.Fatal("Get() returned nil after Initialize")
}
const workers = 4
var wg sync.WaitGroup
errCh := make(chan error, workers)
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if _, err := svc.GetMediaDuration(ctx, sample); err != nil {
errCh <- err
}
}()
}
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(25 * time.Second):
t.Fatal("concurrent GetMediaDuration calls deadlocked with MaxProbe=2")
}
close(errCh)
for err := range errCh {
if err != nil {
t.Fatalf("GetMediaDuration() error = %v", err)
}
}
}
+55 -20
View File
@@ -5,10 +5,15 @@ package imagemeta
import (
"encoding/binary"
"io"
"math"
"os"
)
const heicMetadataScanLimit = 32 * 1024 * 1024
const (
heicMetadataInitialScan = 256 * 1024
heicMetadataScanLimit = 32 * 1024 * 1024
maxBMFFRecursionDepth = 64
)
type heicTransform struct {
irot uint8
@@ -23,19 +28,37 @@ func parseHEICTransform(f *os.File) heicTransform {
if err != nil {
return heicTransform{}
}
size := info.Size()
if size > heicMetadataScanLimit {
size = heicMetadataScanLimit
}
if size < 12 {
fileSize := info.Size()
if fileSize < 12 {
return heicTransform{}
}
buf := make([]byte, size)
n, err := f.ReadAt(buf, 0)
if err != nil && err != io.EOF {
return heicTransform{}
scanLimit := int64(heicMetadataInitialScan)
if fileSize < scanLimit {
scanLimit = fileSize
}
for {
buf := make([]byte, scanLimit)
n, readErr := f.ReadAt(buf, 0)
if readErr != nil && readErr != io.EOF {
return heicTransform{}
}
data := buf[:n]
if result := parseHEICPrimaryIrot(data); result.found || findBMFFBox(data, "meta") != nil {
return result
}
if scanLimit >= fileSize || scanLimit >= heicMetadataScanLimit {
return parseHEICPrimaryIrot(data)
}
next := scanLimit * 2
if next > heicMetadataScanLimit {
next = heicMetadataScanLimit
}
if next > fileSize {
next = fileSize
}
scanLimit = next
}
return parseHEICPrimaryIrot(buf[:n])
}
func parseHEICPrimaryIrot(data []byte) heicTransform {
@@ -75,10 +98,13 @@ func parseHEICPrimaryIrot(data []byte) heicTransform {
}
func findBMFFBox(data []byte, typ string) []byte {
return findBMFFBoxRange(data, 0, len(data), typ)
return findBMFFBoxRange(data, 0, len(data), typ, 0)
}
func findBMFFBoxRange(data []byte, start, end int, typ string) []byte {
func findBMFFBoxRange(data []byte, start, end int, typ string, depth int) []byte {
if depth > maxBMFFRecursionDepth {
return nil
}
pos := start
for pos+8 <= end {
size, header, ok := bmffBoxHeader(data, pos, end)
@@ -86,14 +112,14 @@ func findBMFFBoxRange(data []byte, start, end int, typ string) []byte {
break
}
boxEnd := pos + size
if boxEnd > end {
if boxEnd > end || boxEnd < pos {
break
}
if string(data[pos+4:pos+8]) == typ {
return data[pos:boxEnd]
}
if isBMFFContainerType(data[pos+4 : pos+8]) {
if child := findBMFFBoxRange(data, pos+header, boxEnd, typ); child != nil {
if child := findBMFFBoxRange(data, pos+header, boxEnd, typ, depth+1); child != nil {
return child
}
}
@@ -107,7 +133,7 @@ func findBMFFChildBox(container []byte, typ string) []byte {
if payload == nil {
return nil
}
return findBMFFBoxRange(payload, 0, len(payload), typ)
return findBMFFBoxRange(payload, 0, len(payload), typ, 0)
}
func isBMFFContainerType(typ []byte) bool {
@@ -131,7 +157,14 @@ func bmffBoxHeader(data []byte, pos, end int) (size int, header int, ok bool) {
if pos+16 > end {
return 0, 0, false
}
return int(binary.BigEndian.Uint64(data[pos+8 : pos+16])), 16, true
size64 := binary.BigEndian.Uint64(data[pos+8 : pos+16])
if size64 < 16 || size64 > math.MaxInt32 {
return 0, 0, false
}
return int(size64), 16, true
}
if size32 < 8 {
return 0, 0, false
}
return int(size32), 8, true
}
@@ -156,7 +189,7 @@ func bmffBoxPayload(box []byte) []byte {
func isBMFFFullBoxType(typ string) bool {
switch typ {
case "meta", "pitm", "irot", "imir", "ispe", "ipma", "hdlr", "iloc", "iinf", "infe":
case "meta", "pitm", "ispe", "ipma", "hdlr", "iloc", "iinf", "infe":
return true
default:
return false
@@ -202,8 +235,10 @@ func parseIPCOProperties(ipco []byte) []ipcoProperty {
}
child := payload[pos : pos+size]
prop := ipcoProperty{typ: string(child[4:8])}
if prop.typ == "irot" && len(child) >= 13 {
prop.irot = child[12] & 0x03
if prop.typ == "irot" {
if payload := bmffBoxPayload(child); len(payload) >= 1 {
prop.irot = payload[0] & 0x03
}
}
props = append(props, prop)
pos += size
@@ -97,6 +97,33 @@ func TestGetOrientationSyntheticHEICRotate(t *testing.T) {
}
}
func TestFindBMFFBoxRangeRespectsDepthLimit(t *testing.T) {
target := bmffBox("targ", []byte{1, 2, 3})
nested := target
for i := 0; i < maxBMFFRecursionDepth+4; i++ {
nested = bmffBox("meta", nested)
}
if got := findBMFFBoxRange(nested, 0, len(nested), "targ", 0); got != nil {
t.Fatal("expected depth limit to prevent finding deeply nested box")
}
}
func TestParseIPCOPropertiesReadsPlainIrotBox(t *testing.T) {
ipco := bmffBox("ipco", bmffIrotBox(2))
props := parseIPCOProperties(ipco)
if len(props) != 1 || props[0].irot != 2 {
t.Fatalf("parseIPCOProperties() = %+v, want irot=2", props)
}
}
func TestParseIPCOPropertiesReadsExtendedSizeIrotBox(t *testing.T) {
ipco := bmffBox("ipco", bmffExtendedIrotBox(1))
props := parseIPCOProperties(ipco)
if len(props) != 1 || props[0].irot != 1 {
t.Fatalf("parseIPCOProperties() = %+v, want irot=1 from extended-size box", props)
}
}
type heicItemFixture struct {
itemID uint16
irot uint8
@@ -220,7 +247,18 @@ func bmffPITMBox(itemID uint16) []byte {
}
func bmffIrotBox(angle uint8) []byte {
return bmffFullBox("irot", 0, []byte{angle & 0x03})
return bmffBox("irot", []byte{angle & 0x03})
}
func bmffExtendedIrotBox(angle uint8) []byte {
payload := []byte{angle & 0x03}
size := 16 + len(payload)
out := make([]byte, size)
binary.BigEndian.PutUint32(out[0:4], 1)
copy(out[4:8], "irot")
binary.BigEndian.PutUint64(out[8:16], uint64(size))
copy(out[16:], payload)
return out
}
func bmffIPMABox(entries []ipmaEntryFixture) []byte {
+20 -8
View File
@@ -18,6 +18,7 @@ const (
tiffTypeASCII = 2
tiffTypeShort = 3
tiffTypeLong = 4
tiffTypeIFD = 13
tiffTypeUndefined = 7
maxTIFFIFDsVisited = 128
maxTIFFIFDQueueLen = 128
@@ -205,19 +206,23 @@ func tiffTagUint32s(data []byte, order binary.ByteOrder, typ uint16, count uint3
if count > maxTIFFSubIFDsPerEntry {
count = maxTIFFSubIFDsPerEntry
}
elemSize := tiffTagByteSize(typ)
if elemSize <= 0 {
return nil
}
var raw []byte
switch {
case typ == tiffTypeLong && count == 1:
var b [4]byte
order.PutUint32(b[:], valueOffset)
raw = b[:]
case tiffTagByteSize(typ)*int(count) <= 4:
case elemSize*int(count) <= 4:
var b [4]byte
order.PutUint32(b[:], valueOffset)
raw = b[:count*uint32(tiffTagByteSize(typ))]
raw = b[:count*uint32(elemSize)]
default:
off := int(valueOffset)
n := int(count) * tiffTagByteSize(typ)
n := int(count) * elemSize
if off < 0 || off+n > len(data) {
return nil
}
@@ -225,11 +230,18 @@ func tiffTagUint32s(data []byte, order binary.ByteOrder, typ uint16, count uint3
}
out := make([]uint32, 0, count)
for i := 0; i < int(count); i++ {
pos := i * 4
if pos+4 > len(raw) {
pos := i * elemSize
if pos+elemSize > len(raw) {
break
}
out = append(out, order.Uint32(raw[pos:pos+4]))
switch typ {
case tiffTypeShort:
out = append(out, uint32(order.Uint16(raw[pos:pos+2])))
case tiffTypeLong, tiffTypeIFD:
out = append(out, order.Uint32(raw[pos:pos+4]))
default:
return nil
}
}
return out
}
@@ -240,9 +252,9 @@ func tiffTagByteSize(typ uint16) int {
return 1
case tiffTypeShort:
return 2
case tiffTypeLong:
case tiffTypeLong, tiffTypeIFD:
return 4
default:
return 1
return 0
}
}
@@ -93,9 +93,31 @@ func TestCollectTIFFPreviewCandidatesBoundsIFDTraversal(t *testing.T) {
}
}
func TestTiffTagUint32sSupportsIFDType(t *testing.T) {
data := buildSyntheticTIFF(tiffFixture{
thumbnailJPEG: smallJPEG,
subIFDJPEG: largeJPEG,
subIFDType: tiffTypeIFD,
})
f := openBytesFile(t, data)
candidates, err := collectTIFFPreviewCandidates(f)
if err != nil {
t.Fatal(err)
}
preview, err := readLargestJPEGPreview(f, candidates)
if err != nil {
t.Fatal(err)
}
if len(preview) != len(largeJPEG) {
t.Fatalf("preview size = %d, want %d (SubIFD type IFD)", len(preview), len(largeJPEG))
}
}
type tiffFixture struct {
thumbnailJPEG []byte
subIFDJPEG []byte
subIFDType uint16
badJpgFromRaw bool
badJPEGInterchg bool
}
@@ -116,6 +138,9 @@ func openBytesFile(t *testing.T, data []byte) *os.File {
func buildSyntheticTIFF(fix tiffFixture) []byte {
order := binary.LittleEndian
if fix.subIFDType == 0 {
fix.subIFDType = tiffTypeLong
}
buf := make([]byte, 64*1024)
thumbOff := uint32(4096)
@@ -149,7 +174,7 @@ func buildSyntheticTIFF(fix tiffFixture) []byte {
writeIFD(ifd1Off, ifd1Entries, 0)
ifd0Entries := []tiffEntry{
{tag: tiffTagSubIFDs, typ: tiffTypeLong, count: 1, value: ifd1Off},
{tag: tiffTagSubIFDs, typ: fix.subIFDType, count: 1, value: ifd1Off},
{tag: tiffTagJPEGInterchange, typ: tiffTypeLong, count: 1, value: thumbOff},
{tag: tiffTagJPEGInterchangeLen, typ: tiffTypeLong, count: 1, value: uint32(len(fix.thumbnailJPEG))},
}
@@ -8,6 +8,7 @@ package preview
#include <mupdf/fitz.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
static void fb_silence_warnings(void *user, const char *message) {
(void)user;
@@ -17,6 +18,25 @@ static void fb_silence_warnings(void *user, const char *message) {
static double fb_dmin(double a, double b) { return a < b ? a : b; }
static double fb_dmax(double a, double b) { return a > b ? a : b; }
static int fb_rect_usable(fz_rect r) {
if (fz_is_infinite_rect(r) || fz_is_empty_rect(r)) {
return 0;
}
if (r.x1 <= r.x0 || r.y1 <= r.y0) {
return 0;
}
if (!isfinite(r.x0) || !isfinite(r.x1) || !isfinite(r.y0) || !isfinite(r.y1)) {
return 0;
}
return 1;
}
static int fb_bbox_within(fz_irect bbox, int max_w, int max_h) {
int w = bbox.x1 - bbox.x0;
int h = bbox.y1 - bbox.y0;
return w > 0 && h > 0 && w <= max_w && h <= max_h;
}
// fb_render_document_page_jpeg renders one page to JPEG bytes.
// max_width/max_height <= 0 selects original mode (~150 DPI). Otherwise renders into that box.
// resize_mode_fill: 0 = fit (no upscale), 1 = fill (center crop to exact box).
@@ -85,6 +105,9 @@ static int fb_render_document_page_jpeg(
page = fz_load_page(ctx, doc, page_num);
fz_rect bounds = fz_bound_page(ctx, page);
if (!fb_rect_usable(bounds)) {
fz_throw(ctx, FZ_ERROR_GENERIC, "invalid page bounds");
}
fz_matrix ctm;
fz_irect bbox;
@@ -92,7 +115,32 @@ static int fb_render_document_page_jpeg(
double dpi = 150.0;
ctm = fz_scale(dpi / 72.0, dpi / 72.0);
bounds = fz_transform_rect(bounds, ctm);
if (!fb_rect_usable(bounds)) {
fz_throw(ctx, FZ_ERROR_GENERIC, "invalid transformed page bounds");
}
bbox = fz_round_rect(bounds);
{
const int max_orig_w = 4096;
const int max_orig_h = 4096;
if (!fb_bbox_within(bbox, max_orig_w, max_orig_h)) {
int w = bbox.x1 - bbox.x0;
int h = bbox.y1 - bbox.y0;
if (w <= 0 || h <= 0) {
fz_throw(ctx, FZ_ERROR_GENERIC, "invalid page bounds");
}
double scale = fb_dmin((double)max_orig_w / (double)w, (double)max_orig_h / (double)h);
ctm = fz_concat(ctm, fz_scale(scale, scale));
bounds = fz_bound_page(ctx, page);
bounds = fz_transform_rect(bounds, ctm);
if (!fb_rect_usable(bounds)) {
fz_throw(ctx, FZ_ERROR_GENERIC, "invalid transformed page bounds");
}
bbox = fz_round_rect(bounds);
if (!fb_bbox_within(bbox, max_orig_w, max_orig_h)) {
fz_throw(ctx, FZ_ERROR_GENERIC, "page exceeds maximum render size");
}
}
}
} else {
double page_w = bounds.x1 - bounds.x0;
double page_h = bounds.y1 - bounds.y0;
-10
View File
@@ -17,11 +17,6 @@ func (s *Service) convertHEICToJPEGWithFFmpeg(ctx context.Context, filePath stri
return nil, ctx.Err()
}
if err := s.ffmpegService.Acquire(ctx); err != nil {
return nil, err
}
defer s.ffmpegService.Release()
var quality string
switch previewSize {
case "large":
@@ -46,11 +41,6 @@ func (s *Service) convertImageWithFFmpeg(ctx context.Context, filePath string, p
return nil, ctx.Err()
}
if err := s.ffmpegService.Acquire(ctx); err != nil {
return nil, err
}
defer s.ffmpegService.Release()
var width, height int
var quality string
switch previewSize {
+5 -4
View File
@@ -286,7 +286,7 @@ func (s *Service) generateRawPreview(ctx context.Context, file iteminfo.Extended
return s.generateImagePreview(ctx, file, previewSize)
case previewTypeVideo:
return s.generateVideoPreviewBytes(ctx, file, seekPercentage)
return s.generateVideoPreviewBytes(ctx, file, previewSize, seekPercentage)
case previewTypeAudio:
if file.Metadata != nil && len(file.Metadata.AlbumArt) > 0 {
@@ -331,7 +331,7 @@ func (s *Service) generateHEICPreview(ctx context.Context, file iteminfo.Extende
}
// generateVideoPreviewBytes generates preview frame from video file
func (s *Service) generateVideoPreviewBytes(ctx context.Context, file iteminfo.ExtendedFileInfo, seekPercentage int) ([]byte, error) {
func (s *Service) generateVideoPreviewBytes(ctx context.Context, file iteminfo.ExtendedFileInfo, previewSize string, seekPercentage int) ([]byte, error) {
// Check if this video format is enabled for preview generation
ext := strings.TrimPrefix(strings.ToLower(filepath.Ext(file.Name)), ".")
if !settings.CanConvertVideo(ext) {
@@ -343,7 +343,7 @@ func (s *Service) generateVideoPreviewBytes(ctx context.Context, file iteminfo.E
videoSeekPercentage = 10
}
imageBytes, err := s.GenerateVideoPreview(ctx, file.RealPath, videoSeekPercentage)
imageBytes, err := s.GenerateVideoPreview(ctx, file.RealPath, previewSize, videoSeekPercentage)
if err != nil {
// Don't log client cancellations as errors
if ctx.Err() != context.Canceled {
@@ -506,7 +506,8 @@ func GeneratePreviewWithMD5(ctx context.Context, file iteminfo.ExtendedFileInfo,
// When we got bytes from type-specific path, regular images are already resized; others need resize below.
previewType := determinePreviewType(file)
if !fromEmbeddedPreview && (previewType == previewTypeImage ||
(previewType == previewTypeDocument && settings.Env.MuPdfAvailable)) {
(previewType == previewTypeDocument && settings.Env.MuPdfAvailable) ||
(previewType == previewTypeVideo && settings.Env.FFmpegAvailable)) {
if err := service.fileCache.Store(ctx, cacheKey, imageBytes); err != nil {
logger.Errorf("failed to cache image: %v", err)
}
+16 -2
View File
@@ -6,14 +6,16 @@ import (
"fmt"
"os"
"github.com/gtsteffaniak/go-ffmpeg/ops"
"github.com/gtsteffaniak/go-logger/logger"
)
// GenerateVideoPreview generates a single preview image from a video using ffmpeg.
// videoPath: path to the input video file.
// previewSize: small, large, xlarge, or original.
// percentageSeek: percentage of video duration to seek to (0–100).
// Returns: JPEG image bytes.
func (s *Service) GenerateVideoPreview(ctx context.Context, videoPath string, percentageSeek int) ([]byte, error) {
func (s *Service) GenerateVideoPreview(ctx context.Context, videoPath, previewSize string, percentageSeek int) ([]byte, error) {
if s.ffmpegService == nil {
logger.Errorf("FFmpeg service not available for file '%s'", videoPath)
return nil, fmt.Errorf("FFmpeg service not available")
@@ -25,8 +27,20 @@ func (s *Service) GenerateVideoPreview(ctx context.Context, videoPath string, pe
return nil, fmt.Errorf("video file not accessible: %w", err)
}
params, err := ffmpegPreviewParamsForSize(previewSize)
if err != nil {
return nil, err
}
var buf bytes.Buffer
err := s.ffmpegService.VideoPreview(ctx, &buf, videoPath, percentageSeek)
err = s.ffmpegService.VideoPreview(ctx, &buf, ops.PreviewOptions{
Input: videoPath,
SeekPercent: float64(percentageSeek),
Width: params.width,
Height: params.height,
ScaleMode: params.scaleMode,
Quality: params.quality,
})
if err != nil {
return nil, err
}
+35
View File
@@ -0,0 +1,35 @@
package preview
import (
"github.com/gtsteffaniak/go-ffmpeg/ops"
)
type ffmpegPreviewParams struct {
width int
height int
scaleMode ops.ScaleMode
quality int
}
func ffmpegPreviewParamsForSize(previewSize string) (ffmpegPreviewParams, error) {
if previewSize == "original" {
return ffmpegPreviewParams{quality: 10}, nil
}
opts, err := getPreviewOptions(previewSize)
if err != nil {
return ffmpegPreviewParams{}, err
}
params := ffmpegPreviewParams{
width: opts.Width,
height: opts.Height,
quality: 10, // fastest MJPEG encode (-q:v 10); visual quality comes from target dimensions
}
if opts.ResizeMode == ResizeModeFill {
params.scaleMode = ops.ScaleFill
} else {
params.scaleMode = ops.ScaleFit
}
return params, nil
}
@@ -0,0 +1,73 @@
package preview
import (
"testing"
"github.com/gtsteffaniak/go-ffmpeg/ops"
"github.com/stretchr/testify/require"
)
func TestFfmpegPreviewParamsForSize(t *testing.T) {
t.Parallel()
tests := []struct {
name string
size string
wantW int
wantH int
wantMode ops.ScaleMode
wantQual int
wantError bool
}{
{
name: "small fill",
size: "small",
wantW: 256,
wantH: 256,
wantMode: ops.ScaleFill,
wantQual: 10,
},
{
name: "large fit",
size: "large",
wantW: 640,
wantH: 640,
wantMode: ops.ScaleFit,
wantQual: 10,
},
{
name: "xlarge fit",
size: "xlarge",
wantW: 1024,
wantH: 1024,
wantMode: ops.ScaleFit,
wantQual: 10,
},
{
name: "original no scale",
size: "original",
wantQual: 10,
},
{
name: "unsupported",
size: "huge",
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := ffmpegPreviewParamsForSize(tt.size)
if tt.wantError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantW, got.width)
require.Equal(t, tt.wantH, got.height)
require.Equal(t, tt.wantMode, got.scaleMode)
require.Equal(t, tt.wantQual, got.quality)
})
}
}
+72
View File
@@ -0,0 +1,72 @@
package quota
import (
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
"github.com/gtsteffaniak/filebrowser/backend/internal/state"
)
// CopyMoveContext describes a copy/move/rename admission check.
type CopyMoveContext struct {
Principal *users.User
Action string
FromSourceName string
ToSourceName string
FromSourcePath string
ToSourcePath string
FromIndexPath string
ToIndexPath string
IsSrcDir bool
OverwriteBytes int64
ShareHash string
ShareLimitBytes int64
SessionID string
}
// ReserveCopyMove reserves quota before a copy/move/rename.
func ReserveCopyMove(ctx CopyMoveContext) error {
itemSize, ok := state.IndexedPathBytes(ctx.FromSourceName, ctx.FromIndexPath, ctx.IsSrcDir)
if !ok {
if state.HasApplicableQuotas(ctx.Principal, ctx.ToSourcePath, ctx.ToIndexPath, ctx.ShareHash, ctx.ShareLimitBytes) {
return newError(CodeUsageUnknown, "", "", 0, 0, 0, "indexed size unavailable for quota check")
}
return nil
}
checks := state.BuildCopyMoveReserveChecks(
ctx.Principal,
ctx.ToSourceName,
ctx.ToSourcePath,
ctx.FromIndexPath,
ctx.ToIndexPath,
ctx.Action,
itemSize,
ctx.OverwriteBytes,
ctx.ShareHash,
ctx.ShareLimitBytes,
)
if len(checks) == 0 {
return nil
}
if err := state.ReserveQuota(ctx.SessionID, checks); err != nil {
return mapReserveError(err, checks)
}
return nil
}
// CommitCopyMove commits reserved quota after a successful copy/move/rename.
func CommitCopyMove(sessionID string) error {
return state.CommitQuota(sessionID, 0)
}
// ReleaseCopyMove releases quota reservation on failure.
func ReleaseCopyMove(sessionID string) {
state.ReleaseQuota(sessionID)
}
// OverwriteBytesAtPath returns indexed bytes at dest when replacing an existing path.
func OverwriteBytesAtPath(sourceName, indexPath string, isDir bool) int64 {
size, ok := state.IndexedPathBytes(sourceName, indexPath, isDir)
if !ok {
return 0
}
return size
}
+95
View File
@@ -0,0 +1,95 @@
package quota
import (
"errors"
"fmt"
)
const (
CodeExceeded = "quota_exceeded"
CodeMeasurementUnavailable = "quota_measurement_unavailable"
CodePathNotCountable = "quota_path_not_countable"
CodeUsageUnknown = "quota_usage_unknown"
CodeReservedCapacity = "quota_reserved_capacity"
CodeLengthRequired = "quota_length_required"
CodeRootImmutable = "quota_root_immutable"
)
// Error is returned when a quota check fails.
type Error struct {
Code string
QuotaKind string
QuotaID string
LimitBytes int64
UsedBytes int64
ReservedBytes int64
Message string
}
func (e *Error) Error() string {
return e.DisplayMessage()
}
// DisplayMessage returns a user-facing error string (no internal IDs).
func (e *Error) DisplayMessage() string {
if e.Message != "" {
return e.Message
}
return e.defaultMessage()
}
func (e *Error) defaultMessage() string {
switch e.Code {
case CodeExceeded:
switch e.QuotaKind {
case "share":
return "Share storage quota exceeded"
case "scope":
return "Source storage quota exceeded"
case "folder":
return "Folder storage quota exceeded"
}
return "Storage quota exceeded"
case CodeMeasurementUnavailable:
return "Storage measurement is not available yet"
case CodePathNotCountable:
return "Storage for this path cannot be measured"
case CodeUsageUnknown:
return "Storage usage is not available for quota check"
case CodeReservedCapacity:
return "Storage quota reservation failed"
case CodeLengthRequired:
return "Upload size is required when storage quota applies"
case CodeRootImmutable:
return "Root storage quota cannot be changed"
default:
return fmt.Sprintf("quota error: %s", e.Code)
}
}
func newError(code, kind, id string, limit, used, reserved int64, msg string) *Error {
return &Error{
Code: code,
QuotaKind: kind,
QuotaID: id,
LimitBytes: limit,
UsedBytes: used,
ReservedBytes: reserved,
Message: msg,
}
}
// IsQuotaError reports whether err is a quota Error.
func IsQuotaError(err error) bool {
var qe *Error
return errors.As(err, &qe)
}
// AsError unwraps a quota Error.
func AsError(err error) (*Error, bool) {
var qe *Error
if errors.As(err, &qe) {
return qe, true
}
return nil, false
}
+18
View File
@@ -0,0 +1,18 @@
package quota
import "testing"
func TestErrorDisplayMessage(t *testing.T) {
exceeded := newError(CodeExceeded, "folder", "uuid-id", 100, 90, 5, "")
if exceeded.DisplayMessage() != "Folder storage quota exceeded" {
t.Fatalf("folder exceeded: got %q", exceeded.DisplayMessage())
}
if exceeded.Error() != "Folder storage quota exceeded" {
t.Fatalf("Error() should match DisplayMessage: got %q", exceeded.Error())
}
custom := newError(CodeExceeded, "folder", "uuid-id", 100, 90, 5, "Custom limit message")
if custom.DisplayMessage() != "Custom limit message" {
t.Fatalf("custom message: got %q", custom.DisplayMessage())
}
}
+122
View File
@@ -0,0 +1,122 @@
package quota
import (
"os"
quotadb "github.com/gtsteffaniak/filebrowser/backend/internal/database/quota"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
"github.com/gtsteffaniak/filebrowser/backend/internal/state"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
)
// UploadContext describes an upload admission check.
type UploadContext struct {
Principal *users.User
SourceName string
SourcePath string
DestIndexPath string
DestRealPath string
ShareHash string
ShareLimitBytes int64
SessionID string
TotalSize int64
HasKnownSize bool
}
// CheckUploadLength verifies size is known when quotas apply.
func CheckUploadLength(ctx UploadContext) error {
shareLimit := ctx.ShareLimitBytes
if !state.HasApplicableQuotas(ctx.Principal, ctx.SourcePath, ctx.DestIndexPath, ctx.ShareHash, shareLimit) {
return nil
}
if !ctx.HasKnownSize || ctx.TotalSize < 0 {
return newError(CodeLengthRequired, "", "", 0, 0, 0, "upload size required when storage quota applies")
}
return nil
}
// ReserveUpload reserves quota for an upload session.
func ReserveUpload(ctx UploadContext) error {
delta := uploadReserveDelta(ctx)
if delta <= 0 {
return nil
}
checks := state.BuildReserveChecks(ctx.Principal, ctx.SourceName, ctx.SourcePath, ctx.DestIndexPath, ctx.ShareHash, ctx.ShareLimitBytes, delta)
if len(checks) == 0 {
return nil
}
if err := state.ReserveQuota(ctx.SessionID, checks); err != nil {
return mapReserveError(err, checks)
}
return nil
}
// CommitUpload commits quota after successful upload.
func CommitUpload(ctx UploadContext) error {
commitDelta := uploadCommitDelta(ctx)
if commitDelta > 0 {
return state.CommitQuota(ctx.SessionID, commitDelta)
}
state.ReleaseQuota(ctx.SessionID)
if commitDelta < 0 {
return state.ApplyAccountedUsageDelta(ctx.Principal, ctx.SourceName, ctx.SourcePath, ctx.DestIndexPath, ctx.ShareHash, commitDelta)
}
return nil
}
// ReleaseUpload releases quota reservation on failure.
func ReleaseUpload(sessionID string) {
state.ReleaseQuota(sessionID)
}
// PrincipalForUpload returns the user whose quota is charged.
func PrincipalForUpload(actor *users.User, shareOwnerID uint64, shareHash string) *users.User {
if shareHash != "" && shareOwnerID != 0 {
owner, err := state.GetUserByID(shareOwnerID)
if err == nil {
return &owner
}
}
return actor
}
// UploadCommitDelta returns signed byte change for an upload (negative when file shrinks).
func UploadCommitDelta(ctx UploadContext) int64 {
return uploadCommitDelta(ctx)
}
func uploadCommitDelta(ctx UploadContext) int64 {
if !ctx.HasKnownSize {
return 0
}
oldSize := int64(0)
if ctx.DestRealPath != "" {
if stat, err := os.Stat(ctx.DestRealPath); err == nil && !stat.IsDir() {
oldSize = stat.Size()
}
}
return ctx.TotalSize - oldSize
}
func uploadReserveDelta(ctx UploadContext) int64 {
delta := uploadCommitDelta(ctx)
if delta < 0 {
return 0
}
return delta
}
// ScopeQuotaForSource returns scope quota snapshot for sidebar.
func ScopeQuotaForSource(user *users.User, sourceName string) (quotadb.Snapshot, bool) {
sourceInfo, ok := settings.Config.Server.NameToSource[sourceName]
if !ok {
return quotadb.Snapshot{}, false
}
for _, bs := range user.BackendScopes {
if bs.Path != sourceInfo.Path || bs.Quota == nil || bs.Quota.LimitBytes <= 0 {
continue
}
return state.ScopeQuotaSnapshot(user, sourceName, bs.Quota), true
}
return quotadb.Snapshot{}, false
}
+49
View File
@@ -0,0 +1,49 @@
package quota
import (
"os"
"path/filepath"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
)
func TestUploadCommitDelta_shrinkReturnsNegative(t *testing.T) {
dir := t.TempDir()
dest := filepath.Join(dir, "file.bin")
if err := os.WriteFile(dest, make([]byte, 100), 0644); err != nil {
t.Fatal(err)
}
ctx := UploadContext{
Principal: &users.User{},
DestRealPath: dest,
TotalSize: 50,
HasKnownSize: true,
}
if got := UploadCommitDelta(ctx); got != -50 {
t.Fatalf("UploadCommitDelta shrink: got %d want -50", got)
}
if got := uploadReserveDelta(ctx); got != 0 {
t.Fatalf("uploadReserveDelta shrink: got %d want 0", got)
}
}
func TestUploadCommitDelta_growthPositive(t *testing.T) {
dir := t.TempDir()
dest := filepath.Join(dir, "file.bin")
if err := os.WriteFile(dest, make([]byte, 40), 0644); err != nil {
t.Fatal(err)
}
ctx := UploadContext{
Principal: &users.User{},
DestRealPath: dest,
TotalSize: 100,
HasKnownSize: true,
}
if got := UploadCommitDelta(ctx); got != 60 {
t.Fatalf("UploadCommitDelta growth: got %d want 60", got)
}
if got := uploadReserveDelta(ctx); got != 60 {
t.Fatalf("uploadReserveDelta growth: got %d want 60", got)
}
}
+28
View File
@@ -0,0 +1,28 @@
package quota
import (
"github.com/gtsteffaniak/filebrowser/backend/internal/state"
)
// mapReserveError converts reserve failures to quota errors.
func mapReserveError(err error, checks []state.QuotaReserveCheck) error {
if err == nil {
return nil
}
for _, chk := range checks {
if chk.LimitBytes <= 0 || chk.DeltaBytes <= 0 {
continue
}
reserved := state.SessionReservedForQuota(chk.QuotaID)
if chk.UsedBytes+reserved+chk.DeltaBytes > chk.LimitBytes {
return newError(CodeExceeded, chk.Kind, chk.QuotaID, chk.LimitBytes, chk.UsedBytes, reserved, "")
}
}
for _, chk := range checks {
if chk.LimitBytes > 0 {
reserved := state.SessionReservedForQuota(chk.QuotaID)
return newError(CodeExceeded, chk.Kind, chk.QuotaID, chk.LimitBytes, chk.UsedBytes, reserved, "")
}
}
return newError(CodeExceeded, "", "", 0, 0, 0, "")
}
+142
View File
@@ -0,0 +1,142 @@
package sharedefaults
import (
"encoding/json"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/share"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
)
// NormalizeUploadShareEditable ensures upload shares always request create permission.
func NormalizeUploadShareEditable(editable *share.ShareEditable) {
if editable != nil && editable.ShareType == "upload" {
editable.AllowCreate = true
}
}
// ApplyDefaultsToEditable merges default template values into editable for new shares.
func ApplyDefaultsToEditable(editable *share.ShareEditable, defaults settings.ShareDefaults) {
if editable == nil {
return
}
current := EditableToDefaults(*editable)
currentBytes, err := json.Marshal(current)
if err != nil {
return
}
defaultBytes, err := json.Marshal(defaults)
if err != nil {
return
}
mergedBytes, err := mergeJSON(defaultBytes, currentBytes)
if err != nil {
return
}
var merged settings.ShareDefaults
if err := json.Unmarshal(mergedBytes, &merged); err != nil {
return
}
*editable = DefaultsToEditable(merged)
}
// ApplyEnforcedDefaults overwrites enforced fields on editable from the defaults template.
func ApplyEnforcedDefaults(editable *share.ShareEditable, defaults settings.ShareDefaults, enforced settings.ShareDefaultsEnforcement) {
if editable == nil {
return
}
paths := settings.ShareEnforcedPathSet(enforced)
if len(paths) == 0 {
return
}
currentBytes, err := json.Marshal(EditableToDefaults(*editable))
if err != nil {
return
}
patchBytes, err := patchJSONForPaths(defaults, paths)
if err != nil {
return
}
mergedBytes, err := mergeJSON(currentBytes, patchBytes)
if err != nil {
return
}
var merged settings.ShareDefaults
if err := json.Unmarshal(mergedBytes, &merged); err != nil {
return
}
*editable = DefaultsToEditable(merged)
}
// ValidateEditableNotEnforced rejects requests where enforced fields differ from the defaults template.
func ValidateEditableNotEnforced(after *share.ShareEditable, enforced settings.ShareDefaultsEnforcement, defaults settings.ShareDefaults) error {
if after == nil {
return nil
}
paths := settings.ShareEnforcedPathSet(enforced)
if len(paths) == 0 {
return nil
}
afterDefaults := EditableToDefaults(*after)
for path := range paths {
expected, ok := settings.ShareDefaultsValueAtPath(defaults, path)
if !ok {
continue
}
actual, ok := settings.ShareDefaultsValueAtPath(afterDefaults, path)
if !ok || !jsonValuesEqual(expected, actual) {
return settings.ErrEnforcedShareValueMismatch{Path: path}
}
}
return nil
}
func patchJSONForPaths(source settings.ShareDefaults, paths map[string]struct{}) ([]byte, error) {
patchMap := make(map[string]interface{}, len(paths))
for path := range paths {
val, ok := settings.ShareDefaultsValueAtPath(source, path)
if !ok {
continue
}
patchMap[path] = val
}
return json.Marshal(patchMap)
}
func mergeJSON(baseJSON, patchJSON []byte) ([]byte, error) {
var base map[string]interface{}
if err := json.Unmarshal(baseJSON, &base); err != nil {
return nil, err
}
var patch map[string]interface{}
if err := json.Unmarshal(patchJSON, &patch); err != nil {
return nil, err
}
deepMergeMaps(base, patch)
return json.Marshal(base)
}
func deepMergeMaps(base, patch map[string]interface{}) {
for key, patchVal := range patch {
baseVal, exists := base[key]
if !exists {
base[key] = patchVal
continue
}
baseMap, baseOK := baseVal.(map[string]interface{})
patchMap, patchOK := patchVal.(map[string]interface{})
if baseOK && patchOK {
deepMergeMaps(baseMap, patchMap)
continue
}
base[key] = patchVal
}
}
func jsonValuesEqual(a, b interface{}) bool {
ab, errA := json.Marshal(a)
bb, errB := json.Marshal(b)
if errA != nil || errB != nil {
return false
}
return string(ab) == string(bb)
}
@@ -0,0 +1,74 @@
package sharedefaults
import (
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/share"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
)
func TestApplyDefaultsToEditable_mergesClientOverDefaults(t *testing.T) {
defaults := settings.ShareDefaults{
ShareType: "normal",
AllowModify: true,
DisableDownload: true,
Title: "Default title",
}
editable := share.ShareEditable{
FrontendShareInfo: share.FrontendShareInfo{
ShareType: "upload",
Title: "Custom",
},
}
ApplyDefaultsToEditable(&editable, defaults)
if editable.ShareType != "upload" {
t.Fatalf("client shareType should win, got %q", editable.ShareType)
}
if editable.Title != "Custom" {
t.Fatalf("client title should win, got %q", editable.Title)
}
if !editable.AllowModify || !editable.DisableDownload {
t.Fatal("expected defaults applied for unset fields")
}
}
func TestValidateEditableNotEnforced_blocksMismatch(t *testing.T) {
defaults := settings.ShareDefaults{
AllowModify: true,
SidebarLinks: []users.SidebarLink{{Name: "Info", Category: "shareInfo", Target: "#"}},
}
enforced := settings.ShareDefaultsEnforcement{AllowModify: true, SidebarLinks: true}
editable := share.ShareEditable{
FrontendShareInfo: share.FrontendShareInfo{
AllowModify: false,
SidebarLinks: []users.SidebarLink{{Name: "Other", Category: "custom", Target: "/x"}},
},
}
if err := ValidateEditableNotEnforced(&editable, enforced, defaults); err == nil {
t.Fatal("expected enforcement error")
}
}
func TestValidateEditableNotEnforced_blocksZeroEnforcedValue(t *testing.T) {
defaults := settings.ShareDefaults{AllowModify: false}
enforced := settings.ShareDefaultsEnforcement{AllowModify: true}
editable := share.ShareEditable{
FrontendShareInfo: share.FrontendShareInfo{AllowModify: true},
}
if err := ValidateEditableNotEnforced(&editable, enforced, defaults); err == nil {
t.Fatal("expected enforcement error for zero-valued enforced default")
}
}
func TestApplyEnforcedDefaults_appliesZeroBool(t *testing.T) {
defaults := settings.ShareDefaults{AllowModify: false}
enforced := settings.ShareDefaultsEnforcement{AllowModify: true}
editable := share.ShareEditable{
FrontendShareInfo: share.FrontendShareInfo{AllowModify: true},
}
ApplyEnforcedDefaults(&editable, defaults, enforced)
if editable.AllowModify {
t.Fatal("expected enforced allowModify=false")
}
}
+89
View File
@@ -0,0 +1,89 @@
package sharedefaults
import (
"github.com/gtsteffaniak/filebrowser/backend/internal/database/share"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
)
// DefaultsToEditable converts defaults template to a share editable payload.
func DefaultsToEditable(d settings.ShareDefaults) share.ShareEditable {
return share.ShareEditable{
FrontendShareInfo: share.FrontendShareInfo{
ShareTheme: d.ShareTheme,
DisableAnonymous: d.DisableAnonymous,
DisableThumbnails: d.DisableThumbnails,
KeepAfterExpiration: d.KeepAfterExpiration,
ThemeColor: d.ThemeColor,
Title: d.Title,
Description: d.Description,
Favicon: d.Favicon,
QuickDownload: d.QuickDownload,
HideNavButtons: d.HideNavButtons,
DisableSidebar: d.DisableSidebar,
DisableShareCard: d.DisableShareCard,
EnforceDarkLightMode: d.EnforceDarkLightMode,
ViewMode: d.ViewMode,
EnableOnlyOffice: d.EnableOnlyOffice,
ShareType: d.ShareType,
AllowDelete: d.AllowDelete,
AllowCreate: d.AllowCreate,
AllowModify: d.AllowModify,
DisableFileViewer: d.DisableFileViewer,
DisableDownload: d.DisableDownload,
AllowReplacements: d.AllowReplacements,
SidebarLinks: append([]users.SidebarLink(nil), d.SidebarLinks...),
ShowHidden: d.ShowHidden,
DisableLoginOption: d.DisableLoginOption,
},
ShareLimits: share.ShareLimits{
MaxBandwidth: d.MaxBandwidth,
AllowedUsernames: append([]string(nil), d.AllowedUsernames...),
PerUserDownloadLimit: d.PerUserDownloadLimit,
ExtractEmbeddedSubtitles: d.ExtractEmbeddedSubtitles,
DownloadsLimit: d.DownloadsLimit,
QuotaLimitBytes: d.QuotaLimitBytes,
HideFileExt: d.HideFileExt,
Banner: d.Banner,
},
}
}
// EditableToDefaults extracts defaults-relevant fields from a share editable payload.
func EditableToDefaults(e share.ShareEditable) settings.ShareDefaults {
return settings.ShareDefaults{
ShareTheme: e.ShareTheme,
DisableAnonymous: e.DisableAnonymous,
DisableThumbnails: e.DisableThumbnails,
KeepAfterExpiration: e.KeepAfterExpiration,
ThemeColor: e.ThemeColor,
Title: e.Title,
Description: e.Description,
Favicon: e.Favicon,
QuickDownload: e.QuickDownload,
HideNavButtons: e.HideNavButtons,
DisableSidebar: e.DisableSidebar,
DisableShareCard: e.DisableShareCard,
EnforceDarkLightMode: e.EnforceDarkLightMode,
ViewMode: e.ViewMode,
EnableOnlyOffice: e.EnableOnlyOffice,
ShareType: e.ShareType,
AllowDelete: e.AllowDelete,
AllowCreate: e.AllowCreate,
AllowModify: e.AllowModify,
DisableFileViewer: e.DisableFileViewer,
DisableDownload: e.DisableDownload,
AllowReplacements: e.AllowReplacements,
SidebarLinks: append([]users.SidebarLink(nil), e.SidebarLinks...),
ShowHidden: e.ShowHidden,
DisableLoginOption: e.DisableLoginOption,
MaxBandwidth: e.MaxBandwidth,
AllowedUsernames: append([]string(nil), e.AllowedUsernames...),
PerUserDownloadLimit: e.PerUserDownloadLimit,
ExtractEmbeddedSubtitles: e.ExtractEmbeddedSubtitles,
DownloadsLimit: e.DownloadsLimit,
QuotaLimitBytes: e.QuotaLimitBytes,
HideFileExt: e.HideFileExt,
Banner: e.Banner,
}
}
@@ -0,0 +1,47 @@
package sharedefaults
import (
"encoding/json"
"fmt"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/share"
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
)
var sharePostBodyMetaKeys = map[string]struct{}{
"hash": {},
"password": {},
"path": {},
}
// MergeEditableUpdate overlays a partial POST body onto existing share editable fields.
// Only keys present in patchJSON replace existing values.
func MergeEditableUpdate(existing share.ShareEditable, patchJSON []byte) (share.ShareEditable, error) {
var patchMap map[string]json.RawMessage
if err := json.Unmarshal(patchJSON, &patchMap); err != nil {
return share.ShareEditable{}, fmt.Errorf("unmarshal share update patch: %w", err)
}
for key := range sharePostBodyMetaKeys {
delete(patchMap, key)
}
if len(patchMap) == 0 {
return existing, nil
}
trimmedPatch, err := json.Marshal(patchMap)
if err != nil {
return share.ShareEditable{}, fmt.Errorf("marshal share update patch: %w", err)
}
baseBytes, err := json.Marshal(existing)
if err != nil {
return share.ShareEditable{}, fmt.Errorf("marshal existing share editable: %w", err)
}
mergedBytes, err := settings.MergeUserDefaultsPatchJSONBytes(baseBytes, trimmedPatch)
if err != nil {
return share.ShareEditable{}, err
}
var merged share.ShareEditable
if err := json.Unmarshal(mergedBytes, &merged); err != nil {
return share.ShareEditable{}, fmt.Errorf("unmarshal merged share editable: %w", err)
}
return merged, nil
}
@@ -0,0 +1,31 @@
package sharedefaults
import (
"testing"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/share"
)
func TestMergeEditableUpdate_preservesOmittedFields(t *testing.T) {
existing := share.ShareEditable{
FrontendShareInfo: share.FrontendShareInfo{
AllowModify: true,
AllowDelete: false,
ShareType: "normal",
},
}
patch := []byte(`{"hash":"abc","allowDelete":true}`)
merged, err := MergeEditableUpdate(existing, patch)
if err != nil {
t.Fatal(err)
}
if !merged.AllowModify {
t.Fatal("expected allowModify preserved from existing share")
}
if !merged.AllowDelete {
t.Fatal("expected allowDelete updated from patch")
}
if merged.ShareType != "normal" {
t.Fatalf("shareType=%q want normal", merged.ShareType)
}
}
Loaded 100 of 480 files, more files were not shown because too many files have changed in this diff. Show more