From 50c02c34f57cf5a4aac519c42560375e0d7cbdcd Mon Sep 17 00:00:00 2001 From: Danny Valentine Date: Wed, 22 Jan 2025 16:28:37 +0000 Subject: [PATCH 1/2] feat: Add support for DELETE /resources/backup/:asset_id --- lib/api.js | 12 +++++ lib/v2/api.js | 1 + test/integration/api/admin/api_spec.js | 61 ++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/lib/api.js b/lib/api.js index 5e5fdfe7..aa1092a7 100644 --- a/lib/api.js +++ b/lib/api.js @@ -178,6 +178,18 @@ exports.delete_all_resources = function delete_all_resources(callback, options = }), callback, options); }; +exports.delete_backed_up_assets = (assetId, versionIds, callback, options = {}) => { + const params = deleteBackupParams(versionIds); + + return call_api('delete', ['resources', 'backup', assetId], params, callback, options); +} + +const deleteBackupParams = (versionIds = []) => { + return { + "version_ids[]": Array.isArray(versionIds) ? versionIds : [versionIds] + }; +}; + const createRelationParams = (publicIds = []) => { return { assets_to_relate: Array.isArray(publicIds) ? publicIds : [publicIds] diff --git a/lib/v2/api.js b/lib/v2/api.js index deebe8e0..4f109841 100644 --- a/lib/v2/api.js +++ b/lib/v2/api.js @@ -75,5 +75,6 @@ v1_adapters(exports, api, { add_related_assets_by_asset_id: 2, delete_related_assets: 2, delete_related_assets_by_asset_id: 2, + delete_backed_up_assets: 2, config: 0 }); diff --git a/test/integration/api/admin/api_spec.js b/test/integration/api/admin/api_spec.js index 227026c1..a3e851a7 100644 --- a/test/integration/api/admin/api_spec.js +++ b/test/integration/api/admin/api_spec.js @@ -921,6 +921,67 @@ describe("api", function () { }); }); }); + describe('delete_backed_up_assets', function () { + it('should delete specific version IDs', async function () { + this.timeout(TIMEOUT.LARGE); + + // Process: + // - Upload to the same public ID three times + // - Delete a single version (string) + // - Delete a two versions (array) + // - Cleanup + + // Perform three uploads + const firstUpload = await uploadImage({ + public_id: PUBLIC_ID_BACKUP_1, + backup: true + }); + await wait(1000)(); + + const secondUpload = await uploadImage({ + public_id: PUBLIC_ID_BACKUP_1, + backup: true, + angle: '0', // To create a unique version + overwrite: true + }); + await wait(1000)(); + + const thirdUpload = await uploadImage({ + public_id: PUBLIC_ID_BACKUP_1, + backup: true, + angle: '100', // To create a unique version + overwrite: true + }); + await wait(1000)(); + + // Ensure all files were uploaded correctly + expect(firstUpload).not.to.be(null); + expect(secondUpload).not.to.be(null); + expect(thirdUpload).not.to.be(null); + + // Get the asset ID and versions of the uploaded asset + const resourceResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true}); + const assetId = resourceResp.asset_id; + const firstAssetVersion = resourceResp.versions[0].version_id; + const secondAssetVersion = resourceResp.versions[1].version_id; + const thirdAssetVersion = resourceResp.versions[2].version_id; + + // Delete the first version + const removeSingleVersion = await cloudinary.v2.api.delete_backed_up_assets(assetId, firstAssetVersion); + const removeSingleVersionResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true}); + expect(removeSingleVersionResp.versions).not.to.contain(firstAssetVersion); + + // Delete the remaining two versions + const removeMultipleVersions = await cloudinary.v2.api.delete_backed_up_assets(assetId, [secondAssetVersion, thirdAssetVersion]); + const removeMultipleVersionsResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true}); + expect(removeMultipleVersionsResp.versions).not.to.contain(secondAssetVersion); + expect(removeMultipleVersionsResp.versions).not.to.contain(thirdAssetVersion); + + // Cleanup, + const finalDeleteResp = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]); + expect(finalDeleteResp).to.have.property("deleted"); + }); + }); describe("update", function () { describe("notification url", function () { var writeSpy, xhr; From 30ec92c5be3cf1d8699aea4e471ad1b742ba9b74 Mon Sep 17 00:00:00 2001 From: Danny Valentine Date: Tue, 4 Feb 2025 12:17:56 +0000 Subject: [PATCH 2/2] Remove testing bloat --- test/integration/api/admin/api_spec.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/integration/api/admin/api_spec.js b/test/integration/api/admin/api_spec.js index a3e851a7..873d5311 100644 --- a/test/integration/api/admin/api_spec.js +++ b/test/integration/api/admin/api_spec.js @@ -936,7 +936,6 @@ describe("api", function () { public_id: PUBLIC_ID_BACKUP_1, backup: true }); - await wait(1000)(); const secondUpload = await uploadImage({ public_id: PUBLIC_ID_BACKUP_1, @@ -944,7 +943,6 @@ describe("api", function () { angle: '0', // To create a unique version overwrite: true }); - await wait(1000)(); const thirdUpload = await uploadImage({ public_id: PUBLIC_ID_BACKUP_1, @@ -952,7 +950,6 @@ describe("api", function () { angle: '100', // To create a unique version overwrite: true }); - await wait(1000)(); // Ensure all files were uploaded correctly expect(firstUpload).not.to.be(null); @@ -976,10 +973,6 @@ describe("api", function () { const removeMultipleVersionsResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true}); expect(removeMultipleVersionsResp.versions).not.to.contain(secondAssetVersion); expect(removeMultipleVersionsResp.versions).not.to.contain(thirdAssetVersion); - - // Cleanup, - const finalDeleteResp = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]); - expect(finalDeleteResp).to.have.property("deleted"); }); }); describe("update", function () {