from os import path,rename from BitMover_ui import MainWindow from _file_stuff import create_folder, path_exists, cmp_files from _hashing import xx_hash class MediaImporter(MainWindow): def __init__(self,*args,**kwargs): super(MediaImporter,self).__init__(*args,**kwargs) self.chunk_size = 16 * 1024 self.path_file_source = None self.path_file_destination = None self.destination_original_path = None self.path_file_destination_original = None self.path_file_destination = None def is_video(self,f): if self.files[f]['type'] == 'video': r = True else: r = False return r def is_image(self,f): if self.files[f]['type'] == 'image': r = True else: r = False return r def t_copy_files(self, import_progress_callback, current_file_progress_callback, imported_file_count_callback ): """ Copy Files. """ count = int(0) for file in self.files: self.src_dir = self.files[file]['folders']['source_path'] self.dst_dir = self.files[file]['folders']['destination'] self.path_file_source = path.join(self.src_dir, self.files[file]['name']) self.path_file_destination = path.join(self.dst_dir, self.files[file]['name']) self.destination_original_path = path.join(self.dst_dir, self.files[file]['folders']['destination_original']) self.path_file_destination_original = path.join(self.dst_dir, self.files[file]['folders']['destination_original'], self.files[file]['name']) self.imp_dialog.set_importing_file(self.path_file_source) self.copy_a_file( file, current_file_progress_callback) if self.check_store_original(file) is True: self.copy_a_file(file, current_file_progress_callback) count += 1 imported_file_count_callback.emit(count) import_progress_callback.emit(round((count / self.file_total) * 100, 1)) self.imp_dialog.add_to_imported_list(self.path_file_source) def copy_a_file(self, _f, current_file_progress_callback): size = path.getsize(self.path_file_source) create_folder(self.dst_dir) self.check_duplicate(_f) if self.is_video(_f): self.chunk_size = (1024 * 1024) * 5 else: self.chunk_size = (16 * 1024) * 1 with open(self.path_file_source, 'rb') as fs: with open(self.path_file_destination, 'wb') as fd: while True: chunk = fs.read(self.chunk_size) if not chunk: break fd.write(chunk) dst_size = path.getsize(self.path_file_destination) current_file_progress_callback.emit(round((dst_size / size) * 100, 1)) def check_store_original(self,_f): if self.config['store_originals'] is True: if self.is_image(_f): self.dst_dir = self.destination_original_path self.path_file_destination = self.path_file_destination_original r = True else: r = False else: r = False return r def check_duplicate(self,_f): if path_exists(self.path_file_destination): check_match = cmp_files(self.path_file_source, self.path_file_destination) if check_match is False: print(f'\nFound duplicate for {self.path_file_source}, renaming destination with hash appended.') base, extension = path.splitext(self.files[_f]['name']) f_xxhash = xx_hash(self.path_file_destination) file_name_hash = base + '_' + f_xxhash + extension rename(self.path_file_destination, path.join(self.dst_dir, file_name_hash))