librarian.js
// Build the media library in the database
Build the media library in the database
Build the media library in the database
async function parse(rootPath) {
  try { await fs.promises.access(rootPath) } catch {
    db.log(`[LIBRARIAN] Unable to access "${rootPath}", new library will not be parsed.`, 'error');
    return false;
  }
  const rootContents = await fs.promises.readdir(rootPath);
  db.log(`[LIBRARIAN] Loaded source directory "${rootPath}"`);
  let moviePath, musicPath, photoPath, bookPath;
  const fuzzyNames = {
    movie: ['movie', 'video'],
    music: ['music', 'audio', 'song'],
    books: ['book', 'documents', 'epub', 'pdf'],
    photo: ['photo', 'picture'] 
  }
  rootContents.forEach(file => {
    db.log(`[LIBRARIAN] -- ${file.isDirectory() ? '(directory) ' : ''}${path.join(rootPath, file)}`);
    if (file.isDirectory()) {
      const dirName = file.toLowerCase();      
      if (fuzzyNames.movie.some(fuzzyName => dirName.includes(fuzzyName)) && !moviePath) moviePath = path.join(rootPath, dirName);
      if (fuzzyNames.music.some(fuzzyName => dirName.includes(fuzzyName)) && !musicPath) musicPath = path.join(rootPath, dirName);
      if (fuzzyNames.books.some(fuzzyName => dirName.includes(fuzzyName)) && !bookPath ) bookPath  = path.join(rootPath, dirName);
      if (fuzzyNames.photo.some(fuzzyName => dirName.includes(fuzzyName)) && !photoPath) photoPath = path.join(rootPath, dirName);
    }
  });
  if (moviePath) db.log(`[LIBRARIAN] Assuming MOVIE directory is "${moviePath}"`);
  if (musicPath) db.log(`[LIBRARIAN] Assuming MUSIC directory is "${musicPath}"`);
  if (photoPath) db.log(`[LIBRARIAN] Assuming PHOTO directory is "${photoPath}"`);
  if (bookPath ) db.log(`[LIBRARIAN] Assuming BOOK directory is "${bookPath}"`);
  let mediaContents = {};
  if (moviePath) mediaContents['movie'] = await fs.promises.readdir(moviePath);
  if (musicPath) mediaContents['music'] = await fs.promises.readdir(musicPath);
  if (photoPath) mediaContents['photo'] = await fs.promises.readdir(photoPath);
  if (bookPath ) mediaContents['book']  = await fs.promises.readdir(bookPath);
  for (const [mediaType, mediaDirectory] of Object.entries(mediaContents)) {
    mediaDirectory.forEach(item => {
      if (item.isDirectory()) {
      } else {
      }
    })
  }
}
async function parse ( rootPath ) { try { await fs . promises . access ( rootPath ) } catch { db . log ( ` ${ rootPath } ` , 'error' ) ; return false ; } const rootContents = await fs . promises . readdir ( rootPath ) ; db . log ( ` ${ rootPath } ` ) ; let moviePath , musicPath , photoPath , bookPath ; const fuzzyNames = { movie : [ 'movie' , 'video' ] , music : [ 'music' , 'audio' , 'song' ] , books : [ 'book' , 'documents' , 'epub' , 'pdf' ] , photo : [ 'photo' , 'picture' ] } rootContents . forEach ( file => { db . log ( ` ${ file . isDirectory ( ) ? '(directory) ' : '' } ${ path . join ( rootPath , file ) } ` ) ; if ( file . isDirectory ( ) ) { const dirName = file . toLowerCase ( ) ; if ( fuzzyNames . movie . some ( fuzzyName => dirName . includes ( fuzzyName ) ) && ! moviePath ) moviePath = path . join ( rootPath , dirName ) ; if ( fuzzyNames . music . some ( fuzzyName => dirName . includes ( fuzzyName ) ) && ! musicPath ) musicPath = path . join ( rootPath , dirName ) ; if ( fuzzyNames . books . some ( fuzzyName => dirName . includes ( fuzzyName ) ) && ! bookPath ) bookPath = path . join ( rootPath , dirName ) ; if ( fuzzyNames . photo . some ( fuzzyName => dirName . includes ( fuzzyName ) ) && ! photoPath ) photoPath = path . join ( rootPath , dirName ) ; } } ) ; if ( moviePath ) db . log ( ` ${ moviePath } ` ) ; if ( musicPath ) db . log ( ` ${ musicPath } ` ) ; if ( photoPath ) db . log ( ` ${ photoPath } ` ) ; if ( bookPath ) db . log ( ` ${ bookPath } ` ) ; let mediaContents = { } ; if ( moviePath ) mediaContents [ 'movie' ] = await fs . promises . readdir ( moviePath ) ; if ( musicPath ) mediaContents [ 'music' ] = await fs . promises . readdir ( musicPath ) ; if ( photoPath ) mediaContents [ 'photo' ] = await fs . promises . readdir ( photoPath ) ; if ( bookPath ) mediaContents [ 'book' ] = await fs . promises . readdir ( bookPath ) ; for ( const [ mediaType , mediaDirectory ] of Object . entries ( mediaContents ) ) { mediaDirectory . forEach ( item => { if ( item . isDirectory ( ) ) { } else { } } ) } }
Build the media library in the database
Build the media library in the database
// Try to access the root path passed to the function // If the fs library can not access the path, do nothing and display the error // Loaded file path // Read in everything in that directory // Identify where different types of media may be stored // Words that may be conducive to different folders and types of media // For the contents in the root directory // Log what we see in the directory // Assign the paths based on the contents of the directory name // Notify of decisions // Read in the contents of the directories // Traverse the contents of each of the media directories // If it is a directory (usually there will be metadata and supporting imagery in the directory) // If it is a singular file