Skip to content

Commit

Permalink
style: try to remove withUser for GET /api/resources (withUser removi…
Browse files Browse the repository at this point in the history
…ng step-1)
  • Loading branch information
lovehunter9 committed Feb 27, 2025
1 parent 26c4b5f commit be54af5
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 647 deletions.
14 changes: 7 additions & 7 deletions packages/backend/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ user created with the credentials from options "username" and "password".`,
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"
//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")
//var watchDirs []string
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import (
"github.com/filebrowser/filebrowser/v2/rules"
)

var DefaultFs = afero.NewBasePathFs(afero.NewOsFs(), "/")
var DefaultSorting = Sorting{
By: "name",
Asc: true,
}

// FileInfo describes a file.
type FileInfo struct {
*Listing
Expand Down
85 changes: 9 additions & 76 deletions packages/backend/http/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,22 @@ import (
"github.com/filebrowser/filebrowser/v2/fileutils"
)

// func recursiveSize(file *files.FileInfo) {
// if file.IsDir {
// for _, info := range file.Items {
// //fmt.Println(info)
// recursiveSize(info)
// }
// if file.Listing != nil {
// file.Size += file.Listing.Size
// file.FileSize += file.Listing.FileSize
// }
// }
// return
// }

func resourceGetSync(w http.ResponseWriter, r *http.Request, stream int) (int, error) {
// src is like [repo-id]/path/filename
src := r.URL.Path
src, err := unescapeURLIfEscaped(src) // url.QueryUnescape(src)
if err != nil {
return http.StatusBadRequest, err
}
fmt.Println("src Path:", src)
src = strings.Trim(src, "/") + "/"
//if !strings.Contains(src, "/") {
// err := e.New("invalid path format: path must contain at least one '/'")
// fmt.Println("Error:", err)
// return errToStatus(err), err
//}

firstSlashIdx := strings.Index(src, "/")

repoID := src[:firstSlashIdx]

lastSlashIdx := strings.LastIndex(src, "/")

// don't use, because this is only used for folders
// won't use, because this func is only used for folders
filename := src[lastSlashIdx+1:]

prefix := ""
Expand All @@ -70,7 +50,6 @@ func resourceGetSync(w http.ResponseWriter, r *http.Request, stream int) (int, e
if prefix == "" {
prefix = "/"
}
//prefix = url.QueryEscape(prefix)
prefix = escapeURLWithSpace(prefix)

fmt.Println("repo-id:", repoID)
Expand Down Expand Up @@ -122,7 +101,6 @@ func resourceGetSync(w http.ResponseWriter, r *http.Request, stream int) (int, e
return errToStatus(err), err
}
}
//body, _ := ioutil.ReadAll(response.Body)
streamSyncDirents(w, r, body, repoID)
return 0, nil
}
Expand Down Expand Up @@ -150,7 +128,7 @@ func resourceGetSync(w http.ResponseWriter, r *http.Request, stream int) (int, e
return 0, nil
}

var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
func resourceGetHandler(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
start := time.Now()
fmt.Println("Function resourceGetHandler starts at", start)

Expand Down Expand Up @@ -207,9 +185,6 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
headers := r.Header.Clone()
headers.Set("Content-Type", "application/json")
headers.Set("X-Signature", "temp_signature")
//headers := map[string]string{
// "X-Signature": "temp_signature",
//}

mountedData, err = files.FetchDiskInfo(url, headers)
if err != nil {
Expand All @@ -223,9 +198,6 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
usbHeaders := r.Header.Clone()
usbHeaders.Set("Content-Type", "application/json")
usbHeaders.Set("X-Signature", "temp_signature")
//headers := map[string]string{
// "X-Signature": "temp_signature",
//}

usbData, err := files.FetchDiskInfo(usbUrl, usbHeaders)
if err != nil {
Expand All @@ -239,9 +211,6 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
hddHeaders := r.Header.Clone()
hddHeaders.Set("Content-Type", "application/json")
hddHeaders.Set("X-Signature", "temp_signature")
//headers := map[string]string{
// "X-Signature": "temp_signature",
//}

hddData, err := files.FetchDiskInfo(hddUrl, hddHeaders)
if err != nil {
Expand All @@ -266,19 +235,19 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
var file *files.FileInfo
if mountedData != nil {
file, err = files.NewFileInfoWithDiskInfo(files.FileOptions{
Fs: d.user.Fs,
Fs: files.DefaultFs, // d.user.Fs,
Path: r.URL.Path,
Modify: d.user.Perm.Modify,
Modify: true, // d.user.Perm.Modify,
Expand: true,
ReadHeader: d.server.TypeDetectionByHeader,
Checker: d,
Content: true,
}, mountedData)
} else {
file, err = files.NewFileInfo(files.FileOptions{
Fs: d.user.Fs,
Fs: files.DefaultFs, // d.user.Fs,
Path: r.URL.Path,
Modify: d.user.Perm.Modify,
Modify: true, // d.user.Perm.Modify,
Expand: true,
ReadHeader: d.server.TypeDetectionByHeader,
Checker: d,
Expand Down Expand Up @@ -319,16 +288,12 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
}

if file.IsDir {
// fmt.Println(file)
// file.Size = file.Listing.Size
// recursiveSize(file)
if files.CheckPath(file.Path, files.ExternalPrefix, "/") {
file.ExternalType = files.GetExternalType(file.Path, mountedData)
}
file.Listing.Sorting = d.user.Sorting
file.Listing.Sorting = files.DefaultSorting // d.user.Sorting
file.Listing.ApplySort()
if stream == 1 {
//return streamJSON(w, r, file)
streamListingItems(w, r, file.Listing, d, mountedData)
elapsed := time.Since(start)
fmt.Printf("Function resourceGetHandler execution time: %v\n", elapsed)
Expand All @@ -338,7 +303,6 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
fmt.Printf("Function resourceGetHandler execution time: %v\n", elapsed)
return renderJSON(w, r, file)
}
//return renderJSON(w, r, file)
}

if checksum := r.URL.Query().Get("checksum"); checksum != "" {
Expand All @@ -355,39 +319,8 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d

if file.Type == "video" {
osSystemServer := "system-server.user-system-" + xBflUser
//osSystemServer := v.Get("OS_SYSTEM_SERVER")
//if osSystemServer == nil {
// log.Println("need env OS_SYSTEM_SERVER")
//}

/*
showLogUrl := fmt.Sprintf("http://%s/legacy/v1alpha1/api.intent/v1/server/intent/send", os.Getenv("OS_SYSTEM_SERVER"))
listTobeSend := "{\"action\": \"view\",\"category\": \"container_log\",\"data\": {\"statefulset\": \"geth\",\"container\": \"geth\"}}"
showLogRequestOption := &HttpCallRequestOption{
// Header: map[string]string{"X-Access-Token": accessToken, "type":"application/json"},
Header: map[string]string{"type":"application/json"},
Url: showLogUrl,
Timeout: 1000 * 10,
JsonStr: listTobeSend,
TestOKFun: func(bodyStr string) bool {
log.Println("bodyis:", bodyStr)
code := gjson.Get(bodyStr, "code").Int()
return code == 0
},
}
showLogBody, listOk, showLogErr := NewHttpCall().PostJsonCall(showLogRequestOption)
if showLogErr != nil {
err = showLogErr
return
}
if !listOk {
err = errors.WithMessage(GetNoEmptyError(err), "show log error")
return
}
message = showLogBody
*/

httpposturl := fmt.Sprintf("http://%s/legacy/v1alpha1/api.intent/v1/server/intent/send", osSystemServer) // os.Getenv("OS_SYSTEM_SERVER"))
httpposturl := fmt.Sprintf("http://%s/legacy/v1alpha1/api.intent/v1/server/intent/send", osSystemServer)

fmt.Println("HTTP JSON POST URL:", httpposturl)

Expand Down Expand Up @@ -418,7 +351,7 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
elapsed := time.Since(start)
fmt.Printf("Function resourceGetHandler execution time: %v\n", elapsed)
return renderJSON(w, r, file)
})
}

func resourceDeleteHandler(fileCache FileCache) handleFunc {
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
Expand Down
128 changes: 0 additions & 128 deletions packages/backend/runner/commands.go

This file was deleted.

Loading

0 comments on commit be54af5

Please sign in to comment.