Compare commits

..
8 Commits
Author SHA1 Message Date
Graham Steffaniak 6c876613bf Fix user creation (#276) 2025-01-06 14:59:51 -06:00
Graham Steffaniak b4b44fdad0 Update config.yaml 2025-01-06 10:03:00 -06:00
Graham Steffaniak 46631837fa Windows compatibility (#270) 2025-01-06 08:42:11 -06:00
Graham Steffaniak d880518029 initial v0.3.5 commit 2025-01-05 12:44:49 -06:00
Graham Steffaniak 93925d8430 updated roadmap 2024-12-26 11:38:29 -06:00
Graham Steffaniak 0270711a22 v0.3.4 initial release 2024-12-26 11:31:04 -06:00
Graham Steffaniak d2a3e50d37 v0.3.3 release (#257) 2024-12-16 18:01:55 -06:00
Graham Steffaniak 266a76459d v0.3.2 release (#250) 2024-12-02 11:14:50 -06:00
153 changed files with 2673 additions and 2626 deletions

No files matched your search

+16 -16
View File
@@ -7,22 +7,22 @@ on:
- "v[0-9]+.[0-9]+.[0-9]+"
jobs:
test_frontend:
name: Push release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3.0.0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.0.0
- name: Build
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile.playwright
push: false
#test_frontend:
# name: Push release
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# - name: Set up QEMU
# uses: docker/setup-qemu-action@v3.0.0
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v3.0.0
# - name: Build
# uses: docker/build-push-action@v6
# with:
# context: .
# file: ./Dockerfile.playwright
# push: false
push_pr_to_registry:
name: Push PR
runs-on: ubuntu-latest
+1 -1
View File
@@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.23.3'
go-version: 'stable'
- uses: golangci/golangci-lint-action@v5
with:
version: v1.60
+1 -1
View File
@@ -23,7 +23,7 @@ jobs:
id: extract_branch
- uses: actions/setup-go@v5
with:
go-version: 1.22.2
go-version: 'stable'
- name: Create Release
uses: softprops/action-gh-release@v2
with:
+4 -4
View File
@@ -10,7 +10,7 @@ permissions:
jobs:
push_release_to_registry:
name: Push dev release
name: Push release
runs-on: ubuntu-latest
steps:
- name: Checkout
@@ -35,16 +35,16 @@ jobs:
JSON="${{ steps.meta.outputs.tags }}"
# Use jq to remove 'v' from the version field
JSON=$(echo "$JSON" | sed 's/filebrowser:v/filebrowser:/')
echo "CLEANED_TAG=$JSON" >> $GITHUB_ENV
echo "cleaned_tag=$JSON" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
build-args: |
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
context: .
platforms: linux/amd64
file: ./Dockerfile
push: true
tags: ${{ env.CLEANED_TAG }}
tags: ${{ steps.modify-json.outputs.cleaned_tag }}
labels: ${{ steps.meta.outputs.labels }}
+3 -2
View File
@@ -3,8 +3,8 @@
_old
rice-box.go
.idea/
/filebrowser
/filebrowser.exe
/backend
/backend.exe
/frontend/dist
/frontend/pkg
/frontend/test-results
@@ -14,6 +14,7 @@ rice-box.go
/backend/test_config.yaml
/backend/srv
/backend/http/dist
/backend/http/embed/*
.DS_Store
node_modules
+67 -1
View File
@@ -2,6 +2,72 @@
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).
## v0.3.5
**New Features**
- More indexing configuration options possible. However consider waiting on using this feature, because I will soon have a full onboarding experience in the UI to manage sources instead.
- added config file options "sources" in the server config.
- can enable/disable indexing a specified list of directories/files
- can enable/disable indexing hidden files
- prepped for multiple sources (not supported yet!)
- Theme and Branding support (see updates to [configuration wiki](https://github.com/gtsteffaniak/filebrowser/wiki/Configuration) on how to use)
- Automatically expire shares https://github.com/gtsteffaniak/filebrowser/issues/208
**Notes**:
- MacOS application files (ending in ".app") were previously treated as folders, now they are treated as a single file.
- No longer indexes "$RECYCLE.BIN" or "System Volume Information" directories.
- Icon styling tweaked so all icons have a background.
- Updated Login page styling.
- Settings profile menu has been simplified, password changes happen in user management.
- Improved windows compatibility and built on windows platform to fix false windows defender warning.
- If no "root" location is provided in the server config, the default is the **current directory** (rather than `/srv` like before)
**Bugfixes**:
- Fixed setting share expiration time would not work due to type conversion error.
- More safari fixes related to text-selection.
- Sort by name value sorting ignores the extension, only sorts by name https://github.com/gtsteffaniak/filebrowser/issues/230
- Fixed manual language selection issue.
- Fixed exact date time issue.
New login page:
<img width="300" alt="image" src="https://github.com/user-attachments/assets/d3ed359e-a969-4f6a-9f72-94d2b68aba49" />
Example branding in sidebar:
<img width="500" alt="image2" src="https://github.com/user-attachments/assets/d8ee14ca-4495-4106-9d26-631a5937e134" />
Example user settings page:
<img width="500" alt="image3" src="https://github.com/user-attachments/assets/79757a11-669e-4597-bd3d-e41efd667a1e" />
## v0.3.4
**Bugfixes**:
- Safari right-click actions.
- Some small image viewer behavior
- Progressive webapp "install to homescreen" fix.
## v0.3.3
**New Features**
- Navigating remembers your previous scroll position when opening items and then navigating backwards.
- New Icons with larger selection of file types
- file "type" is shown on item info page.
- added optional non-root "filebrowser" user for docker image. See https://github.com/gtsteffaniak/filebrowser/issues/251
- File preview supports more file types:
- images: jpg, bmp, gif, tiff, png, svg, heic, webp
**Notes**:
- The file "type" is now either "directory" or a specific mimetype such as "text/xml".
- update safari styling
**Bugfixes**:
- Delete/move file/folders sometimes wouldn't work.
- Possible fix for context menu not showing issue. See https://github.com/gtsteffaniak/filebrowser/issues/251
- Fixed drag/drop not refreshing immediately to reflect changes.
## v0.3.2
**New Features**
@@ -186,7 +252,7 @@ This change focuses on minimizing and simplifying build process.
- The shell feature has been deprecated.
- Custom commands can be executed within the Docker container if needed.
- The JSON config file is no longer used.
- All configurations are now performed via the advanced `filebrowser.yaml`.
- All configurations are now performed via the advanced `config.yaml`.
- The only allowed flag is specifying the config file.
- Removed old code for migrating database versions.
- Eliminated all unused `cmd` code.
+8 -4
View File
@@ -6,8 +6,8 @@ COPY ./backend ./
#RUN swag init --output swagger/docs
RUN ln -s swagger /usr/local/go/src/
RUN go build -ldflags="-w -s \
-X 'github.com/gtsteffaniak/filebrowser/version.Version=${VERSION}' \
-X 'github.com/gtsteffaniak/filebrowser/version.CommitSHA=${REVISION}'" \
-X 'github.com/gtsteffaniak/filebrowser/backend/version.Version=${VERSION}' \
-X 'github.com/gtsteffaniak/filebrowser/backend/version.CommitSHA=${REVISION}'" \
-o filebrowser .
FROM node:slim AS nbuild
@@ -20,8 +20,12 @@ RUN npm run build-docker
FROM alpine:latest
ENV FILEBROWSER_NO_EMBEDED="true"
RUN apk --no-cache add ca-certificates mailcap
COPY --from=base /app/filebrowser* ./
WORKDIR /home/filebrowser
RUN adduser -D -s /bin/true -u 1000 filebrowser
USER filebrowser
COPY --from=base --chown=filebrowser:1000 /app/filebrowser* ./
COPY --from=nbuild --chown=filebrowser:1000 /app/dist/ ./http/dist/
USER root
# exposing default port for auto discovery.
EXPOSE 80
COPY --from=nbuild /app/dist/ ./http/dist/
ENTRYPOINT [ "./filebrowser" ]
+1 -1
View File
@@ -8,7 +8,7 @@ WORKDIR /app
COPY ./frontend/package.json ./
RUN npm i --maxsockets 1
RUN npx playwright install --with-deps firefox
COPY [ "backend/filebrowser.yaml", "./" ]
COPY [ "backend/config.yaml", "./" ]
COPY ./frontend/ ./frontend
WORKDIR /app/frontend
RUN npm run build-docker
+35 -121
View File
@@ -6,57 +6,53 @@
</p>
<h3 align="center">FileBrowser Quantum - A modern web-based file manager</h3>
<p align="center">
<img width="800" src="https://github.com/user-attachments/assets/e4a47229-66f8-4838-9575-dd2413596688" title="Main Screenshot">
<img width="800" src="https://github.com/user-attachments/assets/b16acd67-0292-437a-a06c-bc83f95758e6" title="Main Screenshot">
</p>
> [!Note]
> Starting with `v0.3.0` API routes have been slightly altered for friendly usage outside of the UI. The resources API returns items in separate `files` and `folder` objects now.
> Starting with v0.3.3, configuration file mapping is different to support non-root user. Now, the default config file name is `config.yaml` and in docker the path is `/home/filebrowser/config.yaml` and `/home/filebrowser/<database_file>`. Please read the usage below to properly update your config to point the new config location. (open an issue for any help needed)
> [!WARNING]
> - There is no stable version yet. Always check release notes for bug fixes on functionality that may have been changed. If you notice any unexpected behavior -- please open an issue to have it fixed soon.
> - If on windows, please use docker. The windows binary is unstable and may not work.
> There is no stable version yet. Always check release notes for bug fixes on functionality that may have been changed. If you notice any unexpected behavior -- please open an issue to have it fixed soon.
FileBrowser Quantum is a fork of the file browser opensource project with the following changes:
FileBrowser Quantum is a fork of the file browser open-source project with the following changes:
1. [x] Indexes files efficiently. See [indexing readme](./docs/indexing.md)
1. ✅ Indexes files efficiently. (See [indexing Wiki](https://github.com/gtsteffaniak/filebrowser/wiki/Indexing) for more info.)
- Real-time search results as you type
- Search supports file/folder sizes and many file type filters.
- Enhanced interactive results that show file/folder sizes.
1. [x] Revamped and simplified GUI navbar and sidebar menu.
- Additional compact view mode as well as refreshed view mode
styles.
2. ✅ Revamped and simplified GUI navbar and sidebar menu.
- Additional compact view mode as well as refreshed view mode styles.
- Many graphical and user experience improvements.
- right-click context menu
1. [x] Revamped and simplified configuration via `filebrowser.yml` config file.
1. [x] Better listing browsing
- Switching view modes is instant
- Folder sizes are shown as well
- Changing Sort order is instant
- The entire directory is loaded in 1/3 the time
1. [x] Developer API support
- Can create long-live API Tokens.
3. ✅ Revamped and simplified configuration via `config.yaml` config file.
4. ✅ Better listing browsing
- Instantly Switches view modes and sort order without reloading data.
- Folder sizes are displayed
- Navigating remembers the scroll position, navigating back keeps the last scroll position.
5. ✅ Developer API support
- Ability to create long-live API Tokens.
- Helpful Swagger page available at `/swagger` endpoint.
Notable features that this fork *does not* have (removed):
- jobs/runners are not supported yet (planned).
- shell commands are completely removed and will not be returned.
- themes and branding are not fully supported yet (planned).
- ❌ jobs/runners are not supported yet (planned).
- ❌ per-user rules are not supported yet (planned).
- ❌ pagination for directory items for extremely large directories.
- ❌ shell commands are completely removed and will not be returned.
- see feature matrix below for more.
- pagination for directory items, so large directories with more than 100,000 items may be slow to load or not load at all.
## About
FileBrowser Quantum provides a file-managing interface within a specified directory
and can be used to upload, delete, preview, rename, and edit your files.
It allows the creation of multiple users and each user can have its
directory.
It allows the creation of multiple users and each user can have its directory.
This repository is a fork of the original [filebrowser](https://github.com/filebrowser/filebrowser)
with a collection of changes that make this program work better in terms of
aesthetics and performance. Improved search, simplified UI
(without removing features) and more secure and up-to-date
build are just a few examples.
This repository is a fork of the original [filebrowser](https://github.com/filebrowser/filebrowser)
with a collection of changes that make this program work better in terms of
aesthetics and performance. Improved search, simplified UI
(without removing features) and more secure and up-to-date
build are just a few examples.
FileBrowser Quantum differs significantly from the original.
There are hundreds of thousands of lines changed and they are generally
@@ -67,10 +63,11 @@ focus of this fork is on a few key principles:
- Minimize external dependencies and standard library usage.
- Of course -- adding much-needed features.
For more, see the [Q&A Wiki](https://github.com/gtsteffaniak/filebrowser/wiki/Q&A)
## Look
One way you can observe the improved user experience is how I changed
the UI. The Navbar is simplified to a three-component system :
The UI has a simple three-component navigation system :
1. (Left) The slide-out action panel button
2. (Middle) The powerful search bar.
@@ -84,108 +81,29 @@ a popup menu.
<p align="center">
<img width="800" src="https://github.com/user-attachments/assets/2be7a6c5-0f95-4d9f-bc05-484ee71246d8" title="Search GIF">
<img width="800" src="https://github.com/user-attachments/assets/f55a6f1f-b930-4399-98b5-94da6e90527a" title="Navigation GIF">
<img width="800" src="https://github.com/user-attachments/assets/75226dc4-9802-46f0-9e3c-e4403d3275da" title="Main Screenshot">
<img width="800" src="https://github.com/user-attachments/assets/93b019de-d38f-4aaa-bde3-3ba4e99ecd25" title="Main Screenshot">
</p>
## Install
## Install and Configuration
Using docker:
1. docker run (no persistent db):
```
docker run -it -v /path/to/folder:/srv -p 80:80 gtstef/filebrowser
```
1. docker compose:
- with local storage
```
services:
filebrowser:
volumes:
- '/path/to/folder:/srv' # required (for now not configurable)
- './database:/database' # optional if you want db to persist - configure a path under "database" dir in config file.
- './filebrowser.yaml:/filebrowser.yaml' # required
ports:
- '80:80'
image: gtstef/filebrowser
restart: always
```
- with network share
```
services:
filebrowser:
volumes:
- 'storage:/srv' # required (for now not configurable)
- './database:/database' # optional if you want db to persist - configure a path under "database" dir in config file.
- './filebrowser.yaml:/filebrowser.yaml' # required
ports:
- '80:80'
image: gtstef/filebrowser
restart: always
volumes:
storage:
driver_opts:
type: cifs
o: "username=admin,password=password,rw" # enter valid info here
device: "//192.168.1.100/share/" # enter valid info here
```
Not using docker (not recommended), download your binary from releases and run with your custom config file:
```
./filebrowser -c <filebrowser.yml or other /path/to/config.yaml>
```
See the [Configuration Wiki](https://github.com/gtsteffaniak/filebrowser/wiki/Configuration)
## Command Line Usage
There are very few commands available. There are 3 actions done via the command line:
1. Running the program, as shown in the install step. The only argument used is the config file if you choose to override the default "filebrowser.yaml"
2. Checking the version info via `./filebrowser version`
3. Updating the DB, which currently only supports adding users via `./filebrowser set -u username,password [-a] [-s "example/scope"]`
See the [CLI Wiki](https://github.com/gtsteffaniak/filebrowser/wiki/CLI)
## API Usage
FileBrowser Quantum allows for the creation of API tokens which can create users, access file information, and update user settings just like what can be done from the UI. You can create API tokens from the settings page via "API Management" section. This section will only show up if the user has "API" permissions, which can be granted by editing the user in user management.
Regardless of whether a user has API permissions, anyone can visit the swagger page which is found at `/swagger`. This swagger page uses a short-live token (2-hour exp) that the UI uses, but allows for quick access to all the API's and their described usage and requirements:
![image](https://github.com/user-attachments/assets/12abd1f6-21d3-4437-98ed-9b0da6cf2c73)
When using the API outside of swagger, you will need to set the API token as a bearer token authentication type. This means the authorization header will look like `Authorization: Bearer <token>`. For example in Postman:
Successful Request:
<p align="center"><img width="500" alt="image" src="https://github.com/user-attachments/assets/4f18fa8a-8d87-4f40-9dc7-3d4407769b59"></p>
Failed Request
<p align="center"><img width="500" alt="image" src="https://github.com/user-attachments/assets/4da0deae-f93d-4d94-83b1-68806afb343a"></p>
See the [API Wiki](https://github.com/gtsteffaniak/filebrowser/wiki/API)
## Configuration
All configuration is now done via a single configuration file:
`filebrowser.yaml`, here is an example of minimal [configuration
file](./backend/filebrowser.yaml).
View the [Configuration Help Page](./docs/configuration.md) for available
configuration options and other help.
Configuration is done via the `config.yaml`, see the [Configuration Wiki](https://github.com/gtsteffaniak/filebrowser/wiki/Configuration) for available configuration options and other help.
## Migration from the original filebrowser
If you currently use the original filebrowser but want to try using this.
I would recommend that you start fresh without reusing the database. However,
If you want to migrate your existing database to FileBrowser Quantum,
visit the [migration
readme](./docs/migration.md)
See the [Migration Wiki](https://github.com/gtsteffaniak/filebrowser/wiki/Migration)
## Comparison Chart
@@ -228,7 +146,7 @@ file space quotas | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
text-based files editor | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
office file support | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ |
Themes | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ |
Branding support | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ |
Branding support | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ |
activity log | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
Comments support | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
collaboration on same file | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
@@ -237,7 +155,3 @@ Starred/pinned files | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
Content preview icons | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ |
Plugins support | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ |
Chromecast support | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
## Roadmap
see [Roadmap Page](./docs/roadmap.md)
+12 -13
View File
@@ -6,7 +6,7 @@ builds:
# Build configuration for darwin and linux
- id: default
ldflags: &ldflags
- -s -w -X github.com/gtsteffaniak/filebrowser/version.Version={{ .Version }} -X github.com/gtsteffaniak/filebrowser/version.CommitSHA={{ .ShortCommit }}
- -s -w -X github.com/gtsteffaniak/filebrowser/backend/version.Version={{ .Version }} -X github.com/gtsteffaniak/filebrowser/backend/version.CommitSHA={{ .ShortCommit }}
main: main.go
binary: filebrowser
goos:
@@ -17,22 +17,21 @@ builds:
- arm
- arm64
goarm:
- 6
- 7
- "6"
- "7"
hooks:
post:
post:
- upx {{ .Path }} # Compress the binary with UPX
# Build configuration for windows without arm
- id: windows
ldflags: *ldflags
main: main.go
binary: filebrowser
goos:
- windows
goarch:
- amd64
- arm64
# - id: windows
# ldflags: *ldflags
# main: main.go
# binary: filebrowser
# goos:
# - windows
# goarch:
# - amd64
archives:
- name_template: "{{.Os}}-{{.Arch}}{{if .Arm}}v{{.Arm}}{{end}}-{{ .ProjectName }}"
+1 -1
View File
@@ -3,7 +3,7 @@ package auth
import (
"net/http"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
// Auther is the authentication interface.
+3 -3
View File
@@ -9,9 +9,9 @@ import (
"os/exec"
"strings"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
type hookCred struct {
+3 -4
View File
@@ -7,8 +7,8 @@ import (
"os"
"strings"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
type jsonCred struct {
@@ -24,7 +24,6 @@ type JSONAuth struct {
// Auth authenticates the user via a json in content body.
func (a JSONAuth) Auth(r *http.Request, userStore *users.Storage) (*users.User, error) {
config := &settings.Config
var cred jsonCred
if r.Body == nil {
@@ -47,7 +46,7 @@ func (a JSONAuth) Auth(r *http.Request, userStore *users.Storage) (*users.User,
return nil, os.ErrPermission
}
}
u, err := userStore.Get(config.Server.Root, cred.Username)
u, err := userStore.Get(files.RootPaths["default"], cred.Username)
if err != nil || !users.CheckPwd(cred.Password, u.Password) {
return nil, os.ErrPermission
}
+3 -3
View File
@@ -3,8 +3,8 @@ package auth
import (
"net/http"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
// MethodNoAuth is used to identify no auth.
@@ -15,7 +15,7 @@ type NoAuth struct{}
// Auth uses authenticates user 1.
func (a NoAuth) Auth(r *http.Request, usr *users.Storage) (*users.User, error) {
return usr.Get(settings.Config.Server.Root, uint(1))
return usr.Get(files.RootPaths["default"], uint(1))
}
// LoginPage tells that no auth doesn't require a login page.
+4 -5
View File
@@ -4,10 +4,9 @@ import (
"net/http"
"os"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
// MethodProxyAuth is used to identify no auth.
@@ -21,7 +20,7 @@ type ProxyAuth struct {
// Auth authenticates the user via an HTTP header.
func (a ProxyAuth) Auth(r *http.Request, usr *users.Storage) (*users.User, error) {
username := r.Header.Get(a.Header)
user, err := usr.Get(settings.Config.Server.Root, username)
user, err := usr.Get(files.RootPaths["default"], username)
if err == errors.ErrNotExist {
return nil, os.ErrPermission
}
+1 -1
View File
@@ -1,7 +1,7 @@
package auth
import (
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
// StorageBackend is a storage backend for auth storage.
+51 -28
View File
@@ -1,43 +1,66 @@
== Running benchmark ==
/usr/local/go/bin/go
? github.com/gtsteffaniak/filebrowser [no test files]
? github.com/gtsteffaniak/filebrowser/auth [no test files]
? github.com/gtsteffaniak/filebrowser/cmd [no test files]
? github.com/gtsteffaniak/filebrowser/backend [no test files]
? github.com/gtsteffaniak/filebrowser/backend/auth [no test files]
? github.com/gtsteffaniak/filebrowser/backend/cmd [no test files]
PASS
ok github.com/gtsteffaniak/filebrowser/diskcache 0.004s
? github.com/gtsteffaniak/filebrowser/errors [no test files]
2024/10/07 12:46:34 could not update unknown type: unknown
ok github.com/gtsteffaniak/filebrowser/backend/diskcache 0.005s
? github.com/gtsteffaniak/filebrowser/backend/errors [no test files]
/home/graham/git/filebrowser /home/graham/git/filebrowser
/home/graham/git/filebrowser/files/file.go /home/graham/git/filebrowser
/home/graham/git/filebrowser/mnt/doesnt/exist /home/graham/git/filebrowser
2025/01/04 14:04:55 Initializing index and assessing file system complexity
2025/01/04 14:04:55 Starting full scan
2025/01/04 14:04:55 Index assessment : complexity=simple directories=0 files=0
2025/01/04 14:04:55 Time Spent Indexing : 0 seconds
2025/01/04 14:04:55 Next scan in 1m0s
2025/01/04 14:04:56 Initializing index and assessing file system complexity
2025/01/04 14:04:56 Starting full scan
2025/01/04 14:04:56 Index assessment : complexity=simple directories=0 files=0
2025/01/04 14:04:56 Time Spent Indexing : 0 seconds
2025/01/04 14:04:56 Next scan in 1m0s
goos: linux
goarch: amd64
pkg: github.com/gtsteffaniak/filebrowser/files
pkg: github.com/gtsteffaniak/filebrowser/backend/files
cpu: 11th Gen Intel(R) Core(TM) i5-11320H @ 3.20GHz
BenchmarkFillIndex-8 10 3847878 ns/op 758424 B/op 5567 allocs/op
BenchmarkSearchAllIndexes-8 10 780431 ns/op 173444 B/op 2014 allocs/op
BenchmarkFillIndex-8 2025/01/04 14:04:57 Initializing index and assessing file system complexity
2025/01/04 14:04:57 Starting full scan
2025/01/04 14:04:57 Index assessment : complexity=simple directories=0 files=0
2025/01/04 14:04:57 Time Spent Indexing : 0 seconds
2025/01/04 14:04:57 Next scan in 1m0s
10 3515090 ns/op 34273 B/op 451 allocs/op
BenchmarkCheckIndexExclude-8 10 156.2 ns/op 0 B/op 0 allocs/op
BenchmarkCheckIndexConditionsInclude-8 10 98.00 ns/op 0 B/op 0 allocs/op
2025/01/04 14:04:58 Initializing index and assessing file system complexity
2025/01/04 14:04:58 Starting full scan
2025/01/04 14:04:58 Index assessment : complexity=simple directories=0 files=0
2025/01/04 14:04:58 Time Spent Indexing : 0 seconds
2025/01/04 14:04:58 Next scan in 1m0s
BenchmarkSearchAllIndexes-8 2025/01/04 14:04:59 Initializing index and assessing file system complexity
2025/01/04 14:04:59 Starting full scan
2025/01/04 14:04:59 Index assessment : complexity=simple directories=0 files=0
2025/01/04 14:04:59 Time Spent Indexing : 0 seconds
2025/01/04 14:04:59 Next scan in 1m0s
10 766822 ns/op 34230 B/op 900 allocs/op
PASS
ok github.com/gtsteffaniak/filebrowser/files 0.073s
ok github.com/gtsteffaniak/filebrowser/backend/files 5.094s
PASS
ok github.com/gtsteffaniak/filebrowser/fileutils 0.003s
2024/10/07 12:46:34 h: 401 <nil>
2024/10/07 12:46:34 h: 401 <nil>
2024/10/07 12:46:34 h: 401 <nil>
2024/10/07 12:46:34 h: 401 <nil>
2024/10/07 12:46:34 h: 401 <nil>
2024/10/07 12:46:34 h: 401 <nil>
ok github.com/gtsteffaniak/filebrowser/backend/fileutils 0.002s
PASS
ok github.com/gtsteffaniak/filebrowser/http 0.080s
ok github.com/gtsteffaniak/filebrowser/backend/http 0.184s
PASS
ok github.com/gtsteffaniak/filebrowser/img 0.137s
ok github.com/gtsteffaniak/filebrowser/backend/img 0.123s
PASS
ok github.com/gtsteffaniak/filebrowser/rules 0.002s
ok github.com/gtsteffaniak/filebrowser/backend/runner 0.004s
PASS
ok github.com/gtsteffaniak/filebrowser/runner 0.003s
ok github.com/gtsteffaniak/filebrowser/backend/settings 0.005s
? github.com/gtsteffaniak/filebrowser/backend/share [no test files]
? github.com/gtsteffaniak/filebrowser/backend/storage [no test files]
? github.com/gtsteffaniak/filebrowser/backend/storage/bolt [no test files]
? github.com/gtsteffaniak/filebrowser/backend/swagger/docs [no test files]
PASS
ok github.com/gtsteffaniak/filebrowser/settings 0.004s
? github.com/gtsteffaniak/filebrowser/share [no test files]
? github.com/gtsteffaniak/filebrowser/storage [no test files]
? github.com/gtsteffaniak/filebrowser/storage/bolt [no test files]
ok github.com/gtsteffaniak/filebrowser/backend/users 0.003s
PASS
ok github.com/gtsteffaniak/filebrowser/users 0.002s
? github.com/gtsteffaniak/filebrowser/utils [no test files]
? github.com/gtsteffaniak/filebrowser/version [no test files]
ok github.com/gtsteffaniak/filebrowser/backend/utils 0.002s
? github.com/gtsteffaniak/filebrowser/backend/version [no test files]
+30 -19
View File
@@ -7,17 +7,17 @@ import (
"os"
"strings"
"github.com/gtsteffaniak/filebrowser/diskcache"
"github.com/gtsteffaniak/filebrowser/files"
fbhttp "github.com/gtsteffaniak/filebrowser/http"
"github.com/gtsteffaniak/filebrowser/img"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/swagger/docs"
"github.com/gtsteffaniak/filebrowser/backend/diskcache"
"github.com/gtsteffaniak/filebrowser/backend/files"
fbhttp "github.com/gtsteffaniak/filebrowser/backend/http"
"github.com/gtsteffaniak/filebrowser/backend/img"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/swagger/docs"
"github.com/swaggo/swag"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/version"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/version"
)
func getStore(config string) (*storage.Storage, bool) {
@@ -31,13 +31,14 @@ func getStore(config string) (*storage.Storage, bool) {
}
func generalUsage() {
fmt.Printf(`usage: ./html-web-crawler <command> [options] --urls <urls>
fmt.Printf(`usage: ./filebrowser <command> [options]
commands:
collect Collect data from URLs
crawl Crawl URLs and collect data
install Install chrome browser for javascript enabled scraping.
Note: Consider instead to install via native package manager,
then set "CHROME_EXECUTABLE" in the environment
-v Print the version
-c Print the default config file
set -u Username and password for the new user
set -a Create user as admin
set -s Specify a user scope
set -h Print this help message
` + "\n")
}
@@ -47,7 +48,7 @@ func StartFilebrowser() {
var help bool
// Override the default usage output to use generalUsage()
flag.Usage = generalUsage
flag.StringVar(&configPath, "c", "filebrowser.yaml", "Path to the config file.")
flag.StringVar(&configPath, "c", "config.yaml", "Path to the config file, default: config.yaml")
flag.BoolVar(&help, "h", false, "Get help about commands")
// Parse global flags (before subcommands)
@@ -67,7 +68,7 @@ func StartFilebrowser() {
setCmd.StringVar(&user, "u", "", "Comma-separated username and password: \"set -u <username>,<password>\"")
setCmd.BoolVar(&asAdmin, "a", false, "Create user as admin user, used in combination with -u")
setCmd.StringVar(&scope, "s", "", "Specify a user scope, otherwise default user config scope is used")
setCmd.StringVar(&dbConfig, "c", "filebrowser.yaml", "Path to the config file.")
setCmd.StringVar(&dbConfig, "c", "config.yaml", "Path to the config file, default: config.yaml")
// Parse subcommand flags only if a subcommand is specified
if len(os.Args) > 1 {
@@ -122,14 +123,24 @@ func StartFilebrowser() {
log.Printf("Using Config file : %v", configPath)
log.Println("Embeded frontend :", os.Getenv("FILEBROWSER_NO_EMBEDED") != "true")
log.Println(database)
log.Println("Sources :", settings.Config.Server.Root)
sources := []string{}
for _, v := range settings.Config.Server.Sources {
sources = append(sources, v.Name+": "+v.Path)
}
log.Println("Sources :", sources)
serverConfig := settings.Config.Server
swagInfo := docs.SwaggerInfo
swagInfo.BasePath = serverConfig.BaseURL
swag.Register(docs.SwaggerInfo.InstanceName(), swagInfo)
// initialize indexing and schedule indexing ever n minutes (default 5)
go files.InitializeIndex(serverConfig.Indexing)
sourceConfigs := settings.Config.Server.Sources
if len(sourceConfigs) == 0 {
log.Fatal("No sources configured, exiting...")
}
for _, source := range sourceConfigs {
go files.Initialize(source)
}
if err := rootCMD(store, &serverConfig); err != nil {
log.Fatal("Error starting filebrowser:", err)
}
+4 -4
View File
@@ -5,10 +5,10 @@ import (
"github.com/spf13/cobra"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func init() {
+4 -4
View File
@@ -6,10 +6,10 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func init() {
+4 -4
View File
@@ -5,10 +5,10 @@ import (
"github.com/spf13/cobra"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func init() {
+1 -1
View File
@@ -1,7 +1,7 @@
package cmd
import (
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/spf13/cobra"
)
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"github.com/spf13/cobra"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
var usersCmd = &cobra.Command{
+3 -3
View File
@@ -3,9 +3,9 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func init() {
+2 -2
View File
@@ -1,8 +1,8 @@
package cmd
import (
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/utils"
"github.com/spf13/cobra"
)
+3 -3
View File
@@ -3,9 +3,9 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func init() {
+3 -3
View File
@@ -8,9 +8,9 @@ import (
"github.com/spf13/cobra"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func init() {
+2 -2
View File
@@ -3,8 +3,8 @@ package cmd
import (
"log"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/utils"
"github.com/spf13/cobra"
)
+3 -3
View File
@@ -3,9 +3,9 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func init() {
+2 -2
View File
@@ -10,8 +10,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func mustGetString(flags *pflag.FlagSet, flag string) string {
@@ -1,7 +1,7 @@
server:
port: 80
baseURL: "/"
root: "./srv"
root: "/srv"
auth:
method: password
signup: false
Binary file not shown.
+23 -2
View File
@@ -24,11 +24,24 @@ var documentTypes = []string{
".pdf", // Portable Document Format
".odt", // OpenDocument Text
".rtf", // Rich Text Format
".conf",
".bash_history",
".gitignore",
".htpasswd",
".profile",
".dockerignore",
".editorconfig",
// Presentation Formats
".ppt", ".pptx", // Microsoft PowerPoint
".odp", // OpenDocument Presentation
// google docs
".gdoc",
// google sheet
".gsheet",
// Spreadsheet Formats
".xls", ".xlsx", // Microsoft Excel
".ods", // OpenDocument Spreadsheet
@@ -102,6 +115,16 @@ type SearchOptions struct {
Terms []string
}
func extendedMimeTypeCheck(extension string) string {
if isDoc(extension) {
return "application/document"
}
if isText(extension) {
return "text/plain"
}
return "blob"
}
func ParseSearch(value string) SearchOptions {
opts := SearchOptions{
Conditions: map[string]bool{
@@ -199,8 +222,6 @@ func IsMatchingType(extension string, matchType string) bool {
switch matchType {
case "doc":
return isDoc(extension)
case "pdf":
return extension == ".pdf"
case "text":
return isText(extension)
case "archive":
+1 -1
View File
@@ -22,7 +22,7 @@ func TestIsMatchingType(t *testing.T) {
extension string
expectedType string
}{
{".pdf", "pdf"},
{".pdf", "doc"},
{".doc", "doc"},
{".docx", "doc"},
{".json", "text"},
+58 -126
View File
@@ -11,6 +11,7 @@ import (
"io"
"mime"
"net/http"
"os"
"path/filepath"
"sort"
@@ -20,11 +21,9 @@ import (
"time"
"unicode/utf8"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/fileutils"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/fileutils"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
var (
@@ -56,11 +55,13 @@ type ExtendedFileInfo struct {
Subtitles []string `json:"subtitles,omitempty"`
Checksums map[string]string `json:"checksums,omitempty"`
Token string `json:"token,omitempty"`
RealPath string `json:"-"`
}
// FileOptions are the options when getting a file info.
type FileOptions struct {
Path string // realpath
Source string
IsDir bool
Modify bool
Expand bool
@@ -74,9 +75,15 @@ func (f FileOptions) Components() (string, string) {
}
func FileInfoFaster(opts FileOptions) (ExtendedFileInfo, error) {
index := GetIndex(rootPath)
opts.Path = index.makeIndexPath(opts.Path)
response := ExtendedFileInfo{}
if opts.Source == "" {
opts.Source = "default"
}
index := GetIndex(opts.Source)
if index == nil {
return response, fmt.Errorf("could not get index: %v ", opts.Source)
}
opts.Path = index.makeIndexPath(opts.Path)
// Lock access for the specific path
pathMutex := getMutex(opts.Path)
pathMutex.Lock()
@@ -85,7 +92,7 @@ func FileInfoFaster(opts FileOptions) (ExtendedFileInfo, error) {
return response, os.ErrPermission
}
_, isDir, err := GetRealPath(opts.Path)
realPath, isDir, err := index.GetRealPath(opts.Path)
if err != nil {
return response, err
}
@@ -119,13 +126,14 @@ func FileInfoFaster(opts FileOptions) (ExtendedFileInfo, error) {
return response, err
}
if opts.Content {
content, err := getContent(opts.Path)
content, err := getContent("default", opts.Path)
if err != nil {
return response, err
}
response.Content = content
}
response.FileInfo = info
response.RealPath = realPath
return response, nil
}
@@ -159,61 +167,26 @@ func GetChecksum(fullPath, algo string) (map[string]string, error) {
return subs, nil
}
// RealPath gets the real path for the file, resolving symlinks if supported.
func (i *FileInfo) RealPath() string {
realPath, _, _ := GetRealPath(rootPath, i.Path)
realPath, err := filepath.EvalSymlinks(realPath)
if err == nil {
return realPath
}
return i.Path
}
func GetRealPath(relativePath ...string) (string, bool, error) {
combined := []string{settings.Config.Server.Root}
for _, path := range relativePath {
combined = append(combined, strings.TrimPrefix(path, settings.Config.Server.Root))
}
joinedPath := filepath.Join(combined...)
isDir, _ := utils.RealPathCache.Get(joinedPath + ":isdir").(bool)
cached, ok := utils.RealPathCache.Get(joinedPath).(string)
if ok && cached != "" {
return cached, isDir, nil
}
// Convert relative path to absolute path
absolutePath, err := filepath.Abs(joinedPath)
if err != nil {
return absolutePath, false, fmt.Errorf("could not get real path: %v, %s", combined, err)
}
// Resolve symlinks and get the real path
realPath, isDir, err := resolveSymlinks(absolutePath)
if err == nil {
utils.RealPathCache.Set(joinedPath, realPath)
utils.RealPathCache.Set(joinedPath+":isdir", isDir)
}
return realPath, isDir, err
}
func DeleteFiles(absPath string, opts FileOptions) error {
func DeleteFiles(source, absPath string, dirPath string) error {
err := os.RemoveAll(absPath)
if err != nil {
return err
}
index := GetIndex(rootPath)
err = index.RefreshFileInfo(opts)
index := GetIndex(source)
refreshConfig := FileOptions{Path: dirPath, IsDir: true}
err = index.RefreshFileInfo(refreshConfig)
if err != nil {
return err
}
return nil
}
func MoveResource(realsrc, realdst string, isSrcDir bool) error {
func MoveResource(source, realsrc, realdst string, isSrcDir bool) error {
err := fileutils.MoveFile(realsrc, realdst)
if err != nil {
return err
}
index := GetIndex(rootPath)
index := GetIndex(source)
// refresh info for source and dest
err = index.RefreshFileInfo(FileOptions{
Path: realsrc,
@@ -233,12 +206,12 @@ func MoveResource(realsrc, realdst string, isSrcDir bool) error {
return nil
}
func CopyResource(realsrc, realdst string, isSrcDir bool) error {
func CopyResource(source, realsrc, realdst string, isSrcDir bool) error {
err := fileutils.CopyFile(realsrc, realdst)
if err != nil {
return err
}
index := GetIndex(rootPath)
index := GetIndex(source)
refreshConfig := FileOptions{Path: realdst, IsDir: true}
if !isSrcDir {
refreshConfig.Path = filepath.Dir(realdst)
@@ -251,14 +224,14 @@ func CopyResource(realsrc, realdst string, isSrcDir bool) error {
}
func WriteDirectory(opts FileOptions) error {
realPath, _, _ := GetRealPath(rootPath, opts.Path)
idx := GetIndex(opts.Source)
realPath, _, _ := idx.GetRealPath(opts.Path)
// Ensure the parent directories exist
err := os.MkdirAll(realPath, 0775)
if err != nil {
return err
}
index := GetIndex(rootPath)
err = index.RefreshFileInfo(opts)
err = idx.RefreshFileInfo(opts)
if err != nil {
return errors.ErrEmptyKey
}
@@ -266,7 +239,8 @@ func WriteDirectory(opts FileOptions) error {
}
func WriteFile(opts FileOptions, in io.Reader) error {
dst, _, _ := GetRealPath(rootPath, opts.Path)
idx := GetIndex(opts.Source)
dst, _, _ := idx.GetRealPath(opts.Path)
parentDir := filepath.Dir(dst)
// Create the directory and all necessary parents
err := os.MkdirAll(parentDir, 0775)
@@ -288,8 +262,7 @@ func WriteFile(opts FileOptions, in io.Reader) error {
}
opts.Path = parentDir
opts.IsDir = true
index := GetIndex(rootPath)
return index.RefreshFileInfo(opts)
return idx.RefreshFileInfo(opts)
}
// resolveSymlinks resolves symlinks in the given path
@@ -321,8 +294,9 @@ func resolveSymlinks(path string) (string, bool, error) {
}
// addContent reads and sets content based on the file type.
func getContent(path string) (string, error) {
realPath, _, err := GetRealPath(rootPath, path)
func getContent(source, path string) (string, error) {
idx := GetIndex(source)
realPath, _, err := idx.GetRealPath(path)
if err != nil {
return "", err
}
@@ -341,74 +315,28 @@ func getContent(path string) (string, error) {
return stringContent, nil
}
// detectType detects the file type.
func (i *ItemInfo) detectType(path string, modify, saveContent, readHeader bool) error {
// DetectType detects the MIME type of a file and updates the ItemInfo struct.
func (i *ItemInfo) DetectType(realPath string, saveContent bool) {
name := i.Name
var contentErr error
ext := filepath.Ext(name)
var buffer []byte
if readHeader {
buffer = i.readFirstBytes(path)
mimetype := mime.TypeByExtension(ext)
if mimetype == "" {
http.DetectContentType(buffer)
}
}
for _, fileType := range AllFiletypeOptions {
if IsMatchingType(ext, fileType) {
i.Type = fileType
}
switch i.Type {
case "text":
if !modify {
i.Type = "textImmutable"
}
if saveContent {
return contentErr
}
case "video":
// TODO add back somewhere else, not during metadata fetch
//parentDir := strings.TrimRight(path, name)
//i.detectSubtitles(parentDir)
case "doc":
if ext == ".pdf" {
i.Type = "pdf"
return nil
}
if saveContent {
return nil
}
}
}
// Attempt MIME detection by file extension
i.Type = strings.Split(mime.TypeByExtension(ext), ";")[0]
if i.Type == "" {
i.Type = "blob"
if saveContent {
return contentErr
i.Type = extendedMimeTypeCheck(ext)
}
if i.Type == "blob" {
// Read only the first 512 bytes for efficient MIME detection
file, err := os.Open(realPath)
if err != nil {
} else {
defer file.Close()
buffer := make([]byte, 512)
n, _ := file.Read(buffer) // Ignore errors from Read
i.Type = strings.Split(http.DetectContentType(buffer[:n]), ";")[0]
}
}
return nil
}
// readFirstBytes reads the first bytes of the file.
func (i *ItemInfo) readFirstBytes(path string) []byte {
file, err := os.Open(path)
if err != nil {
i.Type = "blob"
return nil
}
defer file.Close()
buffer := make([]byte, 512) //nolint:gomnd
n, err := file.Read(buffer)
if err != nil && err != io.EOF {
i.Type = "blob"
return nil
}
return buffer[:n]
}
// TODO add subtitles back
@@ -484,9 +412,11 @@ func Exists(path string) bool {
func (info *FileInfo) SortItems() {
sort.Slice(info.Folders, func(i, j int) bool {
nameWithoutExt := strings.Split(info.Folders[i].Name, ".")[0]
nameWithoutExt2 := strings.Split(info.Folders[j].Name, ".")[0]
// Convert strings to integers for numeric sorting if both are numeric
numI, errI := strconv.Atoi(info.Folders[i].Name)
numJ, errJ := strconv.Atoi(info.Folders[j].Name)
numI, errI := strconv.Atoi(nameWithoutExt)
numJ, errJ := strconv.Atoi(nameWithoutExt2)
if errI == nil && errJ == nil {
return numI < numJ
}
@@ -494,9 +424,11 @@ func (info *FileInfo) SortItems() {
return strings.ToLower(info.Folders[i].Name) < strings.ToLower(info.Folders[j].Name)
})
sort.Slice(info.Files, func(i, j int) bool {
nameWithoutExt := strings.Split(info.Files[i].Name, ".")[0]
nameWithoutExt2 := strings.Split(info.Files[j].Name, ".")[0]
// Convert strings to integers for numeric sorting if both are numeric
numI, errI := strconv.Atoi(info.Files[i].Name)
numJ, errJ := strconv.Atoi(info.Files[j].Name)
numI, errI := strconv.Atoi(nameWithoutExt)
numJ, errJ := strconv.Atoi(nameWithoutExt2)
if errI == nil && errJ == nil {
return numI < numJ
}
+27 -17
View File
@@ -1,11 +1,14 @@
package files
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/settings"
)
func Test_GetRealPath(t *testing.T) {
@@ -13,7 +16,7 @@ func Test_GetRealPath(t *testing.T) {
if err != nil {
return
}
trimPrefix := filepath.Dir(filepath.Dir(cwd)) + "/"
trimPrefix := filepath.Dir(filepath.Dir(cwd))
tests := []struct {
name string
paths []string
@@ -31,20 +34,20 @@ func Test_GetRealPath(t *testing.T) {
path string
isDir bool
}{
path: "backend/files",
path: "",
isDir: true,
},
},
{
name: "current directory",
paths: []string{
"./file.go",
"./files/file.go",
},
want: struct {
path string
isDir bool
}{
path: "backend/files/file.go",
path: "/files/file.go",
isDir: false,
},
},
@@ -62,9 +65,16 @@ func Test_GetRealPath(t *testing.T) {
},
},
}
idx := Index{
Source: settings.Source{
Path: trimPrefix,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
realPath, isDir, _ := GetRealPath(tt.paths...)
realPath, isDir, _ := idx.GetRealPath(tt.paths...)
fmt.Println(realPath, trimPrefix)
adjustedRealPath := strings.TrimPrefix(realPath, trimPrefix)
if tt.want.path != adjustedRealPath || tt.want.isDir != isDir {
t.Errorf("expected %v:%v but got: %v:%v", tt.want.path, tt.want.isDir, adjustedRealPath, isDir)
@@ -83,30 +93,30 @@ func TestSortItems(t *testing.T) {
name: "Numeric and Lexicographical Sorting",
input: FileInfo{
Folders: []ItemInfo{
{Name: "10"},
{Name: "2"},
{Name: "10.txt"},
{Name: "2.txt"},
{Name: "apple"},
{Name: "Banana"},
},
Files: []ItemInfo{
{Name: "File2"},
{Name: "File10"},
{Name: "File2.txt"},
{Name: "File10.txt"},
{Name: "File1"},
{Name: "banana"},
},
},
expected: FileInfo{
Folders: []ItemInfo{
{Name: "2"},
{Name: "10"},
{Name: "2.txt"},
{Name: "10.txt"},
{Name: "apple"},
{Name: "Banana"},
},
Files: []ItemInfo{
{Name: "banana"},
{Name: "File1"},
{Name: "File10"},
{Name: "File2"},
{Name: "File10.txt"},
{Name: "File2.txt"},
},
},
},
@@ -114,8 +124,8 @@ func TestSortItems(t *testing.T) {
name: "Only Lexicographical Sorting",
input: FileInfo{
Folders: []ItemInfo{
{Name: "dog"},
{Name: "Cat"},
{Name: "dog.txt"},
{Name: "Cat.txt"},
{Name: "apple"},
},
Files: []ItemInfo{
@@ -127,8 +137,8 @@ func TestSortItems(t *testing.T) {
expected: FileInfo{
Folders: []ItemInfo{
{Name: "apple"},
{Name: "Cat"},
{Name: "dog"},
{Name: "Cat.txt"},
{Name: "dog.txt"},
},
Files: []ItemInfo{
{Name: "apple"},
+130 -46
View File
@@ -5,16 +5,17 @@ import (
"log"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
type Index struct {
Root string
settings.Source
Directories map[string]*FileInfo
NumDirs uint64
NumFiles uint64
@@ -30,32 +31,42 @@ type Index struct {
}
var (
rootPath string = "/srv"
indexes []*Index
indexes map[string]*Index
indexesMutex sync.RWMutex
RootPaths map[string]string
)
func InitializeIndex(enabled bool) {
if enabled {
func Initialize(source settings.Source) {
indexesMutex.RLock()
newIndex := Index{
Source: source,
Directories: make(map[string]*FileInfo),
}
if RootPaths == nil {
RootPaths = make(map[string]string)
}
RootPaths[source.Name] = source.Path
indexes = make(map[string]*Index)
indexes[newIndex.Source.Name] = &newIndex
indexesMutex.RUnlock()
if !newIndex.Source.Config.Disabled {
time.Sleep(time.Second)
if settings.Config.Server.Root != "" {
rootPath = settings.Config.Server.Root
}
si := GetIndex(rootPath)
log.Println("Initializing index and assessing file system complexity")
si.RunIndexing("/", false)
go si.setupIndexingScanners()
newIndex.RunIndexing("/", false)
go newIndex.setupIndexingScanners()
} else {
log.Println("Indexing disabled for source: ", newIndex.Source.Name)
}
}
// Define a function to recursively index files and directories
func (si *Index) indexDirectory(adjustedPath string, quick, recursive bool) error {
realPath := strings.TrimRight(si.Root, "/") + adjustedPath
func (idx *Index) indexDirectory(adjustedPath string, quick, recursive bool) error {
realPath := strings.TrimRight(idx.Source.Path, "/") + adjustedPath
// Open the directory
dir, err := os.Open(realPath)
if err != nil {
si.RemoveDirectory(adjustedPath) // Remove, must have been deleted
idx.RemoveDirectory(adjustedPath) // Remove, must have been deleted
return err
}
defer dir.Close()
@@ -69,21 +80,21 @@ func (si *Index) indexDirectory(adjustedPath string, quick, recursive bool) erro
combinedPath = "/"
}
// get whats currently in cache
si.mu.RLock()
idx.mu.RLock()
cacheDirItems := []ItemInfo{}
modChange := true // default to true
cachedDir, exists := si.Directories[adjustedPath]
cachedDir, exists := idx.Directories[adjustedPath]
if exists && quick {
modChange = dirInfo.ModTime() != cachedDir.ModTime
cacheDirItems = cachedDir.Folders
}
si.mu.RUnlock()
idx.mu.RUnlock()
// If the directory has not been modified since the last index, skip expensive readdir
// recursively check cached dirs for mod time changes as well
if !modChange && recursive {
for _, item := range cacheDirItems {
err = si.indexDirectory(combinedPath+item.Name, quick, true)
err = idx.indexDirectory(combinedPath+item.Name, quick, true)
if err != nil {
fmt.Printf("error indexing directory %v : %v", combinedPath+item.Name, err)
}
@@ -92,9 +103,9 @@ func (si *Index) indexDirectory(adjustedPath string, quick, recursive bool) erro
}
if quick {
si.mu.Lock()
si.FilesChangedDuringIndexing = true
si.mu.Unlock()
idx.mu.Lock()
idx.FilesChangedDuringIndexing = true
idx.mu.Unlock()
}
// Read directory contents
@@ -109,36 +120,56 @@ func (si *Index) indexDirectory(adjustedPath string, quick, recursive bool) erro
// Process each file and directory in the current directory
for _, file := range files {
isDir := file.IsDir()
fullCombined := combinedPath + file.Name()
if idx.shouldSkip(isDir, isHidden(file, ""), fullCombined) {
continue
}
itemInfo := &ItemInfo{
Name: file.Name(),
ModTime: file.ModTime(),
}
if file.IsDir() {
// fix for .app files on macos which are technically directories, but we don't want to treat them as such
if isDir && strings.HasSuffix(file.Name(), ".app") {
isDir = false
}
if isDir {
// skip non-indexable dirs.
if file.Name() == "$RECYCLE.BIN" || file.Name() == "System Volume Information" {
continue
}
dirPath := combinedPath + file.Name()
if recursive {
// Recursively index the subdirectory
err = si.indexDirectory(dirPath, quick, recursive)
err = idx.indexDirectory(dirPath, quick, recursive)
if err != nil {
log.Printf("Failed to index directory %s: %v", dirPath, err)
continue
}
}
realDirInfo, exists := si.GetMetadataInfo(dirPath, true)
realDirInfo, exists := idx.GetMetadataInfo(dirPath, true)
if exists {
itemInfo.Size = realDirInfo.Size
}
totalSize += itemInfo.Size
itemInfo.Type = "directory"
dirInfos = append(dirInfos, *itemInfo)
si.NumDirs++
idx.NumDirs++
} else {
_ = itemInfo.detectType(combinedPath+file.Name(), true, false, false)
itemInfo.DetectType(fullCombined, false)
itemInfo.Size = file.Size()
fileInfos = append(fileInfos, *itemInfo)
totalSize += itemInfo.Size
si.NumFiles++
idx.NumFiles++
}
}
if totalSize == 0 && idx.Source.Config.IgnoreZeroSizeFolders {
return nil
}
// Create FileInfo for the current directory
dirFileInfo := &FileInfo{
Path: adjustedPath,
@@ -155,22 +186,23 @@ func (si *Index) indexDirectory(adjustedPath string, quick, recursive bool) erro
dirFileInfo.SortItems()
// Update the current directory metadata in the index
si.UpdateMetadata(dirFileInfo)
idx.UpdateMetadata(dirFileInfo)
return nil
}
func (si *Index) makeIndexPath(subPath string) string {
func (idx *Index) makeIndexPath(subPath string) string {
subPath = strings.ReplaceAll(subPath, "\\", "/")
if strings.HasPrefix(subPath, "./") {
subPath = strings.TrimPrefix(subPath, ".")
}
if si.Root == subPath || subPath == "." {
if idx.Source.Path == subPath || subPath == "." {
return "/"
}
// clean path
subPath = strings.TrimSuffix(subPath, "/")
// remove index prefix
adjustedPath := strings.TrimPrefix(subPath, si.Root)
adjustedPath := strings.TrimPrefix(subPath, idx.Source.Path)
// remove trailing slash
adjustedPath = strings.TrimSuffix(adjustedPath, "/")
if !strings.HasPrefix(adjustedPath, "/") {
@@ -179,43 +211,65 @@ func (si *Index) makeIndexPath(subPath string) string {
return adjustedPath
}
func (si *Index) recursiveUpdateDirSizes(childInfo *FileInfo, previousSize int64) {
func (idx *Index) recursiveUpdateDirSizes(childInfo *FileInfo, previousSize int64) {
parentDir := utils.GetParentDirectoryPath(childInfo.Path)
parentInfo, exists := si.GetMetadataInfo(parentDir, true)
parentInfo, exists := idx.GetMetadataInfo(parentDir, true)
if !exists || parentDir == "" {
return
}
newSize := parentInfo.Size - previousSize + childInfo.Size
parentInfo.Size += newSize
si.UpdateMetadata(parentInfo)
si.recursiveUpdateDirSizes(parentInfo, newSize)
idx.UpdateMetadata(parentInfo)
idx.recursiveUpdateDirSizes(parentInfo, newSize)
}
func (si *Index) RefreshFileInfo(opts FileOptions) error {
func (idx *Index) GetRealPath(relativePath ...string) (string, bool, error) {
combined := append([]string{idx.Source.Path}, relativePath...)
joinedPath := filepath.Join(combined...)
isDir, _ := utils.RealPathCache.Get(joinedPath + ":isdir").(bool)
cached, ok := utils.RealPathCache.Get(joinedPath).(string)
if ok && cached != "" {
return cached, isDir, nil
}
// Convert relative path to absolute path
absolutePath, err := filepath.Abs(joinedPath)
if err != nil {
return absolutePath, false, fmt.Errorf("could not get real path: %v, %s", joinedPath, err)
}
// Resolve symlinks and get the real path
realPath, isDir, err := resolveSymlinks(absolutePath)
if err == nil {
utils.RealPathCache.Set(joinedPath, realPath)
utils.RealPathCache.Set(joinedPath+":isdir", isDir)
}
return realPath, isDir, err
}
func (idx *Index) RefreshFileInfo(opts FileOptions) error {
refreshOptions := FileOptions{
Path: opts.Path,
IsDir: opts.IsDir,
}
if !refreshOptions.IsDir {
refreshOptions.Path = si.makeIndexPath(filepath.Dir(refreshOptions.Path))
refreshOptions.Path = idx.makeIndexPath(filepath.Dir(refreshOptions.Path))
refreshOptions.IsDir = true
} else {
refreshOptions.Path = si.makeIndexPath(refreshOptions.Path)
refreshOptions.Path = idx.makeIndexPath(refreshOptions.Path)
}
err := si.indexDirectory(refreshOptions.Path, false, false)
err := idx.indexDirectory(refreshOptions.Path, false, false)
if err != nil {
return fmt.Errorf("file/folder does not exist to refresh data: %s", refreshOptions.Path)
}
file, exists := si.GetMetadataInfo(refreshOptions.Path, true)
file, exists := idx.GetMetadataInfo(refreshOptions.Path, true)
if !exists {
return fmt.Errorf("file/folder does not exist in metadata: %s", refreshOptions.Path)
}
current, firstExisted := si.GetMetadataInfo(refreshOptions.Path, true)
current, firstExisted := idx.GetMetadataInfo(refreshOptions.Path, true)
refreshParentInfo := firstExisted && current.Size != file.Size
//utils.PrintStructFields(*file)
result := si.UpdateMetadata(file)
result := idx.UpdateMetadata(file)
if !result {
return fmt.Errorf("file/folder does not exist in metadata: %s", refreshOptions.Path)
}
@@ -223,7 +277,37 @@ func (si *Index) RefreshFileInfo(opts FileOptions) error {
return nil
}
if refreshParentInfo {
si.recursiveUpdateDirSizes(file, current.Size)
idx.recursiveUpdateDirSizes(file, current.Size)
}
return nil
}
func isHidden(file os.FileInfo, realpath string) bool {
return file.Name()[0] == '.'
}
func (idx *Index) shouldSkip(isDir bool, isHidden bool, fullCombined string) bool {
// check inclusions first
if isDir && len(idx.Source.Config.Include.Folders) > 0 {
if !slices.Contains(idx.Source.Config.Include.Folders, fullCombined) {
return true
}
}
if !isDir && len(idx.Source.Config.Include.Files) > 0 {
if !slices.Contains(idx.Source.Config.Include.Files, fullCombined) {
return true
}
}
// check exclusions
if isDir && slices.Contains(idx.Source.Config.Exclude.Folders, fullCombined) {
return true
}
if !isDir && slices.Contains(idx.Source.Config.Exclude.Files, fullCombined) {
return true
}
if idx.Source.Config.IgnoreHidden && isHidden {
return true
}
return false
}
+46 -48
View File
@@ -3,8 +3,6 @@ package files
import (
"log"
"time"
"github.com/gtsteffaniak/filebrowser/settings"
)
// schedule in minutes
@@ -19,99 +17,99 @@ var scanSchedule = []time.Duration{
4 * time.Hour, // 4 hours for quick scan & 20 hours for a full scan
}
func (si *Index) newScanner(origin string) {
func (idx *Index) newScanner(origin string) {
fullScanAnchor := 3
fullScanCounter := 0 // every 5th scan is a full scan
for {
// Determine sleep time with modifiers
fullScanCounter++
sleepTime := scanSchedule[si.currentSchedule] + si.SmartModifier
if si.assessment == "simple" {
sleepTime = scanSchedule[si.currentSchedule] - si.SmartModifier
sleepTime := scanSchedule[idx.currentSchedule] + idx.SmartModifier
if idx.assessment == "simple" {
sleepTime = scanSchedule[idx.currentSchedule] - idx.SmartModifier
}
if settings.Config.Server.IndexingInterval > 0 {
sleepTime = time.Duration(settings.Config.Server.IndexingInterval) * time.Minute
if idx.Source.Config.IndexingInterval > 0 {
sleepTime = time.Duration(idx.Source.Config.IndexingInterval) * time.Minute
}
// Log and sleep before indexing
log.Printf("Next scan in %v\n", sleepTime)
time.Sleep(sleepTime)
si.scannerMu.Lock()
idx.scannerMu.Lock()
if fullScanCounter == 5 {
si.RunIndexing(origin, false) // Full scan
idx.RunIndexing(origin, false) // Full scan
fullScanCounter = 0
} else {
si.RunIndexing(origin, true) // Quick scan
idx.RunIndexing(origin, true) // Quick scan
}
si.scannerMu.Unlock()
idx.scannerMu.Unlock()
// Adjust schedule based on file changes
if si.FilesChangedDuringIndexing {
if idx.FilesChangedDuringIndexing {
// Move to at least the full-scan anchor or reduce interval
if si.currentSchedule > fullScanAnchor {
si.currentSchedule = fullScanAnchor
} else if si.currentSchedule > 0 {
si.currentSchedule--
if idx.currentSchedule > fullScanAnchor {
idx.currentSchedule = fullScanAnchor
} else if idx.currentSchedule > 0 {
idx.currentSchedule--
}
} else {
// Increment toward the longest interval if no changes
if si.currentSchedule < len(scanSchedule)-1 {
si.currentSchedule++
if idx.currentSchedule < len(scanSchedule)-1 {
idx.currentSchedule++
}
}
if si.assessment == "simple" && si.currentSchedule > 3 {
si.currentSchedule = 3
if idx.assessment == "simple" && idx.currentSchedule > 3 {
idx.currentSchedule = 3
}
// Ensure `currentSchedule` stays within bounds
if si.currentSchedule < 0 {
si.currentSchedule = 0
} else if si.currentSchedule >= len(scanSchedule) {
si.currentSchedule = len(scanSchedule) - 1
if idx.currentSchedule < 0 {
idx.currentSchedule = 0
} else if idx.currentSchedule >= len(scanSchedule) {
idx.currentSchedule = len(scanSchedule) - 1
}
}
}
func (si *Index) RunIndexing(origin string, quick bool) {
prevNumDirs := si.NumDirs
prevNumFiles := si.NumFiles
func (idx *Index) RunIndexing(origin string, quick bool) {
prevNumDirs := idx.NumDirs
prevNumFiles := idx.NumFiles
if quick {
log.Println("Starting quick scan")
} else {
log.Println("Starting full scan")
si.NumDirs = 0
si.NumFiles = 0
idx.NumDirs = 0
idx.NumFiles = 0
}
startTime := time.Now()
si.FilesChangedDuringIndexing = false
idx.FilesChangedDuringIndexing = false
// Perform the indexing operation
err := si.indexDirectory("/", quick, true)
err := idx.indexDirectory("/", quick, true)
if err != nil {
log.Printf("Error during indexing: %v", err)
}
// Update the LastIndexed time
si.LastIndexed = time.Now()
si.indexingTime = int(time.Since(startTime).Seconds())
idx.LastIndexed = time.Now()
idx.indexingTime = int(time.Since(startTime).Seconds())
if !quick {
// update smart indexing
if si.indexingTime < 3 || si.NumDirs < 10000 {
si.assessment = "simple"
si.SmartModifier = 4 * time.Minute
} else if si.indexingTime > 120 || si.NumDirs > 500000 {
si.assessment = "complex"
modifier := si.indexingTime / 10 // seconds
si.SmartModifier = time.Duration(modifier) * time.Minute
if idx.indexingTime < 3 || idx.NumDirs < 10000 {
idx.assessment = "simple"
idx.SmartModifier = 4 * time.Minute
} else if idx.indexingTime > 120 || idx.NumDirs > 500000 {
idx.assessment = "complex"
modifier := idx.indexingTime / 10 // seconds
idx.SmartModifier = time.Duration(modifier) * time.Minute
} else {
si.assessment = "normal"
idx.assessment = "normal"
}
log.Printf("Index assessment : complexity=%v directories=%v files=%v \n", si.assessment, si.NumDirs, si.NumFiles)
if si.NumDirs != prevNumDirs || si.NumFiles != prevNumFiles {
si.FilesChangedDuringIndexing = true
log.Printf("Index assessment : complexity=%v directories=%v files=%v \n", idx.assessment, idx.NumDirs, idx.NumFiles)
if idx.NumDirs != prevNumDirs || idx.NumFiles != prevNumFiles {
idx.FilesChangedDuringIndexing = true
}
}
log.Printf("Time Spent Indexing : %v seconds\n", si.indexingTime)
log.Printf("Time Spent Indexing : %v seconds\n", idx.indexingTime)
}
func (si *Index) setupIndexingScanners() {
go si.newScanner("/")
func (idx *Index) setupIndexingScanners() {
go idx.newScanner("/")
}
+112 -9
View File
@@ -7,20 +7,23 @@ import (
"testing"
"time"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/backend/settings"
)
func BenchmarkFillIndex(b *testing.B) {
InitializeIndex(false)
si := GetIndex(settings.Config.Server.Root)
Initialize(settings.Source{
Name: "test",
Path: "/srv",
})
idx := GetIndex("test")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
si.createMockData(50, 3) // 1000 dirs, 3 files per dir
idx.createMockData(50, 3) // 1000 dirs, 3 files per dir
}
}
func (si *Index) createMockData(numDirs, numFilesPerDir int) {
func (idx *Index) createMockData(numDirs, numFilesPerDir int) {
for i := 0; i < numDirs; i++ {
dirPath := generateRandomPath(rand.Intn(3) + 1)
files := []ItemInfo{} // Slice of FileInfo
@@ -40,7 +43,7 @@ func (si *Index) createMockData(numDirs, numFilesPerDir int) {
Files: files,
}
si.UpdateMetadata(dirInfo)
idx.UpdateMetadata(dirInfo)
}
}
@@ -78,6 +81,7 @@ func TestMakeIndexPath(t *testing.T) {
subPath string
expected string
}{
// Linux
{"Root path returns slash", "/", "/"},
{"Dot-prefixed returns slash", ".", "/"},
{"Double-dot prefix ignored", "./", "/"},
@@ -87,15 +91,114 @@ func TestMakeIndexPath(t *testing.T) {
{"Trailing slash removed", "/test/", "/test"},
{"Subpath without root prefix", "/other/test", "/other/test"},
{"Complex nested paths", "/nested/path", "/nested/path"},
// Windows
{"Mixed slash", "/first\\second", "/first/second"},
{"Windows slash", "\\first\\second", "/first/second"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
si := &Index{Root: "/"}
result := si.makeIndexPath(tt.subPath)
idx := &Index{Source: settings.Source{Path: "/"}}
result := idx.makeIndexPath(tt.subPath)
if result != tt.expected {
t.Errorf("makeIndexPath(%q) = %q; want %q", tt.name, result, tt.expected)
t.Errorf("makeIndexPath(%q)\ngot %q\nwant %q", tt.name, result, tt.expected)
}
})
}
}
func TestMakeIndexPathRoot(t *testing.T) {
tests := []struct {
name string
subPath string
expected string
}{
// Linux
{"Root path returns slash", "/rootpath", "/"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
idx := &Index{Source: settings.Source{Path: "/rootpath", Name: "default"}}
result := idx.makeIndexPath(tt.subPath)
if result != tt.expected {
t.Errorf("makeIndexPath(%q)\ngot %q\nwant %q", tt.name, result, tt.expected)
}
})
}
}
func BenchmarkCheckIndexExclude(b *testing.B) {
tests := []struct {
isDir bool
isHidden bool
fullPath string
}{
{false, false, "/test/.test"},
{true, false, "/test/.test"},
{true, true, "/test/.test"},
{false, false, "/test/filepath"},
{false, true, "/test/filepath"},
{true, true, "/test/filepath"},
}
b.ResetTimer()
b.ReportAllocs()
idx := Index{
Source: settings.Source{
Name: "files",
Config: settings.IndexConfig{
IgnoreHidden: true,
Exclude: settings.IndexFilter{
Files: []string{"test", "filepath", ".test", ".filepath", "test", "filepath", ".test", ".filepath"},
Folders: []string{"test", "filepath", ".test", ".filepath", "test", "filepath", ".test", ".filepath"},
FileEndsWith: []string{".zip", ".tar", ".jpeg"},
},
},
},
}
for i := 0; i < b.N; i++ {
for _, v := range tests {
idx.shouldSkip(v.isDir, v.isHidden, v.fullPath)
}
}
}
func BenchmarkCheckIndexConditionsInclude(b *testing.B) {
tests := []struct {
isDir bool
isHidden bool
fullPath string
}{
{false, false, "/test/.test"},
{true, false, "/test/.test"},
{true, true, "/test/.test"},
{false, false, "/test/filepath"},
{false, true, "/test/filepath"},
{true, true, "/test/filepath"},
}
b.ResetTimer()
b.ReportAllocs()
idx2 := Index{
Source: settings.Source{
Name: "files",
Config: settings.IndexConfig{
IgnoreHidden: true,
Include: settings.IndexFilter{
Files: []string{"test", "filepath", ".test", ".filepath", "test", "filepath", ".test", ".filepath"},
Folders: []string{"test", "filepath", ".test", ".filepath", "test", "filepath", ".test", ".filepath"},
FileEndsWith: []string{".zip", ".tar", ".jpeg"},
},
},
},
}
for i := 0; i < b.N; i++ {
for _, v := range tests {
idx2.shouldSkip(v.isDir, v.isHidden, v.fullPath)
}
}
}
+22 -22
View File
@@ -6,7 +6,7 @@ import (
"strings"
"sync"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
var (
@@ -14,27 +14,27 @@ var (
maxSearchResults = 100
)
type searchResult struct {
type SearchResult struct {
Path string `json:"path"`
Type string `json:"type"`
Size int64 `json:"size"`
}
func (si *Index) Search(search string, scope string, sourceSession string) []searchResult {
func (idx *Index) Search(search string, scope string, sourceSession string) []SearchResult {
// Remove slashes
scope = si.makeIndexPath(scope)
scope = idx.makeIndexPath(scope)
runningHash := utils.GenerateRandomHash(4)
sessionInProgress.Store(sourceSession, runningHash) // Store the value in the sync.Map
searchOptions := ParseSearch(search)
results := make(map[string]searchResult, 0)
results := make(map[string]SearchResult, 0)
count := 0
var directories []string
cachedDirs, ok := utils.SearchResultsCache.Get(si.Root + scope).([]string)
cachedDirs, ok := utils.SearchResultsCache.Get(idx.Source.Path + scope).([]string)
if ok {
directories = cachedDirs
} else {
directories = si.getDirsInScope(scope)
utils.SearchResultsCache.Set(si.Root+scope, directories)
directories = idx.getDirsInScope(scope)
utils.SearchResultsCache.Set(idx.Source.Path+scope, directories)
}
for _, searchTerm := range searchOptions.Terms {
if searchTerm == "" {
@@ -43,12 +43,12 @@ func (si *Index) Search(search string, scope string, sourceSession string) []sea
if count > maxSearchResults {
break
}
si.mu.Lock()
idx.mu.Lock()
for _, dirName := range directories {
scopedPath := strings.TrimPrefix(strings.TrimPrefix(dirName, scope), "/") + "/"
si.mu.Unlock()
dir, found := si.GetReducedMetadata(dirName, true)
si.mu.Lock()
idx.mu.Unlock()
dir, found := idx.GetReducedMetadata(dirName, true)
idx.mu.Lock()
if !found {
continue
}
@@ -62,7 +62,7 @@ func (si *Index) Search(search string, scope string, sourceSession string) []sea
}
matches := reducedDir.containsSearchTerm(searchTerm, searchOptions)
if matches {
results[scopedPath] = searchResult{Path: scopedPath, Type: "directory", Size: dir.Size}
results[scopedPath] = SearchResult{Path: scopedPath, Type: "directory", Size: dir.Size}
count++
}
// search files first
@@ -74,24 +74,24 @@ func (si *Index) Search(search string, scope string, sourceSession string) []sea
}
value, found := sessionInProgress.Load(sourceSession)
if !found || value != runningHash {
si.mu.Unlock()
return []searchResult{}
idx.mu.Unlock()
return []SearchResult{}
}
if count > maxSearchResults {
break
}
matches := item.containsSearchTerm(searchTerm, searchOptions)
if matches {
results[scopedPath] = searchResult{Path: scopedPath, Type: item.Type, Size: item.Size}
results[scopedPath] = SearchResult{Path: scopedPath, Type: item.Type, Size: item.Size}
count++
}
}
}
si.mu.Unlock()
idx.mu.Unlock()
}
// Sort keys based on the number of elements in the path after splitting by "/"
sortedKeys := make([]searchResult, 0, len(results))
sortedKeys := make([]SearchResult, 0, len(results))
for _, v := range results {
sortedKeys = append(sortedKeys, v)
}
@@ -169,11 +169,11 @@ func (fi ItemInfo) containsSearchTerm(searchTerm string, options SearchOptions)
return true
}
func (si *Index) getDirsInScope(scope string) []string {
func (idx *Index) getDirsInScope(scope string) []string {
newList := []string{}
si.mu.Lock()
defer si.mu.Unlock()
for k := range si.Directories {
idx.mu.Lock()
defer idx.mu.Unlock()
for k := range idx.Directories {
if strings.HasPrefix(k, scope) || scope == "" {
newList = append(newList, k)
}
+17 -16
View File
@@ -4,14 +4,15 @@ import (
"reflect"
"testing"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/stretchr/testify/assert"
)
func BenchmarkSearchAllIndexes(b *testing.B) {
InitializeIndex(false)
si := GetIndex(rootPath)
Initialize(settings.Source{Name: "test", Path: "/srv"})
idx := GetIndex("test")
si.createMockData(50, 3) // 50 dirs, 3 files per dir
idx.createMockData(50, 3) // 50 dirs, 3 files per dir
// Generate 100 random search terms
searchTerms := generateRandomSearchTerms(100)
@@ -21,7 +22,7 @@ func BenchmarkSearchAllIndexes(b *testing.B) {
for i := 0; i < b.N; i++ {
// Execute the SearchAllIndexes function
for _, term := range searchTerms {
si.Search(term, "/", "test")
idx.Search(term, "/", "test")
}
}
}
@@ -74,14 +75,14 @@ func TestParseSearch(t *testing.T) {
}
func TestSearchWhileIndexing(t *testing.T) {
InitializeIndex(false)
si := GetIndex(rootPath)
Initialize(settings.Source{Name: "test", Path: "/srv"})
idx := GetIndex("test")
searchTerms := generateRandomSearchTerms(10)
for i := 0; i < 5; i++ {
go si.createMockData(100, 100) // Creating mock data concurrently
go idx.createMockData(100, 100) // Creating mock data concurrently
for _, term := range searchTerms {
go si.Search(term, "/", "test") // Search concurrently
go idx.Search(term, "/", "test") // Search concurrently
}
}
}
@@ -119,12 +120,12 @@ func TestSearchIndexes(t *testing.T) {
tests := []struct {
search string
scope string
expectedResult []searchResult
expectedResult []SearchResult
}{
{
search: "audio",
scope: "/new/",
expectedResult: []searchResult{
expectedResult: []SearchResult{
{
Path: "test/audio.wav",
Type: "audio",
@@ -135,7 +136,7 @@ func TestSearchIndexes(t *testing.T) {
{
search: "test",
scope: "/",
expectedResult: []searchResult{
expectedResult: []SearchResult{
{
Path: "test/",
Type: "directory",
@@ -151,7 +152,7 @@ func TestSearchIndexes(t *testing.T) {
{
search: "archive",
scope: "/",
expectedResult: []searchResult{
expectedResult: []SearchResult{
{
Path: "firstDir/archive.zip",
Type: "archive",
@@ -167,7 +168,7 @@ func TestSearchIndexes(t *testing.T) {
{
search: "arch",
scope: "/firstDir",
expectedResult: []searchResult{
expectedResult: []SearchResult{
{
Path: "archive.zip",
Type: "archive",
@@ -178,7 +179,7 @@ func TestSearchIndexes(t *testing.T) {
{
search: "isdir",
scope: "/",
expectedResult: []searchResult{
expectedResult: []SearchResult{
{
Path: "firstDir/thisIsDir/",
Type: "directory",
@@ -189,7 +190,7 @@ func TestSearchIndexes(t *testing.T) {
{
search: "IsDir type:largerThan=1",
scope: "/",
expectedResult: []searchResult{
expectedResult: []SearchResult{
{
Path: "firstDir/thisIsDir/",
Type: "directory",
@@ -200,7 +201,7 @@ func TestSearchIndexes(t *testing.T) {
{
search: "video",
scope: "/",
expectedResult: []searchResult{
expectedResult: []SearchResult{
{
Path: "new/test/video.MP4",
Type: "video",
+28 -42
View File
@@ -2,27 +2,25 @@ package files
import (
"path/filepath"
"github.com/gtsteffaniak/filebrowser/settings"
)
// UpdateFileMetadata updates the FileInfo for the specified directory in the index.
func (si *Index) UpdateMetadata(info *FileInfo) bool {
si.mu.Lock()
defer si.mu.Unlock()
si.Directories[info.Path] = info
func (idx *Index) UpdateMetadata(info *FileInfo) bool {
idx.mu.Lock()
defer idx.mu.Unlock()
idx.Directories[info.Path] = info
return true
}
// GetMetadataInfo retrieves the FileInfo from the specified directory in the index.
func (si *Index) GetReducedMetadata(target string, isDir bool) (*FileInfo, bool) {
si.mu.Lock()
defer si.mu.Unlock()
checkDir := si.makeIndexPath(target)
func (idx *Index) GetReducedMetadata(target string, isDir bool) (*FileInfo, bool) {
idx.mu.Lock()
defer idx.mu.Unlock()
checkDir := idx.makeIndexPath(target)
if !isDir {
checkDir = si.makeIndexPath(filepath.Dir(target))
checkDir = idx.makeIndexPath(filepath.Dir(target))
}
dir, exists := si.Directories[checkDir]
dir, exists := idx.Directories[checkDir]
if !exists {
return nil, false
}
@@ -48,42 +46,30 @@ func (si *Index) GetReducedMetadata(target string, isDir bool) (*FileInfo, bool)
}
// GetMetadataInfo retrieves the FileInfo from the specified directory in the index.
func (si *Index) GetMetadataInfo(target string, isDir bool) (*FileInfo, bool) {
si.mu.RLock()
defer si.mu.RUnlock()
checkDir := si.makeIndexPath(target)
func (idx *Index) GetMetadataInfo(target string, isDir bool) (*FileInfo, bool) {
idx.mu.RLock()
defer idx.mu.RUnlock()
checkDir := idx.makeIndexPath(target)
if !isDir {
checkDir = si.makeIndexPath(filepath.Dir(target))
checkDir = idx.makeIndexPath(filepath.Dir(target))
}
dir, exists := si.Directories[checkDir]
dir, exists := idx.Directories[checkDir]
return dir, exists
}
func (si *Index) RemoveDirectory(path string) {
si.mu.Lock()
defer si.mu.Unlock()
si.NumDeleted++
delete(si.Directories, path)
func (idx *Index) RemoveDirectory(path string) {
idx.mu.Lock()
defer idx.mu.Unlock()
idx.NumDeleted++
delete(idx.Directories, path)
}
func GetIndex(root string) *Index {
for _, index := range indexes {
if index.Root == root {
return index
}
}
if settings.Config.Server.Root != "" {
rootPath = settings.Config.Server.Root
}
newIndex := &Index{
Root: rootPath,
Directories: map[string]*FileInfo{},
NumDirs: 0,
NumFiles: 0,
}
newIndex.Directories["/"] = &FileInfo{}
func GetIndex(name string) *Index {
indexesMutex.Lock()
indexes = append(indexes, newIndex)
indexesMutex.Unlock()
return newIndex
defer indexesMutex.Unlock()
index, ok := indexes[name]
if !ok {
return nil
}
return index
}
+5 -1
View File
@@ -3,6 +3,7 @@ package files
import (
"testing"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/stretchr/testify/assert"
)
@@ -207,7 +208,10 @@ func TestRemoveDirectory(t *testing.T) {
func init() {
testIndex = Index{
Root: "/",
Source: settings.Source{
Path: "/",
Name: "test",
},
NumFiles: 10,
NumDirs: 5,
Directories: map[string]*FileInfo{
+9 -9
View File
@@ -1,4 +1,4 @@
module github.com/gtsteffaniak/filebrowser
module github.com/gtsteffaniak/filebrowser/backend
go 1.22.5
@@ -7,7 +7,7 @@ require (
github.com/disintegration/imaging v1.6.2
github.com/dsoprea/go-exif/v3 v3.0.1
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568
github.com/goccy/go-yaml v1.15.6
github.com/goccy/go-yaml v1.15.13
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/google/go-cmp v0.6.0
github.com/shirou/gopsutil/v3 v3.24.5
@@ -17,9 +17,9 @@ require (
github.com/stretchr/testify v1.9.0
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.4
golang.org/x/crypto v0.29.0
golang.org/x/image v0.22.0
golang.org/x/text v0.20.0
golang.org/x/crypto v0.31.0
golang.org/x/image v0.23.0
golang.org/x/text v0.21.0
)
require (
@@ -37,15 +37,15 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/swaggo/files v1.0.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.etcd.io/bbolt v1.3.11 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/tools v0.27.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/tools v0.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
+18 -18
View File
@@ -47,8 +47,8 @@ github.com/go-openapi/spec v0.21.0 h1:LTVzPc3p/RzRnkQqLRndbAzjY0d0BCL72A6j3CdL9Z
github.com/go-openapi/spec v0.21.0/go.mod h1:78u6VdPw81XU44qEWGhtr982gJ5BWg2c0I5XwVMotYk=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/goccy/go-yaml v1.15.6 h1:gy5kf1yjMia3/c3wWD+u1z3lU5XlhpT8FZGaLJU9cOA=
github.com/goccy/go-yaml v1.15.6/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/goccy/go-yaml v1.15.13 h1:Xd87Yddmr2rC1SLLTm2MNDcTjeO/GYo0JGiww6gSTDg=
github.com/goccy/go-yaml v1.15.13/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
@@ -78,8 +78,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
@@ -114,11 +114,11 @@ go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g=
golang.org/x/image v0.22.0/go.mod h1:9hPFhljd4zZ1GNSIZJ49sqbp45GKK9t6w+iXvGqZUz4=
golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=
golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
@@ -133,12 +133,12 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20221002022538-bcab6841153b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -153,8 +153,8 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
@@ -163,13 +163,13 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"strings"
"time"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
func createApiKeyHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
+7 -6
View File
@@ -16,11 +16,12 @@ import (
"github.com/golang-jwt/jwt/v4/request"
"golang.org/x/crypto/bcrypt"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/share"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/share"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
var (
@@ -126,7 +127,7 @@ func signupHandler(w http.ResponseWriter, r *http.Request) {
user.Username = info.Username
user.Password = info.Password
userHome, err := config.MakeUserDir(user.Username, user.Scope, config.Server.Root)
userHome, err := config.MakeUserDir(user.Username, user.Scope, files.RootPaths["default"])
if err != nil {
log.Printf("create user: failed to mkdir user home dir: [%s]", userHome)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+6 -6
View File
@@ -11,10 +11,10 @@ import (
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/runner"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/runner"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
type requestContext struct {
@@ -97,7 +97,7 @@ func withUserHelper(fn handleFunc) handleFunc {
if settings.Config.Auth.Method == "noauth" {
var err error
// Retrieve the user from the store and store it in the context
data.user, err = store.Users.Get(config.Server.Root, "admin")
data.user, err = store.Users.Get(files.RootPaths["default"], "admin")
if err != nil {
return http.StatusInternalServerError, err
}
@@ -127,7 +127,7 @@ func withUserHelper(fn handleFunc) handleFunc {
w.Header().Add("X-Renew-Token", "true")
}
// Retrieve the user from the store and store it in the context
data.user, err = store.Users.Get(config.Server.Root, tk.BelongsTo)
data.user, err = store.Users.Get(files.RootPaths["default"], tk.BelongsTo)
if err != nil {
return http.StatusInternalServerError, err
}
+9 -9
View File
@@ -8,15 +8,15 @@ import (
"time"
"github.com/asdine/storm/v3"
"github.com/gtsteffaniak/filebrowser/diskcache"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/img"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/share"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/storage/bolt"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/diskcache"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/img"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/share"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/storage/bolt"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
func setupTestEnv(t *testing.T) {
+17 -14
View File
@@ -8,9 +8,11 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/img"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/img"
)
type ImgService interface {
@@ -41,17 +43,18 @@ type FileCache interface {
// @Router /api/preview [get]
func previewHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
path := r.URL.Query().Get("path")
source := r.URL.Query().Get("source")
previewSize := r.URL.Query().Get("size")
if previewSize != "small" {
previewSize = "large"
}
if path == "" {
return http.StatusBadRequest, fmt.Errorf("invalid request path")
}
response, err := files.FileInfoFaster(files.FileOptions{
Path: filepath.Join(d.user.Scope, path),
Modify: d.user.Perm.Modify,
Source: source,
Expand: true,
ReadHeader: config.Server.TypeDetectionByHeader,
Checker: d.user,
@@ -64,7 +67,7 @@ func previewHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (
return http.StatusBadRequest, fmt.Errorf("can't create preview for directory")
}
setContentDisposition(w, r, fileInfo.Name)
if fileInfo.Type != "image" {
if !strings.HasPrefix(fileInfo.Type, "image") {
return http.StatusNotImplemented, fmt.Errorf("can't create preview for %s type", fileInfo.Type)
}
@@ -87,26 +90,25 @@ func previewHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (
if err != nil {
return errToStatus(err), err
}
cacheKey := previewCacheKey(fileInfo, previewSize)
cacheKey := previewCacheKey(response.RealPath, previewSize, fileInfo.ModTime)
resizedImage, ok, err := fileCache.Load(r.Context(), cacheKey)
if err != nil {
return errToStatus(err), err
}
if !ok {
resizedImage, err = createPreview(imgSvc, fileCache, fileInfo, previewSize)
resizedImage, err = createPreview(imgSvc, fileCache, response, previewSize)
if err != nil {
return errToStatus(err), err
}
}
w.Header().Set("Cache-Control", "private")
http.ServeContent(w, r, fileInfo.RealPath(), fileInfo.ModTime, bytes.NewReader(resizedImage))
http.ServeContent(w, r, response.RealPath, fileInfo.ModTime, bytes.NewReader(resizedImage))
return 0, nil
}
func createPreview(imgSvc ImgService, fileCache FileCache, file *files.FileInfo, previewSize string) ([]byte, error) {
fd, err := os.Open(file.RealPath())
func createPreview(imgSvc ImgService, fileCache FileCache, file files.ExtendedFileInfo, previewSize string) ([]byte, error) {
fd, err := os.Open(file.RealPath)
if err != nil {
return nil, err
}
@@ -137,7 +139,7 @@ func createPreview(imgSvc ImgService, fileCache FileCache, file *files.FileInfo,
}
go func() {
cacheKey := previewCacheKey(file, previewSize)
cacheKey := previewCacheKey(file.RealPath, previewSize, file.FileInfo.ModTime)
if err := fileCache.Store(context.Background(), cacheKey, buf.Bytes()); err != nil {
fmt.Printf("failed to cache resized image: %v", err)
}
@@ -147,12 +149,13 @@ func createPreview(imgSvc ImgService, fileCache FileCache, file *files.FileInfo,
}
// Generates a cache key for the preview image
func previewCacheKey(f *files.FileInfo, previewSize string) string {
return fmt.Sprintf("%x%x%x", f.RealPath(), f.ModTime.Unix(), previewSize)
func previewCacheKey(realPath, previewSize string, modTime time.Time) string {
return fmt.Sprintf("%x%x%x", realPath, modTime.Unix(), previewSize)
}
func rawFileHandler(w http.ResponseWriter, r *http.Request, file *files.FileInfo) (int, error) {
realPath, _, _ := files.GetRealPath(file.Path)
idx := files.GetIndex("default")
realPath, _, _ := idx.GetRealPath(file.Path)
fd, err := os.Open(realPath)
if err != nil {
return http.StatusInternalServerError, err
+4 -5
View File
@@ -6,11 +6,10 @@ import (
"net/http"
"strings"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/users"
_ "github.com/gtsteffaniak/filebrowser/swagger/docs"
_ "github.com/gtsteffaniak/filebrowser/backend/swagger/docs"
)
func publicShareHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
@@ -18,7 +17,7 @@ func publicShareHandler(w http.ResponseWriter, r *http.Request, d *requestContex
if !ok {
return http.StatusInternalServerError, fmt.Errorf("failed to assert type *files.FileInfo")
}
file.Path = strings.TrimPrefix(file.Path, settings.Config.Server.Root)
file.Path = strings.TrimPrefix(file.Path, files.RootPaths["default"])
return renderJSON(w, r, file)
}
+12 -4
View File
@@ -5,6 +5,7 @@ import (
"archive/zip"
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"net/http"
@@ -13,7 +14,7 @@ import (
"path/filepath"
"strings"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/backend/files"
)
func setContentDisposition(w http.ResponseWriter, r *http.Request, fileName string) {
@@ -44,12 +45,18 @@ func rawHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int,
if !d.user.Perm.Download {
return http.StatusAccepted, nil
}
files := r.URL.Query().Get("files")
encodedFiles := r.URL.Query().Get("files")
// Decode the URL-encoded path
files, err := url.QueryUnescape(encodedFiles)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("invalid path encoding: %v", err)
}
return rawFilesHandler(w, r, d, strings.Split(files, ","))
}
func addFile(path string, d *requestContext, tarWriter *tar.Writer, zipWriter *zip.Writer) error {
realPath, _, _ := files.GetRealPath(d.user.Scope, path)
idx := files.GetIndex("default")
realPath, _, _ := idx.GetRealPath(d.user.Scope, path)
if !d.user.Check(realPath) {
return nil
}
@@ -137,7 +144,8 @@ func addSingleFile(realPath, archivePath string, zipWriter *zip.Writer, tarWrite
func rawFilesHandler(w http.ResponseWriter, r *http.Request, d *requestContext, fileList []string) (int, error) {
filePath := fileList[0]
fileName := filepath.Base(filePath)
realPath, isDir, err := files.GetRealPath(d.user.Scope, filePath)
idx := files.GetIndex("default")
realPath, isDir, err := idx.GetRealPath(d.user.Scope, filePath)
if err != nil {
return http.StatusInternalServerError, err
}
+103 -55
View File
@@ -13,9 +13,9 @@ import (
"github.com/shirou/gopsutil/v3/disk"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
// resourceGetHandler retrieves information about a resource.
@@ -25,6 +25,7 @@ import (
// @Accept json
// @Produce json
// @Param path query string true "Path to the resource"
// @Param source query string false "Source name for the desired source, default is used if not provided"
// @Param source query string false "Name for the desired source, default is used if not provided"
// @Param content query string false "Include file content if true"
// @Param checksum query string false "Optional checksum validation"
@@ -33,12 +34,20 @@ import (
// @Failure 500 {object} map[string]string "Internal server error"
// @Router /api/resources [get]
func resourceGetHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
// TODO source := r.URL.Query().Get("source")
path := r.URL.Query().Get("path")
encodedPath := r.URL.Query().Get("path")
source := r.URL.Query().Get("source")
if source == "" {
source = "default"
}
// Decode the URL-encoded path
path, err := url.QueryUnescape(encodedPath)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("invalid path encoding: %v", err)
}
fileInfo, err := files.FileInfoFaster(files.FileOptions{
Path: filepath.Join(d.user.Scope, path),
Modify: d.user.Perm.Modify,
Source: source,
Expand: true,
ReadHeader: config.Server.TypeDetectionByHeader,
Checker: d.user,
@@ -70,6 +79,7 @@ func resourceGetHandler(w http.ResponseWriter, r *http.Request, d *requestContex
// @Accept json
// @Produce json
// @Param path query string true "Path to the resource"
// @Param source query string false "Source name for the desired source, default is used if not provided"
// @Param source query string false "Name for the desired source, default is used if not provided"
// @Success 200 "Resource deleted successfully"
// @Failure 403 {object} map[string]string "Forbidden"
@@ -78,17 +88,22 @@ func resourceGetHandler(w http.ResponseWriter, r *http.Request, d *requestContex
// @Router /api/resources [delete]
func resourceDeleteHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
// TODO source := r.URL.Query().Get("source")
path := r.URL.Query().Get("path")
encodedPath := r.URL.Query().Get("path")
source := r.URL.Query().Get("source")
if source == "" {
source = "default"
}
// Decode the URL-encoded path
path, err := url.QueryUnescape(encodedPath)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("invalid path encoding: %v", err)
}
if path == "/" || !d.user.Perm.Delete {
return http.StatusForbidden, nil
}
realPath, isDir, err := files.GetRealPath(d.user.Scope, path)
if err != nil {
return http.StatusNotFound, err
}
fileOpts := files.FileOptions{
Path: filepath.Join(d.user.Scope, path),
IsDir: isDir,
Source: source,
Modify: d.user.Perm.Modify,
Expand: false,
ReadHeader: config.Server.TypeDetectionByHeader,
@@ -100,12 +115,12 @@ func resourceDeleteHandler(w http.ResponseWriter, r *http.Request, d *requestCon
}
// delete thumbnails
err = delThumbs(r.Context(), fileCache, fileInfo.FileInfo)
err = delThumbs(r.Context(), fileCache, fileInfo)
if err != nil {
return errToStatus(err), err
}
err = files.DeleteFiles(realPath, fileOpts)
err = files.DeleteFiles(source, fileInfo.RealPath, filepath.Dir(fileInfo.RealPath))
if err != nil {
return errToStatus(err), err
}
@@ -120,6 +135,7 @@ func resourceDeleteHandler(w http.ResponseWriter, r *http.Request, d *requestCon
// @Accept json
// @Produce json
// @Param path query string true "Path to the resource"
// @Param source query string false "Source name for the desired source, default is used if not provided"
// @Param source query string false "Name for the desired source, default is used if not provided"
// @Param override query bool false "Override existing file if true"
// @Success 200 "Resource created successfully"
@@ -129,20 +145,29 @@ func resourceDeleteHandler(w http.ResponseWriter, r *http.Request, d *requestCon
// @Failure 500 {object} map[string]string "Internal server error"
// @Router /api/resources [post]
func resourcePostHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
// TODO source := r.URL.Query().Get("source")
path := r.URL.Query().Get("path")
encodedPath := r.URL.Query().Get("path")
source := r.URL.Query().Get("source")
if source == "" {
source = "default"
}
// Decode the URL-encoded path
path, err := url.QueryUnescape(encodedPath)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("invalid path encoding: %v", err)
}
if !d.user.Perm.Create || !d.user.Check(path) {
return http.StatusForbidden, nil
}
fileOpts := files.FileOptions{
Path: filepath.Join(d.user.Scope, path),
Source: source,
Modify: d.user.Perm.Modify,
Expand: false,
Checker: d.user,
}
// Directories creation on POST.
if strings.HasSuffix(path, "/") {
err := files.WriteDirectory(fileOpts)
err = files.WriteDirectory(fileOpts)
if err != nil {
return errToStatus(err), err
}
@@ -159,7 +184,7 @@ func resourcePostHandler(w http.ResponseWriter, r *http.Request, d *requestConte
return http.StatusForbidden, nil
}
err = delThumbs(r.Context(), fileCache, fileInfo.FileInfo)
err = delThumbs(r.Context(), fileCache, fileInfo)
if err != nil {
return errToStatus(err), err
}
@@ -179,6 +204,7 @@ func resourcePostHandler(w http.ResponseWriter, r *http.Request, d *requestConte
// @Accept json
// @Produce json
// @Param path query string true "Path to the resource"
// @Param source query string false "Source name for the desired source, default is used if not provided"
// @Param source query string false "Name for the desired source, default is used if not provided"
// @Success 200 "Resource updated successfully"
// @Failure 403 {object} map[string]string "Forbidden"
@@ -187,8 +213,18 @@ func resourcePostHandler(w http.ResponseWriter, r *http.Request, d *requestConte
// @Failure 500 {object} map[string]string "Internal server error"
// @Router /api/resources [put]
func resourcePutHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
// TODO source := r.URL.Query().Get("source")
path := r.URL.Query().Get("path")
source := r.URL.Query().Get("source")
if source == "" {
source = "default"
}
encodedPath := r.URL.Query().Get("path")
// Decode the URL-encoded path
path, err := url.QueryUnescape(encodedPath)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("invalid path encoding: %v", err)
}
if !d.user.Perm.Modify || !d.user.Check(path) {
return http.StatusForbidden, nil
}
@@ -198,13 +234,9 @@ func resourcePutHandler(w http.ResponseWriter, r *http.Request, d *requestContex
return http.StatusMethodNotAllowed, nil
}
realPath, isDir, err := files.GetRealPath(d.user.Scope, path)
if err != nil {
return http.StatusNotFound, err
}
fileOpts := files.FileOptions{
Path: realPath,
IsDir: isDir,
Path: filepath.Join(d.user.Scope, path),
Source: source,
Modify: d.user.Perm.Modify,
Expand: false,
ReadHeader: config.Server.TypeDetectionByHeader,
@@ -221,6 +253,7 @@ func resourcePutHandler(w http.ResponseWriter, r *http.Request, d *requestContex
// @Accept json
// @Produce json
// @Param from query string true "Path from resource"
// @Param source query string false "Source name for the desired source, default is used if not provided"
// @Param destination query string true "Destination path for the resource"
// @Param action query string true "Action to perform (copy, rename)"
// @Param overwrite query bool false "Overwrite if destination exists"
@@ -233,27 +266,37 @@ func resourcePutHandler(w http.ResponseWriter, r *http.Request, d *requestContex
// @Router /api/resources [patch]
func resourcePatchHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
// TODO source := r.URL.Query().Get("source")
src := r.URL.Query().Get("from")
dst := r.URL.Query().Get("destination")
action := r.URL.Query().Get("action")
dst, err := url.QueryUnescape(dst)
if !d.user.Check(src) || !d.user.Check(dst) {
return http.StatusForbidden, nil
source := r.URL.Query().Get("source")
if source == "" {
source = "default"
}
encodedFrom := r.URL.Query().Get("from")
// Decode the URL-encoded path
src, err := url.QueryUnescape(encodedFrom)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("invalid path encoding: %v", err)
}
dst := r.URL.Query().Get("destination")
dst, err = url.QueryUnescape(dst)
if err != nil {
return errToStatus(err), err
}
if !d.user.Check(src) || !d.user.Check(dst) {
return http.StatusForbidden, nil
}
if dst == "/" || src == "/" {
return http.StatusForbidden, nil
}
idx := files.GetIndex(source)
// check target dir exists
parentDir, _, err := files.GetRealPath(d.user.Scope, filepath.Dir(dst))
parentDir, _, err := idx.GetRealPath(d.user.Scope, filepath.Dir(dst))
if err != nil {
return http.StatusNotFound, err
}
realDest := parentDir + "/" + filepath.Base(dst)
realSrc, isSrcDir, err := files.GetRealPath(d.user.Scope, src)
realSrc, isSrcDir, err := idx.GetRealPath(d.user.Scope, src)
if err != nil {
return http.StatusNotFound, err
}
@@ -267,7 +310,7 @@ func resourcePatchHandler(w http.ResponseWriter, r *http.Request, d *requestCont
return http.StatusForbidden, nil
}
err = d.RunHook(func() error {
return patchAction(r.Context(), action, realSrc, realDest, d, fileCache, isSrcDir)
return patchAction(r.Context(), action, realSrc, realDest, d, fileCache, isSrcDir, source)
}, action, realSrc, realDest, d.user)
return errToStatus(err), err
@@ -289,27 +332,29 @@ func addVersionSuffix(source string) string {
return source
}
func delThumbs(ctx context.Context, fileCache FileCache, file *files.FileInfo) error {
if err := fileCache.Delete(ctx, previewCacheKey(file, "small")); err != nil {
func delThumbs(ctx context.Context, fileCache FileCache, file files.ExtendedFileInfo) error {
if err := fileCache.Delete(ctx, previewCacheKey(file.RealPath, "small", file.FileInfo.ModTime)); err != nil {
return err
}
return nil
}
func patchAction(ctx context.Context, action, src, dst string, d *requestContext, fileCache FileCache, isSrcDir bool) error {
func patchAction(ctx context.Context, action, src, dst string, d *requestContext, fileCache FileCache, isSrcDir bool, index string) error {
switch action {
// TODO: use enum
case "copy":
if !d.user.Perm.Create {
return errors.ErrPermissionDenied
}
return files.CopyResource(src, dst, isSrcDir)
err := files.CopyResource(index, src, dst, isSrcDir)
return err
case "rename", "move":
if !d.user.Perm.Rename {
return errors.ErrPermissionDenied
}
fileInfo, err := files.FileInfoFaster(files.FileOptions{
Path: src,
Source: index,
IsDir: isSrcDir,
Modify: d.user.Perm.Modify,
Expand: false,
@@ -321,11 +366,11 @@ func patchAction(ctx context.Context, action, src, dst string, d *requestContext
}
// delete thumbnails
err = delThumbs(ctx, fileCache, fileInfo.FileInfo)
err = delThumbs(ctx, fileCache, fileInfo)
if err != nil {
return err
}
return files.MoveResource(src, dst, isSrcDir)
return files.MoveResource(index, src, dst, isSrcDir)
default:
return fmt.Errorf("unsupported action %s: %w", action, errors.ErrInvalidRequestParams)
}
@@ -342,7 +387,7 @@ type DiskUsageResponse struct {
// @Tags Resources
// @Accept json
// @Produce json
// @Param source query string false "Name for the desired source, default is used if not provided"
// @Param source query string false "Source name for the desired source, default is used if not provided"
// @Success 200 {object} DiskUsageResponse "Disk usage details"
// @Failure 404 {object} map[string]string "Directory not found"
// @Failure 500 {object} map[string]string "Internal server error"
@@ -350,22 +395,19 @@ type DiskUsageResponse struct {
func diskUsage(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
source := r.URL.Query().Get("source")
if source == "" {
source = "/"
source = "default"
}
value, ok := utils.DiskUsageCache.Get(source).(DiskUsageResponse)
if ok {
return renderJSON(w, r, &value)
}
fPath, isDir, err := files.GetRealPath(d.user.Scope, source)
if err != nil {
return errToStatus(err), err
rootPath, ok := files.RootPaths[source]
if !ok {
return 400, fmt.Errorf("bad source path provided: %v", source)
}
if !isDir {
return http.StatusNotFound, fmt.Errorf("not a directory: %s", source)
}
usage, err := disk.UsageWithContext(r.Context(), fPath)
usage, err := disk.UsageWithContext(r.Context(), rootPath)
if err != nil {
return errToStatus(err), err
}
@@ -378,10 +420,16 @@ func diskUsage(w http.ResponseWriter, r *http.Request, d *requestContext) (int,
}
func inspectIndex(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
isDir := r.URL.Query().Get("isDir") == "true"
index := files.GetIndex(config.Server.Root)
info, _ := index.GetReducedMetadata(path, isDir)
encodedPath := r.URL.Query().Get("path")
source := r.URL.Query().Get("source")
if source == "" {
source = "default"
}
// Decode the URL-encoded path
path, _ := url.QueryUnescape(encodedPath)
isNotDir := r.URL.Query().Get("isDir") == "false" // default to isDir true
index := files.GetIndex(source)
info, _ := index.GetReducedMetadata(path, !isNotDir)
renderJSON(w, r, info) // nolint:errcheck
}
+3 -3
View File
@@ -10,9 +10,9 @@ import (
"os"
"text/template"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/version"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/version"
httpSwagger "github.com/swaggo/http-swagger" // http-swagger middleware
)
+7 -4
View File
@@ -4,8 +4,7 @@ import (
"net/http"
"strings"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/backend/files"
)
// searchHandler handles search requests for files based on the provided query.
@@ -49,16 +48,20 @@ import (
// @Param query query string true "Search query"
// @Param scope query string false "path within user scope to search, for example '/first/second' to search within the second directory only"
// @Param SessionId header string false "User session ID, add unique value to prevent collisions"
// @Success 200 {array} files.searchResult "List of search results"
// @Success 200 {array} files.SearchResult "List of search results"
// @Failure 400 {object} map[string]string "Bad Request"
// @Router /api/search [get]
func searchHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
query := r.URL.Query().Get("query")
source := r.URL.Query().Get("source")
if source == "" {
source = "default"
}
searchScope := strings.TrimPrefix(r.URL.Query().Get("scope"), ".")
searchScope = strings.TrimPrefix(searchScope, "/")
// Retrieve the User-Agent and X-Auth headers from the request
sessionId := r.Header.Get("SessionId")
index := files.GetIndex(settings.Config.Server.Root)
index := files.GetIndex(source)
userScope := strings.TrimPrefix(d.user.Scope, ".")
combinedScope := strings.TrimPrefix(userScope+"/"+searchScope, "/")
+2 -2
View File
@@ -4,8 +4,8 @@ import (
"encoding/json"
"net/http"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
type settingsData struct {
+3 -2
View File
@@ -12,8 +12,8 @@ import (
"golang.org/x/crypto/bcrypt"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/share"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/share"
)
// shareListHandler returns a list of all share links.
@@ -68,6 +68,7 @@ func shareGetsHandler(w http.ResponseWriter, r *http.Request, d *requestContext)
if err == errors.ErrNotExist {
return renderJSON(w, r, []*share.Link{})
}
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error getting share info from server")
}
+7 -6
View File
@@ -7,14 +7,13 @@ import (
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"github.com/gtsteffaniak/filebrowser/auth"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/version"
"github.com/gtsteffaniak/filebrowser/backend/auth"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/version"
)
var templateRenderer *TemplateRenderer
@@ -45,14 +44,14 @@ func handleWithStaticData(w http.ResponseWriter, r *http.Request, file, contentT
data := map[string]interface{}{
"Name": config.Frontend.Name,
"DisableExternal": config.Frontend.DisableExternal,
"DisableExternal": config.Frontend.DisableDefaultLinks,
"DisableUsedPercentage": config.Frontend.DisableUsedPercentage,
"darkMode": settings.Config.UserDefaults.DarkMode,
"Color": config.Frontend.Color,
"BaseURL": config.Server.BaseURL,
"Version": version.Version,
"CommitSHA": version.CommitSHA,
"StaticURL": path.Join(config.Server.BaseURL, "static"),
"StaticURL": config.Server.BaseURL + "static",
"Signup": settings.Config.Auth.Signup,
"NoAuth": config.Auth.Method == "noauth",
"AuthMethod": config.Auth.Method,
@@ -62,6 +61,8 @@ func handleWithStaticData(w http.ResponseWriter, r *http.Request, file, contentT
"EnableThumbs": config.Server.EnableThumbnails,
"ResizePreview": config.Server.ResizePreview,
"EnableExec": config.Server.EnableExec,
"ReCaptchaHost": config.Auth.Recaptcha.Host,
"ExternalLinks": config.Frontend.ExternalLinks,
}
if config.Frontend.Files != "" {
+22 -17
View File
@@ -12,10 +12,10 @@ import (
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/storage"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/storage"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
var (
@@ -55,26 +55,29 @@ func userGetHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (
if givenUserIdString == "self" {
givenUserId = d.user.ID
} else if givenUserIdString == "" {
if !d.user.Perm.Admin {
return http.StatusForbidden, nil
}
users, err := store.Users.Gets(config.Server.Root)
userList, err := store.Users.Gets(files.RootPaths["default"])
if err != nil {
return http.StatusInternalServerError, err
}
for _, u := range users {
selfUserList := []*users.User{}
for _, u := range userList {
u.Password = ""
}
for _, u := range users {
u.ApiKeys = nil
if u.ID == d.user.ID {
selfUserList = append(selfUserList, u)
}
}
sort.Slice(users, func(i, j int) bool {
return users[i].ID < users[j].ID
sort.Slice(userList, func(i, j int) bool {
return userList[i].ID < userList[j].ID
})
return renderJSON(w, r, users)
if !d.user.Perm.Admin {
userList = selfUserList
}
return renderJSON(w, r, userList)
} else {
num, _ := strconv.ParseUint(givenUserIdString, 10, 32)
givenUserId = uint(num)
@@ -85,7 +88,7 @@ func userGetHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (
}
// Fetch the user details
u, err := store.Users.Get(config.Server.Root, givenUserId)
u, err := store.Users.Get(files.RootPaths["default"], givenUserId)
if err == errors.ErrNotExist {
return http.StatusNotFound, err
}
@@ -156,7 +159,8 @@ func usersPostHandler(w http.ResponseWriter, r *http.Request, d *requestContext)
}
// Validate the user's scope
_, _, err := files.GetRealPath(config.Server.Root, d.user.Scope)
idx := files.GetIndex("default")
_, _, err := idx.GetRealPath(d.user.Scope)
if err != nil {
return http.StatusBadRequest, err
}
@@ -214,7 +218,8 @@ func userPutHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (
}
// Validate the user's scope
_, _, err := files.GetRealPath(config.Server.Root, d.user.Scope)
idx := files.GetIndex("default")
_, _, err := idx.GetRealPath(d.user.Scope)
if err != nil {
return http.StatusBadRequest, err
}
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"net/http"
"os"
libErrors "github.com/gtsteffaniak/filebrowser/errors"
libErrors "github.com/gtsteffaniak/filebrowser/backend/errors"
)
func errToStatus(err error) int {
+19
View File
@@ -261,3 +261,22 @@ func getEmbeddedThumbnail(in io.Reader) ([]byte, io.Reader, error) {
thm, err := ifd.Thumbnail()
return thm, wrappedReader, err
}
// CreateThumbnail takes raw image data and creates a thumbnail image.
func CreateThumbnail(rawData io.Reader, width, height int) (image.Image, error) {
// Decode the raw image to get an image.Image.
img, _, err := image.Decode(rawData)
if err != nil {
return nil, fmt.Errorf("failed to decode image: %w", err)
}
// Resize the image to create a thumbnail using the specified dimensions.
thumb := imaging.Fit(img, width, height, imaging.Lanczos)
// Optionally, convert the thumbnail to grayscale if needed.
// Uncomment the line below if you want the result to be grayscale.
// thumb = imaging.Grayscale(thumb)
// Return the resized thumbnail image.
return thumb, nil
}
+1 -1
View File
@@ -1,7 +1,7 @@
package main
import (
"github.com/gtsteffaniak/filebrowser/cmd"
"github.com/gtsteffaniak/filebrowser/backend/cmd"
)
func main() {
+1 -1
View File
@@ -3,7 +3,7 @@ package runner
import (
"os/exec"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/backend/settings"
)
// ParseCommand parses the command taking in account if the current
+6 -5
View File
@@ -7,9 +7,9 @@ import (
"os/exec"
"strings"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
// Runner is a commands runner.
@@ -20,8 +20,9 @@ type Runner struct {
// RunHook runs the hooks for the before and after event.
func (r *Runner) RunHook(fn func() error, evt, path, dst string, user *users.User) error {
path, _, _ = files.GetRealPath(user.Scope, path)
dst, _, _ = files.GetRealPath(user.Scope, dst)
idx := files.GetIndex("default")
path, _, _ = idx.GetRealPath(user.Scope, path)
dst, _, _ = idx.GetRealPath(user.Scope, dst)
if r.Enabled {
if val, ok := r.Commands["before_"+evt]; ok {
+42 -11
View File
@@ -1,13 +1,15 @@
package settings
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/goccy/go-yaml"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/version"
)
var Config Settings
@@ -21,21 +23,50 @@ func Initialize(configFile string) {
}
Config.UserDefaults.Perm = Config.UserDefaults.Permissions
// Convert relative path to absolute path
realRoot, err := filepath.Abs(Config.Server.Root)
if err != nil {
log.Fatalf("Error getting root path: %v", err)
if len(Config.Server.Sources) > 0 {
// TODO allow multipe sources not named default
for _, source := range Config.Server.Sources {
realPath, err := filepath.Abs(source.Path)
if err != nil {
log.Fatalf("Error getting source path: %v", err)
}
source.Path = realPath
source.Name = "default" // Modify the local copy of the map value
Config.Server.Sources["default"] = source // Assign the modified value back to the map
}
} else {
realPath, err := filepath.Abs(Config.Server.Root)
if err != nil {
log.Fatalf("Error getting source path: %v", err)
}
Config.Server.Sources = map[string]Source{
"default": {
Name: "default",
Path: realPath,
},
}
}
_, err = os.Stat(realRoot)
if err != nil {
log.Fatalf("ERROR: Configured Root Path does not exist! %v", err)
}
Config.Server.Root = realRoot
baseurl := strings.Trim(Config.Server.BaseURL, "/")
if baseurl == "" {
Config.Server.BaseURL = "/"
} else {
Config.Server.BaseURL = "/" + baseurl + "/"
}
if !Config.Frontend.DisableDefaultLinks {
Config.Frontend.ExternalLinks = append(Config.Frontend.ExternalLinks, ExternalLink{
Text: "FileBrowser Quantum",
Url: "https://github.com/gtsteffaniak/filebrowser",
})
Config.Frontend.ExternalLinks = append(Config.Frontend.ExternalLinks, ExternalLink{
Text: fmt.Sprintf("(%v)", version.Version),
Title: version.CommitSHA,
Url: "https://github.com/gtsteffaniak/filebrowser/releases/",
})
Config.Frontend.ExternalLinks = append(Config.Frontend.ExternalLinks, ExternalLink{
Text: "Help",
Url: "https://github.com/gtsteffaniak/filebrowser/wiki",
})
}
}
func loadConfigFile(configFile string) []byte {
@@ -71,8 +102,7 @@ func setDefaults() Settings {
BaseURL: "",
Database: "database.db",
Log: "stdout",
Root: "/srv",
Indexing: true,
Root: ".",
},
Auth: Auth{
TokenExpirationTime: "2h",
@@ -93,6 +123,7 @@ func setDefaults() Settings {
DisableSettings: false,
ViewMode: "normal",
Locale: "en",
GallerySize: 3,
Permissions: users.Permissions{
Create: false,
Rename: false,
+1 -1
View File
@@ -3,7 +3,7 @@ package settings
import (
"testing"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
func TestSettings_MakeUserDir(t *testing.T) {
+1 -1
View File
@@ -3,7 +3,7 @@ package settings
import (
"crypto/rand"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
const DefaultUsersHomeBasePath = "/users"
+1 -2
View File
@@ -41,14 +41,13 @@ func TestConfigLoadSpecificValues(t *testing.T) {
}{
{"Auth.Method", Config.Auth.Method, newConfig.Auth.Method},
{"Auth.Method", Config.Auth.Method, newConfig.Auth.Method},
{"Frontend.disableExternal", Config.Frontend.DisableExternal, newConfig.Frontend.DisableExternal},
{"UserDefaults.HideDotfiles", Config.UserDefaults.HideDotfiles, newConfig.UserDefaults.HideDotfiles},
{"Server.Database", Config.Server.Database, newConfig.Server.Database},
}
for _, tc := range testCases {
if tc.globalVal == tc.newVal {
t.Errorf("Differences should have been found:\n\tConfig.%s: %v \n\tSetConfig: %v \n", tc.fieldName, tc.globalVal, tc.newVal)
t.Errorf("Differences should have been found:\nConfig.%s: %v \nSetConfig: %v \n", tc.fieldName, tc.globalVal, tc.newVal)
}
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
package settings
import (
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
// StorageBackend is a settings storage backend.
+55 -25
View File
@@ -1,7 +1,7 @@
package settings
import (
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
type Settings struct {
@@ -35,33 +35,62 @@ type Recaptcha struct {
}
type Server struct {
IndexingInterval uint32 `json:"indexingInterval"`
NumImageProcessors int `json:"numImageProcessors"`
Socket string `json:"socket"`
TLSKey string `json:"tlsKey"`
TLSCert string `json:"tlsCert"`
EnableThumbnails bool `json:"enableThumbnails"`
ResizePreview bool `json:"resizePreview"`
EnableExec bool `json:"enableExec"`
TypeDetectionByHeader bool `json:"typeDetectionByHeader"`
AuthHook string `json:"authHook"`
Port int `json:"port"`
BaseURL string `json:"baseURL"`
Address string `json:"address"`
Log string `json:"log"`
Database string `json:"database"`
Root string `json:"root"`
UserHomeBasePath string `json:"userHomeBasePath"`
CreateUserDir bool `json:"createUserDir"`
Indexing bool `json:"indexing"`
NumImageProcessors int `json:"numImageProcessors"`
Socket string `json:"socket"`
TLSKey string `json:"tlsKey"`
TLSCert string `json:"tlsCert"`
EnableThumbnails bool `json:"enableThumbnails"`
ResizePreview bool `json:"resizePreview"`
EnableExec bool `json:"enableExec"`
TypeDetectionByHeader bool `json:"typeDetectionByHeader"`
AuthHook string `json:"authHook"`
Port int `json:"port"`
BaseURL string `json:"baseURL"`
Address string `json:"address"`
Log string `json:"log"`
Database string `json:"database"`
Root string `json:"root"`
UserHomeBasePath string `json:"userHomeBasePath"`
CreateUserDir bool `json:"createUserDir"`
Sources map[string]Source `json:"sources"`
}
type Source struct {
Path string `json:"path"`
Name string
Config IndexConfig `json:"config"`
}
type IndexConfig struct {
IndexingInterval uint32 `json:"indexingInterval"`
Disabled bool `json:"disabled"`
MaxWatchers int `json:"maxWatchers"`
NeverWatch []string `json:"neverWatchPaths"`
IgnoreHidden bool `json:"ignoreHidden"`
IgnoreZeroSizeFolders bool `json:"ignoreZeroSizeFolders"`
Exclude IndexFilter `json:"exclude"`
Include IndexFilter `json:"include"`
}
type IndexFilter struct {
Files []string `json:"files"`
Folders []string `json:"folders"`
FileEndsWith []string `json:"fileEndsWith"`
}
type Frontend struct {
Name string `json:"name"`
DisableExternal bool `json:"disableExternal"`
DisableUsedPercentage bool `json:"disableUsedPercentage"`
Files string `json:"files"`
Color string `json:"color"`
Name string `json:"name"`
DisableDefaultLinks bool `json:"disableDefaultLinks"`
DisableUsedPercentage bool `json:"disableUsedPercentage"`
Files string `json:"files"`
Color string `json:"color"`
ExternalLinks []ExternalLink `json:"externalLinks"`
}
type ExternalLink struct {
Text string `json:"text"`
Title string `json:"title"`
Url string `json:"url"`
}
// UserDefaults is a type that holds the default values
@@ -86,4 +115,5 @@ type UserDefaults struct {
Commands []string `json:"commands,omitempty"`
HideDotfiles bool `json:"hideDotfiles"`
DateFormat bool `json:"dateFormat"`
ThemeColor string `json:"themeColor"`
}
+1 -1
View File
@@ -3,7 +3,7 @@ package share
import (
"time"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/backend/errors"
)
// StorageBackend is the interface to implement for a share storage.
+2 -2
View File
@@ -2,8 +2,8 @@ package bolt
import (
"github.com/asdine/storm/v3"
"github.com/gtsteffaniak/filebrowser/auth"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/backend/auth"
"github.com/gtsteffaniak/filebrowser/backend/errors"
)
type authBackend struct {
+4 -4
View File
@@ -3,10 +3,10 @@ package bolt
import (
"github.com/asdine/storm/v3"
"github.com/gtsteffaniak/filebrowser/auth"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/share"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/backend/auth"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/share"
"github.com/gtsteffaniak/filebrowser/backend/users"
)
// NewStorage creates a storage.Storage based on Bolt DB.
+1 -1
View File
@@ -2,7 +2,7 @@ package bolt
import (
"github.com/asdine/storm/v3"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/backend/settings"
)
type settingsBackend struct {
+19 -3
View File
@@ -1,11 +1,14 @@
package bolt
import (
"log"
"time"
"github.com/asdine/storm/v3"
"github.com/asdine/storm/v3/q"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/share"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/share"
)
type shareBackend struct {
@@ -59,7 +62,20 @@ func (s shareBackend) Gets(path string, id uint) ([]*share.Link, error) {
return v, errors.ErrNotExist
}
return v, err
filteredList := []*share.Link{}
// automatically delete and clear expired shares
for i := range v {
if v[i].Expire < time.Now().Unix() {
err = s.Delete(v[i].PasswordHash)
if err != nil {
log.Println("expired share could not be deleted: ", err.Error())
}
} else {
filteredList = append(filteredList, v[i])
}
}
return filteredList, err
}
func (s shareBackend) Save(l *share.Link) error {
+3 -3
View File
@@ -6,9 +6,9 @@ import (
"github.com/asdine/storm/v3"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
type usersBackend struct {
+1 -1
View File
@@ -3,7 +3,7 @@ package bolt
import (
"github.com/asdine/storm/v3"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/backend/errors"
)
func get(db *storm.DB, name string, to interface{}) error {
+11 -10
View File
@@ -7,14 +7,14 @@ import (
"path/filepath"
"github.com/asdine/storm/v3"
"github.com/gtsteffaniak/filebrowser/auth"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/files"
"github.com/gtsteffaniak/filebrowser/settings"
"github.com/gtsteffaniak/filebrowser/share"
"github.com/gtsteffaniak/filebrowser/storage/bolt"
"github.com/gtsteffaniak/filebrowser/users"
"github.com/gtsteffaniak/filebrowser/utils"
"github.com/gtsteffaniak/filebrowser/backend/auth"
"github.com/gtsteffaniak/filebrowser/backend/errors"
"github.com/gtsteffaniak/filebrowser/backend/files"
"github.com/gtsteffaniak/filebrowser/backend/settings"
"github.com/gtsteffaniak/filebrowser/backend/share"
"github.com/gtsteffaniak/filebrowser/backend/storage/bolt"
"github.com/gtsteffaniak/filebrowser/backend/users"
"github.com/gtsteffaniak/filebrowser/backend/utils"
)
// Storage is a storage powered by a Backend which makes the necessary
@@ -116,14 +116,15 @@ func CreateUser(userInfo users.User, asAdmin bool) error {
newUser.Perm = settings.AdminPerms()
}
// create new home directory
userHome, err := settings.Config.MakeUserDir(newUser.Username, newUser.Scope, settings.Config.Server.Root)
userHome, err := settings.Config.MakeUserDir(newUser.Username, newUser.Scope, files.RootPaths["default"])
if err != nil {
log.Printf("create user: failed to mkdir user home dir: [%s]", userHome)
return err
}
newUser.Scope = userHome
log.Printf("user: %s, home dir: [%s].", newUser.Username, userHome)
_, _, err = files.GetRealPath(settings.Config.Server.Root, newUser.Scope)
idx := files.GetIndex("default")
_, _, err = idx.GetRealPath(newUser.Scope)
if err != nil {
log.Println("user path is not valid", newUser.Scope)
return nil
+60 -4
View File
@@ -199,6 +199,12 @@ const docTemplate = `{
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
@@ -265,6 +271,12 @@ const docTemplate = `{
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
@@ -334,6 +346,12 @@ const docTemplate = `{
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
@@ -409,6 +427,12 @@ const docTemplate = `{
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
@@ -469,6 +493,12 @@ const docTemplate = `{
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Destination path for the resource",
@@ -579,7 +609,7 @@ const docTemplate = `{
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/files.searchResult"
"$ref": "#/definitions/files.SearchResult"
}
}
},
@@ -857,7 +887,7 @@ const docTemplate = `{
"parameters": [
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
}
@@ -1186,7 +1216,7 @@ const docTemplate = `{
}
}
},
"files.searchResult": {
"files.SearchResult": {
"type": "object",
"properties": {
"path": {
@@ -1260,18 +1290,38 @@ const docTemplate = `{
}
}
},
"settings.ExternalLink": {
"type": "object",
"properties": {
"text": {
"type": "string"
},
"title": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
"settings.Frontend": {
"type": "object",
"properties": {
"color": {
"type": "string"
},
"disableExternal": {
"disableDefaultLinks": {
"type": "boolean"
},
"disableUsedPercentage": {
"type": "boolean"
},
"externalLinks": {
"type": "array",
"items": {
"$ref": "#/definitions/settings.ExternalLink"
}
},
"files": {
"type": "string"
},
@@ -1342,6 +1392,9 @@ const docTemplate = `{
"stickySidebar": {
"type": "boolean"
},
"themeColor": {
"type": "string"
},
"viewMode": {
"type": "string"
}
@@ -1542,6 +1595,9 @@ const docTemplate = `{
"stickySidebar": {
"type": "boolean"
},
"themeColor": {
"type": "string"
},
"username": {
"type": "string"
},
+60 -4
View File
@@ -188,6 +188,12 @@
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
@@ -254,6 +260,12 @@
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
@@ -323,6 +335,12 @@
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
@@ -398,6 +416,12 @@
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
@@ -458,6 +482,12 @@
"in": "query",
"required": true
},
{
"type": "string",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
},
{
"type": "string",
"description": "Destination path for the resource",
@@ -568,7 +598,7 @@
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/files.searchResult"
"$ref": "#/definitions/files.SearchResult"
}
}
},
@@ -846,7 +876,7 @@
"parameters": [
{
"type": "string",
"description": "Name for the desired source, default is used if not provided",
"description": "Source name for the desired source, default is used if not provided",
"name": "source",
"in": "query"
}
@@ -1175,7 +1205,7 @@
}
}
},
"files.searchResult": {
"files.SearchResult": {
"type": "object",
"properties": {
"path": {
@@ -1249,18 +1279,38 @@
}
}
},
"settings.ExternalLink": {
"type": "object",
"properties": {
"text": {
"type": "string"
},
"title": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
"settings.Frontend": {
"type": "object",
"properties": {
"color": {
"type": "string"
},
"disableExternal": {
"disableDefaultLinks": {
"type": "boolean"
},
"disableUsedPercentage": {
"type": "boolean"
},
"externalLinks": {
"type": "array",
"items": {
"$ref": "#/definitions/settings.ExternalLink"
}
},
"files": {
"type": "string"
},
@@ -1331,6 +1381,9 @@
"stickySidebar": {
"type": "boolean"
},
"themeColor": {
"type": "string"
},
"viewMode": {
"type": "string"
}
@@ -1531,6 +1584,9 @@
"stickySidebar": {
"type": "boolean"
},
"themeColor": {
"type": "string"
},
"username": {
"type": "string"
},
+41 -4
View File
@@ -31,7 +31,7 @@ definitions:
type:
type: string
type: object
files.searchResult:
files.SearchResult:
properties:
path:
type: string
@@ -79,14 +79,27 @@ definitions:
userHomeBasePath:
type: string
type: object
settings.ExternalLink:
properties:
text:
type: string
title:
type: string
url:
type: string
type: object
settings.Frontend:
properties:
color:
type: string
disableExternal:
disableDefaultLinks:
type: boolean
disableUsedPercentage:
type: boolean
externalLinks:
items:
$ref: '#/definitions/settings.ExternalLink'
type: array
files:
type: string
name:
@@ -133,6 +146,8 @@ definitions:
type: object
stickySidebar:
type: boolean
themeColor:
type: string
viewMode:
type: string
type: object
@@ -267,6 +282,8 @@ definitions:
$ref: '#/definitions/users.Sorting'
stickySidebar:
type: boolean
themeColor:
type: string
username:
type: string
viewMode:
@@ -398,6 +415,10 @@ paths:
name: path
required: true
type: string
- description: Source name for the desired source, default is used if not provided
in: query
name: source
type: string
- description: Name for the desired source, default is used if not provided
in: query
name: source
@@ -439,6 +460,10 @@ paths:
name: path
required: true
type: string
- description: Source name for the desired source, default is used if not provided
in: query
name: source
type: string
- description: Name for the desired source, default is used if not provided
in: query
name: source
@@ -483,6 +508,10 @@ paths:
name: from
required: true
type: string
- description: Source name for the desired source, default is used if not provided
in: query
name: source
type: string
- description: Destination path for the resource
in: query
name: destination
@@ -544,6 +573,10 @@ paths:
name: path
required: true
type: string
- description: Source name for the desired source, default is used if not provided
in: query
name: source
type: string
- description: Name for the desired source, default is used if not provided
in: query
name: source
@@ -594,6 +627,10 @@ paths:
name: path
required: true
type: string
- description: Source name for the desired source, default is used if not provided
in: query
name: source
type: string
- description: Name for the desired source, default is used if not provided
in: query
name: source
@@ -658,7 +695,7 @@ paths:
description: List of search results
schema:
items:
$ref: '#/definitions/files.searchResult'
$ref: '#/definitions/files.SearchResult'
type: array
"400":
description: Bad Request
@@ -842,7 +879,7 @@ paths:
- application/json
description: Returns the total and used disk space for a specified directory.
parameters:
- description: Name for the desired source, default is used if not provided
- description: Source name for the desired source, default is used if not provided
in: query
name: source
type: string
-113
View File
@@ -1,113 +0,0 @@
== Running tests ==
/usr/local/go/bin/go
? github.com/gtsteffaniak/filebrowser [no test files]
? github.com/gtsteffaniak/filebrowser/auth [no test files]
? github.com/gtsteffaniak/filebrowser/cmd [no test files]
? github.com/gtsteffaniak/filebrowser/errors [no test files]
? github.com/gtsteffaniak/filebrowser/files [no test files]
=== RUN TestFileCache
--- PASS: TestFileCache (0.00s)
PASS
ok github.com/gtsteffaniak/filebrowser/diskcache (cached)
=== RUN TestCommonPrefix
=== RUN TestCommonPrefix/sub_folder
=== RUN TestCommonPrefix/relative_path
=== RUN TestCommonPrefix/no_common_path
=== RUN TestCommonPrefix/same_lvl
--- PASS: TestCommonPrefix (0.00s)
--- PASS: TestCommonPrefix/sub_folder (0.00s)
--- PASS: TestCommonPrefix/relative_path (0.00s)
--- PASS: TestCommonPrefix/no_common_path (0.00s)
--- PASS: TestCommonPrefix/same_lvl (0.00s)
PASS
ok github.com/gtsteffaniak/filebrowser/fileutils (cached)
? github.com/gtsteffaniak/filebrowser/settings [no test files]
? github.com/gtsteffaniak/filebrowser/share [no test files]
? github.com/gtsteffaniak/filebrowser/storage [no test files]
? github.com/gtsteffaniak/filebrowser/storage/bolt [no test files]
2023/09/02 13:07:17 Error opening YAML file: open filebrowser.yaml: no such file or directory
FAIL github.com/gtsteffaniak/filebrowser/http 0.008s
=== RUN TestService_Resize
=== RUN TestService_Resize/convert_to_png
=== RUN TestService_Resize/convert_to_tiff
=== RUN TestService_Resize/resize_bmp
=== RUN TestService_Resize/resize_with_medium_quality
=== RUN TestService_Resize/resize_with_low_quality
=== RUN TestService_Resize/get_thumbnail_from_file_with_APP0_JFIF
=== RUN TestService_Resize/fill_upscale
=== RUN TestService_Resize/fit_upscale
=== RUN TestService_Resize/convert_to_gif
=== RUN TestService_Resize/convert_to_bmp
=== RUN TestService_Resize/resize_tiff
=== RUN TestService_Resize/resize_with_high_quality
=== RUN TestService_Resize/fill_downscale
=== RUN TestService_Resize/keep_original_format
=== RUN TestService_Resize/convert_to_unknown
=== RUN TestService_Resize/get_thumbnail_from_file_without_APP0_JFIF
=== RUN TestService_Resize/resize_for_higher_quality_levels
=== RUN TestService_Resize/broken_file
=== RUN TestService_Resize/fit_downscale
=== RUN TestService_Resize/convert_to_jpeg
=== RUN TestService_Resize/resize_png
=== RUN TestService_Resize/resize_gif
=== RUN TestService_Resize/resize_with_unknown_quality
=== RUN TestService_Resize/resize_from_file_without_IFD1_thumbnail
--- PASS: TestService_Resize (1.36s)
--- PASS: TestService_Resize/convert_to_png (0.01s)
--- PASS: TestService_Resize/convert_to_tiff (0.01s)
--- PASS: TestService_Resize/resize_bmp (0.01s)
--- PASS: TestService_Resize/resize_with_medium_quality (0.01s)
--- PASS: TestService_Resize/resize_with_low_quality (0.01s)
--- PASS: TestService_Resize/get_thumbnail_from_file_with_APP0_JFIF (0.02s)
--- PASS: TestService_Resize/fill_upscale (0.01s)
--- PASS: TestService_Resize/fit_upscale (0.00s)
--- PASS: TestService_Resize/convert_to_gif (0.01s)
--- PASS: TestService_Resize/convert_to_bmp (0.01s)
--- PASS: TestService_Resize/resize_tiff (0.01s)
--- PASS: TestService_Resize/resize_with_high_quality (0.01s)
--- PASS: TestService_Resize/fill_downscale (0.01s)
--- PASS: TestService_Resize/keep_original_format (0.01s)
--- PASS: TestService_Resize/convert_to_unknown (0.01s)
--- PASS: TestService_Resize/get_thumbnail_from_file_without_APP0_JFIF (0.03s)
--- PASS: TestService_Resize/resize_for_higher_quality_levels (0.03s)
--- PASS: TestService_Resize/broken_file (0.00s)
--- PASS: TestService_Resize/fit_downscale (0.01s)
--- PASS: TestService_Resize/convert_to_jpeg (0.01s)
--- PASS: TestService_Resize/resize_png (0.02s)
--- PASS: TestService_Resize/resize_gif (0.02s)
--- PASS: TestService_Resize/resize_with_unknown_quality (0.01s)
--- PASS: TestService_Resize/resize_from_file_without_IFD1_thumbnail (1.09s)
=== RUN TestService_FormatFromExtension
=== RUN TestService_FormatFromExtension/gif
=== RUN TestService_FormatFromExtension/tiff
=== RUN TestService_FormatFromExtension/bmp
=== RUN TestService_FormatFromExtension/unknown
=== RUN TestService_FormatFromExtension/jpg
=== RUN TestService_FormatFromExtension/jpeg
=== RUN TestService_FormatFromExtension/png
--- PASS: TestService_FormatFromExtension (0.00s)
--- PASS: TestService_FormatFromExtension/gif (0.00s)
--- PASS: TestService_FormatFromExtension/tiff (0.00s)
--- PASS: TestService_FormatFromExtension/bmp (0.00s)
--- PASS: TestService_FormatFromExtension/unknown (0.00s)
--- PASS: TestService_FormatFromExtension/jpg (0.00s)
--- PASS: TestService_FormatFromExtension/jpeg (0.00s)
--- PASS: TestService_FormatFromExtension/png (0.00s)
PASS
ok github.com/gtsteffaniak/filebrowser/img (cached)
=== RUN TestMatchHidden
--- PASS: TestMatchHidden (0.00s)
PASS
ok github.com/gtsteffaniak/filebrowser/rules (cached)
2023/09/02 13:07:17 Error opening YAML file: open filebrowser.yaml: no such file or directory
FAIL github.com/gtsteffaniak/filebrowser/runner 0.007s
=== RUN TestParseSearch
--- PASS: TestParseSearch (0.00s)
PASS
ok github.com/gtsteffaniak/filebrowser/search (cached)
? github.com/gtsteffaniak/filebrowser/version [no test files]
testing: warning: no tests to run
PASS
ok github.com/gtsteffaniak/filebrowser/users (cached) [no tests to run]
FAIL
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"sync"
"time"
"github.com/gtsteffaniak/filebrowser/errors"
"github.com/gtsteffaniak/filebrowser/backend/errors"
)
// StorageBackend is the interface to implement for a users storage.
+1
View File
@@ -55,6 +55,7 @@ type User struct {
HideDotfiles bool `json:"hideDotfiles"`
DateFormat bool `json:"dateFormat"`
GallerySize int `json:"gallerySize"`
ThemeColor string `json:"themeColor"`
}
var PublicUser = User{
-225
View File
@@ -1,225 +0,0 @@
# Configuration Help
This document covers the available configuration options, their defaults, and how they affect the functionality of filebrowser.
## All possible configurations
Here is an expanded config file which includes all possible configurations:
```
server:
CreateUserDir: false
UserHomeBasePath: ""
indexing: true
numImageProcessors: 4
socket: ""
tlsKey: ""
tlsCert: ""
enableThumbnails: false
resizePreview: true
typeDetectionByHeader: true
port: 80
baseURL: "/"
address: ""
log: "stdout"
database: "/database/database.db"
root: "/srv"
auth:
adminUsername: admin
adminPassword: admin
recaptcha:
host: ""
key: ""
secret: ""
tokenExpirationTime: 2h
header: ""
method: json
signup: false
frontend:
name: ""
disableExternal: false
disableUsedPercentage: true
files: ""
color: ""
userDefaults:
settingsAllowed: true
darkMode: false
scope: ""
locale: "en"
viewMode: ""
singleClick: true
sorting:
by: ""
asc: true
permissions:
admin: true
execute: true
create: true
rename: true
modify: true
delete: true
share: true
download: true
hideDotfiles: false
dateFormat: false
```
Here are the defaults if nothing is set:
```
server:
enableThumbnails: true
enableExec: false
port: 80
numImageProcessors: 4
baseURL: ""
database: database.db
log: stdout
root: /srv
auth:
adminUsername: admin
adminPassword: admin
method: password
recaptcha:
host: ""
userDefaults:
settingsAllowed: true
darkMode: false
scope: ""
locale: "en"
scope: "."
lockPassword: false
hideDotfiles: true
permissions:
create: true
rename: true
modify: true
delete: true
share: true
download: true
```
## About each configuration
# Configuration Settings Documentation
## About each configuration
### Server configuration settings
- `indexingInterval`: This optional paramter disables smart indexing and specifies a time in minutes the system waits before checking for filesystem changes. See [indexing readme](indexing.md) for more information.
- `indexing`: This enables or disables indexing. (Note: search will not work without indexing) Default: `true`
- `numImageProcessors`: This is the number of image processors available. Default: `4`
- `socket`: This is the socket configuration.
- `tlsKey`: This is the TLS key configuration.
- `tlsCert`: This is the TLS certificate configuration.
- `enableThumbnails`: This boolean value determines whether thumbnails are enabled on ui. Default: `true`
- `resizePreview`: This boolean value determines whether preview resizing is enabled. Default: `false`
- `typeDetectionByHeader`: This boolean value determines whether type detection is based on headers.
- `port`: This is the port number on which the server is running. Default: `80`
- `baseURL`: This is the base URL for the server. Default: `""`
- `address`: This is the server address configuration. Default: `0.0.0.0`
- `log`: This specifies the log destination. Default: `stdout`
- `database`: This is the database file path + filename that will be created if it does not already exist. If it exists, it will use the existing file. Default `database.db`
- `root`: This is the root directory path. Default: `/srv`
- `CreateUserDir`: Boolean to create user directory on user creation. Default: `false`
- `UserHomeBasePath`: String to define user home directory base path. Default: `""`
### Auth configuration settings
- `recaptcha`:
- `host`: This is the host for reCAPTCHA.
- `key`: This is the reCAPTCHA key.
- `secret`: This is the reCAPTCHA secret.
- `header`: This is the authentication header.
- `method`: This is the authentication method used (e.g., "json"). Possible values:
- `password` - username and password
- `hook` - hook authentication
- `proxy` - proxy authentication
- `oath` - oath authentication
- `noauth` - no authentication/login required.
- `Signup`: This boolean value indicates whether user signup is enabled on the login page. NOTE: Be mindful of `userDefaults` settings if enabled. Default: `false`
- `AdminUsername`: This is the username of the admin user. Default: `admin`
- `AdminPassword`: This is the password of the admin user. Default: `admin`
### Frontend configuration settings
- `name`: This is the name of the frontend.
- `disableExternal`: This boolean value determines whether external access is disabled.
- `disableUsedPercentage`: This boolean value determines whether used percentage is disabled.
- `files`: This is the files configuration.
- `theme`: This is the theme configuration.
- `color`: This is the color configuration.
### UserDefaults configuration settings
- `darkMode`: Determines whether dark mode is enabled for the user (`true` or `false`)
- `settingsAllowed`: Determines whether settings page is enabled for the user (`true` or `false`)
- `scope`: This is a scope of the permissions, "." or "./" means all directories, "./downloads" would mean only the downloads folder.
- `locale`: String locale configuration. Default: `en`
- `viewMode`: This is the view mode configuration. Possible values: `normal`, `compact`, `list`, and `gallery`. default: `normal`
- `singleClick`: This boolean value determines whether single-click is enabled. (`true` or `false`)
- `sorting`:
- `by`: This is the sorting method used (e.g., "asc").
- `asc`: This boolean value determines the sorting order is ascending or descending. (`true` or `false`)
- `permissions`:
- `admin`: This boolean value determines whether admin permissions are granted.
- `execute`: This boolean value determines whether execute permissions are granted.
- `create`: This boolean value determines whether create permissions are granted.
- `rename`: This boolean value determines whether rename permissions are granted.
- `modify`: This boolean value determines whether modify permissions are granted.
- `delete`: This boolean value determines whether delete permissions are granted.
- `share`: This boolean value determines whether share permissions are granted.
- `download`: This boolean value determines whether download permissions are granted.
- `api`: Ability to create and manage API keys.
- `hideDotfiles`: This boolean value determines whether dotfiles are hidden. (`true` or `false`)
- `dateFormat`: This boolean value determines whether date formatting is enabled. (`true` or `false`)
-13
View File
@@ -1,13 +0,0 @@
# Contributing Guide
If you would like to contribute, please open a pull request against main or the latest `dev_` branch thats currently in progress.
A PR is required to have:
1. A clear description about why it was opened
2. A short title that best describes the issue.
3. Test evidence for anything that is not self evident or covered by unit tests.
Unit tests should be updated to pass before merging. So, the best way to handle this is to create a fork and test your changes there, then merge to this repo. You can also create a draft pull request if it is not fully ready.
Please don't hesitate to open an issue for any ideas you have, but cannot contribute directly for whatever reason.
-189
View File
@@ -1,189 +0,0 @@
# About Indexing on FileBrowser Quantum
The most significant feature is the index, this document intends to demystify how it works so you can understand how to ensure your index closely matches the current state of your filesystem.
## How does the index work?
The approach used by this repo includes filesystem watchers that periodically scan the directory tree for changes. By default, this uses a smart scan strategy, but you can also configure a set interval in your config file.
The `scan interval` is the break time between scans and does not include the time a scan takes. A typical scan can vary dramatically, but here are some expectations for SSD-based disks:
| # folders | # files | time to index | memory usage (RAM) |
|---|---|---|---|
| 10,000 | 10,000 | ~ 0-5 seconds | 15 MB |
| 2,000 | 250,000 | ~ 0-5 seconds | 300 MB |
| 50,000 | 50,000 | ~ 5-30 seconds | 150 MB |
| 250,000 | 10,000 | ~ 2-5 minutes | 300 MB |
| 500,000 | 500,000 | ~ 5+ minutes | 500+ MB |
### Smart Scanning
1. There is a floating `smart scan interval` that ranges from **1 minute - 4 hours** depending on the complexity of your filesystem
2. The smart interval changes based on how often it discovers changed files:
- ```
// Schedule in minutes
var scanSchedule = []time.Duration{
5 * time.Minute, // 5 minute quick scan & 25 minutes for a full scan
10 * time.Minute,
20 * time.Minute, // [3] element is 20 minutes, reset anchor for full scan
40 * time.Minute,
1 * time.Hour,
2 * time.Hour,
3 * time.Hour,
4 * time.Hour, // 4 hours for quick scan & 20 hours for a full scan
}
```
3. The `smart scan interval` performs a `quick scan` 4 times in a row, followed by a 5th `full scan` which completely rebuilds the index.
- A `quick scan` is limited to detecting directory changes, but is 10x faster than a full scan. Here is what a quick scan can see:
1. New files or folders created.
2. Files or folders deleted.
3. Renaming of files or folders.
- A quick scan **cannot** detect when a file has been updated, for example when you save a file and the size increases.
- A `full scan` is a complete re-indexing. This is always more disk and computationally intense but will capture individual file changes.
4. The `smart scan interval` changes based on several things. A `simple` complexity enables scans every 1 minute if changes happen frequently and a maximum full scan interval of every 100 minutes. `high` complexity indicates a minimum scanning interval of 10 minutes.
- **under 10,000 folders** or **Under 3 seconds** to index is awlays considered `simple` complexity.
- **more than 500,000 folders** or **Over 2 minutes** to index is always considered `high` complexity.
### Manual Scanning Interval
If you don't like the behavior of smart scanning, you can configure set intervals instead by setting `indexingInterval` to a number greater than 0. This will make FileBrowser Quantum always scan at the given interval in minutes.
The scan behavior is still 4 quick scans at a given interval, followed by a 5th full scan.
### System requirements
You can expect FileBrowser Quantum to use 100 MB of RAM for a typical installation. If you have many files and folders then the requirement could climb to multiple Gigabytes. Please monitor your system on the first run to know your specific requirements.
### Why does FileBrowser Quantum index the way it does?
The smart indexing method uses filesystem scanners because it allows a low-footprint design that can cater to individual filesystem complexity. There are a few options for monitoring a filesystem for changes:
1. **Option 1**: Recursive Traversal with ReadDir
- This is quite computationally intensive but creates an accurate record of the filesystem
- Requires periodic scanning to remain updated
- Low overhead and straightforward implementation.
2. **Option 2**: Use File System Monitoring (Real-Time or Periodic Check) such as `fsnotify`
- This allows for event-based reactions to filesystem changes.
- Requires extra overhead.
- Relies on OS level features and behavior differs between OS's
- Requires OS-level configuration to ulimits in order to properly watch a large filesystem.
3. **Option 3**: Directory Metadata Heuristics.
- Using ModTime to determine when directory structures change.
- Has minimal insight into actual file changes.
- Much faster to scan for changes than Recursive transversal.
Ultimately, FileBrowser Quantum uses a combination of 1 and 3 to perform index updates. Using something like fsnotify is a non-starter for large filesystems, where it would require manual host OS tuning to work at all. Besides, I can essentially offer the same behavior by creating "watchers" for top-level folders (a feature to come in the future). However, right now there is a single root-level watcher that works over the entire index.
The main disadvantage of the approach is the delay caused by the scanning interval.
### How to manually refresh the index?
There is currently no way to manually trigger a new full indexing. This will come in a future release when the "jobs" functionality is added back.
However, if you want to force-refresh a certain directory, this happens every time you **view it** in the UI or via the resources API.
This also means the resources API is always up to date with the current status of the filesystem. When you "look" at a specific folder, you are causing the index to be refreshed at that location.
### What information does the index have?
You can see what the index looks like by using the resources API via the GET method, which returns individual directory information -- all of this information is stored in the index.
Here is an example:
```
{
"name": "filebrowser",
"size": 274467914,
"modified": "2024-11-23T19:18:57.68013727-06:00",
"type": "directory",
"files": [
{
"name": ".dockerignore",
"size": 73,
"modified": "2024-11-20T18:14:44.91135413-06:00",
"type": "blob"
},
{
"name": ".DS_Store",
"size": 6148,
"modified": "2024-11-22T14:45:15.901211088-06:00",
"type": "blob"
},
{
"name": ".gitignore",
"size": 455,
"modified": "2024-11-23T19:18:57.616132373-06:00",
"type": "blob"
},
{
"name": "CHANGELOG.md",
"size": 9325,
"modified": "2024-11-23T19:18:57.616646332-06:00",
"type": "text"
},
{
"name": "Dockerfile",
"size": 769,
"modified": "2024-11-23T19:18:57.616941333-06:00",
"type": "blob"
},
{
"name": "Dockerfile.playwright",
"size": 542,
"modified": "2024-11-23T19:18:57.617151875-06:00",
"type": "blob"
},
{
"name": "makefile",
"size": 1311,
"modified": "2024-11-23T19:18:57.68017352-06:00",
"type": "blob"
},
{
"name": "README.md",
"size": 10625,
"modified": "2024-11-23T19:18:57.617464334-06:00",
"type": "text"
}
],
"folders": [
{
"name": ".git",
"size": 60075460,
"modified": "2024-11-24T14:44:42.52180215-06:00",
"type": "directory"
},
{
"name": ".github",
"size": 11584,
"modified": "2024-11-20T18:14:44.911805335-06:00",
"type": "directory"
},
{
"name": "backend",
"size": 29247172,
"modified": "2024-11-23T19:18:57.667109624-06:00",
"type": "directory"
},
{
"name": "docs",
"size": 14272,
"modified": "2024-11-24T13:46:12.082024018-06:00",
"type": "directory"
},
{
"name": "frontend",
"size": 185090178,
"modified": "2024-11-24T14:44:39.880678934-06:00",
"type": "directory"
}
],
"path": "/filebrowser"
}
```
### Can I disable the index and still use FileBrowser Quantum?
You can disable the index by setting `indexing: false` in your config file. You will still be able to browse your files, but the search will not work and you may run into issues as it's not intended to be used without indexing.
I'm not sure why you would run it like this, if you have a good reason please open an issue request on how you would like it to work -- and why you would run it without the index.
-25
View File
@@ -1,25 +0,0 @@
# Migration help
It is possible to use the same database as used by filebrowser/filebrowser,
but you will need to follow the following process:
1. Create a configuration file as mentioned above.
2. Copy your database file from the original filebrowser to the path of
the new one.
3. Update the configuration file to use the database (under server in
filebrowser.yml)
4. If you are using docker, update the docker-compose file or docker run
command to use the config file as described in the install section
above.
5. If you are not using docker, just make sure you run filebrowser -c
filebrowser.yml and have a valid filebrowser config.
Note: share links will not work and will need to be re-created after migration.
The filebrowser Quantum application should run with the same user and rules that
you have from the original. But keep in mind the differences that may not work
the same way, but all user configuration should be available.
The windows binary is particularly untested, I would advise using docker if testing on windows.
-23
View File
@@ -1,23 +0,0 @@
# Planned Roadmap
upcoming 0.3.x releases, ordered by priority:
- More filetype icons and refreshed icons.
- more filetype previews - eg. office, photoshop, vector, 3d files.
- Enable mobile search with same features as desktop
- Enable mobile search with same features as desktop
- Theme configuration from settings
- introduce jobs as replacement to runners.
- Add Job status to the sidebar
- index status.
- Job status from users
- upload status
- opentelemetry metrics
Unplanned Future releases:
- multiple sources https://github.com/filebrowser/filebrowser/issues/2514
- Add tools to sidebar
- duplicate file detector.
- bulk rename https://github.com/filebrowser/filebrowser/issues/2473
- metrics tracker - user access, file access, download count, last login, etc
- support minio, s3, and backblaze sources https://github.com/filebrowser/filebrowser/issues/2544
+1 -1
View File
@@ -10,6 +10,7 @@
"scripts": {
"dev": "vite dev",
"build": "vite build && cp -r dist/* ../backend/http/embed",
"build-windows": "vite build && robocopy dist ../backend/http/embed /e",
"build-docker": "vite build",
"watch": "vite build --watch",
"typecheck": "vue-tsc -p ./tsconfig.json --noEmit",
@@ -24,7 +25,6 @@
"clipboard": "^2.0.4",
"css-vars-ponyfill": "^2.4.3",
"file-loader": "^6.2.0",
"material-icons": "^1.10.5",
"normalize.css": "^8.0.1",
"qrcode.vue": "^3.4.1",
"vue": "^3.4.21",
+1 -1
View File
@@ -14,7 +14,7 @@ export default defineConfig({
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
forbidOnly: Boolean(process.env.CI),
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
+92 -72
View File
@@ -1,17 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
{{ if .ReCaptcha }}
<script src="{{ .ReCaptchaHost }}/recaptcha/api.js?render=explicit" data-vite-ignore></script>
<script src="{{ .ReCaptchaHost }}/recaptcha/api.js?render=explicit" data-vite-ignore></script>
{{ end }}
<title>{{ if .Name }}{{ .Name }}{{ else }}FileBrowser Quantum{{ end }}</title>
<link rel="icon" type="image/png" sizes="256x256" href="{{ .StaticURL }}/img/icons/favicon-256x256.png">
<link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Symbols+Outlined" rel="stylesheet">
<!-- Add to home screen for Android and modern mobile browsers -->
<link rel="manifest" id="manifestPlaceholder" crossorigin="use-credentials">
<meta name="theme-color" content="{{ if .Color }}{{ .Color }}{{ else }}#2979ff{{ end }}">
@@ -19,7 +22,7 @@
<!-- Add to home screen for Safari on iOS/iPadOS -->
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="assets">
<link rel="apple-touch-icon" href="{{ .StaticURL }}/img/icons/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="180x180" href="{{ .StaticURL }}/img/icons/android-chrome-256x256.png">
<!-- Add to home screen for Windows -->
<meta name="msapplication-TileImage" content="{{ .StaticURL }}/img/icons/mstile-144x144.png">
@@ -27,94 +30,110 @@
<!-- Inject Some Variables and generate the manifest json -->
<script>
window.FileBrowser = JSON.parse('{{ .globalVars }}');
var fullStaticURL = window.location.origin + window.FileBrowser.StaticURL;
window.FileBrowser = JSON.parse('{{ .globalVars }}');
var dynamicManifest = {
"name": window.FileBrowser.Name || 'FileBrowser Quantum',
"short_name": window.FileBrowser.Name || 'FileBrowser',
"name": window.FileBrowser.Name || '',
"icons": [
{
"src": fullStaticURL + "/img/icons/android-chrome-256x256.png",
"src": window.location.origin + "{{ .StaticURL }}/img/icons/android-chrome-256x256.png",
"sizes": "512x512",
"type": "image/png"
}
},
{
"src": window.location.origin + "{{ .StaticURL }}/img/icons/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
],
"start_url": fullStaticURL,
"start_url": window.location.origin + "{{ .BaseURL }}",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": window.FileBrowser.Color || "#455a64"
}
const stringManifest = JSON.stringify(dynamicManifest);
const blob = new Blob([stringManifest], {type: 'application/json'});
const blob = new Blob([stringManifest], { type: 'application/json' });
const manifestURL = URL.createObjectURL(blob);
document.querySelector('#manifestPlaceholder').setAttribute('href', manifestURL);
</script>
<style>
#loading {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #fff;
z-index: 9999;
transition: .1s ease opacity;
-webkit-transition: .1s ease opacity;
}
#loading.done {
opacity: 0;
}
#loading .spinner {
width: 70px;
text-align: center;
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#loading .spinner > div {
width: 18px;
height: 18px;
background-color: #333;
border-radius: 100%;
display: inline-block;
-webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both;
animation: sk-bouncedelay 1.4s infinite ease-in-out both;
}
#loading .spinner .bounce1 {
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
#loading .spinner .bounce2 {
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
@-webkit-keyframes sk-bouncedelay {
0%, 80%, 100% { -webkit-transform: scale(0) }
40% { -webkit-transform: scale(1.0) }
}
@keyframes sk-bouncedelay {
0%, 80%, 100% {
-webkit-transform: scale(0);
transform: scale(0);
} 40% {
-webkit-transform: scale(1.0);
transform: scale(1.0);
#loading {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #fff;
z-index: 9999;
transition: .1s ease opacity;
-webkit-transition: .1s ease opacity;
}
#loading.done {
opacity: 0;
}
#loading .spinner {
width: 70px;
text-align: center;
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#loading .spinner>div {
width: 18px;
height: 18px;
background-color: #333;
border-radius: 100%;
display: inline-block;
-webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both;
animation: sk-bouncedelay 1.4s infinite ease-in-out both;
}
#loading .spinner .bounce1 {
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
#loading .spinner .bounce2 {
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
@-webkit-keyframes sk-bouncedelay {
0%,
80%,
100% {
-webkit-transform: scale(0)
}
40% {
-webkit-transform: scale(1.0)
}
}
@keyframes sk-bouncedelay {
0%,
80%,
100% {
-webkit-transform: scale(0);
transform: scale(0);
}
40% {
-webkit-transform: scale(1.0);
transform: scale(1.0);
}
}
}
</style>
</head>
<body>
<div id="app"></div>
@@ -138,7 +157,8 @@
<script type="module" src="/src/main.ts"></script>
{{ if .CSS }}
<link rel="stylesheet" href="{{ .StaticURL }}/custom.css" >
<link rel="stylesheet" href="{{ .StaticURL }}/custom.css">
{{ end }}
</body>
</html>
</html>
-20
View File
@@ -1,20 +0,0 @@
{
"name": "FileBrowser",
"short_name": "FileBrowser",
"icons": [
{
"src": "./img/icons/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "./static/img/icons/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"start_url": "./",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#455a64"
}
+17 -12
View File
@@ -6,7 +6,7 @@ import { notify } from "@/notify";
// Notify if errors occur
export async function fetchFiles(url, content = false) {
try {
let path = removePrefix(url, "files");
let path = encodeURIComponent(removePrefix(url, "files"));
const apiPath = getApiPath("api/resources",{path: path, content: content});
const res = await fetchURL(apiPath);
const data = await res.json();
@@ -24,7 +24,8 @@ async function resourceAction(url, method, content) {
if (content) {
opts.body = content;
}
const apiPath = getApiPath("api/resources", { path: url });
let path = encodeURIComponent(removePrefix(url, "files"));
const apiPath = getApiPath("api/resources", { path: path });
const res = await fetchURL(apiPath, opts);
return res;
} catch (err) {
@@ -35,7 +36,8 @@ async function resourceAction(url, method, content) {
export async function remove(url) {
try {
return await resourceAction(url, "DELETE");
let path = encodeURIComponent(removePrefix(url, "files"));
return await resourceAction(path, "DELETE");
} catch (err) {
notify.showError(err.message || "Error deleting resource");
throw err;
@@ -44,7 +46,8 @@ export async function remove(url) {
export async function put(url, content = "") {
try {
return await resourceAction(url, "PUT", content);
let path = encodeURIComponent(removePrefix(url, "files"));
return await resourceAction(path, "PUT", content);
} catch (err) {
notify.showError(err.message || "Error putting resource");
throw err;
@@ -58,14 +61,14 @@ export function download(format, files) {
try {
let fileargs = "";
if (files.length === 1) {
fileargs = removePrefix(files[0], "files")
fileargs = decodeURI(removePrefix(files[0], "files"))
} else {
for (let file of files) {
fileargs += removePrefix(file,"files") + ",";
fileargs += decodeURI(removePrefix(file,"files")) + ",";
}
fileargs = fileargs.substring(0, fileargs.length - 1);
}
const apiPath = getApiPath("api/raw", { files: fileargs, algo: format });
const apiPath = getApiPath("api/raw", { files: encodeURIComponent(fileargs), algo: format });
const url = window.origin+apiPath
window.open(url);
} catch (err) {
@@ -130,9 +133,11 @@ export async function moveCopy(items, action = "copy", overwrite = false, rename
}
try {
for (let item of items) {
let toPath = encodeURIComponent(removePrefix(decodeURI(item.to), "files"));
let fromPath = encodeURIComponent(removePrefix(decodeURI(item.from), "files"));
let localParams = { ...params };
localParams.destination = item.to;
localParams.from = item.from;
localParams.destination = toPath;
localParams.from = fromPath;
const apiPath = getApiPath("api/resources", localParams);
promises.push(fetch(apiPath, { method: "PATCH" }));
}
@@ -157,7 +162,7 @@ export async function checksum(url, algo) {
export function getDownloadURL(path, inline) {
try {
const params = {
files: removePrefix(path,"files"),
files: encodeURIComponent(removePrefix(decodeURI(path),"files")),
...(inline && { inline: "true" }),
};
const apiPath = getApiPath("api/raw", params);
@@ -171,7 +176,7 @@ export function getDownloadURL(path, inline) {
export function getPreviewURL(path, size, modified) {
try {
const params = {
path: path,
path: encodeURIComponent(removePrefix(decodeURI(path),"files")),
size: size,
key: Date.parse(modified),
inline: "true",
@@ -190,7 +195,7 @@ export function getSubtitlesURL(file) {
for (const sub of file.subtitles) {
const params = {
inline: "true",
path: sub
path: encodeURIComponent(removePrefix(sub,"files"))
};
const apiPath = getApiPath("api/raw", params);
return window.origin+apiPath
-1
View File
@@ -61,7 +61,6 @@ export async function fetchJSON(url, opts) {
export function adjustedData(data, url) {
data.url = url;
if (data.type === "directory") {
if (!data.url.endsWith("/")) data.url += "/";
+1 -1
View File
@@ -1,5 +1,5 @@
<template>
<button @click="action" :aria-label="label" :title="label" class="action">
<button @click="action" :aria-label="label" :title="label" class="action no-select">
<i class="material-icons">{{ icon }}</i>
<span>{{ label }}</span>
<span v-if="counter > 0" class="counter">{{ counter }}</span>
+3 -3
View File
@@ -21,8 +21,8 @@
type="range"
id="gallery-size"
name="gallery-size"
min="0"
max="10"
min="1"
max="8"
@input="updateGallerySize"
@change="commitGallerySize"
/>
@@ -104,7 +104,7 @@ export default {
},
showShare() {
return (
state.user?.perm && state.user?.perm.share && state.user.username != "publicUser"
state.user?.perm && state.user?.perm.share && state.user.username != "publicUser" && getters.currentView() != "share"
);
},
},
+27 -16
View File
@@ -1,14 +1,14 @@
<template>
<div class="button-group">
<button v-if="isDisabled" disabled>
No options for folders
<div @click="preventDefaults" class="button-group">
<button v-if="isDisabled" >
{{ disableMessage }}
</button>
<template v-else>
<button
v-for="(btn, index) in buttons"
:key="index"
:class="{ active: activeButton === index }"
@click="setActiveButton(index, btn.label)"
@click="setActiveButton(index, btn.value)"
>
{{ btn.label }}
</button>
@@ -19,6 +19,10 @@
<script>
export default {
props: {
disableMessage: {
type: String,
default: "No options for folders",
},
buttons: {
type: Array,
default: () => [],
@@ -28,30 +32,34 @@ export default {
default: false,
},
initialActive: {
type: Number,
default: null,
type: String,
default: "",
},
},
data() {
return {
activeButton: this.initialActive,
activeButton: null, // Initially no button is active
};
},
methods: {
setActiveButton(index, label) {
if (label == "Only Folders" && this.activeButton != index) {
preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
},
setActiveButton(index, value) {
if (value === "Only Folders" && this.activeButton !== index) {
this.$emit("disableAll");
}
if (label == "Only Folders" && this.activeButton == index) {
if (value === "Only Folders" && this.activeButton === index) {
this.$emit("enableAll");
}
if (label == "Only Files" && this.activeButton != index) {
if (value === "Only Files" && this.activeButton !== index) {
this.$emit("enableAll");
}
// If the clicked button is already active, de-select it
if (this.activeButton === index) {
this.activeButton = null;
this.$emit("remove-button-clicked", this.buttons[index].value);
this.$emit("remove-button-clicked", value);
} else {
// Emit remove-button-clicked for all other indexes
this.buttons.forEach((button, idx) => {
@@ -61,7 +69,7 @@ export default {
});
this.activeButton = index;
this.$emit("button-clicked", this.buttons[index].value);
this.$emit("button-clicked", value);
}
},
},
@@ -69,7 +77,9 @@ export default {
initialActive: {
immediate: true,
handler(newVal) {
this.activeButton = newVal;
// Find the button whose value matches initialActive
const initialIndex = this.buttons.findIndex((btn) => btn.value === newVal);
this.activeButton = initialIndex !== -1 ? initialIndex : null; // Set to matching button index or null
},
},
},
@@ -82,6 +92,7 @@ export default {
display: flex;
flex-wrap: wrap;
border: 1px solid #ccc;
border-top:none;
border-radius: 1em;
overflow: hidden;
}
@@ -96,9 +107,9 @@ button {
transition: background-color 0.3s;
/* Add borders */
border-right: 1px solid #ccc;
border-top: 1px solid #ccc;
}
/* Remove the border from the last button */
.button-group > button:last-child {
border-right: none;
}
@@ -112,7 +123,7 @@ button:disabled {
}
button.active {
background-color: var(--blue) !important;
background-color: var(--primaryColor) !important;
color: #ffffff;
}
</style>
+5 -6
View File
@@ -7,8 +7,8 @@
top: `${top}px`,
left: `${left}px`,
}"
class="button"
:class="{ 'dark-mode': isDarkMode, mobile: isMobile }"
class="button no-select"
:class="{ 'dark-mode': isDarkMode, centered: centered }"
>
<div v-if="selectedCount > 0" class="button selected-count-header">
<span>{{ selectedCount }} selected</span>
@@ -108,8 +108,8 @@ export default {
user() {
return state.user;
},
isMobile() {
return getters.isMobile();
centered() {
return getters.isMobile() || ( !this.posX || !this.posY );
},
showContext() {
if (getters.currentPromptName() == "ContextMenu" && state.prompts != []) {
@@ -158,7 +158,6 @@ export default {
return mutations.showHover(value);
},
setPositions() {
console.log("Setting positions");
const contextProps = getters.currentPrompt().props;
let tempX = contextProps.posX;
let tempY = contextProps.posY;
@@ -204,7 +203,7 @@ export default {
justify-content: center;
}
#context-menu.mobile {
#context-menu.centered {
top: 50% !important;
left: 50% !important;
-webkit-transform: translate(-50%, -50%);
+143
View File
@@ -0,0 +1,143 @@
<template>
<span>
<!-- Material Icon -->
<i v-if="isMaterialIcon" :class="[classes, { active: active }]" class="icon"> {{ materialIcon }} </i>
</span>
</template>
<script>
import { getTypeInfo } from "@/utils/mimetype";
export default {
name: "Icon",
props: {
mimetype: {
type: String,
required: true,
},
active: {
type: Boolean,
}
},
data() {
return {
materialIcon: "",
classes: "",
svgPath: "",
};
},
computed: {
isMaterialIcon() {
return this.materialIcon !== "";
},
},
methods: {
getIconForType(mimetype) {
return getTypeInfo(mimetype);
},
},
mounted() {
const result = this.getIconForType(this.mimetype);
this.classes = result.classes || "material-icons"; // Default class
this.color = result.color || "lightgray"; // Default color
this.materialIcon = result.materialIcon || "";
this.svgPath = result.svgPath || ""; // For SVG file paths
},
};
</script>
<style scoped>
.file-icons [aria-label^="."] {
opacity: 0.33;
}
.file-icons [aria-label$=".bak"] {
opacity: 0.33;
}
.svg-icons {
display: flex;
max-width: 100px;
}
.icon {
font-size: 1.5rem;
/* Default size */
fill: currentColor;
/* Uses inherited color */
border-radius: 0.2em;
padding: .1em;
background: var(--alt-background);
}
.purple-icons {
color: purple;
}
/* Icon Colors */
.blue-icons {
color: var(--icon-blue);
}
/* Icon Colors */
.primary-icons {
color: var(--primaryColor);
}
.primary-icons.active {
text-shadow: 0px 0px 1px #000;
}
.lightblue-icons {
color: lightskyblue;
}
.orange-icons {
color: lightcoral;
}
.tan-icons {
color: tan;
}
.plum-icons {
color: plum;
}
.red-icons {
color: rgb(246, 70, 70);
}
.beige-icons {
color: beige;
}
.deep-blue-icons {
color: rgb(29, 95, 191);
}
.green-icons {
color: rgb(23, 128, 74);
}
.red-orange-icons {
color: rgb(255, 147, 111);
}
.gray-icons {
color: gray;
}
.skyblue-icons {
color: rgb(42, 170, 242);
}
.lightgray-icons {
color: rgb(176, 176, 176);
}
.yellow-icons {
color: yellow;
}
</style>
Loaded 100 of 153 files, more files were not shown because too many files have changed in this diff. Show more