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..873d5311 100644 --- a/test/integration/api/admin/api_spec.js +++ b/test/integration/api/admin/api_spec.js @@ -921,6 +921,60 @@ 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 + }); + + const secondUpload = await uploadImage({ + public_id: PUBLIC_ID_BACKUP_1, + backup: true, + angle: '0', // To create a unique version + overwrite: true + }); + + const thirdUpload = await uploadImage({ + public_id: PUBLIC_ID_BACKUP_1, + backup: true, + angle: '100', // To create a unique version + overwrite: true + }); + + // 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); + }); + }); describe("update", function () { describe("notification url", function () { var writeSpy, xhr;