def upload_objects(objects): links = sorted(obj.url for obj in objects) for obj in objects: if isinstance(obj, HtmlToStore): get_backend().store_html(obj, links) else: get_backend().store_object(obj, links)
def upload_objects(objects, metadata_url=None): # TODO: make metadata_url mandatory (need to support it for uploads too) links = sorted(obj.url for obj in objects) for obj in objects: if isinstance(obj, HtmlToStore): get_backend().store_html(obj, links, metadata_url) else: get_backend().store_object(obj, links, metadata_url)
def paste(): """Paste and redirect.""" text = request.form['text'] # TODO: make this better assert 0 <= len(text) <= ONE_MB, len(text) with UploadedFile.from_text(text) as uf: get_backend().store_object(uf) lang = request.form['language'] if lang != 'rendered-markdown': with HtmlToStore.from_html( render_template( 'paste.html', text=text, highlighter=get_highlighter(text, lang), raw_url=app.config['FILE_URL'].format(name=uf.name), )) as paste_obj: get_backend().store_html(paste_obj) else: with HtmlToStore.from_html( render_template( 'markdown.html', text=text, raw_url=app.config['FILE_URL'].format(name=uf.name), )) as paste_obj: get_backend().store_html(paste_obj) url = app.config['HTML_URL'].format(name=paste_obj.name) return redirect(url)
def upload(): """Process an upload and return JSON status.""" uploaded_files = [] for f in request.files.getlist('file'): try: with UploadedFile.from_http_file(f) as uf: get_backend().store_object(uf) # If it looks like text, make a pastebin as well. pb = None if uf.num_bytes < ONE_MB and not uf.probably_binary: # We can't know for sure it's utf8, so this might fail. # If so, we just won't make a pastebin for this file. try: text = uf.full_content.decode('utf8') except UnicodeDecodeError: pass else: with HtmlToStore.from_html( render_template( 'paste.html', text=text, highlighter=get_highlighter(text, None), raw_url=app.config['FILE_URL'].format( name=uf.name), )) as pb: get_backend().store_html(pb) uploaded_files.append((uf, pb)) except FileTooLargeError: return jsonify({ 'success': False, 'error': '{} exceeded the maximum file size limit of {}'.format( f.filename, human_size(app.config['MAX_UPLOAD_SIZE']), ), }) with HtmlToStore.from_html( render_template( 'details.html', uploads=uploaded_files, )) as details_obj: get_backend().store_html(details_obj) url = app.config['HTML_URL'].format(name=details_obj.name) if 'json' in request.args: return jsonify({ 'success': True, 'redirect': url, }) else: return redirect(url)