Skip to content

Commit

Permalink
Delete all files on empty DELETE body
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellrgn committed Mar 22, 2024
1 parent 5ecbbb6 commit 13a1afe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
11 changes: 11 additions & 0 deletions server/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ export const DbLinks = {

return true;
},
async deleteAllFiles(linkId: string) {

db.query(
`delete from shlink_file where shlink = :linkId`,
{
linkId
}
);

return true;
},
async addEndpoint(linkId: string, endpoint: types.HealthLinkEndpoint): Promise<string> {
const id = randomStringWithEntropy(32);

Expand Down
21 changes: 16 additions & 5 deletions server/routers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,27 @@ export const shlApiRouter = new oak.Router()
.delete('/shl/:shlId/file', async (context) => {
const managementToken = await context.request.headers.get('authorization')?.split(/bearer /i)[1]!;
const currentFileBody = await context.request.body({type: 'bytes'});

const currentFileValue = await currentFileBody.value;
console.log("Current file body is: '"+currentFileValue+"'");
const shl = db.DbLinks.getManagedShl(context.params.shlId, managementToken);
if (!shl) {
throw new Error(`Can't manage SHLink ` + context.params.shlId);
}

const deleted = db.DbLinks.deleteFile(shl.id, await currentFileBody.value);
context.response.body = {
...shl,
deleted,
if (currentFileValue.length > 0) {
console.log("deleting single file");
const success = db.DbLinks.deleteFile(shl.id, currentFileValue);
context.response.body = {
...shl,
success,
}
} else {
console.log("deleting all files for shlink "+shl.id);
const success = db.DbLinks.deleteAllFiles(shl.id);
context.response.body = {
...shl,
success,
}
}
})
.post('/shl/:shlId/endpoint', async (context) => {
Expand Down

0 comments on commit 13a1afe

Please sign in to comment.