mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Fix:Save metadata files when updating library items #1952
This commit is contained in:
		
							parent
							
								
									22323f606d
								
							
						
					
					
						commit
						80aea0c82d
					
				| @ -405,12 +405,14 @@ class Database { | |||||||
| 
 | 
 | ||||||
|   async createLibraryItem(oldLibraryItem) { |   async createLibraryItem(oldLibraryItem) { | ||||||
|     if (!this.sequelize) return false |     if (!this.sequelize) return false | ||||||
|  |     await oldLibraryItem.saveMetadata() | ||||||
|     await this.models.libraryItem.fullCreateFromOld(oldLibraryItem) |     await this.models.libraryItem.fullCreateFromOld(oldLibraryItem) | ||||||
|     this.libraryItems.push(oldLibraryItem) |     this.libraryItems.push(oldLibraryItem) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   updateLibraryItem(oldLibraryItem) { |   async updateLibraryItem(oldLibraryItem) { | ||||||
|     if (!this.sequelize) return false |     if (!this.sequelize) return false | ||||||
|  |     await oldLibraryItem.saveMetadata() | ||||||
|     return this.models.libraryItem.fullUpdateFromOld(oldLibraryItem) |     return this.models.libraryItem.fullUpdateFromOld(oldLibraryItem) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -418,8 +420,11 @@ class Database { | |||||||
|     if (!this.sequelize) return false |     if (!this.sequelize) return false | ||||||
|     let updatesMade = 0 |     let updatesMade = 0 | ||||||
|     for (const oldLibraryItem of oldLibraryItems) { |     for (const oldLibraryItem of oldLibraryItems) { | ||||||
|  |       await oldLibraryItem.saveMetadata() | ||||||
|       const hasUpdates = await this.models.libraryItem.fullUpdateFromOld(oldLibraryItem) |       const hasUpdates = await this.models.libraryItem.fullUpdateFromOld(oldLibraryItem) | ||||||
|       if (hasUpdates) updatesMade++ |       if (hasUpdates) { | ||||||
|  |         updatesMade++ | ||||||
|  |       } | ||||||
|     } |     } | ||||||
|     return updatesMade |     return updatesMade | ||||||
|   } |   } | ||||||
| @ -427,6 +432,7 @@ class Database { | |||||||
|   async createBulkLibraryItems(oldLibraryItems) { |   async createBulkLibraryItems(oldLibraryItems) { | ||||||
|     if (!this.sequelize) return false |     if (!this.sequelize) return false | ||||||
|     for (const oldLibraryItem of oldLibraryItems) { |     for (const oldLibraryItem of oldLibraryItems) { | ||||||
|  |       await oldLibraryItem.saveMetadata() | ||||||
|       await this.models.libraryItem.fullCreateFromOld(oldLibraryItem) |       await this.models.libraryItem.fullCreateFromOld(oldLibraryItem) | ||||||
|       this.libraryItems.push(oldLibraryItem) |       this.libraryItems.push(oldLibraryItem) | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -105,7 +105,6 @@ class Server { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     await this.cleanUserData() // Remove invalid user item progress
 |     await this.cleanUserData() // Remove invalid user item progress
 | ||||||
|     await this.purgeMetadata() // Remove metadata folders without library item
 |  | ||||||
|     await this.cacheManager.ensureCachePaths() |     await this.cacheManager.ensureCachePaths() | ||||||
| 
 | 
 | ||||||
|     await this.backupManager.init() |     await this.backupManager.init() | ||||||
| @ -243,36 +242,6 @@ class Server { | |||||||
|     await this.scanner.scanFilesChanged(fileUpdates) |     await this.scanner.scanFilesChanged(fileUpdates) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   // Remove unused /metadata/items/{id} folders
 |  | ||||||
|   async purgeMetadata() { |  | ||||||
|     const itemsMetadata = Path.join(global.MetadataPath, 'items') |  | ||||||
|     if (!(await fs.pathExists(itemsMetadata))) return |  | ||||||
|     const foldersInItemsMetadata = await fs.readdir(itemsMetadata) |  | ||||||
| 
 |  | ||||||
|     let purged = 0 |  | ||||||
|     await Promise.all(foldersInItemsMetadata.map(async foldername => { |  | ||||||
|       const itemFullPath = fileUtils.filePathToPOSIX(Path.join(itemsMetadata, foldername)) |  | ||||||
| 
 |  | ||||||
|       const hasMatchingItem = Database.libraryItems.find(li => { |  | ||||||
|         if (!li.media.coverPath) return false |  | ||||||
|         return itemFullPath === fileUtils.filePathToPOSIX(Path.dirname(li.media.coverPath)) |  | ||||||
|       }) |  | ||||||
|       if (!hasMatchingItem) { |  | ||||||
|         Logger.debug(`[Server] Purging unused metadata ${itemFullPath}`) |  | ||||||
| 
 |  | ||||||
|         await fs.remove(itemFullPath).then(() => { |  | ||||||
|           purged++ |  | ||||||
|         }).catch((err) => { |  | ||||||
|           Logger.error(`[Server] Failed to delete folder path ${itemFullPath}`, err) |  | ||||||
|         }) |  | ||||||
|       } |  | ||||||
|     })) |  | ||||||
|     if (purged > 0) { |  | ||||||
|       Logger.info(`[Server] Purged ${purged} unused library item metadata`) |  | ||||||
|     } |  | ||||||
|     return purged |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   // Remove user media progress with items that no longer exist & remove seriesHideFrom that no longer exist
 |   // Remove user media progress with items that no longer exist & remove seriesHideFrom that no longer exist
 | ||||||
|   async cleanUserData() { |   async cleanUserData() { | ||||||
|     for (const _user of Database.users) { |     for (const _user of Database.users) { | ||||||
|  | |||||||
| @ -523,7 +523,10 @@ class LibraryItem { | |||||||
|     return this.media.getDirectPlayTracklist(episodeId) |     return this.media.getDirectPlayTracklist(episodeId) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   // Saves metadata.abs file
 |   /** | ||||||
|  |    * Save metadata.json/metadata.abs file | ||||||
|  |    * @returns {boolean} true if saved | ||||||
|  |    */ | ||||||
|   async saveMetadata() { |   async saveMetadata() { | ||||||
|     if (this.mediaType === 'video' || this.mediaType === 'music') return |     if (this.mediaType === 'video' || this.mediaType === 'music') return | ||||||
| 
 | 
 | ||||||
| @ -556,6 +559,7 @@ class LibraryItem { | |||||||
|           await newLibraryFile.setDataFromPath(metadataFilePath, `metadata.json`) |           await newLibraryFile.setDataFromPath(metadataFilePath, `metadata.json`) | ||||||
|           this.libraryFiles.push(newLibraryFile) |           this.libraryFiles.push(newLibraryFile) | ||||||
|         } |         } | ||||||
|  |         Logger.debug(`[LibraryItem] Success saving abmetadata to "${metadataFilePath}"`) | ||||||
| 
 | 
 | ||||||
|         return true |         return true | ||||||
|       }).catch((error) => { |       }).catch((error) => { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user