Skip to content

Commit d6c51e2

Browse files
feat: Add support for DELETE /resources/backup/:asset_id (#700)
* feat: Add support for DELETE /resources/backup/:asset_id * Remove testing bloat --------- Co-authored-by: cloudinary-pkoniu <[email protected]>
1 parent c072e37 commit d6c51e2

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

lib/api.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ exports.delete_all_resources = function delete_all_resources(callback, options =
178178
}), callback, options);
179179
};
180180

181+
exports.delete_backed_up_assets = (assetId, versionIds, callback, options = {}) => {
182+
const params = deleteBackupParams(versionIds);
183+
184+
return call_api('delete', ['resources', 'backup', assetId], params, callback, options);
185+
}
186+
187+
const deleteBackupParams = (versionIds = []) => {
188+
return {
189+
"version_ids[]": Array.isArray(versionIds) ? versionIds : [versionIds]
190+
};
191+
};
192+
181193
const createRelationParams = (publicIds = []) => {
182194
return {
183195
assets_to_relate: Array.isArray(publicIds) ? publicIds : [publicIds]

lib/v2/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ v1_adapters(exports, api, {
7575
add_related_assets_by_asset_id: 2,
7676
delete_related_assets: 2,
7777
delete_related_assets_by_asset_id: 2,
78+
delete_backed_up_assets: 2,
7879
config: 0
7980
});

test/integration/api/admin/api_spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,60 @@ describe("api", function () {
921921
});
922922
});
923923
});
924+
describe('delete_backed_up_assets', function () {
925+
it('should delete specific version IDs', async function () {
926+
this.timeout(TIMEOUT.LARGE);
927+
928+
// Process:
929+
// - Upload to the same public ID three times
930+
// - Delete a single version (string)
931+
// - Delete a two versions (array)
932+
// - Cleanup
933+
934+
// Perform three uploads
935+
const firstUpload = await uploadImage({
936+
public_id: PUBLIC_ID_BACKUP_1,
937+
backup: true
938+
});
939+
940+
const secondUpload = await uploadImage({
941+
public_id: PUBLIC_ID_BACKUP_1,
942+
backup: true,
943+
angle: '0', // To create a unique version
944+
overwrite: true
945+
});
946+
947+
const thirdUpload = await uploadImage({
948+
public_id: PUBLIC_ID_BACKUP_1,
949+
backup: true,
950+
angle: '100', // To create a unique version
951+
overwrite: true
952+
});
953+
954+
// Ensure all files were uploaded correctly
955+
expect(firstUpload).not.to.be(null);
956+
expect(secondUpload).not.to.be(null);
957+
expect(thirdUpload).not.to.be(null);
958+
959+
// Get the asset ID and versions of the uploaded asset
960+
const resourceResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true});
961+
const assetId = resourceResp.asset_id;
962+
const firstAssetVersion = resourceResp.versions[0].version_id;
963+
const secondAssetVersion = resourceResp.versions[1].version_id;
964+
const thirdAssetVersion = resourceResp.versions[2].version_id;
965+
966+
// Delete the first version
967+
const removeSingleVersion = await cloudinary.v2.api.delete_backed_up_assets(assetId, firstAssetVersion);
968+
const removeSingleVersionResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true});
969+
expect(removeSingleVersionResp.versions).not.to.contain(firstAssetVersion);
970+
971+
// Delete the remaining two versions
972+
const removeMultipleVersions = await cloudinary.v2.api.delete_backed_up_assets(assetId, [secondAssetVersion, thirdAssetVersion]);
973+
const removeMultipleVersionsResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, {versions: true});
974+
expect(removeMultipleVersionsResp.versions).not.to.contain(secondAssetVersion);
975+
expect(removeMultipleVersionsResp.versions).not.to.contain(thirdAssetVersion);
976+
});
977+
});
924978
describe("update", function () {
925979
describe("notification url", function () {
926980
var writeSpy, xhr;

0 commit comments

Comments
 (0)