mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 19:17:13 -05:00 
			
		
		
		
	Add: more error handling
This commit is contained in:
		
							parent
							
								
									001e40d633
								
							
						
					
					
						commit
						d43b6a924d
					
				@ -306,13 +306,20 @@ class Document(models.Model):
 | 
				
			|||||||
        elif not os.path.isfile(Document.filename_to_path(self.filename)):
 | 
					        elif not os.path.isfile(Document.filename_to_path(self.filename)):
 | 
				
			||||||
            recovered_filename = self.find_renamed_document()
 | 
					            recovered_filename = self.find_renamed_document()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # If we have found the file, save filename and clean up empty dirs
 | 
					            # If we have found the file so update the filename
 | 
				
			||||||
            if recovered_filename is not None:
 | 
					            if recovered_filename is not None:
 | 
				
			||||||
 | 
					                logger = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					                logger.warning("Filename of document " + str(self.id) +
 | 
				
			||||||
 | 
					                               " has changed and was successfully updated")
 | 
				
			||||||
                self.filename = recovered_filename
 | 
					                self.filename = recovered_filename
 | 
				
			||||||
                self.save()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                # Remove all empty subdirectories from MEDIA_ROOT
 | 
				
			||||||
                Document.delete_all_empty_subdirectories(
 | 
					                Document.delete_all_empty_subdirectories(
 | 
				
			||||||
                        Document.filename_to_path(""))
 | 
					                        Document.filename_to_path(""))
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                logger = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					                logger.error("File of document " + str(self.id) + " has " +
 | 
				
			||||||
 | 
					                             "gone and could not be recovered")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self.filename
 | 
					        return self.filename
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -477,6 +484,10 @@ def update_filename(sender, instance, **kwargs):
 | 
				
			|||||||
    if instance.filename is None:
 | 
					    if instance.filename is None:
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Check is file exists and update filename otherwise
 | 
				
			||||||
 | 
					    if not os.path.isfile(Document.filename_to_path(instance.filename)):
 | 
				
			||||||
 | 
					        instance.filename = instance.source_filename
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Build the new filename
 | 
					    # Build the new filename
 | 
				
			||||||
    new_filename = instance.generate_source_filename()
 | 
					    new_filename = instance.generate_source_filename()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -500,6 +511,11 @@ def update_filename(sender, instance, **kwargs):
 | 
				
			|||||||
    except PermissionError:
 | 
					    except PermissionError:
 | 
				
			||||||
        # Do not update filename in object
 | 
					        # Do not update filename in object
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					    except FileNotFoundError:
 | 
				
			||||||
 | 
					        logger = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					        logger.error("Renaming of document " + str(instance.id) + " failed " +
 | 
				
			||||||
 | 
					                     "as file " + instance.filename + " was no longer present")
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Delete empty directory
 | 
					    # Delete empty directory
 | 
				
			||||||
    old_dir = os.path.dirname(instance.filename)
 | 
					    old_dir = os.path.dirname(instance.filename)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user