2020const path = require ( 'node:path' ) ;
2121const fs = require ( "fs" ) ;
2222const { app } = require ( "electron" ) ;
23+ const { platform } = require ( 'node:os' ) ;
2324
2425const OPTIONS_FILE_PATH = path . join ( app . getPath ( 'userData' ) , "config.json" ) ;
2526const OPTIONS_DEFAULT_FILE_PATH = path . join ( __dirname , ".." , ".." , "config-default.json" ) ;
@@ -72,7 +73,12 @@ function setOptions (options) {
7273}
7374
7475async function isAlreadyDownloaded ( e , title , onlyAudio , format , playlistName ) {
76+ if ( platform ( ) === "darwin" ) { title = sanitizeFilename ( title ) }
7577 const isPath = path . join ( onlyAudio ? app . getPath ( 'music' ) : app . getPath ( 'videos' ) , 'HypLoad' , sanitizeFolderName ( playlistName ) , `${ title } .${ format } ` ) ; ;
78+ console . log ( isPath ) ;
79+
80+ console . log ( fs . readdirSync ( path . join ( app . getPath ( 'music' ) , "HypLoad" , sanitizeFolderName ( playlistName ) ) ) ) ;
81+
7682 return new Promise ( ( resolve ) => {
7783 fs . access ( isPath , fs . constants . F_OK , ( err ) => {
7884 if ( err ) {
@@ -89,19 +95,39 @@ async function isAlreadyDownloaded(e, title, onlyAudio, format, playlistName) {
8995
9096function sanitizeFolderName ( name ) {
9197 console . log ( 'name' , name ) ;
92- const forbidden = / [ < > : " / \\ | ? * \x00 - \x1F ] / g;
93- const reserved = / ^ ( c o n | p r n | a u x | n u l | c o m [ 1 - 9 ] | l p t [ 1 - 9 ] ) $ / i;
94- let cleaned = name . replace ( forbidden , "_" ) . trim ( ) ;
95- cleaned = cleaned . replace ( / [ . ] + $ / g, "" ) ;
96- if ( reserved . test ( cleaned ) ) {
97- cleaned += "_playlist" ;
98- }
99- if ( ! cleaned . length ) {
100- cleaned = "playlist" ;
98+ if ( platform ( ) === "darwin" ) { return sanitizeFilename ( name ) }
99+ else {
100+ const forbidden = / [ < > : " / \\ | ? * \x00 - \x1F ] / g;
101+ const reserved = / ^ ( c o n | p r n | a u x | n u l | c o m [ 1 - 9 ] | l p t [ 1 - 9 ] ) $ / i;
102+ let cleaned = name . replace ( forbidden , "_" ) . trim ( ) ;
103+ cleaned = cleaned . replace ( / [ . ] + $ / g, "" ) ;
104+ if ( reserved . test ( cleaned ) ) {
105+ cleaned += "_playlist" ;
106+ }
107+ if ( ! cleaned . length ) {
108+ cleaned = "" ;
109+ }
110+ return cleaned ;
101111 }
102- return cleaned ;
103112}
104113
114+ function sanitizeFilename ( str ) {
115+ const map = {
116+ '/' : '/' ,
117+ '\\' : '\' ,
118+ ':' : ':' ,
119+ '*' : '*' ,
120+ '?' : '?' ,
121+ '"' : '"' ,
122+ '<' : '<' ,
123+ '>' : '>' ,
124+ '|' : '|'
125+ } ;
126+
127+ return str . replace ( / [ \/ \\ : \* \? " < > | ] / g, m => map [ m ] ) ;
128+ }
129+
130+
105131
106132
107133module . exports = { getOptions, setOptions, isAlreadyDownloaded, getJSONFile, saveJSONFile, sanitizeFolderName } ;
0 commit comments