def index(): envelope = request.get_json() if not envelope: msg = 'no Pub/Sub message received' print(f'error: {msg}') return f'Bad Request: {msg}', 410 if not isinstance(envelope, dict) or 'message' not in envelope: msg = 'invalid Pub/Sub message format' print(f'error: {msg}') return f'Bad Request: {msg}', 411 pubsub_message = envelope['message'] name = 'World' if isinstance(pubsub_message, dict) and 'data' in pubsub_message: print(base64.b64decode(pubsub_message['data']).decode('utf-8')) try: data = json.loads( base64.b64decode(pubsub_message['data']).decode('utf-8')) except Exception as e: msg = ('Invalid Pub/Sub message: ' 'data property is not valid base64 encoded JSON') print(f'error: {e}') return f'Bad Request: {msg}', 412 # Validate the message is a Cloud Storage event. if not data["name"] or not data["data"]: msg = (data) print(f'error: {msg}') return f'Bad Request: {msg}', 413 file_name = "test.jpg" bucket_name = "dynartwork-277815.appspot.com" blob = storage_client.bucket(bucket_name).get_blob(file_name) print(data) try: image.blur_offensive_images(data) image.build_image(data) # Flush the stdout to avoid log buffering. sys.stdout.flush() return ('', 204) except Exception as e: print(f'error: {e}') return ('', 500) # Flush the stdout to avoid log buffering. sys.stdout.flush() return ('', 414)
def test_process_offensive_image(storage_client, vision_client, __blur_image, capsys): result = UserDict() result.safe_search_annotation = UserDict() result.safe_search_annotation.adult = 5 result.safe_search_annotation.violence = 5 vision_client.safe_search_detection = MagicMock(return_value=result) filename = str(uuid.uuid4()) data = {'bucket': 'my-bucket', 'name': filename} image.blur_offensive_images(data) out, _ = capsys.readouterr() assert 'Analyzing %s.' % filename in out assert 'The image %s was detected as inappropriate.' % filename in out assert image.__blur_image.called
def test_process_safe_image(storage_client, vision_client, __blur_image, capsys): result = UserDict() result.safe_search_annotation = UserDict() result.safe_search_annotation.adult = 1 result.safe_search_annotation.violence = 1 vision_client.safe_search_detection = MagicMock(return_value=result) filename = str(uuid.uuid4()) data = {"bucket": "my-bucket", "name": filename} image.blur_offensive_images(data) out, _ = capsys.readouterr() assert "Analyzing %s." % filename in out assert "The image %s was detected as OK." % filename in out assert __blur_image.called is False
def index(): envelope = request.get_json() if not envelope: msg = 'no Pub/Sub message received' print(f'error: {msg}') return f'Bad Request: {msg}', 400 if not isinstance(envelope, dict) or 'message' not in envelope: msg = 'invalid Pub/Sub message format' print(f'error: {msg}') return f'Bad Request: {msg}', 400 # Decode the Pub/Sub message. pubsub_message = envelope['message'] if isinstance(pubsub_message, dict) and 'data' in pubsub_message: try: data = json.loads( base64.b64decode(pubsub_message['data']).decode()) except Exception as e: msg = ('Invalid Pub/Sub message: ' 'data property is not valid base64 encoded JSON') print(f'error: {e}') return f'Bad Request: {msg}', 400 # Validate the message is a Cloud Storage event. if not data["name"] or not data["bucket"]: msg = ('Invalid Cloud Storage notification: ' 'expected name and bucket properties') print(f'error: {msg}') return f'Bad Request: {msg}', 400 try: image.blur_offensive_images(data) # Flush the stdout to avoid log buffering. sys.stdout.flush() return ('', 204) except Exception as e: print(f'error: {e}') return ('', 500) return ('', 500)
def index(): envelope = request.get_json() if not envelope: msg = "no Pub/Sub message received" print(f"error: {msg}") return f"Bad Request: {msg}", 400 if not isinstance(envelope, dict) or "message" not in envelope: msg = "invalid Pub/Sub message format" print(f"error: {msg}") return f"Bad Request: {msg}", 400 # Decode the Pub/Sub message. pubsub_message = envelope["message"] if isinstance(pubsub_message, dict) and "data" in pubsub_message: try: data = json.loads( base64.b64decode(pubsub_message["data"]).decode()) except Exception as e: msg = ("Invalid Pub/Sub message: " "data property is not valid base64 encoded JSON") print(f"error: {e}") return f"Bad Request: {msg}", 400 # Validate the message is a Cloud Storage event. if not data["name"] or not data["bucket"]: msg = ("Invalid Cloud Storage notification: " "expected name and bucket properties") print(f"error: {msg}") return f"Bad Request: {msg}", 400 try: image.blur_offensive_images(data) return ("", 204) except Exception as e: print(f"error: {e}") return ("", 500) return ("", 500)