Skip to content

Commit

Permalink
fix: delete space resources with space
Browse files Browse the repository at this point in the history
removed the resources used by the space only

close #11
  • Loading branch information
hassan committed Dec 21, 2018
1 parent 3ca28f8 commit cf1e1e1
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ app.on('ready', () => {
ipcMain.on(DELETE_SPACE_CHANNEL, async (event, { id }) => {
try {
let spaces = [];
let spaceImageUrl = '';
const spacesPath = `${savedSpacesPath}/${spacesFileName}`;
fs.readFile(spacesPath, 'utf8', async (err, data) => {
if (err) {
Expand All @@ -243,6 +244,53 @@ app.on('ready', () => {
);
} else {
spaces = JSON.parse(data);
const allResources = [];
const spaceResources = [];
for (const space of spaces) {
const { phases, id: spaceId, image: imageUrl } = space;
if ( spaceId === id ) {
// to get the extension of the background image for the space to be deleted
spaceImageUrl = imageUrl;
}
for (const phase of phases) {
const { items = [] } = phase;
for (let i = 0; i < items.length; i++) {
const { resource } = items[i];
if (resource) {
const {
hash,
type,
} = resource;
const fileName = `${hash}.${type}`;
const filePath = `${savedSpacesPath}/${fileName}`;
const fileAvailable = await checkFileAvailable(filePath);
if (fileAvailable) {
if ( spaceId === id ) {
spaceResources.push(filePath);
} else {
allResources.push(filePath);
}
}
}
}
}
}
// resources in the space but not used by other spaces
const allResourcesSet = new Set(allResources);
const spaceDisntictResources = new Set([...spaceResources].filter(filePath => !allResourcesSet.has(filePath)));
const extension = spaceImageUrl.match(/[^\\]*\.(\w+)$/)[1];
const backgroundImagePath = `${savedSpacesPath}/background-${id}.${extension}`;
const backgroundImageExists = await checkFileAvailable(backgroundImagePath);
if (backgroundImageExists) {
spaceDisntictResources.add(backgroundImagePath);
}
// delete all resources used by the space to be deleted only
[...spaceDisntictResources].forEach(filePath =>
fs.unlink(filePath, (err) => {
if (err) {
console.log(err);
}
}));
const newSpaces = spaces.filter(el => Number(el.id) !== Number(id));
const spacesString = JSON.stringify(newSpaces);
await fsPromises.writeFile(`${savedSpacesPath}/${spacesFileName}`, spacesString);
Expand Down

0 comments on commit cf1e1e1

Please sign in to comment.