diff --git a/src/controllers/fileStore.controller.js b/src/controllers/fileStore.controller.js index 95a76621..d4b79d36 100644 --- a/src/controllers/fileStore.controller.js +++ b/src/controllers/fileStore.controller.js @@ -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; diff --git a/src/models/file-store/file-store.model.js b/src/models/file-store/file-store.model.js index 1e8e49bd..58251bfb 100644 --- a/src/models/file-store/file-store.model.js +++ b/src/models/file-store/file-store.model.js @@ -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'; @@ -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; diff --git a/src/routes/v1/resources/filestore.js b/src/routes/v1/resources/filestore.js index a25703f7..083943fe 100644 --- a/src/routes/v1/resources/filestore.js +++ b/src/routes/v1/resources/filestore.js @@ -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); });