Skip to content

Commit

Permalink
Store version on delete in all cases (#86)
Browse files Browse the repository at this point in the history
* Store on delete in all cases

Fixes: #85
  • Loading branch information
bosschaert authored Oct 17, 2024
1 parent e4b15cf commit e407d91
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 54 deletions.
14 changes: 2 additions & 12 deletions src/routes/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import putObject from '../storage/object/put.js';
import deleteObjects from '../storage/object/delete.js';

import putHelper from '../helpers/source.js';
import { postObjectVersion } from '../storage/version/put.js';

async function invalidateCollab(api, url, env) {
const invPath = `/api/v1/${api}?doc=${url}`;
Expand All @@ -24,17 +23,8 @@ async function invalidateCollab(api, url, env) {
await env.dacollab.fetch(invURL);
}

export async function deleteSource({ req, env, daCtx }) {
await postObjectVersion(req, env, daCtx);
const resp = await deleteObjects(env, daCtx);

if (resp.status === 204) {
const initiator = req.headers.get('x-da-initiator');
if (initiator !== 'collab') {
await invalidateCollab('deleteadmin', req.url, env);
}
}
return resp;
export async function deleteSource({ env, daCtx }) {
return /* await */ deleteObjects(env, daCtx);
}

export async function postSource({ req, env, daCtx }) {
Expand Down
30 changes: 26 additions & 4 deletions src/storage/object/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

import getS3Config from '../utils/config.js';
import { postObjectVersionWithLabel } from '../version/put.js';

function buildInput(org, key) {
return {
Expand All @@ -25,16 +26,37 @@ function buildInput(org, key) {
};
}

export async function deleteObject(client, org, Key) {
async function invalidateCollab(api, url, env) {
const invPath = `/api/v1/${api}?doc=${url}`;

// Use dacollab service binding, hostname is not relevant
const invURL = `https://localhost${invPath}`;
await env.dacollab.fetch(invURL);
}

export async function deleteObject(client, daCtx, Key, env, isMove = false) {
const fname = Key.split('/').pop();

if (fname.includes('.') && !Key.endsWith('.props')) {
await postObjectVersionWithLabel(isMove ? 'Moved' : 'Deleted', env, daCtx);
}

let resp;
try {
const delCommand = new DeleteObjectCommand({ Bucket: `${org}-content`, Key });
const delCommand = new DeleteObjectCommand({ Bucket: `${daCtx.org}-content`, Key });
const url = await getSignedUrl(client, delCommand, { expiresIn: 3600 });
return fetch(url, { method: 'DELETE' });
resp = await fetch(url, { method: 'DELETE' });
} catch (e) {
// eslint-disable-next-line no-console
console.log(`There was an error deleting ${Key}.`);
return e;
}

if (Key.endsWith('.html')) {
await invalidateCollab('deleteadmin', `${daCtx.origin}/source/${daCtx.org}/${Key}`, env);
}

return resp;
}

export default async function deleteObjects(env, daCtx) {
Expand All @@ -59,7 +81,7 @@ export default async function deleteObjects(env, daCtx) {
await Promise.all(
new Array(1).fill(null).map(async () => {
while (sourceKeys.length) {
await deleteObject(client, daCtx.org, sourceKeys.pop());
await deleteObject(client, daCtx, sourceKeys.pop(), env);
}
}),
);
Expand Down
2 changes: 1 addition & 1 deletion src/storage/object/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default async function moveObject(env, daCtx, details) {
const copied = await copyFile(client, daCtx.org, key, details);
// Only delete the source if the file was successfully copied
if (copied.$metadata.httpStatusCode === 200) {
const deleted = await deleteObject(client, daCtx.org, key);
const deleted = await deleteObject(client, daCtx, key, env, true);
result.status = deleted.status === 204 ? 204 : deleted.status;
} else {
result.status = copied.$metadata.httpStatusCode;
Expand Down
21 changes: 12 additions & 9 deletions src/storage/version/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,7 @@ export async function putObjectWithVersion(env, daCtx, update, body) {
}
}

export async function postObjectVersion(req, env, daCtx) {
let reqJSON;
try {
reqJSON = await req.json();
} catch (e) {
// no body
}
const label = reqJSON?.label;

export async function postObjectVersionWithLabel(label, env, daCtx) {
const { body, contentLength, contentType } = await getObject(env, daCtx);
const { org, key } = daCtx;

Expand All @@ -159,3 +151,14 @@ export async function postObjectVersion(req, env, daCtx) {

return { status: resp === 200 ? 201 : resp };
}

export async function postObjectVersion(req, env, daCtx) {
let reqJSON;
try {
reqJSON = await req.json();
} catch (e) {
// no body
}
const label = reqJSON?.label;
return /* await */ postObjectVersionWithLabel(label, env, daCtx);
}
1 change: 1 addition & 0 deletions src/utils/daCtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default async function getDaCtx(req, env) {
org,
users,
fullKey,
origin: new URL(req.url).origin,
};

// Get org properties
Expand Down
32 changes: 4 additions & 28 deletions test/routes/source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,12 @@ describe('Source Route', () => {
assert.deepStrictEqual(called, ['getObject']);
});

it('Test deleteSource', async () => {
const req = {
headers: new Map(),
url: 'http://somehost.com/somedoc.html'
};

const daCalled = []
const dacollab = { fetch: (u) => daCalled.push(u) };

const env = { dacollab };
it('Test getSource with', async () => {
const env = {};
const daCtx = {};

const postObjVerCalled = [];
const postObjVerResp = async (r, e, c) => {
if (r === req && e === env && c === daCtx) {
postObjVerCalled.push('postObjectVersion');
return {status: 201};
}
};

const deleteCalled = [];
const deleteResp = async (e, c) => {
if (e === env && c === daCtx) {
deleteCalled.push('deleteObject');
return {status: 204};
}
};
Expand All @@ -177,17 +159,11 @@ describe('Source Route', () => {
'../../src/routes/source.js', {
'../../src/storage/object/delete.js': {
default: deleteResp
},
'../../src/storage/version/put.js': {
postObjectVersion: postObjVerResp
}
}
);
const resp = await deleteSource({req, env, daCtx});

const resp = await deleteSource({env, daCtx});
assert.equal(204, resp.status);
assert.deepStrictEqual(['postObjectVersion'], postObjVerCalled);
assert.deepStrictEqual(deleteCalled, ['deleteObject']);
assert.deepStrictEqual(daCalled,
['https://localhost/api/v1/deleteadmin?doc=http://somehost.com/somedoc.html']);
});
});
Loading

0 comments on commit e407d91

Please sign in to comment.