Skip to content

Commit

Permalink
add encoded filename as part of delete api url (#3141)
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx authored Dec 6, 2023
1 parent 607d94f commit 6767bfa
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 39 deletions.
9 changes: 5 additions & 4 deletions integration/user_api_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,9 @@ func DeleteObject(bucketName, path string, recursive, allVersions bool) (*http.R
DELETE:
{{baseUrl}}/buckets/bucketName/objects?path=Y2VzYXJpby50eHQ=&recursive=false&all_versions=false
*/
url := "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects?path=" +
path + "&recursive=" + strconv.FormatBool(recursive) + "&all_versions=" +
prefixEncoded := base64.StdEncoding.EncodeToString([]byte(path))
url := "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects?prefix=" +
prefixEncoded + "&recursive=" + strconv.FormatBool(recursive) + "&all_versions=" +
strconv.FormatBool(allVersions)
request, err := http.NewRequest(
"DELETE",
Expand Down Expand Up @@ -1639,7 +1640,6 @@ func TestDeleteObject(t *testing.T) {
assert := assert.New(t)
bucketName := "testdeleteobjectbucket1"
fileName := "testdeleteobjectfile"
path := "dGVzdGRlbGV0ZW9iamVjdGZpbGUxLnR4dA==" // fileName encoded base64
numberOfFiles := 2

// 1. Create bucket
Expand All @@ -1662,8 +1662,9 @@ func TestDeleteObject(t *testing.T) {
}
}

objPathFull := fileName + "1.txt" // would be encoded in DeleteObject util method.
// 3. Delete only one object from the bucket.
deleteResponse, deleteError := DeleteObject(bucketName, path, false, false)
deleteResponse, deleteError := DeleteObject(bucketName, objPathFull, false, false)
assert.Nil(deleteError)
if deleteError != nil {
log.Println(deleteError)
Expand Down
2 changes: 1 addition & 1 deletion portal-ui/src/api/consoleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2092,7 +2092,7 @@ export class Api<
deleteObject: (
bucketName: string,
query: {
path: string;
prefix: string;
version_id?: string;
recursive?: boolean;
all_versions?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { hasPermission } from "../../../../../../common/SecureComponent";
import { IAM_SCOPES } from "../../../../../../common/SecureComponent/permissions";
import { useSelector } from "react-redux";
import { BucketVersioningResponse } from "api/consoleApi";
import { api } from "../../../../../../api";
import { encodeURLString } from "../../../../../../common/utils";

interface IDeleteObjectProps {
closeDeleteModalAndRefresh: (refresh: boolean) => void;
Expand Down Expand Up @@ -86,13 +88,37 @@ const DeleteObject = ({
}

if (toSend) {
invokeDeleteApi(
"POST",
`/api/v1/buckets/${selectedBucket}/delete-objects?all_versions=${deleteVersions}${
bypassGovernance ? "&bypass=true" : ""
}`,
toSend,
);
if (selectedObjects.length === 1) {
const firstObject = selectedObjects[0];
api.buckets
.deleteObject(selectedBucket, {
prefix: encodeURLString(firstObject),
all_versions: deleteVersions,
bypass: bypassGovernance,
recursive: firstObject.endsWith("/"), //if it is just a prefix
})
.then(onDelSuccess)
.catch((err) => {
dispatch(
setErrorSnackMessage({
errorMessage: `Could not delete object. ${err.statusText}. ${
retentionConfig
? "Please check retention mode and if object is WORM protected."
: ""
}`,
detailedError: "",
}),
);
});
} else {
invokeDeleteApi(
"POST",
`/api/v1/buckets/${selectedBucket}/delete-objects?all_versions=${deleteVersions}${
bypassGovernance ? "&bypass=true" : ""
}`,
toSend,
);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const DeleteNonCurrentVersions = ({
if (deleteLoading) {
api.buckets
.deleteObject(selectedBucket, {
path: selectedObject,
prefix: selectedObject,
non_current_versions: true,
bypass: bypassGovernance,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const DeleteObject = ({
const recursive = decodedSelectedObject.endsWith("/");
invokeDeleteApi(
"DELETE",
`/api/v1/buckets/${selectedBucket}/objects?path=${selectedObject}${
`/api/v1/buckets/${selectedBucket}/objects?prefix=${selectedObject}${
selectedVersion !== ""
? `&version_id=${selectedVersion}`
: `&recursive=${recursive}&all_versions=${deleteVersions}`
Expand Down
12 changes: 4 additions & 8 deletions portal-ui/tests/permissions-3/bucketDeleteAllVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
import * as roles from "../utils/roles";
import * as elements from "../utils/elements";
import * as functions from "../utils/functions";
import { bucketsElement } from "../utils/elements-menu";
import { testBucketBrowseButtonFor } from "../utils/functions";
import { Selector } from "testcafe";
import * as constants from "../utils/constants";
import { deleteAllVersions } from "../utils/elements";

fixture("For user with Bucket Read & Write permissions").page(
"http://localhost:9090",
Expand All @@ -45,6 +42,9 @@ test
.setFilesToUpload(elements.uploadInput, "../uploads/test.txt")
.wait(1000);
})("All versions of an object can be deleted from a bucket", async (t) => {
const versionRows = Selector(
"div.ReactVirtualized__Grid.ReactVirtualized__Table__Grid > div > div:nth-child(1)",
);
await t
.useRole(roles.bucketReadWrite)
.navigateTo("http://localhost:9090/browser")
Expand All @@ -55,11 +55,7 @@ test
.click(elements.deleteButton)
.click(elements.deleteAllVersions)
.click(Selector("button:enabled").withExactText("Delete").nth(1))
.expect(
Selector(
"div.ReactVirtualized__Grid.ReactVirtualized__Table__Grid > div > div:nth-child(1)",
).exists,
)
.expect(versionRows.exists)
.notOk();
})
.after(async (t) => {
Expand Down
4 changes: 2 additions & 2 deletions restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions restapi/operations/object/delete_object_parameters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions restapi/operations/object/delete_object_urlbuilder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions restapi/user_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ func registerObjectsHandlers(api *operations.ConsoleAPI) {
})
// delete object
api.ObjectDeleteObjectHandler = objectApi.DeleteObjectHandlerFunc(func(params objectApi.DeleteObjectParams, session *models.Principal) middleware.Responder {
fmt.Println("ObjectDeleteObjectHandler", params.Prefix)
if err := getDeleteObjectResponse(session, params); err != nil {
return objectApi.NewDeleteObjectDefault(err.Code).WithPayload(err.APIError)
}
return objectApi.NewDeleteObjectOK()
})
// delete multiple objects
api.ObjectDeleteMultipleObjectsHandler = objectApi.DeleteMultipleObjectsHandlerFunc(func(params objectApi.DeleteMultipleObjectsParams, session *models.Principal) middleware.Responder {
fmt.Println("ObjectDeleteMultipleObjectsHandler", params)

if err := getDeleteMultiplePathsResponse(session, params); err != nil {
return objectApi.NewDeleteMultipleObjectsDefault(err.Code).WithPayload(err.APIError)
}
Expand Down Expand Up @@ -764,8 +767,8 @@ func getDeleteObjectResponse(session *models.Principal, params objectApi.DeleteO
ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
defer cancel()
var prefix string
if params.Path != "" {
encodedPrefix := SanitizeEncodedPrefix(params.Path)
if params.Prefix != "" {
encodedPrefix := SanitizeEncodedPrefix(params.Prefix)
decodedPrefix, err := base64.StdEncoding.DecodeString(encodedPrefix)
if err != nil {
return ErrorWithContext(ctx, err)
Expand Down
2 changes: 1 addition & 1 deletion swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ paths:
in: path
required: true
type: string
- name: path
- name: prefix
in: query
required: true
type: string
Expand Down

0 comments on commit 6767bfa

Please sign in to comment.