Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions functions/imagemagick/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ Functions for your project.

1. Create a Cloud Storage Bucket:

gsutil mb gs://YOUR_BUCKET_NAME
gsutil mb gs://YOUR_INPUT_BUCKET_NAME

This storage bucket is used to upload images for the function to check.

1. Create a second Cloud Storage Bucket:

gsutil mb gs://YOUR_OUTPUT_BUCKET_NAME

This second storage bucket is used to store blurred images. (Un-blurred images will not be saved to this bucket.)

This is necessary because saving the blurred image to the input bucket would cause your function to be invoked a second time with the blurred image itself.

1. Deploy the `blur_offensive_images` function with a Storage trigger:

gcloud functions deploy blur_offensive_images --trigger-bucket=YOUR_BUCKET_NAME --runtime python37
gcloud functions deploy blur_offensive_images --trigger-bucket=YOUR_INPUT_BUCKET_NAME --set-env-vars BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME --runtime python37

* Replace `YOUR_BUCKET_NAME` with the name of the Cloud Storage Bucket you created earlier.
* Replace `YOUR_INPUT_BUCKET_NAME` and `YOUR_OUTPUT_BUCKET_NAME` with the names of the respective Cloud Storage Buckets you created earlier.

1. Upload an offensive image to the Storage bucket, such as this image of
a flesh-eating zombie: https://cdn.pixabay.com/photo/2015/09/21/14/24/zombie-949916_1280.jpg
Expand Down
12 changes: 7 additions & 5 deletions functions/imagemagick/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ def __blur_image(current_blob):

print(f'Image {file_name} was blurred.')

# Send Blurred image back to the bucket (with a 'blurred-' prefix).
# The prefix is necessary to avoid re-invoking the function upon upload.
new_file_name = f'blurred-{file_name}'
new_blob = current_blob.bucket.blob(new_file_name)
# Upload result to a second bucket, to avoid re-triggering the function.
# You could instead re-upload it to the same bucket + tell your function
# to ignore files marked as blurred (e.g. those with a "blurred" prefix)
blur_bucket_name = os.getenv('BLURRED_BUCKET_NAME')
blur_bucket = storage_client.bucket(blur_bucket_name)
new_blob = blur_bucket.blob(file_name)
new_blob.upload_from_filename(temp_local_filename)
print(f'Blurred image was uploaded to {new_file_name}.')
print(f'Blurred image uploaded to: gs://{blur_bucket_name}/{file_name}')

# Delete the temporary file.
os.remove(temp_local_filename)
Expand Down
9 changes: 6 additions & 3 deletions functions/imagemagick/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,23 @@ def test_process_safe_image(

@patch('main.os')
@patch('main.Image')
def test_blur_image(image_mock, os_mock, capsys):
@patch('main.storage_client')
def test_blur_image(storage_client, image_mock, os_mock, capsys):
filename = str(uuid.uuid4())
blur_bucket = 'blurred-bucket-' + str(uuid.uuid4())

os_mock.remove = MagicMock()
os_mock.path = MagicMock()
os_mock.path.basename = MagicMock(side_effect=(lambda x: x))

os_mock.getenv = MagicMock(return_value=blur_bucket)

image_mock.return_value = image_mock
image_mock.__enter__.return_value = image_mock

blob = UserDict()
blob.name = filename
blob.bucket = UserDict()
blob.bucket.blob = MagicMock(return_value=blob)
blob.download_to_filename = MagicMock()
blob.upload_from_filename = MagicMock()

Expand All @@ -102,6 +105,6 @@ def test_blur_image(image_mock, os_mock, capsys):

assert f'Image {filename} was downloaded to' in out
assert f'Image {filename} was blurred.' in out
assert f'Blurred image was uploaded to blurred-{filename}.' in out
assert f'Blurred image uploaded to: gs://{blur_bucket}/{filename}' in out
assert os_mock.remove.called
assert image_mock.resize.called
4 changes: 4 additions & 0 deletions functions/imagemagick/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mock==3.0.5
six==1.12.0
uuid==1.30
pytest==4.6.2