filebrowser 文件系统搭建与使用

下载地址


https://github.com/filebrowser/filebrowser/releases

解压

tar -zxvf linux-amd64-filebrowser.tar.gz -C /data/linux-amd64-filebrowser/

初始化配置

./filebrowser config init

创建admin 账号

./filebrowser users add 账号 密码 --perm.admin=true

设置启动的端口

./filebrowser config set --address 0.0.0.0 --port 9995

最后指定目录开放

filebrowser -r /xxx/path

关于jwt认证信息,由于不支持api,所以手动生成永久jwt,手动ui下载会带auth 2个小时过期了,我们下载用我们生成的永久auth

package http

import (
	"fmt"
	"github.com/asdine/storm/v3"
	"github.com/filebrowser/filebrowser/v2/users"
	"github.com/golang-jwt/jwt/v4"
	"github.com/syyongx/php2go"
	bolt "go.etcd.io/bbolt"
	"log"
	"testing"
	"time"
)


func Test_withUser(t *testing.T) {
	// 替换key
	//./filebrowser config export config.json

	claims := &authToken{
		User: userInfo{
			ID:          1,
			Locale:      "zh-cn",
			ViewMode:    "mosaic",
			SingleClick: false,
			Perm: users.Permissions{
				Admin:    true,
				Execute:  true,
				Create:   true,
				Rename:   true,
				Modify:   true,
				Delete:   true,
				Share:    true,
				Download: true,
			},
			LockPassword: false,
			Commands:     []string{},
			HideDotfiles: false,
			DateFormat:   false,
		},
		RegisteredClaims: jwt.RegisteredClaims{
			IssuedAt:  jwt.NewNumericDate(time.Now()),
			ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24 * 365 * 10)),
			Issuer:    "File Browser",
		},
	}
	str, _ := php2go.Base64Decode("base64的key,需要自己手动获取,下面这个test可以手动获取")
	log.Println(str)
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	signed, err := token.SignedString([]byte(str))
	if err != nil {
		t.Fatal(err)
	}
	t.Log(signed)
}

key获取规则

func TestDb(t *testing.T) {
	type Settings struct {
		Key string `json:"key"`
		// 这里可以继续添加其他字段,如果你想要解析settings里的其他数据
	}
	dbPath := "/Users/admin/go/src/goBoss/my_colly/filebrowser-master/filebrowser.db"
	db, err := storm.Open(dbPath)
	if err != nil {
		t.Fatalf("Failed to open the database: %v", err)
	}

	err = db.Bolt.View(func(tx *bolt.Tx) error {
		return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			fmt.Println("Bucket:", string(name))
			return b.ForEach(func(k, v []byte) error {
				fmt.Printf("Key: %s, Value: %s\n", string(k), string(v))
				return nil
			})
		})
	})

	if err != nil {
		log.Fatal(err)
	}

	// -------------------------  单独读取  -----------------------
	var settingsJson Settings
	err = db.Get("config", "settings", &settingsJson)
	if err != nil {
		t.Fatalf("Failed to get settings: %v", err)
	}

	fmt.Println("Key:", settingsJson.Key)
}

设置自动启

vim /etc/systemd/system/filebrowser.service

[Unit]
Description=FileBrowser Service
After=network.target

[Service]
ExecStart=/data/linux-amd64-filebrowser/filebrowser -r /data/linux-amd64-filebrowser/dir/
Restart=always
# User=ec2-user
WorkingDirectory=/data/linux-amd64-filebrowser

[Install]
WantedBy=multi-user.target





sudo systemctl daemon-reload
sudo systemctl start filebrowser
sudo systemctl enable filebrowser
sudo systemctl status filebrowser

你可能感兴趣的:(filebrowser)