Skip to content

Commit b201ad7

Browse files
Merge pull request #73 from kaleido-io/delete-data
Add DELETE method for blobs
2 parents a21c1ec + efaf00e commit b201ad7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Diff for: src/handlers/blobs.ts

+11
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ export const storeBlob = async (file: IFile, filePath: string) => {
6767
return await upsertMetadata(filePath, blobHash, blobSize);
6868
};
6969

70+
export const deleteBlob = async (filePath: string) => {
71+
const resolvedFilePath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY, filePath);
72+
await deleteMetadata(filePath);
73+
await fs.rm(resolvedFilePath);
74+
}
75+
7076
export const sendBlob = async (blobPath: string, recipientID: string, recipientURL: string, requestId: string | undefined,
7177
senderDestination: string | undefined, recipientDestination: string | undefined) => {
7278
if (sending) {
@@ -157,3 +163,8 @@ export const upsertMetadata = async (filePath: string, hash: string, size: numbe
157163
await fs.writeFile(resolvedFilePath, JSON.stringify(metadata));
158164
return metadata;
159165
};
166+
167+
export const deleteMetadata = async (filePath: string) => {
168+
const resolvedFilePath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY, filePath + utils.constants.METADATA_SUFFIX);
169+
await fs.rm(resolvedFilePath);
170+
};

Diff for: src/routers/api.ts

+13
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,19 @@ router.put('/blobs/*', async (req: Request, res, next) => {
239239
}
240240
});
241241

242+
router.delete('/blobs/*', async (req: Request, res, next) => {
243+
try {
244+
const blobPath = `/${req.params[0]}`;
245+
if (!utils.regexp.FILE_KEY.test(blobPath) || utils.regexp.CONSECUTIVE_DOTS.test(blobPath)) {
246+
throw new RequestError('Invalid path', 400);
247+
}
248+
await blobsHandler.deleteBlob(blobPath);
249+
res.status(204).send();
250+
} catch (err) {
251+
next(err);
252+
}
253+
});
254+
242255
router.post('/transfers', async (req, res, next) => {
243256
try {
244257
if (req.body.path === undefined) {

0 commit comments

Comments
 (0)