Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix the issue of 'invalid cross-device link' when uploading files #10

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
ports:
- "8080:8080"
volumes:
# - ./goflet.json:/app/goflet.json
- ./goflet.json:/app/goflet.json
- ./data:/app/data
- ./upload:/app/upload
extra_hosts:
Expand Down
13 changes: 11 additions & 2 deletions middleware/auth_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,23 @@ func queryMatch(query url.Values, permQuery map[string]string) bool {
return true
}

// replaceMultipleSlashes Replace multiple slashes with a single slash
func replaceMultipleSlashes(input string) string {
for strings.Contains(input, "//") {
// Replace multiple slashes with a single slash
input = strings.ReplaceAll(input, "//", "/")
}
return strings.ReplaceAll(input, "\\", "/") // Replace \ with /
}

// isAuthorized Check if the token is authorized to access the path
func isAuthorized(c *gin.Context, permissions []util.Permission) bool {
currentPath := c.Request.URL.Path
currentPath := replaceMultipleSlashes(c.Request.URL.Path) // Clean the path (only replace multiple slashes)
method := c.Request.Method

for _, perm := range permissions {
// Check if the path matches
match := util.Match(currentPath, perm.Path)
match := util.Match(currentPath, replaceMultipleSlashes(perm.Path))
// Check if the method matches
if match || perm.Path == "*" {
return util.MatchMethod(method, perm.Methods) && queryMatch(c.Request.URL.Query(), perm.Query)
Expand Down
23 changes: 22 additions & 1 deletion storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/vvbbnn00/goflet/cache"
Expand Down Expand Up @@ -202,16 +203,36 @@ func DeleteFile(fsPath string) error {
return nil
}

// moveFileCrossDevice If the source and destination are on different devices, use io.Copy to move the file
func moveFileCrossDevice(src, dst string) error {
err := copyFile(src, dst)
if err != nil {
return err
}
err = os.Remove(src)
if err != nil {
return err
}
return nil
}

// RenameFile moves the file from the old path to the new path
func RenameFile(oldPath, newPath string) error {
retryCount := 0

// Rename the temporary file to the final file
for {
err := os.Rename(oldPath, newPath) // This will replace the file if it already exists
// Check if the error is a link error and the error is EXDEV (cross-device link)
var linkErr *os.LinkError
if errors.As(err, &linkErr) && errors.Is(linkErr.Err, syscall.EXDEV) {
log.Debugf("Moving file across devices: %s -> %s", oldPath, newPath)
err = moveFileCrossDevice(oldPath, newPath)
}
if err == nil {
return nil
}

time.Sleep(time.Duration(retryCount) * time.Millisecond) // Sleep for a while before retrying, max 5 seconds in total
retryCount++
if retryCount >= maxRetryCount {
Expand Down Expand Up @@ -330,7 +351,7 @@ func MoveFile(src, dst *util.Path) error {
}

// Move the folder
err = os.Rename(src.FsPath, dst.FsPath)
err = RenameFile(src.FsPath, dst.FsPath)
if err != nil {
log.Debugf("Error moving folder: %s", err.Error())
return err
Expand Down
Loading