def user_image_resize(user, size): tmp = False if s3.exists(personal(path(user))): tmp = BytesIO(s3.get(personal(path(user)))['Body'].read()) elif s3.exists(original(path(user))): tmp = BytesIO(s3.get(original(path(user)))['Body'].read()) if tmp: image = Image.open(tmp) image.thumbnail((size, size), Image.ANTIALIAS) tmp = BytesIO() image.save(tmp, 'JPEG') tmp.seek(0) return Response(tmp, content_type='image/jpeg') else: return missing
def user_image(user): if request.method == 'GET': if s3.exists(personal(path(user))): obj = s3.get(personal(path(user))) return send_file(obj['Body'], mimetype=obj['ContentType']) elif s3.exists(original(path(user))): obj = s3.get(original(path(user))) return send_file(obj['Body'], mimetype=obj['ContentType']) else: return Response(missing, content_type='image/svg+xml') elif request.method == 'POST' and verify_token(request.args.get('token')): image = request.files['file'] mimetype = from_buffer(image.stream.read(1024), mime=True) s3.put(personal(path(user)), image, mimetype) return 'Personal image uploaded successfully' elif request.method == 'DELETE' and verify_token(request.args.get('token')): s3.delete(personal(path(user))) return 'Personal image deleted successfully' else: return 'You can only edit you own picture!'
def wrapper(*iargs, **ikwargs): pk = f'{func.__name__}-{hashMD5(*iargs)}' refs = findReferenceByPk(pk) bucket = 'hellovictorlee-cache' if refs: keys = [] for ref in refs: keys = keys + ref['keys'] # TODO: keys => batch get return s3.get(bucket=bucket, key=keys[0]) body = func(*iargs, **ikwargs) key = f'{func.__name__}/{uuid.uuid4().hex}.json' s3.put(body=body, bucket=bucket, key=key) dynamodb.put({'pk': pk, 'sk': f'{getUnix()}', 'keys': [key]}) return body
def download(bucket, prefix, output): segment = 0 while True: object = '%s.%03d' % (prefix, segment) # See if object exists r = s3.get(bucket, object) print >>stderr, 'trying', bucket, object if r.status != 200: r.close() return segment != 0 while True: buf = r.read(4096) if not buf: break assert write(output, buf) == len(buf) r.close() segment += 1
@app.route('/me') def me(): user = verify_token(request.args.get('token')) if user: return jsonify({ 'uid': user, 'personal': s3.exists(personal(path(user))) }) else: return jsonify({ 'uid': 'unknown', 'personal': False }) missing = s3.get('missing.svg')['Body'].read() @app.route('/user/<user>/image', methods=['GET', 'POST', 'DELETE']) def user_image(user): if request.method == 'GET': if s3.exists(personal(path(user))): obj = s3.get(personal(path(user))) return send_file(obj['Body'], mimetype=obj['ContentType']) elif s3.exists(original(path(user))): obj = s3.get(original(path(user))) return send_file(obj['Body'], mimetype=obj['ContentType']) else: return Response(missing, content_type='image/svg+xml') elif request.method == 'POST' and verify_token(request.args.get('token')): image = request.files['file']
def readback_meme(ts): return s3.get(MEME_BUCKET, ts)