Skip to content

Commit

Permalink
style: try to remove user storage (withUser removing step-3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovehunter9 committed Feb 28, 2025
1 parent c977cf6 commit 061e80d
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 60 deletions.
37 changes: 1 addition & 36 deletions packages/backend/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/filebrowser/filebrowser/v2/common"
"github.com/filebrowser/filebrowser/v2/my_redis"
"github.com/filebrowser/filebrowser/v2/users"
"io"
"log"
"net"
Expand All @@ -33,7 +34,6 @@ import (
"github.com/filebrowser/filebrowser/v2/rpc"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users"
)

var (
Expand Down Expand Up @@ -132,9 +132,6 @@ user created with the credentials from options "username" and "password".`,
imgSvc := img.New(workersCount)

var fileCache diskcache.Interface = diskcache.NewNoOp()
// cacheDir, err := cmd.Flags().GetString("cache-dir")
//cacheDir := "/data/file_cache"
//checkErr(err)
if diskcache.CacheDir != "" {
if err := os.MkdirAll(diskcache.CacheDir, 0700); err != nil { //nolint:govet,gomnd
log.Fatalf("can't make directory %s: %s", diskcache.CacheDir, err)
Expand All @@ -153,14 +150,6 @@ user created with the credentials from options "username" and "password".`,
wg.Add(1)
go func() {
defer wg.Done()

//url := os.Getenv("ZINC_URI")
//zincHost := os.Getenv("ZINC_HOST")
//zincPort := os.Getenv("ZINC_PORT")
//url := "http://" + zincHost + ":" + zincPort
//if zincHost == "" || zincPort == "" {
// url = "http://localhost:4080"
//}
url := "http://localhost:4080"

watchDirStr := os.Getenv("WATCH_DIR")
Expand Down Expand Up @@ -447,30 +436,6 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) {

err = d.store.Settings.SaveServer(ser)
checkErr(err)

username := getParam(flags, "username")
password := getParam(flags, "password")

if password == "" {
password, err = users.HashPwd("admin")
checkErr(err)
}

if username == "" || password == "" {
log.Fatal("username and password cannot be empty during quick setup")
}

user := &users.User{
Username: username,
Password: password,
LockPassword: false,
}

set.Defaults.Apply(user)
user.Perm.Admin = true

err = d.store.Users.Save(user)
checkErr(err)
}

func initConfig() {
Expand Down
1 change: 0 additions & 1 deletion packages/backend/diskcache/file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,5 @@ func (f *FileCache) getFileName(key string) string {
hasher := sha1.New() //nolint:gosec
_, _ = hasher.Write([]byte(key))
hash := hex.EncodeToString(hasher.Sum(nil))
//return fmt.Sprintf("%s/%s/%s", hash[:1], hash[1:3], hash)
return hash
}
10 changes: 2 additions & 8 deletions packages/backend/files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ var TerminusdHost = os.Getenv("TERMINUSD_HOST")
var ExternalPrefix = os.Getenv("EXTERNAL_PREFIX")

func CheckPath(s, prefix, except string) bool {
// prefix := "/data/External/"

if prefix == "" || except == "" {
return false
}
Expand All @@ -87,7 +85,7 @@ func CheckPath(s, prefix, except string) bool {
remaining = remaining[:len(remaining)-len(except)]
}

return !strings.Contains(remaining, except) // "/")
return !strings.Contains(remaining, except)
}

type Response struct {
Expand Down Expand Up @@ -117,9 +115,6 @@ func FetchDiskInfo(url string, header http.Header) ([]DiskInfo, error) {
return nil, err
}

//for key, value := range header {
// req.Header.Set(key, value)
//}
req.Header = header

resp, err := client.Do(req)
Expand Down Expand Up @@ -452,7 +447,6 @@ func (i *FileInfo) detectType(modify, saveContent, readHeader bool) error {
// a 500 even though it doesn't matter. So we just log it.

mimetype := mime.TypeByExtension(i.Extension)
//fmt.Println("extension mimitype:", mimetype)

var buffer []byte
if readHeader && mimetype == "" {
Expand Down Expand Up @@ -508,7 +502,7 @@ func (i *FileInfo) readFirstBytes() []byte {
}
defer reader.Close()

buffer := make([]byte, 512) //nolint:gomnd
buffer := make([]byte, 512)
n, err := reader.Read(buffer)
if err != nil && err != io.EOF {
log.Print(err)
Expand Down
2 changes: 0 additions & 2 deletions packages/backend/files/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ type Listing struct {
}

// ApplySort applies the sort order using .Order and .Sort
//
//nolint:goconst
func (l Listing) ApplySort() {
// Check '.Order' to know how to sort
if !l.Sorting.Asc {
Expand Down
1 change: 0 additions & 1 deletion packages/backend/fileutils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func ioCopyFileWithBuffer(fs afero.Fs, sourcePath, targetPath string, bufferSize
tempFileName := fmt.Sprintf(".uploading_%s", baseName)
tempFilePath := filepath.Join(dir, tempFileName)

//err = fs.MkdirAll(filepath.Dir(targetPath), 0755)
err = fs.MkdirAll(filepath.Dir(tempFilePath), 0755)
if err != nil {
return err
Expand Down
16 changes: 10 additions & 6 deletions packages/backend/http/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func driveFileToBuffer(file *files.FileInfo, bufferFilePath string) error {
}
fmt.Println("file.Path:", file.Path, ", path:", path)

err = ioCopyFileWithBuffer(path, bufferFilePath, 8*1024*1024)
err = ioCopyFileWithBuffer("/data"+path, bufferFilePath, 8*1024*1024)

//fd, err := file.Fs.Open(path)
//if err != nil {
Expand Down Expand Up @@ -1002,7 +1002,8 @@ func resourcePasteHandler(fileCache FileCache) handleFunc {
override := r.URL.Query().Get("override") == "true"
rename := r.URL.Query().Get("rename") == "true"
if !override && !rename {
if _, err := d.user.Fs.Stat(dst); err == nil {
//if _, err := d.user.Fs.Stat(dst); err == nil {
if _, err := files.DefaultFs.Stat(dst); err == nil {
return http.StatusConflict, nil
}
}
Expand All @@ -1028,7 +1029,7 @@ func resourcePasteHandler(fileCache FileCache) handleFunc {
}
}
if rename && dstType != "google" {
dst = pasteAddVersionSuffix(dst, dstType, d.user.Fs, w, r)
dst = pasteAddVersionSuffix(dst, dstType, files.DefaultFs, w, r) // d.user.Fs, w, r)
}
// Permission for overwriting the file
if override { // && !d.user.Perm.Modify {
Expand Down Expand Up @@ -1796,7 +1797,8 @@ func copyFile(fs afero.Fs, srcType, src, dstType, dst string, d *data, mode os.F

rename := r.URL.Query().Get("rename") == "true"
if rename && dstType != "google" && srcType == "google" {
dst = pasteAddVersionSuffix(dst, dstType, d.user.Fs, w, r)
//dst = pasteAddVersionSuffix(dst, dstType, d.user.Fs, w, r)
dst = pasteAddVersionSuffix(dst, dstType, files.DefaultFs, w, r)
}

// paste
Expand Down Expand Up @@ -2145,12 +2147,14 @@ func pasteActionDiffArch(ctx context.Context, action, srcType, src, dstType, dst
// return errors.ErrPermissionDenied
//}

return doPaste(d.user.Fs, srcType, src, dstType, dst, d, w, r)
//return doPaste(d.user.Fs, srcType, src, dstType, dst, d, w, r)
return doPaste(files.DefaultFs, srcType, src, dstType, dst, d, w, r)
case "rename":
//if !d.user.Perm.Rename {
// return errors.ErrPermissionDenied
//}
err := doPaste(d.user.Fs, srcType, src, dstType, dst, d, w, r)
//err := doPaste(d.user.Fs, srcType, src, dstType, dst, d, w, r)
err := doPaste(files.DefaultFs, srcType, src, dstType, dst, d, w, r)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/http/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,8 @@ func patchAction(ctx context.Context, action, src, dst string, d *data, fileCach
// return errors.ErrPermissionDenied
//}

return fileutils.Copy(d.user.Fs, src, dst)
//return fileutils.Copy(d.user.Fs, src, dst)
return fileutils.Copy(files.DefaultFs, src, dst)
case "rename":
//if !d.user.Perm.Rename {
// return errors.ErrPermissionDenied
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/storage/bolt/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
//"github.com/filebrowser/filebrowser/v2/auth"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users"
//"github.com/filebrowser/filebrowser/v2/users"
)

// NewStorage creates a storage.Storage based on Bolt DB.
func NewStorage(db *storm.DB) (*storage.Storage, error) {
userStore := users.NewStorage(usersBackend{db: db})
//userStore := users.NewStorage(usersBackend{db: db})
settingsStore := settings.NewStorage(settingsBackend{db: db})
//authStore := auth.NewStorage(authBackend{db: db}, userStore)

Expand All @@ -22,7 +22,7 @@ func NewStorage(db *storm.DB) (*storage.Storage, error) {

return &storage.Storage{
//Auth: authStore,
Users: userStore,
//Users: userStore,
Settings: settingsStore,
}, nil
}
3 changes: 1 addition & 2 deletions packages/backend/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package storage

import (
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/users"
)

// Storage is a storage powered by a Backend which makes the necessary
// verifications when fetching and saving data to ensure consistency.
type Storage struct {
Users users.Store
//Users users.Store
Settings *settings.Storage
}

0 comments on commit 061e80d

Please sign in to comment.