Beispiel #1
0
    def purge_unused(self, note, purge_all_links=False):
        """
    Delete files that were linked from the given note but no longer are.

    @type note: model.Note
    @param note: note to search for file links
    @type purge_all_links: bool
    @param purge_all_links: if True, delete all files that are/were linked from this note
    """
        # load metadata for all files with the given note's note_id
        files = self.__database.select_many(
            File, File.sql_load_note_files(note.object_id))
        files_to_delete = dict([(db_file.object_id, db_file)
                                for db_file in files])

        # search through the note's contents for current links to files
        if purge_all_links is False:
            for match in self.FILE_LINK_PATTERN.finditer(note.contents):
                file_id = match.groups(0)[0]

                # we've found a link for file_id, so don't delete that file
                files_to_delete.pop(file_id, None)

        # for each file to delete, delete its metadata from the database and its data from the
        # filesystem
        for (file_id, db_file) in files_to_delete.items():
            self.__database.execute(db_file.sql_delete(), commit=False)
            self.__database.uncache(db_file)
            Upload_file.delete_file(file_id)

        self.__database.commit()
Beispiel #2
0
  def purge_unused( self, note, purge_all_links = False ):
    """
    Delete files that were linked from the given note but no longer are.

    @type note: model.Note
    @param note: note to search for file links
    @type purge_all_links: bool
    @param purge_all_links: if True, delete all files that are/were linked from this note
    """
    # load metadata for all files with the given note's note_id 
    files = self.__database.select_many( File, File.sql_load_note_files( note.object_id ) )
    files_to_delete = dict( [ ( db_file.object_id, db_file ) for db_file in files ] )

    # search through the note's contents for current links to files
    if purge_all_links is False:
      for match in self.FILE_LINK_PATTERN.finditer( note.contents ):
        file_id = match.groups( 0 )[ 0 ]

        # we've found a link for file_id, so don't delete that file
        files_to_delete.pop( file_id, None )

    # for each file to delete, delete its metadata from the database and its data from the
    # filesystem
    for ( file_id, db_file ) in files_to_delete.items():
      self.__database.execute( db_file.sql_delete(), commit = False )
      self.__database.uncache( db_file )
      Upload_file.delete_file( file_id )

    self.__database.commit()