mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Merge pull request #4249 from advplyr/watcher_rescans_update
Update watcher to re-scan library items for non-media file only updates
This commit is contained in:
		
						commit
						2706a9c4aa
					
				@ -246,7 +246,6 @@ class LibraryItem extends Model {
 | 
				
			|||||||
      include
 | 
					      include
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    if (!libraryItem) {
 | 
					    if (!libraryItem) {
 | 
				
			||||||
      Logger.error(`[LibraryItem] Library item not found`)
 | 
					 | 
				
			||||||
      return null
 | 
					      return null
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -407,7 +407,7 @@ class LibraryScanner {
 | 
				
			|||||||
      const folder = library.libraryFolders[0]
 | 
					      const folder = library.libraryFolders[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      const filePathItems = folderGroups[folderId].fileUpdates.map((fileUpdate) => fileUtils.getFilePathItemFromFileUpdate(fileUpdate))
 | 
					      const filePathItems = folderGroups[folderId].fileUpdates.map((fileUpdate) => fileUtils.getFilePathItemFromFileUpdate(fileUpdate))
 | 
				
			||||||
      const fileUpdateGroup = scanUtils.groupFileItemsIntoLibraryItemDirs(library.mediaType, filePathItems, !!library.settings?.audiobooksOnly)
 | 
					      const fileUpdateGroup = scanUtils.groupFileItemsIntoLibraryItemDirs(library.mediaType, filePathItems, !!library.settings?.audiobooksOnly, true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (!Object.keys(fileUpdateGroup).length) {
 | 
					      if (!Object.keys(fileUpdateGroup).length) {
 | 
				
			||||||
        Logger.info(`[LibraryScanner] No important changes to scan for in folder "${folderId}"`)
 | 
					        Logger.info(`[LibraryScanner] No important changes to scan for in folder "${folderId}"`)
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,12 @@ function isMediaFile(mediaType, ext, audiobooksOnly = false) {
 | 
				
			|||||||
  return globals.SupportedAudioTypes.includes(extclean) || globals.SupportedEbookTypes.includes(extclean)
 | 
					  return globals.SupportedAudioTypes.includes(extclean) || globals.SupportedEbookTypes.includes(extclean)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function isScannableNonMediaFile(ext) {
 | 
				
			||||||
 | 
					  if (!ext) return false
 | 
				
			||||||
 | 
					  const extclean = ext.slice(1).toLowerCase()
 | 
				
			||||||
 | 
					  return globals.TextFileTypes.includes(extclean) || globals.MetadataFileTypes.includes(extclean) || globals.SupportedImageTypes.includes(extclean)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function checkFilepathIsAudioFile(filepath) {
 | 
					function checkFilepathIsAudioFile(filepath) {
 | 
				
			||||||
  const ext = Path.extname(filepath)
 | 
					  const ext = Path.extname(filepath)
 | 
				
			||||||
  if (!ext) return false
 | 
					  if (!ext) return false
 | 
				
			||||||
@ -35,27 +41,31 @@ module.exports.checkFilepathIsAudioFile = checkFilepathIsAudioFile
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * @param {string} mediaType
 | 
					 * @param {string} mediaType
 | 
				
			||||||
 * @param {import('./fileUtils').FilePathItem[]} fileItems
 | 
					 * @param {import('./fileUtils').FilePathItem[]} fileItems
 | 
				
			||||||
 * @param {boolean} [audiobooksOnly=false]
 | 
					 * @param {boolean} audiobooksOnly
 | 
				
			||||||
 | 
					 * @param {boolean} [includeNonMediaFiles=false] - Used by the watcher to re-scan when covers/metadata files are added/removed
 | 
				
			||||||
 * @returns {Record<string,string[]>} map of files grouped into potential libarary item dirs
 | 
					 * @returns {Record<string,string[]>} map of files grouped into potential libarary item dirs
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function groupFileItemsIntoLibraryItemDirs(mediaType, fileItems, audiobooksOnly = false) {
 | 
					function groupFileItemsIntoLibraryItemDirs(mediaType, fileItems, audiobooksOnly, includeNonMediaFiles = false) {
 | 
				
			||||||
  // Step 1: Filter out non-book-media files in root dir (with depth of 0)
 | 
					  // Step 1: Filter out non-book-media files in root dir (with depth of 0)
 | 
				
			||||||
  const itemsFiltered = fileItems.filter((i) => {
 | 
					  const itemsFiltered = fileItems.filter((i) => {
 | 
				
			||||||
    return i.deep > 0 || (mediaType === 'book' && isMediaFile(mediaType, i.extension, audiobooksOnly))
 | 
					    return i.deep > 0 || (mediaType === 'book' && isMediaFile(mediaType, i.extension, audiobooksOnly))
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Step 2: Separate media files and other files
 | 
					  // Step 2: Separate media files and other files
 | 
				
			||||||
  //     - Directories without a media file will not be included
 | 
					  //     - Directories without a media file will not be included (unless includeNonMediaFiles is true)
 | 
				
			||||||
  /** @type {import('./fileUtils').FilePathItem[]} */
 | 
					  /** @type {import('./fileUtils').FilePathItem[]} */
 | 
				
			||||||
  const mediaFileItems = []
 | 
					  const mediaFileItems = []
 | 
				
			||||||
  /** @type {import('./fileUtils').FilePathItem[]} */
 | 
					  /** @type {import('./fileUtils').FilePathItem[]} */
 | 
				
			||||||
  const otherFileItems = []
 | 
					  const otherFileItems = []
 | 
				
			||||||
  itemsFiltered.forEach((item) => {
 | 
					  itemsFiltered.forEach((item) => {
 | 
				
			||||||
    if (isMediaFile(mediaType, item.extension, audiobooksOnly)) mediaFileItems.push(item)
 | 
					    if (isMediaFile(mediaType, item.extension, audiobooksOnly) || (includeNonMediaFiles && isScannableNonMediaFile(item.extension))) {
 | 
				
			||||||
    else otherFileItems.push(item)
 | 
					      mediaFileItems.push(item)
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      otherFileItems.push(item)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Step 3: Group audio files in library items
 | 
					  // Step 3: Group media files (or non-media files if includeNonMediaFiles is true) in library items
 | 
				
			||||||
  const libraryItemGroup = {}
 | 
					  const libraryItemGroup = {}
 | 
				
			||||||
  mediaFileItems.forEach((item) => {
 | 
					  mediaFileItems.forEach((item) => {
 | 
				
			||||||
    const dirparts = item.reldirpath.split('/').filter((p) => !!p)
 | 
					    const dirparts = item.reldirpath.split('/').filter((p) => !!p)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user