Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { encrypt } from './utils/safeStorage';
import { LastFMSessionData } from '../@types/last_fm_api';
import { DEFAULT_SONG_PALETTE } from './other/generatePalette';
import isPathADir from './utils/isPathADir';

export const DEFAULT_ARTWORK_SAVE_LOCATION = path.join(app.getPath('userData'), 'song_covers');
export const DEFAULT_FILE_URL = 'nora://localfiles/';
Expand Down Expand Up @@ -569,7 +570,7 @@ function flattenPathArrays<Type extends string[][]>(lists: Type) {
export const getDirectories = async (srcpath: string) => {
try {
const dirs = await fs.readdir(srcpath, { withFileTypes: true });
const filteredDirs = dirs.filter((dir) => dir.isDirectory());
const filteredDirs = dirs.filter((dir) => isPathADir(dir));
const dirsWithFullPaths = filteredDirs.map((dir) => path.join(srcpath, dir.name));

return dirsWithFullPaths;
Expand Down
3 changes: 2 additions & 1 deletion src/main/utils/copyDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs/promises';

import log from '../log';
import makeDir from './makeDir';
import isPathADir from './isPathADir';

async function copyDir(src: string, dest: string) {
try {
Expand All @@ -16,7 +17,7 @@ async function copyDir(src: string, dest: string) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);

if (entry.isDirectory()) await copyDir(srcPath, destPath);
if (isPathADir(entry)) await copyDir(srcPath, destPath);
else await fs.copyFile(srcPath, destPath);
}
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/utils/getDirSize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs/promises';
import path from 'path';
import { isAnErrorWithCode } from './isAnErrorWithCode';
import isPathADir from './isPathADir';

const getDirSize = async (dir: string) => {
try {
Expand All @@ -10,7 +11,7 @@ const getDirSize = async (dir: string) => {
try {
const filepath = path.join(dir, file.name);

if (file.isDirectory()) return getDirSize(filepath);
if (isPathADir(file)) return getDirSize(filepath);
if (file.isFile()) {
const { size } = await fs.stat(filepath);
return size;
Expand Down
35 changes: 35 additions & 0 deletions src/main/utils/isPathADir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import path from 'path';
import fs, { Dirent } from 'fs';

const isPathADir = (pathOrDir: string | Dirent) => {
try {
if (typeof pathOrDir === 'string') {
const stat = fs.statSync(pathOrDir);

if (stat.isDirectory()) return true;

if (!stat.isSymbolicLink()) return false;

const symlinkTarget = fs.readlinkSync(pathOrDir);

const symlinkStat = fs.statSync(symlinkTarget);

return symlinkStat.isDirectory();
}

if (pathOrDir.isDirectory()) return true;

if (!pathOrDir.isSymbolicLink()) return false;

const symlinkPath = path.join(pathOrDir.path, pathOrDir.name);

const symlinkStat = fs.statSync(symlinkPath);

return symlinkStat.isDirectory();
} catch {
return false;
}
};

export default isPathADir;