def update_profile_image(): try: data = request.get_json()['data']['attributes'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if not data['image']: return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if not data['extension']: return ErrorResponse(ExtensionNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data['uid'] image = data['image'] extension = data['extension'] try: imageName = saveToImage(imageFile=image, extension=extension) except Exception: return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() fetch_user, imageLink = update_database(uid, imageName) return jsonify(UpdateUserSchema().dump(fetch_user).data)
def uploadImage(): try: data = request.get_json()['imgFile'] image = data['imgFile'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() extension = data['extension'] try: imageName = saveToImage(imageFile=image, extension=extension) except Exception: return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data['uid'] fetch_user = User.getUser(user_id=uid) if fetch_user is None: return ErrorResponse( UserNotFound(uid).message, 422, { 'Content-Type': 'application/json' }).respond() file_upload = File(filename=imageName, filetype='image', uploader=fetch_user) file_upload.save_to_db() return jsonify(ImageFileSchema().dump(file_upload).data)
def background_color(): try: data = request.get_json()['data']['attributes'] bg_color = data['bg_color'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, {'Content-Type': 'application/json'}).respond() bg_color = '#' + str(bg_color) img = Image.new('RGB', (744, 838), bg_color) buff = BytesIO() img.save(buff, format="JPEG") image_data = base64.b64encode(buff.getvalue()) try: imageName = saveToImage(imageFile=image_data.decode('utf-8'), extension=".png") except Exception: return ErrorResponse(ImageNotFound().message, 422, {'Content-Type': 'application/json'}).respond() uid = data['uid'] fetch_user = User.getUser(user_id=uid) if fetch_user is None: return ErrorResponse(UserNotFound(uid).message, 422, {'Content-Type': 'application/json'}).respond() file_upload = File(filename=imageName, filetype='image', uploader=fetch_user) file_upload.save_to_db() return jsonify(ColorImageSchema().dump(file_upload).data)
def upload_default(): try: data = request.get_json()['data']['attributes'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data.get('uid') image_name = data.get('defaultImage') + ".png" image_data = None with open( os.path.join(app.config.get('BASE_DIR'), 'badge_backgrounds', image_name), "rb") as image_file: image_data = base64.b64encode(image_file.read()) try: imageName = saveToImage(imageFile=image_data.decode('utf-8'), extension=".png") except Exception as e: print(e) return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() fetch_user = User.getUser(user_id=uid) file_upload = File(filename=imageName, filetype='image', uploader=fetch_user) file_upload.save_to_db() return jsonify(DefImageSchem().dump(file_upload).data)