Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
"@types/express": "^4.17.13",
"@types/jsrsasign": "^10.2.1",
"@types/mocha": "^9.1.0",
"@types/mock-fs": "^4.13.1",
"@types/node": "^17.0.23",
"@types/swagger-ui-express": "^4.1.3",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.3",
"@types/yamljs": "^0.2.31",
"chai": "^4.3.6",
"mocha": "^9.2.2",
"mock-fs": "^5.1.4",
"moment": "^2.29.4",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
Expand Down
55 changes: 51 additions & 4 deletions src/handlers/blobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const storeBlob = async (file: IFile, filePath: string) => {
cb();
}
});
file.readableStream.on('error', err => {
file.readableStream.on('error', err => {
reject(err);
});
file.readableStream.pipe(hashCalculator).pipe(writeStream);
Expand Down Expand Up @@ -89,12 +89,12 @@ export const deliverBlob = async ({ blobPath, recipientID, recipientURL, request
const stream = createReadStream(resolvedFilePath);
const formData = new FormData();
let sender = peerID;
if(senderDestination !== undefined) {
if (senderDestination !== undefined) {
formData.append('senderDestination', senderDestination);
sender += '/' + senderDestination
}
let recipient = recipientID;
if(recipientDestination !== undefined) {
if (recipientDestination !== undefined) {
formData.append('recipientDestination', recipientDestination);
recipient += '/' + recipientDestination;
}
Expand Down Expand Up @@ -133,6 +133,52 @@ export const deliverBlob = async ({ blobPath, recipientID, recipientURL, request
}
};

export const deleteBlob = async (filePath: string) => {
const resolvedFilePath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY, filePath);
const resolvedFileMetadataPath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY, filePath + utils.constants.METADATA_SUFFIX);
log.debug(`Deleting blob: ${resolvedFilePath} with metadata: ${resolvedFileMetadataPath}`);

let tempFileCopy: Buffer | null = null;
let tempMetadataCopy = null;
let metadata: IMetadata | null = null;

try {
if ((await utils.fileExists(resolvedFilePath))) {
// Copy to tmp files
tempFileCopy = await fs.readFile(resolvedFilePath);
// Attempt file deletion
await fs.rm(resolvedFilePath);
// Check if deletion succeeded before proceeding to delete the metadata
if (await utils.fileExists(resolvedFilePath)) {
throw new RequestError(`Blob deletion failed`);
}
}

if ((await utils.fileExists(resolvedFileMetadataPath))) {
tempMetadataCopy = await fs.readFile(resolvedFileMetadataPath);
// Retrieve metadata to return with response
metadata = await retrieveMetadata(filePath);
await fs.rm(resolvedFileMetadataPath);
if (await utils.fileExists(resolvedFileMetadataPath)) {
throw new RequestError(`Blob metadata deletion failed`);
}
}
return metadata;
}
catch (err) {
log.error(`Error while attempting to delete Blob: ${err}`);

// Restore any deleted files
if (tempFileCopy && !(await utils.fileExists(resolvedFilePath))) {
await fs.writeFile(resolvedFilePath, <Buffer>tempFileCopy);
}
if (tempMetadataCopy && !(await utils.fileExists(resolvedFileMetadataPath))) {
await fs.writeFile(resolvedFileMetadataPath, <Buffer>tempMetadataCopy);
}
}

}

export const retrieveMetadata = async (filePath: string) => {
const resolvedFilePath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY, filePath + utils.constants.METADATA_SUFFIX);
if (!(await utils.fileExists(resolvedFilePath))) {
Expand All @@ -141,7 +187,7 @@ export const retrieveMetadata = async (filePath: string) => {
try {
const metadataString = await fs.readFile(resolvedFilePath);
return JSON.parse(metadataString.toString()) as IMetadata;
} catch(err) {
} catch (err) {
throw new RequestError(`Invalid blob`);
}
};
Expand All @@ -157,3 +203,4 @@ export const upsertMetadata = async (filePath: string, hash: string, size: numbe
await fs.writeFile(resolvedFilePath, JSON.stringify(metadata));
return metadata;
};

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);
}
const metadata = await blobsHandler.deleteBlob(blobPath);
res.send(metadata);
} catch (err) {
next(err);
}
});

router.post('/transfers', async (req, res, next) => {
try {
if (req.body.path === undefined) {
Expand Down
28 changes: 28 additions & 0 deletions src/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@
application/json:
schema:
$ref: '#/components/schemas/Error'
delete:
tags:
- Blobs
description: Delete blob
responses:
'200':
description: Blob metadata
content:
application/json:
schema:
$ref: '#/components/schemas/BlobMetadata'
'500':
description: Internal error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/transfers:
post:
tags:
Expand Down Expand Up @@ -365,6 +382,17 @@
properties:
hash:
type: string
BlobMetadata:
type: object
required:
- hash
properties:
hash:
type: string
size:
type: number
lastUpdate:
type: number
Transfer:
type: object
required:
Expand Down
69 changes: 69 additions & 0 deletions test/handlers/blobs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as chai from 'chai';
import { expect } from 'chai';
import sinonChai from 'sinon-chai';
import * as blobs from '../../src/handlers/blobs';
import { setLogLevel } from '../../src/lib/logger';
import * as utils from '../../src/lib/utils';
import path from 'path';
import mockFs from 'mock-fs';

chai.use(sinonChai);

let mockFilesystem = {
'some/other/path': {/** another empty directory */ },
};
setLogLevel('debug');

afterEach(() => mockFs.restore());

describe('blobs', () => {
it('deletes both blob file and metadata successfully when both exist', async () => {
const testBlobPath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY);
mockFilesystem[testBlobPath as keyof typeof mockFilesystem] = {
'test-blob': 'file content here',
'test-blob.metadata.json': JSON.stringify({
hash: 'testHash',
lastUpdate: 123,
size: 10
})
};

mockFs(mockFilesystem);
const metadata = await blobs.deleteBlob('test-blob');

expect(metadata?.size).to.equal(10);
expect(metadata?.hash).to.equal('testHash');
expect(metadata?.lastUpdate).to.equal(123);
});

it('deletes metadata that exists when file does not exist', async () => {
const testBlobPath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY);
mockFilesystem[testBlobPath as keyof typeof mockFilesystem] = {
'test-blob.metadata.json': JSON.stringify({
hash: 'testHash',
lastUpdate: 123,
size: 10
})
};

mockFs(mockFilesystem);
const metadata = await blobs.deleteBlob('test-blob');

expect(metadata?.size).to.equal(10);
expect(metadata?.hash).to.equal('testHash');
expect(metadata?.lastUpdate).to.equal(123);
});

it('deletes blob file successfully even if metadata does not exist', async () => {
const testBlobPath = path.join(utils.constants.DATA_DIRECTORY, utils.constants.BLOBS_SUBDIRECTORY);
mockFilesystem[testBlobPath as keyof typeof mockFilesystem] = {
'test-blob': 'file content here',
};

mockFs(mockFilesystem);
const metadata = await blobs.deleteBlob('test-blob');

expect(metadata).to.be.null;
});

});