Skip to content

Commit

Permalink
Store on delete in all cases
Browse files Browse the repository at this point in the history
Fixes: #85
  • Loading branch information
bosschaert committed Oct 11, 2024
1 parent e4b15cf commit c2c60f2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
10 changes: 1 addition & 9 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,16 +23,9 @@ async function invalidateCollab(api, url, env) {
await env.dacollab.fetch(invURL);
}

export async function deleteSource({ req, env, daCtx }) {
await postObjectVersion(req, env, daCtx);
export async function deleteSource({ 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;
}

Expand Down
28 changes: 24 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 { postObjectVersion } from '../version/put.js';

function buildInput(org, key) {
return {
Expand All @@ -25,16 +26,35 @@ 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) {
if (Key.endsWith('.html')) {
await postObjectVersion('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 = 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 +79,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);
result.status = deleted.status === 204 ? 204 : deleted.status;
} else {
result.status = copied.$metadata.httpStatusCode;
Expand Down
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

0 comments on commit c2c60f2

Please sign in to comment.