Skip to content

Commit

Permalink
feat: add delete file endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Aug 4, 2022
1 parent d7147d2 commit 3e3b7ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/controllers/fileStore.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ export const getFileList = async (req, res) => {
}
};

export const deleteFile = async (req, res) => {
try {
await FileStore.deleteFileStorItem(req.params.fileId);
res.status(204).end();
} catch (error) {
res.status(400).json({
message: 'Can not delete file from filestore',
error: error.message,
});
}
};

export const getFile = async (req, res) => {
try {
const { fileId } = req.body;
Expand Down
21 changes: 21 additions & 0 deletions src/models/file-store/file-store.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { sequelize } from '../../database';
import { Organization } from '../organizations';

import datalayer from '../../datalayer';
import { encodeHex } from '../../utils/datalayer-utils';

import ModelTypes from './file-store.modeltypes.cjs';

Expand Down Expand Up @@ -93,6 +94,26 @@ class FileStore extends Model {
});
}

static async deleteFileStorItem(SHA256) {
const myOrganization = await Organization.getHomeOrg();
let fileStoreId = myOrganization.fileStoreId;

if (!fileStoreId) {
fileStoreId = await datalayer.createDataLayerStore();
datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId });
throw new Error('New File store being created, please try again later.');
}

const changeList = {
action: 'delete',
key: encodeHex(SHA256),
};

datalayer.pushChangesWhenStoreIsAvailable(fileStoreId, changeList);

FileStore.destroy({ where: { SHA256, orgUid: myOrganization.org } });
}

static async getFileStoreItem(SHA256) {
const myOrganization = await Organization.getHomeOrg();
let fileStoreId = myOrganization.fileStoreId;
Expand Down
8 changes: 8 additions & 0 deletions src/routes/v1/resources/filestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ FileStoreRouter.post('/get_file', validator.body(getFileSchema), (req, res) => {
return FileStoreController.getFile(req, res);
});

FileStoreRouter.delete(
'/delete_file',
validator.body(getFileSchema),
(req, res) => {
return FileStoreController.deleteFile(req, res);
},
);

FileStoreRouter.get('/get_file_list', (req, res) => {
return FileStoreController.getFileList(req, res);
});
Expand Down

0 comments on commit 3e3b7ec

Please sign in to comment.