def upload(): """Upload file to S3.""" if "file" not in request.files: return jsonify(success=False, message="File key is missing."), 400 file_data = request.files["file"] filename = file_data.filename mime_type = file_data.mimetype _, extension = splitext(filename) if extension not in current_app.config["EDITOR_UPLOAD_ALLOWED_EXTENSIONS"]: return ( jsonify(success=False, message=f"File extension '{extension}' is not supported."), 400, ) key = hash_data(file_data.read()) bucket = current_app.config.get("S3_EDITOR_BUCKET") file_data.seek(0) current_s3_instance.upload_file(file_data, key, filename, mime_type, current_app.config["S3_FILE_ACL"], bucket) file_url = current_s3_instance.get_s3_url(key, bucket) return jsonify({"path": file_url}), 200
def generate_bibliography(): content_length = request.content_length if content_length is not None and content_length > 10 * 1024 * 1024: raise FileTooBigError requested_format = request.args.get("format") bytes_file = request.files["file"] if not allowed_file(bytes_file.filename): raise FileFormatNotSupportedError text_file = TextIOWrapper(bytes_file, errors="ignore") reference_names = get_references(text_file) references, errors = find_references(reference_names, requested_format) if not references: raise NoReferencesFoundError file_data = "\n\n".join(references).encode("utf-8") key = hash_data(file_data) file_data = BytesIO(file_data) filename = get_filename(bytes_file.filename, requested_format) mime_type = get_mimetype(requested_format) bucket = current_app.config.get("S3_BIBLIOGRAPHY_GENERATOR_BUCKET") current_s3_instance.upload_file(file_data, key, filename, mime_type, current_app.config["S3_FILE_ACL"], bucket) download_url = current_s3_instance.get_s3_url(key, bucket) formatted_errors = [] for error in errors: if error["type"] == "ambiguous": formatted_errors.append({ "message": f"Ambiguous reference to {error['ref']} on line {error['line']}" }) else: formatted_errors.append({ "message": f"Reference to {error['ref']} not found on line {error['line']}" }) response = { "data": { "download_url": download_url }, "errors": formatted_errors } return jsonify(response)