def put(**kwargs): if 'namespace' in kwargs: kwargs = _namespace_kwargs(kwargs) stream, filename = request_content() if not stream: return StatusResponse("no post content", 400) cur = model.get_digest(stream) try: return PasteResponse(next(cur), "already exists", filename) except StopIteration: pass if filename: kwargs['mimetype'], _ = guess_type(filename) # this entirely replaces paste headers, which might not be expected behavior headers = dict(allowed_headers(request.headers)) # FIXME: such query; wow invalidate(**kwargs) result = model.put(stream, headers=headers, **kwargs) if result['n']: paste = next(model.get_meta(**kwargs)) return PasteResponse(paste, "updated") return StatusResponse("not found", 404)
def put(**kwargs): if 'namespace' in kwargs: kwargs = _namespace_kwargs(kwargs) stream, filename = request_content() if not stream: return StatusResponse("no post content", 400) cur = model.get_digest(stream) try: return PasteResponse(next(cur), "already exists", filename) except StopIteration: pass if filename: kwargs['mimetype'], _ = guess_type(filename) # FIXME: such query; wow invalidate(**kwargs) result = model.put(stream, **kwargs) if result['n']: paste = next(model.get_meta(**kwargs)) return PasteResponse(paste, "updated") return StatusResponse("not found", 404)
def url(label=None): stream, _ = request_content() if not stream: return StatusResponse("no post content", 400) args = {} if label: label, _ = label if len(label) == 1: return StatusResponse("invalid label", 400) args['label'] = label stream = BytesIO(stream.read().decode('utf-8').split()[0].encode('utf-8')) cur = model.get_digest(stream) try: url = next(cur) status = "already exists" except StopIteration: url = model.insert(stream, redirect=1, **args) status = "created" return PasteResponse(url, status)
def get(sid=None, sha1=None, label=None, namespace=None, lexer=None, handler=None, formatter=None): cur, name, path = _get_paste(model.get_content, sid, sha1, label, namespace) if not cur or not cur.count(): return StatusResponse("not found", 404) paste = next(cur) if paste.get('sunset'): request.max_age = parse_sunset(**paste) - datetime.utcnow() if request.max_age < timedelta(): uuid = UUID(hex=paste['_id']) paste = invalidate(uuid=uuid) model.delete(uuid=uuid) return PasteResponse(paste, "expired", code=410) else: request.max_age = request.max_age.seconds content = model._get(paste.get('content')) if paste.get('redirect'): content = content.decode('utf-8') return redirect(content, content) mimetype, _ = guess_type(name) if not mimetype: mimetype = paste.get('mimetype', 'text/plain') if lexer is not None: return highlight(content, lexer, formatter) if handler is not None: return _handler.get(handler, content, mimetype, path=path) return BaseResponse(content, mimetype=mimetype)
def report(sid=None, sha1=None, label=None, namespace=None): cur, name, path = _get_paste(model.get_meta, sid, sha1, label, namespace) if not cur or not cur.count(): return StatusResponse("not found", 404) paste = next(cur) return PasteResponse(paste, "found")
def delete(**kwargs): if 'namespace' in kwargs: kwargs = _namespace_kwargs(kwargs) paste = invalidate(**kwargs) result = model.delete(**kwargs) if result['n']: return PasteResponse(paste, "deleted") return StatusResponse("not found", 404)
def post(label=None, namespace=None): stream, filename = request_content() if not stream: return StatusResponse("no post content", 400) cur = model.get_digest(stream) args = {} if filename: args['mimetype'], _ = guess_type(filename) for key, value in request_keys('private', 'sunset'): try: if key == 'sunset': args[key] = parse_sunset(value) else: args[key] = int(value) except (ValueError, OverflowError): return StatusResponse({ "invalid request params": {key: value} }, 400) if label: label, _ = label if len(label) == 1: return StatusResponse("invalid label", 400) args['label'] = label if namespace: host = get_host_name(request) if not _auth_namespace(host): return StatusResponse("invalid auth", 403) label, _ = namespace args.update(dict( label=label, namespace=host )) headers = dict(allowed_headers(request.headers)) args['headers'] = headers try: paste = next(cur) uuid = None status = "already exists" except StopIteration: try: paste = model.insert(stream, **args) except errors.DuplicateKeyError: return StatusResponse("label already exists.", 409) invalidate(**paste) uuid = str(UUID(hex=paste['_id'])) status = "created" return PasteResponse(paste, status, filename, uuid)
def url(): stream, _ = request_content() if not stream: return StatusResponse("no post content", 400) stream = BytesIO(stream.read().decode('utf-8').split()[0].encode('utf-8')) cur = model.get_digest(stream) if not cur.count(): url = model.insert(stream, redirect=1) status = "created" else: url = next(cur) status = "already exists" return PasteResponse(url, status)
def report(sid=None, sha1=None, label=None, namespace=None): cur, name, path = _get_paste(model.get_meta, sid, sha1, label, namespace) paste = next(cur) return PasteResponse(paste, "found")