|
| 1 | +""" |
| 2 | +Example Function params & inputs. |
| 3 | +""" |
| 4 | +from firebase_functions import storage_fn, params |
| 5 | +from firebase_admin import initialize_app |
| 6 | + |
| 7 | +initialize_app() |
| 8 | + |
| 9 | +bucket = params.StringParam( |
| 10 | + "BUCKET", |
| 11 | + label="storage bucket", |
| 12 | + description="The bucket to resize images from.", |
| 13 | + input=params.ResourceInput(type=params.ResourceType.STORAGE_BUCKET), |
| 14 | + default=params.STORAGE_BUCKET, |
| 15 | +) |
| 16 | + |
| 17 | +output_path = params.StringParam( |
| 18 | + "OUTPUT_PATH", |
| 19 | + label="storage bucket output path", |
| 20 | + description= |
| 21 | + "The path of in the bucket where processed images will be stored.", |
| 22 | + input=params.TextInput( |
| 23 | + example="/images/processed", |
| 24 | + validation_regex=r"^\/.*$", |
| 25 | + validation_error_message= |
| 26 | + "Must be a valid path starting with a forward slash", |
| 27 | + ), |
| 28 | + default="/images/processed", |
| 29 | +) |
| 30 | + |
| 31 | +image_type = params.ListParam( |
| 32 | + "IMAGE_TYPE", |
| 33 | + label="convert image to preferred types", |
| 34 | + description="The image types you'd like your source image to convert to.", |
| 35 | + input=params.MultiSelectInput([ |
| 36 | + params.SelectOption(value="jpeg", label="jpeg"), |
| 37 | + params.SelectOption(value="png", label="png"), |
| 38 | + params.SelectOption(value="webp", label="webp"), |
| 39 | + ]), |
| 40 | + default=["jpeg", "png"], |
| 41 | +) |
| 42 | + |
| 43 | +delete_original = params.BoolParam( |
| 44 | + "DELETE_ORIGINAL_FILE", |
| 45 | + label="delete the original file", |
| 46 | + description= |
| 47 | + "Do you want to automatically delete the original file from the Cloud Storage?", |
| 48 | + input=params.SelectInput([ |
| 49 | + params.SelectOption(value=True, label="Delete on any resize attempt"), |
| 50 | + params.SelectOption(value=False, label="Don't delete"), |
| 51 | + ],), |
| 52 | + default=True, |
| 53 | +) |
| 54 | + |
| 55 | +image_resize_api_secret = params.SecretParam( |
| 56 | + "IMAGE_RESIZE_API_SECRET", |
| 57 | + label="image resize api secret", |
| 58 | + description="The fake secret key to use for the image resize API.", |
| 59 | +) |
| 60 | + |
| 61 | + |
| 62 | +@storage_fn.on_object_finalized( |
| 63 | + bucket=bucket, |
| 64 | + secrets=[image_resize_api_secret], |
| 65 | +) |
| 66 | +def resize_images(event: storage_fn.CloudEvent[storage_fn.StorageObjectData]): |
| 67 | + """ |
| 68 | + This function will be triggered when a new object is created in the bucket. |
| 69 | + """ |
| 70 | + print("Got a new image:", event) |
| 71 | + print("Selected image types:", image_type.value) |
| 72 | + print("Selected bucket resource:", bucket.value) |
| 73 | + print("Selected output location:", output_path.value) |
| 74 | + print("Testing a not so secret api key:", image_resize_api_secret.value) |
| 75 | + print("Should original images be deleted?:", delete_original.value) |
| 76 | + # TODO: Implement your image resize logic |
0 commit comments