def generate_export_file(project, tasks, output_format, get_args): # prepare for saving now = datetime.now() data = json.dumps(tasks, ensure_ascii=False) md5 = hashlib.md5(json.dumps(data).encode('utf-8')).hexdigest() name = 'project-' + str(project.title) + '-at-' + now.strftime( '%Y-%m-%d-%H-%M') + f'-{md5[0:8]}' input_json = DataExport.save_export_files(project, now, get_args, data, md5, name) converter = Converter(config=project.get_parsed_config(), project_dir=None, upload_dir=os.path.join(settings.MEDIA_ROOT, settings.UPLOAD_DIR)) with get_temp_dir() as tmp_dir: converter.convert(input_json, tmp_dir, output_format, is_dir=False) files = get_all_files_from_dir(tmp_dir) # if only one file is exported - no need to create archive if len(files) == 1: output_file = files[0] ext = os.path.splitext(output_file)[-1] content_type = f'application/{ext}' out = read_bytes_stream(output_file) filename = name + os.path.splitext(output_file)[-1] return out, content_type, filename # otherwise pack output directory into archive shutil.make_archive(tmp_dir, 'zip', tmp_dir) out = read_bytes_stream(os.path.abspath(tmp_dir + '.zip')) content_type = 'application/zip' filename = name + '.zip' return out, content_type, filename
def convert_file(self, to): with get_temp_dir() as tmp_dir: converter = Converter( config=self.project.get_parsed_config(), project_dir=None, upload_dir=tmp_dir, # download_resources=download_resources, ) input_name = pathlib.Path(self.file.name).name input_file_path = pathlib.Path(tmp_dir) / input_name with open(input_file_path, 'wb') as out_file: out_file.write(self.file.open().read()) converter.convert(input_file_path, tmp_dir, to, is_dir=False) files = get_all_files_from_dir(tmp_dir) output_file = [ file_name for file_name in files if pathlib.Path(file_name).name != input_name ][0] out = read_bytes_stream(output_file) filename = pathlib.Path(input_name).stem + pathlib.Path( output_file).suffix return File( out, name=filename, )
def convert_file(self, to_format): with get_temp_dir() as tmp_dir: OUT = 'out' out_dir = pathlib.Path(tmp_dir) / OUT out_dir.mkdir(mode=0o700, parents=True, exist_ok=True) converter = Converter( config=self.project.get_parsed_config(), project_dir=None, upload_dir=out_dir, download_resources=False, ) input_name = pathlib.Path(self.file.name).name input_file_path = pathlib.Path(tmp_dir) / input_name with open(input_file_path, 'wb') as file_: file_.write(self.file.open().read()) converter.convert(input_file_path, out_dir, to_format, is_dir=False) files = get_all_files_from_dir(out_dir) dirs = get_all_dirs_from_dir(out_dir) if len(files) == 0 and len(dirs) == 0: return None elif len(files) == 1 and len(dirs) == 0: output_file = files[0] filename = pathlib.Path(input_name).stem + pathlib.Path(output_file).suffix else: shutil.make_archive(out_dir, 'zip', out_dir) output_file = pathlib.Path(tmp_dir) / (str(out_dir.stem) + '.zip') filename = pathlib.Path(input_name).stem + '.zip' out = read_bytes_stream(output_file) return File( out, name=filename, )