fix (#410): error when moving files with the S3 backend

* url encode paths when moving object in the S3 backend

* fix renaming with the same name

Co-authored-by: Quentin Bramas <bramas@unistra.fr>
This commit is contained in:
Quentin Bramas 2021-09-27 12:06:09 +02:00 committed by GitHub
parent 642f04c955
commit edbf16871c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3/s3manager"
. "github.com/mickael-kerjean/filestash/server/common"
"io"
"net/url"
"os"
"path/filepath"
"strings"
@ -297,6 +298,9 @@ func (s S3Backend) Rm(path string) error {
func (s S3Backend) Mv(from string, to string) error {
f := s.path(from)
t := s.path(to)
if from == to {
return nil
}
client := s3.New(s.createSession(f.bucket))
if f.path == "" {
@ -306,7 +310,7 @@ func (s S3Backend) Mv(from string, to string) error {
// Move Single file
input := &s3.CopyObjectInput{
Bucket: aws.String(t.bucket),
CopySource: aws.String(f.bucket + "/" + f.path),
CopySource: aws.String(f.bucket + "/" + s.urlEncodedPath(f.path)),
Key: aws.String(t.path),
}
if s.params["encryption_key"] != "" {
@ -336,7 +340,7 @@ func (s S3Backend) Mv(from string, to string) error {
},
func(objs *s3.ListObjectsV2Output, lastPage bool) bool {
for _, obj := range objs.Contents {
from := f.bucket + "/" + *obj.Key
from := f.bucket + "/" + s.urlEncodedPath(*obj.Key)
toKey := t.path + strings.TrimPrefix(*obj.Key, f.path)
input := &s3.CopyObjectInput{
CopySource: aws.String(from),
@ -477,3 +481,15 @@ func (s S3Backend) path(p string) S3Path {
path,
}
}
func (s S3Backend) urlEncodedPath(path string) string {
sp := strings.Split(path, "/")
var pathElements []string
for _, x := range sp {
pathElements = append(pathElements, url.QueryEscape(x))
}
encodedPath := strings.Join(pathElements, "/")
return encodedPath
}