Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
chore: implement cleanup on launch
Browse files Browse the repository at this point in the history
  • Loading branch information
sircharlo committed Jun 21, 2024
1 parent e04043b commit f62884e
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/helpers/cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { PathLike } from 'fs';
import { LocalStorage, date } from 'quasar';
import { isInPast } from 'src/helpers/date';
import { electronApi } from 'src/helpers/electron-api';
import { DynamicMediaObject } from 'src/types/media';

const { fileUrlToPath, fs, getUserDataPath, klawSync, path } = electronApi;

const cleanLocalStorage = () => {
try {
for (const storageElementName of [
'additionalMediaMaps',
'customDurations',
'dynamicMedia',
'mediaSort',
]) {
const storageElement = LocalStorage.getItem(storageElementName);
let changesMade = false;
if (storageElement) {
console.log(storageElementName, storageElement);
for (const congregation of Object.keys(storageElement)) {
// @ts-expect-error LocalStorage typing mishap here
for (const dateKey of Object.keys(storageElement[congregation])) {
// if (!key) delete storageElement[key];
const strippedDateKey = dateKey.replace(/\D/g, '');
// strippedKey = YYYYMMDD
const strippedDateKeyAsDate = date.buildDate(
{
day: Number(strippedDateKey.slice(6, 8)),
hours: 0,
minutes: 0,
month: Number(strippedDateKey.slice(4, 6)),
seconds: 0,
year: Number(strippedDateKey.slice(0, 4)),
},
false,
);
console.log(dateKey);
if (isInPast(strippedDateKeyAsDate)) {
console.log('removing', dateKey);
// @ts-expect-error LocalStorage typing mishap here
delete storageElement[congregation][dateKey];
changesMade = true;
}
}
}
}
if (changesMade) LocalStorage.set(storageElementName, storageElement);
}
} catch (error) {
console.error(error);
}
};

function isEmptyDir(directory: PathLike) {
try {
return fs.readdirSync(directory).length === 0;
} catch (error) {
console.error(error);
return false;
}
}
function removeEmptyDirs(rootDir: string) {
try {
const dirs = klawSync(rootDir, {
depthLimit: -1,
nodir: false,
nofile: true,
})
.map((item) => item.path)
.sort((a, b) => b.length - a.length);
dirs.forEach((dir) => {
if (isEmptyDir(dir)) {
console.log(`Removing empty directory: ${dir}`);
fs.rmdirSync(dir);
}
});
} catch (error) {
console.error(error);
}
}

const cleanAdditionalMediaFolder = () => {
try {
const additionalMediaPath = path.join(
getUserDataPath(),
'Additional Media',
);
const additionalMediaMaps = LocalStorage.getItem(
'additionalMediaMaps',
) as Record<string, Record<string, DynamicMediaObject[]>>;
const flattenedFilePaths = (
data: Record<string, Record<string, DynamicMediaObject[]>>,
) => {
return Object.values(data).flatMap((dateObj) =>
Object.values(dateObj).flatMap((files) =>
files.map((file) => path.resolve(fileUrlToPath(file.fileUrl))),
),
);
};
const filePaths = flattenedFilePaths(additionalMediaMaps);
console.log(filePaths);
const dirListing = klawSync(additionalMediaPath, { nodir: true });
for (const file of dirListing) {
const additionalMediaFile = path.resolve(file.path);
if (!filePaths.includes(additionalMediaFile)) {
console.log('removing', additionalMediaFile);
fs.rmSync(additionalMediaFile);
}
}
removeEmptyDirs(additionalMediaPath);
} catch (error) {
console.error(error);
}
};

export { cleanAdditionalMediaFolder, cleanLocalStorage };
4 changes: 4 additions & 0 deletions src/layouts/MainLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ import ObsStatus from 'src/components/media/ObsStatus.vue';
// import ScenePicker from 'src/components/media/ScenePicker.vue';
import SongPicker from 'src/components/media/SongPicker.vue';
import SubtitlesButton from 'src/components/media/SubtitlesButton.vue';
import { cleanAdditionalMediaFolder, cleanLocalStorage } from 'src/helpers/cleanup';
import { getLookupPeriod } from 'src/helpers/date';
import { electronApi } from 'src/helpers/electron-api';
import {
Expand Down Expand Up @@ -605,6 +606,9 @@ if (!migrations.value.includes('firstRun')) {
}
}
cleanLocalStorage()
cleanAdditionalMediaFolder()
const getLocalFiles = async () => {
openFileDialog().then((result) => {
if (result.filePaths.length > 0) {
Expand Down

0 comments on commit f62884e

Please sign in to comment.