def _process_files(self, fs_filenames, root_object, root_phys_path): db_files = root_object.files.all() for file_object in db_files: if file_object.name not in fs_filenames: # if any of images under root doesn't exist -> remove it from db logger.info("scheduling file removal: " + file_object.path) self._removals.append(file_object) else: # update mtime if neeeded self._update_mtime_if_needed(file_object) # add file objects if not found on db db_filenames = {x.name for x in db_files} for missing_file in set(fs_filenames) - db_filenames: file_phys_path = os.path.join(root_phys_path, missing_file) file_mtime = get_mtime_datetime(file_phys_path) if is_jpeg(missing_file): aspect_ratio = self.get_image_aspect_ratio(file_phys_path) raw_filename = self._detect_rawfile(file_phys_path) file_object = Image(name=missing_file, directory=root_object, modification_time=file_mtime, aspect_ratio=aspect_ratio, raw_filename=raw_filename) elif is_video(missing_file): file_object = Video(name=missing_file, directory=root_object, modification_time=file_mtime) else: raise Exception("File should be either Image or Video" + missing_file) file_object.save() logger.info("adding file " + file_object.path)
def synchronize_db_with_collection(self, root_phys_path, dirs, files): # only index files that have correct name - it will be changed anyway during next Runner loop files = sorted([f for f in files if is_jpeg(f) and Renamer.CORRECT_FILENAME_RE.match(f) or is_video(f)]) # find directory object corresponding to root -> create if needed root_web_path = locations.collection_web_path(root_phys_path) root_object = find_or_create_directory(root_web_path) self._process_directories(dirs, root_object, root_web_path) self._process_files(files, root_object, root_phys_path)
def will_output_file(self, f): return is_video(f)