|
36 | 36 | from sqlalchemy import func, cast, Date |
37 | 37 | import boto3 |
38 | 38 | from botocore.config import Config |
39 | | -from botocore.exceptions import ClientError |
40 | 39 |
|
41 | 40 | local_tz = ZoneInfo("America/New_York") |
42 | 41 |
|
@@ -844,37 +843,53 @@ def mutate(self, info, name, net_id, email, encoded_image=None): |
844 | 843 | final_photo_url = None |
845 | 844 |
|
846 | 845 | if encoded_image: |
847 | | - |
848 | | - s3 = boto3.client( |
849 | | - "s3", |
850 | | - endpoint_url=os.getenv("DIGITAL_OCEAN_URL"), |
851 | | - aws_access_key_id=os.getenv("DIGITAL_OCEAN_ACCESS"), |
852 | | - aws_secret_access_key=os.getenv("DIGITAL_OCEAN_SECRET_ACCESS"), |
853 | | - config=Config(s3={"addressing_style": "path"}), |
| 846 | + bucket = "appdev-upload" |
| 847 | + path = f"uplift-dev/user-profile/{net_id}-profile.png" |
| 848 | + region = "nyc3" |
| 849 | + |
| 850 | + logging.info(f"DIGITAL_OCEAN_URL: {os.getenv('DIGITAL_OCEAN_URL')}") |
| 851 | + logging.info( |
| 852 | + "CreateUser profile picture upload: net_id=%s, bucket=%s, key=%s", |
| 853 | + net_id, |
| 854 | + bucket, |
| 855 | + path, |
854 | 856 | ) |
855 | | - |
| 857 | + |
856 | 858 | try: |
857 | 859 | image_data = base64.b64decode(encoded_image, validate=True) |
858 | 860 | except (binascii.Error, ValueError) as err: |
859 | | - raise GraphQLError("Invalid profile image encoding.") |
| 861 | + logging.warning( |
| 862 | + "Invalid profile image encoding: %s: %s", |
| 863 | + type(err).__name__, |
| 864 | + err, |
| 865 | + ) |
| 866 | + raise GraphQLError("Invalid profile image encoding.") |
860 | 867 |
|
861 | 868 | try: |
862 | | - bucket = "appdev-upload" |
863 | | - path = f"uplift-dev/user-profile/{net_id}-profile.png" |
864 | | - region = "nyc3" |
865 | | - |
| 869 | + logging.info("Attempting S3 put_object for new user profile picture...") |
| 870 | + s3 = boto3.client( |
| 871 | + "s3", |
| 872 | + endpoint_url=os.getenv("DIGITAL_OCEAN_URL"), |
| 873 | + aws_access_key_id=os.getenv("DIGITAL_OCEAN_ACCESS"), |
| 874 | + aws_secret_access_key=os.getenv("DIGITAL_OCEAN_SECRET_ACCESS"), |
| 875 | + config=Config(s3={"addressing_style": "path"}), |
| 876 | + ) |
866 | 877 | s3.put_object( |
867 | 878 | Bucket=bucket, |
868 | | - Key=path, |
| 879 | + Key=path, |
869 | 880 | Body=image_data, |
870 | 881 | ContentType="image/png", |
871 | | - ACL="public-read" |
| 882 | + ACL="public-read", |
872 | 883 | ) |
873 | | - |
| 884 | + logging.info("S3 put_object succeeded for new user profile picture") |
874 | 885 | final_photo_url = f"https://{bucket}.{region}.digitaloceanspaces.com/{path}" |
875 | | - except ClientError as e: |
876 | | - print("Upload error:", e) |
877 | | - raise GraphQLError("Error uploading user profile picture.") |
| 886 | + except Exception as e: |
| 887 | + logging.error( |
| 888 | + "S3 upload failed (create user): %s: %s", |
| 889 | + type(e).__name__, |
| 890 | + e, |
| 891 | + ) |
| 892 | + raise GraphQLError(f"S3 error: {type(e).__name__}: {e}") |
878 | 893 |
|
879 | 894 | new_user = UserModel(name=name, net_id=net_id, email=email, encoded_image=final_photo_url) |
880 | 895 | db_session.add(new_user) |
@@ -906,37 +921,55 @@ def mutate(self, info, user_id, name=None, email=None, encoded_image=None): |
906 | 921 | existing_user.email = email |
907 | 922 | if encoded_image is not None: |
908 | 923 | final_photo_url = None |
909 | | - s3 = boto3.client( |
910 | | - "s3", |
911 | | - endpoint_url=os.getenv("DIGITAL_OCEAN_URL"), |
912 | | - aws_access_key_id=os.getenv("DIGITAL_OCEAN_ACCESS"), |
913 | | - aws_secret_access_key=os.getenv("DIGITAL_OCEAN_SECRET_ACCESS"), |
914 | | - config=Config(s3={"addressing_style": "path"}), |
| 924 | + bucket = "appdev-upload" |
| 925 | + path = f"uplift-dev/user-profile/{existing_user.net_id}-profile.png" |
| 926 | + region = "nyc3" |
| 927 | + |
| 928 | + logging.info(f"DIGITAL_OCEAN_URL: {os.getenv('DIGITAL_OCEAN_URL')}") |
| 929 | + logging.info( |
| 930 | + "EditUser profile picture upload: user_id=%s, net_id=%s, bucket=%s, key=%s", |
| 931 | + user_id, |
| 932 | + existing_user.net_id, |
| 933 | + bucket, |
| 934 | + path, |
915 | 935 | ) |
916 | | - |
| 936 | + |
917 | 937 | try: |
918 | 938 | image_data = base64.b64decode(encoded_image, validate=True) |
919 | 939 | except (binascii.Error, ValueError) as err: |
| 940 | + logging.warning( |
| 941 | + "Invalid profile image encoding: %s: %s", |
| 942 | + type(err).__name__, |
| 943 | + err, |
| 944 | + ) |
920 | 945 | raise GraphQLError("Invalid profile image encoding.") |
921 | 946 |
|
922 | 947 | try: |
923 | | - bucket = "appdev-upload" |
924 | | - path = f"uplift-dev/user-profile/{existing_user.net_id}-profile.png" |
925 | | - region = "nyc3" |
926 | | - |
| 948 | + logging.info("Attempting S3 put_object for edited user profile picture...") |
| 949 | + s3 = boto3.client( |
| 950 | + "s3", |
| 951 | + endpoint_url=os.getenv("DIGITAL_OCEAN_URL"), |
| 952 | + aws_access_key_id=os.getenv("DIGITAL_OCEAN_ACCESS"), |
| 953 | + aws_secret_access_key=os.getenv("DIGITAL_OCEAN_SECRET_ACCESS"), |
| 954 | + config=Config(s3={"addressing_style": "path"}), |
| 955 | + ) |
927 | 956 | s3.put_object( |
928 | 957 | Bucket=bucket, |
929 | | - Key=path, |
| 958 | + Key=path, |
930 | 959 | Body=image_data, |
931 | 960 | ContentType="image/png", |
932 | | - ACL="public-read" |
| 961 | + ACL="public-read", |
933 | 962 | ) |
934 | | - |
| 963 | + logging.info("S3 put_object succeeded for edited user profile picture") |
935 | 964 | final_photo_url = f"https://{bucket}.{region}.digitaloceanspaces.com/{path}" |
936 | 965 | existing_user.encoded_image = final_photo_url |
937 | | - except ClientError as e: |
938 | | - print("Upload error:", e) |
939 | | - raise GraphQLError("Error adding new user profile picture.") |
| 966 | + except Exception as e: |
| 967 | + logging.error( |
| 968 | + "S3 upload failed (edit user): %s: %s", |
| 969 | + type(e).__name__, |
| 970 | + e, |
| 971 | + ) |
| 972 | + raise GraphQLError(f"S3 error: {type(e).__name__}: {e}") |
940 | 973 |
|
941 | 974 | db_session.commit() |
942 | 975 | return existing_user |
|
0 commit comments