Skip to content

Commit

Permalink
fix: Ignoring .fuse_hidden files
Browse files Browse the repository at this point in the history
  • Loading branch information
steilerDev committed Aug 14, 2023
1 parent 6005239 commit 2634f0d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
5 changes: 4 additions & 1 deletion app/src/lib/photos-library/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export const LIBRARY_VERSION = 1;
/**
* A list of files, that are safe to ignore for the application.
*/
export const SAFE_FILES = [`.DS_Store`];
export const SAFE_FILES = [
/^\.DS_Store$/,
/^\.fuse_hidden.*/,
];
15 changes: 10 additions & 5 deletions app/src/lib/photos-library/photos-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export class PhotosLibrary {
const libAssets: PLibraryEntities<Asset> = {};
const zonePath = zone === Zones.Primary ? this.primaryAssetDir : this.sharedAssetDir;
(await fs.promises.readdir(zonePath, {withFileTypes: true}))
.filter(file => file.isFile() && !PHOTOS_LIBRARY.SAFE_FILES.includes(file.name))
.filter(file => file.isFile())
.filter(file => PHOTOS_LIBRARY.SAFE_FILES.every(regex => !regex.test(file.name)))
.forEach(file => {
try {
const fileStat = fs.statSync(path.format({
Expand Down Expand Up @@ -227,10 +228,13 @@ export class PhotosLibrary {
withFileTypes: true,
})).some(file => file.isDirectory());

// If there are files in the folders, the folder is treated as archived
const filePresent = (await fs.promises.readdir(thisPath, {
const dirContent = await fs.promises.readdir(thisPath, {
withFileTypes: true,
})).filter(file => !PHOTOS_LIBRARY.SAFE_FILES.includes(file.name)) // Filter out files that are safe to ignore
});

// If there are files in the folders, the folder is treated as archived
const filePresent = dirContent
.filter(file => PHOTOS_LIBRARY.SAFE_FILES.every(regex => !regex.test(file.name)))
.some(file => file.isFile());

if (directoryPresent) {
Expand Down Expand Up @@ -467,7 +471,8 @@ export class PhotosLibrary {

// Checking content
const pathContent = fs.readdirSync(uuidPath, {withFileTypes: true})
.filter(item => !(item.isSymbolicLink() || PHOTOS_LIBRARY.SAFE_FILES.includes(item.name))); // Filter out symbolic links, we are fine with deleting those as well as the 'safe' files
.filter(item => !item.isSymbolicLink()) // Filter out symbolic links, we are fine with deleting those as well as the 'safe' files
.filter(file => PHOTOS_LIBRARY.SAFE_FILES.every(regex => !regex.test(file.name)));
if (pathContent.length > 0) {
throw new iCPSError(LIBRARY_ERR.NOT_EMPTY)
.addMessage(uuidPath)
Expand Down
30 changes: 18 additions & 12 deletions app/test/unit/photos-library.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ describe(`Load state`, () => {
"Aah0dUnhGFNWjAeqKEkB_SNLNpFf": Buffer.from([1, 1, 1, 1, 1, 1]), // eslint-disable-line
"Aah0dUnhGFNWjAeqKEkB_SNLNpFf.xyz": Buffer.from([1, 1, 1, 1, 1, 1]), // Invalid file extension
"Aah0dUnhGFNWjAeqKEkB_SNLNpF-f": Buffer.from([1, 1, 1, 1, 1, 1]), // Invalid file name
".DS_Store": ``, // 'Safe' file
".fuse_hidden1234": ``, // 'Safe' file
},
},
expectedCount: 2,
Expand All @@ -155,6 +157,8 @@ describe(`Load state`, () => {
"Aah0dUnhGFNWjAeqKEkB_SNLNpFf": Buffer.from([1, 1, 1, 1, 1, 1]), // eslint-disable-line
"Aah0dUnhGFNWjAeqKEkB_SNLNpFf.xyz": Buffer.from([1, 1, 1, 1, 1, 1]), // Invalid file extension
"Aah0dUnhGFNWjAeqKEkB_SNLNpF-f": Buffer.from([1, 1, 1, 1, 1, 1]), // Invalid file name
".DS_Store": ``, // 'Safe' file
".fuse_hidden1234": ``, // 'Safe' file
},
},
expectedCount: 2,
Expand All @@ -174,6 +178,8 @@ describe(`Load state`, () => {
"Aah0dUnhGFNWjAeqKEkB_SNLNpFf": Buffer.from([1, 1, 1, 1, 1, 1]), // eslint-disable-line
"Aah0dUnhGFNWjAeqKEkB_SNLNpFf.xyz": Buffer.from([1, 1, 1, 1, 1, 1]), // Invalid file extension
"Aah0dUnhGFNWjAeqKEkB_SNLNpF-f": Buffer.from([1, 1, 1, 1, 1, 1]), // Invalid file name
".DS_Store": ``, // 'Safe' file
".fuse_hidden1234": ``, // 'Safe' file
},
},
expectedCount: 5,
Expand Down Expand Up @@ -437,10 +443,10 @@ describe(`Load state`, () => {
const emptyAlbumUUID = `cc40a239-2beb-483e-acee-e897db1b818a`;
const emptyAlbumName = `Stuff`;

const files: any = {};
for (const safeFileName of SAFE_FILES) {
files[safeFileName] = Buffer.from([1]);
}
const files: any = {
".DS_Store": Buffer.from([1]),
".fuse_hidden0016736000000ccc": Buffer.from([1]),
};

mockfs({
[Config.defaultConfig.dataDir]: {
Expand All @@ -467,10 +473,10 @@ describe(`Load state`, () => {
test(`Load nested state`, async () => {
const emptyAlbumUUID = `cc40a239-2beb-483e-acee-e897db1b818a`;
const emptyAlbumName = `Stuff`;
const files: any = {};
for (const safeFileName of SAFE_FILES) {
files[safeFileName] = Buffer.from([1]);
}
const files: any = {
".DS_Store": Buffer.from([1]),
".fuse_hidden0016736000000ccc": Buffer.from([1]),
};

const folderUUID = `cc40a239-2beb-483e-acee-e897db1b818b`;
const folderName = `Memories`;
Expand Down Expand Up @@ -1521,10 +1527,10 @@ describe(`Write state`, () => {
const albumUUID = `cc40a239-2beb-483e-acee-e897db1b818a`;
const albumName = `Memories`;

const files: any = {};
for (const safeFileName of SAFE_FILES) {
files[safeFileName] = Buffer.from([1]);
}
const files: any = {
".DS_Store": Buffer.from([1]),
".fuse_hidden0016736000000ccc": Buffer.from([1]),
};

mockfs({
[Config.defaultConfig.dataDir]: {
Expand Down

0 comments on commit 2634f0d

Please sign in to comment.