Skip to content

Commit

Permalink
GET /api/resouce stream and recursive for downloading & empty content…
Browse files Browse the repository at this point in the history
… bugfix for search3
  • Loading branch information
lovehunter9 committed Oct 12, 2024
1 parent 6f228f0 commit b25962d
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/backend/files/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package files
import (
"sort"
"strings"
"sync"

"github.com/maruel/natural"
)
Expand All @@ -16,6 +17,7 @@ type Listing struct {
Sorting Sorting `json:"sorting"`
Size int64 `json:"size"`
FileSize int64 `json:"fileSize"`
sync.Mutex
}

// ApplySort applies the sort order using .Order and .Sort
Expand Down
23 changes: 20 additions & 3 deletions packages/backend/http/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ import (
// }

var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
streamStr := r.URL.Query().Get("stream")
stream := 0
var err error
if streamStr != "" {
stream, err = strconv.Atoi(streamStr)
if err != nil {
return http.StatusBadRequest, err
}
}
fmt.Println("stream: ", stream)

xBflUser := r.Header.Get("X-Bfl-User")
fmt.Println("X-Bfl-User: ", xBflUser)

Expand Down Expand Up @@ -75,7 +86,7 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
}

var file *files.FileInfo
var err error
//var err error
if usbData != nil || hddData != nil {
file, err = files.NewFileInfoWithDiskInfo(files.FileOptions{
Fs: d.user.Fs,
Expand Down Expand Up @@ -113,7 +124,14 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
}
file.Listing.Sorting = d.user.Sorting
file.Listing.ApplySort()
return renderJSON(w, r, file)
if stream == 1 {
//return streamJSON(w, r, file)
streamListingItems(w, r, file.Listing, d, usbData, hddData)
return 0, nil
} else {
return renderJSON(w, r, file)
}
//return renderJSON(w, r, file)
}

if checksum := r.URL.Query().Get("checksum"); checksum != "" {
Expand Down Expand Up @@ -190,7 +208,6 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
}

return renderJSON(w, r, file)
})

Expand Down
143 changes: 141 additions & 2 deletions packages/backend/http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package http
import (
"encoding/json"
"errors"
"fmt"
libErrors "github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/files"
"net/http"
"net/url"
"os"
"strings"

libErrors "github.com/filebrowser/filebrowser/v2/errors"
)

func renderJSON(w http.ResponseWriter, _ *http.Request, data interface{}) (int, error) {
Expand All @@ -26,6 +27,144 @@ func renderJSON(w http.ResponseWriter, _ *http.Request, data interface{}) (int,
return 0, nil
}

//func generateData(listing *files.Listing, stopChan <-chan struct{}, dataChan chan<- string) {
// defer close(dataChan)
//
// listing.Lock()
// for _, item := range listing.Items {
// select {
// case <-stopChan:
// listing.Unlock()
// return
// case dataChan <- formatSSEvent(item):
// }
// }
// listing.Unlock()
//
// itemCount := len(listing.Items)
// for itemCount < 100000 {
// select {
// case <-stopChan:
// return
// default:
// item := &files.FileInfo{
// Path: fmt.Sprintf("/path/to/item%d", itemCount),
// Name: fmt.Sprintf("item%d", itemCount),
// Size: int64(itemCount * 100),
// }
// dataChan <- formatSSEvent(item)
// itemCount++
//
// time.Sleep(100 * time.Millisecond)
// }
// }
//}

func generateData(listing *files.Listing, stopChan <-chan struct{}, dataChan chan<- string, d *data, usbData, hddData []files.DiskInfo) {
defer close(dataChan)

var A []*files.FileInfo
listing.Lock()
A = append(A, listing.Items...)
listing.Unlock()

for len(A) > 0 {
//fmt.Println("len(A): ", len(A))
firstItem := A[0]
//fmt.Println("firstItem: ", firstItem.Path)

if firstItem.IsDir {
var file *files.FileInfo
var err error
if usbData != nil || hddData != nil {
file, err = files.NewFileInfoWithDiskInfo(files.FileOptions{
Fs: d.user.Fs,
Path: firstItem.Path, //r.URL.Path,
Modify: d.user.Perm.Modify,
Expand: true,
ReadHeader: d.server.TypeDetectionByHeader,
Checker: d,
Content: true,
}, usbData, hddData)
} else {
file, err = files.NewFileInfo(files.FileOptions{
Fs: d.user.Fs,
Path: firstItem.Path, //r.URL.Path,
Modify: d.user.Perm.Modify,
Expand: true,
ReadHeader: d.server.TypeDetectionByHeader,
Checker: d,
Content: true,
})
}
if err != nil {
fmt.Println(err)
return
}

var nestedItems []*files.FileInfo
if file.Listing != nil {
nestedItems = append(nestedItems, file.Listing.Items...)
}
A = append(nestedItems, A[1:]...)
} else {
dataChan <- formatSSEvent(firstItem)

A = A[1:]
}

select {
case <-stopChan:
return
default:
}
}
}

func formatSSEvent(data interface{}) string {
jsonData, err := json.Marshal(data)
if err != nil {
return ""
}
return fmt.Sprintf("data: %s\n\n", jsonData)
}

func streamListingItems(w http.ResponseWriter, r *http.Request, listing *files.Listing, d *data, usbData, hddData []files.DiskInfo) {
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")

stopChan := make(chan struct{})
dataChan := make(chan string)

go generateData(listing, stopChan, dataChan, d, usbData, hddData)

flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}

for {
select {
case event, ok := <-dataChan:
if !ok {
return
}
_, err := w.Write([]byte(event))
if err != nil {
fmt.Println(err)
return
}
flusher.Flush()

case <-r.Context().Done():
close(stopChan)
return
}
}
}

func errToStatus(err error) int {
switch {
case err == nil:
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/rpc/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ func updateOrInputDocSearch3(filepath, bflName string) error {
}
} else {
newDoc = map[string]interface{}{
"content": "-",
"meta": map[string]interface{}{
"md5": newMd5,
"size": size,
Expand Down Expand Up @@ -643,6 +644,7 @@ func updateOrInputDocSearch3(filepath, bflName string) error {
} else {
doc = map[string]interface{}{
"title": filename,
"content": "-",
"owner_userid": strconv.Itoa(int(ownerUID)),
"resource_uri": filepath,
"service": "files",
Expand Down

0 comments on commit b25962d

Please sign in to comment.