Skip to content

Commit

Permalink
Merge pull request #73 from kaleido-io/delete-data
Browse files Browse the repository at this point in the history
Add DELETE method for blobs
  • Loading branch information
gabriel-indik authored Jan 9, 2023
2 parents a21c1ec + efaf00e commit b201ad7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/handlers/blobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
};
13 changes: 13 additions & 0 deletions src/routers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b201ad7

Please sign in to comment.