From efaf00e5e17a09f73a4488c4fd1a4c20922e788e Mon Sep 17 00:00:00 2001 From: Nicko Guyer Date: Fri, 6 Jan 2023 16:24:33 -0500 Subject: [PATCH] Add DELETE method for blobs Signed-off-by: Nicko Guyer --- src/handlers/blobs.ts | 11 +++++++++++ src/routers/api.ts | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/handlers/blobs.ts b/src/handlers/blobs.ts index 8b4addd..d11cb77 100644 --- a/src/handlers/blobs.ts +++ b/src/handlers/blobs.ts @@ -67,6 +67,12 @@ export const storeBlob = async (file: IFile, filePath: string) => { return await upsertMetadata(filePath, blobHash, blobSize); }; +export const deleteBlob = async (filePath: string) => { + const resolvedFilePath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY, filePath); + await deleteMetadata(filePath); + await fs.rm(resolvedFilePath); +} + export const sendBlob = async (blobPath: string, recipientID: string, recipientURL: string, requestId: string | undefined, senderDestination: string | undefined, recipientDestination: string | undefined) => { if (sending) { @@ -157,3 +163,8 @@ export const upsertMetadata = async (filePath: string, hash: string, size: numbe await fs.writeFile(resolvedFilePath, JSON.stringify(metadata)); return metadata; }; + +export const deleteMetadata = async (filePath: string) => { + const resolvedFilePath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY, filePath + utils.constants.METADATA_SUFFIX); + await fs.rm(resolvedFilePath); +}; \ No newline at end of file diff --git a/src/routers/api.ts b/src/routers/api.ts index b549ba6..42252b4 100644 --- a/src/routers/api.ts +++ b/src/routers/api.ts @@ -239,6 +239,19 @@ router.put('/blobs/*', async (req: Request, res, next) => { } }); +router.delete('/blobs/*', async (req: Request, res, next) => { + try { + const blobPath = `/${req.params[0]}`; + if (!utils.regexp.FILE_KEY.test(blobPath) || utils.regexp.CONSECUTIVE_DOTS.test(blobPath)) { + throw new RequestError('Invalid path', 400); + } + await blobsHandler.deleteBlob(blobPath); + res.status(204).send(); + } catch (err) { + next(err); + } +}); + router.post('/transfers', async (req, res, next) => { try { if (req.body.path === undefined) {