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

add encoded filename as part of upload url #3140

Merged
merged 6 commits into from
Dec 6, 2023
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
21 changes: 13 additions & 8 deletions integration/user_api_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package integration
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -395,9 +396,10 @@ func UploadAnObject(bucketName, fileName string) (*http.Response, error) {
contentType + boundaryEnd
arrayOfBytes := []byte(file)
requestDataBody := bytes.NewReader(arrayOfBytes)
apiURL := "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects/upload" + "?prefix=" + base64.StdEncoding.EncodeToString([]byte(fileName))
request, err := http.NewRequest(
"POST",
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/upload",
apiURL,
requestDataBody,
)
if err != nil {
Expand Down Expand Up @@ -488,9 +490,11 @@ func PutObjectsRetentionStatus(bucketName, prefix, versionID, mode, expires stri
}
requestDataJSON, _ := json.Marshal(requestDataAdd)
requestDataBody := bytes.NewReader(requestDataJSON)
apiURL := "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects/retention?prefix=" + prefix + "&version_id=" + versionID

request, err := http.NewRequest(
"PUT",
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/retention?prefix="+prefix+"&version_id="+versionID,
apiURL,
requestDataBody,
)
if err != nil {
Expand Down Expand Up @@ -726,9 +730,10 @@ func PutObjectsLegalholdStatus(bucketName, prefix, status, versionID string) (*h
}
requestDataJSON, _ := json.Marshal(requestDataAdd)
requestDataBody := bytes.NewReader(requestDataJSON)
apiURL := "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects/legalhold?prefix=" + prefix + "&version_id=" + versionID
request, err := http.NewRequest(
"PUT",
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/legalhold?prefix="+prefix+"&version_id="+versionID,
apiURL,
requestDataBody,
)
if err != nil {
Expand All @@ -747,8 +752,8 @@ func TestPutObjectsLegalholdStatus(t *testing.T) {
// Variables
assert := assert.New(t)
bucketName := "testputobjectslegalholdstatus"
fileName := "testputobjectslegalholdstatus.txt"
prefix := "dGVzdHB1dG9iamVjdHNsZWdhbGhvbGRzdGF0dXMudHh0" // encoded base64
objName := "testputobjectslegalholdstatus.txt" // // encoded base64 of testputobjectslegalholdstatus.txt = dGVzdHB1dG9iamVjdHNsZWdhbGhvbGRzdGF0dXMudHh0
objectNameEncoded := "dGVzdHB1dG9iamVjdHNsZWdhbGhvbGRzdGF0dXMudHh0"
status := "enabled"

// 1. Create bucket
Expand All @@ -759,7 +764,7 @@ func TestPutObjectsLegalholdStatus(t *testing.T) {
// 2. Add object
uploadResponse, uploadError := UploadAnObject(
bucketName,
fileName,
objName,
)
assert.Nil(uploadError)
if uploadError != nil {
Expand All @@ -776,7 +781,7 @@ func TestPutObjectsLegalholdStatus(t *testing.T) {
}

// Get versionID
listResponse, _ := ListObjects(bucketName, prefix, "true")
listResponse, _ := ListObjects(bucketName, "", "true")
bodyBytes, _ := io.ReadAll(listResponse.Body)
listObjs := models.ListObjectsResponse{}
err := json.Unmarshal(bodyBytes, &listObjs)
Expand Down Expand Up @@ -814,7 +819,7 @@ func TestPutObjectsLegalholdStatus(t *testing.T) {
// 3. Put Objects Legal Status
putResponse, putError := PutObjectsLegalholdStatus(
bucketName,
prefix,
objectNameEncoded,
status,
tt.args.versionID,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ const ListObjects = () => {
relativeFolderPath = fileWebkitRelativePath;
}

let prefixPath = "";

if (path !== "" || relativeFolderPath !== "") {
const finalFolderPath = relativeFolderPath
.split("/")
Expand All @@ -530,26 +532,30 @@ const ListObjects = () => {

const pathClean = path.endsWith("/") ? path.slice(0, -1) : path;

encodedPath = encodeURLString(
`${pathClean}${
!pathClean.endsWith("/") &&
finalFolderPath !== "" &&
!finalFolderPath.startsWith("/")
? "/"
: ""
}${finalFolderPath}${
!finalFolderPath.endsWith("/") ||
(finalFolderPath.trim() === "" && !path.endsWith("/"))
? "/"
: ""
}`,
);
prefixPath = `${pathClean}${
!pathClean.endsWith("/") &&
finalFolderPath !== "" &&
!finalFolderPath.startsWith("/")
? "/"
: ""
}${finalFolderPath}${
!finalFolderPath.endsWith("/") ||
(finalFolderPath.trim() === "" && !path.endsWith("/"))
? "/"
: ""
}`;
}

if (encodedPath !== "") {
uploadUrl = `${uploadUrl}?prefix=${encodedPath}`;
if (prefixPath !== "") {
uploadUrl = `${uploadUrl}?prefix=${encodeURLString(
prefixPath + fileName,
)}`;
} else {
uploadUrl = `${uploadUrl}?prefix=${encodeURLString(fileName)}`;
}

encodedPath = encodeURLString(prefixPath);

const identity = encodeURLString(
`${bucketName}-${encodedPath}-${new Date().getTime()}-${Math.random()}`,
);
Expand Down
5 changes: 2 additions & 3 deletions restapi/user_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"io"
"net/http"
"net/url"
"path"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -1030,8 +1029,8 @@ func uploadFiles(ctx context.Context, client MinioClient, params objectApi.PostB
if contentType == "" {
contentType = mimedb.TypeByExtension(filepath.Ext(p.FileName()))
}

_, err = client.putObject(ctx, params.BucketName, path.Join(prefix, path.Clean(p.FileName())), p, size, minio.PutObjectOptions{
objectName := prefix // prefix will have complete object path e.g: /test-prefix/test-object.txt
_, err = client.putObject(ctx, params.BucketName, objectName, p, size, minio.PutObjectOptions{
ContentType: contentType,
DisableMultipart: true, // Do not upload as multipart stream for console uploader.
})
Expand Down
Loading