Skip to content

Commit

Permalink
files to os-system step4: remove or comment barricades, and deal with…
Browse files Browse the repository at this point in the history
… api/paste for all combinations
  • Loading branch information
lovehunter9 committed Jul 19, 2024
1 parent dbc7dc4 commit 1826b47
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 32 deletions.
21 changes: 12 additions & 9 deletions packages/backend/http/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"mime"
"mime/multipart"
"net/http"
Expand All @@ -25,7 +24,6 @@ import (
"github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/files"
"github.com/spf13/afero"
v "github.com/spf13/viper"
)

func ioCopyFile(sourcePath, targetPath string) error {
Expand All @@ -49,13 +47,17 @@ func ioCopyFile(sourcePath, targetPath string) error {
return nil
}

func resourceDriveGetInfo(path string, d *data) (*files.FileInfo, int, error) {
func resourceDriveGetInfo(path string, r *http.Request, d *data) (*files.FileInfo, int, error) {
xBflUser := r.Header.Get("X-Bfl-User")
fmt.Println("X-Bfl-User: ", xBflUser)

d.user, _ = d.store.Users.Get(d.server.Root, uint(1))
//fmt.Println(d.user.Fs)
//fmt.Println(path)
//fmt.Println(d.user.Perm.Modify)
//fmt.Println(d.server.TypeDetectionByHeader)
//fmt.Println(d)
fmt.Println("d.user.Username: ", d.user.Username)
file, err := files.NewFileInfo(files.FileOptions{
Fs: d.user.Fs,
Path: path, //r.URL.Path,
Expand Down Expand Up @@ -92,12 +94,13 @@ func resourceDriveGetInfo(path string, d *data) (*files.FileInfo, int, error) {
//}

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

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

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

Expand Down Expand Up @@ -1940,7 +1943,7 @@ func copyFile(fs afero.Fs, srcType, src, dstType, dst string, d *data, mode os.F
var bufferPath string
// copy/move
if srcType == "drive" {
fileInfo, status, err := resourceDriveGetInfo(src, d)
fileInfo, status, err := resourceDriveGetInfo(src, r, d)
//fmt.Println(fileInfo, status, err)
if status != http.StatusOK {
return os.ErrInvalid
Expand Down
16 changes: 9 additions & 7 deletions packages/backend/http/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/files"
"github.com/filebrowser/filebrowser/v2/fileutils"
v "github.com/spf13/viper"
)

// func recursiveSize(file *files.FileInfo) {
Expand All @@ -39,6 +37,9 @@ import (
// }

var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
xBflUser := r.Header.Get("X-Bfl-User")
fmt.Println("X-Bfl-User: ", xBflUser)

file, err := files.NewFileInfo(files.FileOptions{
Fs: d.user.Fs,
Path: r.URL.Path,
Expand Down Expand Up @@ -74,10 +75,11 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
}

if file.Type == "video" {
osSystemServer := v.Get("OS_SYSTEM_SERVER")
if osSystemServer == nil {
log.Println("need env OS_SYSTEM_SERVER")
}
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"))
Expand Down Expand Up @@ -106,7 +108,7 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
message = showLogBody
*/

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

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

Expand Down
96 changes: 80 additions & 16 deletions packages/gateway/pkg/proxy/backend_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package proxy

import (
"context"
"fmt"
"github.com/Above-Os/files/gateway/pkg/appdata"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
Expand All @@ -36,6 +37,7 @@ const (
API_PREFIX = "/api"
UPLOADER_PREFIX = "/upload"
MEDIA_PREFIX = "/videos"
API_PASTE_PREFIX = "/api/paste"
)

type GatewayHandler func(c echo.Context) (next bool, err error)
Expand Down Expand Up @@ -172,6 +174,24 @@ func minWithNegativeOne(a, b int, aName, bName string) (int, string) {
}
}

func rewriteUrl(path string, pvc string) string {
homeIndex := strings.Index(path, "/Home")
applicationIndex := strings.Index(path, "/Application")
splitIndex, splitName := minWithNegativeOne(homeIndex, applicationIndex, "/Home", "/Application")
if splitIndex != -1 {
if splitName == "/Home" {
firstHalf := path[:splitIndex]
secondHalf := path[splitIndex:]
return firstHalf + "/" + pvc + secondHalf
} else {
firstHalf := path[:splitIndex]
secondHalf := strings.TrimPrefix(path[splitIndex:], splitName)
return firstHalf + "/" + pvc + "/Data" + secondHalf
}
}
return path
}

func (p *BackendProxy) Next(c echo.Context) *middleware.ProxyTarget {
klog.Infof("Request Headers: %+v", c.Request().Header)

Expand Down Expand Up @@ -199,7 +219,44 @@ func (p *BackendProxy) Next(c echo.Context) *middleware.ProxyTarget {
}

var host = ""
if len(node) > 0 {

if strings.HasPrefix(path, API_PASTE_PREFIX) {
oldUrl := c.Request().URL.String()
fmt.Println("old url: ", oldUrl)

parts := strings.Split(oldUrl, "?")
src := parts[0]
fmt.Println("SRC:", src)

parsedURL, _ := url.Parse(oldUrl)
query := parsedURL.Query()
dst := query.Get("destination")
fmt.Println("DST:", dst)

srcType := query.Get("src_type")
dstType := query.Get("dst_type")
fmt.Println("SRC_TYPE:", srcType)
fmt.Println("DST_TYPE:", dstType)

//if srcType != "sync" {
// src = rewriteUrl(src, userPvc)
//}
if dstType == "drive" {
dst = rewriteUrl(dst, userPvc)
query.Set("destination", dst)
} else if srcType == "cache" && dstType == "cache" {
pathSuffix := strings.TrimPrefix(dst, "/AppData")
query.Set("destination", "/AppData/"+cachePvc+pathSuffix)
}

newURL := fmt.Sprintf("%s?%s", src, query.Encode())
fmt.Println("New WHOLE URL:", newURL)

c.Request().URL.Path = src
c.Request().URL.RawQuery = query.Encode()
}

if len(node) > 0 && !strings.HasPrefix(path, API_PASTE_PREFIX) {
klog.Info("Node: ", node[0])
if strings.HasPrefix(path, API_RESOURCES_PREFIX) {
pathSuffix := strings.TrimPrefix(path, API_RESOURCES_PREFIX)
Expand All @@ -208,26 +265,33 @@ func (p *BackendProxy) Next(c echo.Context) *middleware.ProxyTarget {
pathSuffix := strings.TrimPrefix(path, API_RAW_PREFIX)
c.Request().URL.Path = API_RAW_PREFIX + "/" + cachePvc + pathSuffix
}
//} else if strings.HasPrefix(path, API_PASTE_PREFIX) {
//pathSuffix := strings.TrimPrefix(path, API_PASTE_PREFIX+"/AppData")
//c.Request().URL.Path = API_PASTE_PREFIX + "/AppData/" + cachePvc + pathSuffix
//}
host = appdata.GetAppDataServiceEndpoint(node[0])
klog.Info("host: ", host)
klog.Info("new path: ", c.Request().URL.Path)
} else {
klog.Info("Path: ", path)
if strings.HasPrefix(path, API_PREFIX) {
homeIndex := strings.Index(path, "/Home")
applicationIndex := strings.Index(path, "/Application")
splitIndex, splitName := minWithNegativeOne(homeIndex, applicationIndex, "/Home", "/Application")
if splitIndex != -1 {
if splitName == "/Home" {
firstHalf := path[:splitIndex]
secondHalf := path[splitIndex:]
c.Request().URL.Path = firstHalf + "/" + userPvc + secondHalf
} else {
firstHalf := path[:splitIndex]
secondHalf := strings.TrimPrefix(path[splitIndex:], splitName)
c.Request().URL.Path = firstHalf + "/" + userPvc + "/Data" + secondHalf
}
}
if strings.HasPrefix(path, API_PASTE_PREFIX) {
host = "127.0.0.1:8110"
} else if strings.HasPrefix(path, API_PREFIX) {
//homeIndex := strings.Index(path, "/Home")
//applicationIndex := strings.Index(path, "/Application")
//splitIndex, splitName := minWithNegativeOne(homeIndex, applicationIndex, "/Home", "/Application")
//if splitIndex != -1 {
// if splitName == "/Home" {
// firstHalf := path[:splitIndex]
// secondHalf := path[splitIndex:]
// c.Request().URL.Path = firstHalf + "/" + userPvc + secondHalf
// } else {
// firstHalf := path[:splitIndex]
// secondHalf := strings.TrimPrefix(path[splitIndex:], splitName)
// c.Request().URL.Path = firstHalf + "/" + userPvc + "/Data" + secondHalf
// }
//}
c.Request().URL.Path = rewriteUrl(path, userPvc)
host = "127.0.0.1:8110"
} else if strings.HasPrefix(path, UPLOADER_PREFIX) {
host = "127.0.0.1:40030"
Expand Down

0 comments on commit 1826b47

Please sign in to comment.