def _move_files_safe(self, src_web_path, dst_web_path): """ Move file in collection from path1 to path2. Also move associated thumbnail and preview file, and any files with same prefix in collection folder (mainly RAWS) """ src_file_phys_path = locations.collection_phys_path(src_web_path) dst_file_phys_path = locations.collection_phys_path(dst_web_path) # create list of modifications files will be moved in loop at the end of method renames = [(src_file_phys_path, dst_file_phys_path)] # query generators and add all generated miniatures to renames for generator in MINIATURE_GENERATORS: if generator.will_output_file(src_web_path): src_miniature_phys_path = generator.miniature_phys_path(src_file_phys_path) dst_miniature_phys_path = self._calculate_dst_path_after_move(src_web_path, dst_web_path, src_miniature_phys_path) renames.append((src_miniature_phys_path, dst_miniature_phys_path)) # add move of "other files in group" in collection folder self._add_similar_files_modifications(src_file_phys_path, dst_file_phys_path, renames) # move files in batch # TODO: rollback changes (move files back to their original positions) on error for (src, dst) in renames: move_without_overwriting(src, dst, # allow creating destination folders only in trash create_destination_dir=locations.web_path_in_trash(dst_web_path))
def _update_database_after_move(self, src_file_web_path, dst_file_web_path): try: media_file = File.objects.get( name=(os.path.basename(src_file_web_path)), directory__path=os.path.dirname(src_file_web_path)) # create parent directory objects dst_dir_web_path = os.path.dirname(dst_file_web_path) self._create_directories_in_chain(dst_dir_web_path) # if media_file is moved to trash save current timestamp as trash_time # otherwise unset trash_time move_to_trash = locations.web_path_in_trash(dst_file_web_path) if move_to_trash: now = localized_time(datetime.now()) trash_time = now else: trash_time = None # parent directory should exist new_directory = Directory.objects.get(path=dst_dir_web_path) # update directory modification times self._update_directory_mtime(new_directory) self._update_directory_mtime(media_file.directory) media_file.directory = new_directory media_file.trash_time = trash_time media_file.save() except (File.DoesNotExist, Directory.DoesNotExist): raise BadRequestException("Database object doesn't exist: (name={0})".format(src_file_web_path))
def post(self, request, *args, **kwargs): media_file = self.get_object() destination = self.request.data.get('destination', None) if destination is None: return Response({'reason': 'Required "destination" param is missing'}, status=status.HTTP_400_BAD_REQUEST) dst_folder_phys_path = locations.collection_phys_path(destination) move_to_trash = locations.web_path_in_trash(destination) # destination directory should exists unless media_file is moved to trash (directory tree should be created then) if not move_to_trash and not os.path.isdir(dst_folder_phys_path): return Response({'reason': 'Invalid destination directory'}, status=status.HTTP_404_NOT_FOUND) dst_file_web_path = normpath_join(destination, media_file.name) return self._handle_move_request(media_file.path, dst_file_web_path)