-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work-in-progress: convert heic to jpeg on Asset creation
* Consolidated S3 logic and settings in a few places to be more DRY.
- Loading branch information
Showing
5 changed files
with
113 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,88 @@ | ||
from urllib.parse import urlparse | ||
import boto3 | ||
from s3sign.utils import create_presigned_url, s3_config | ||
import requests | ||
from django.conf import settings | ||
from django.utils import timezone | ||
from os.path import basename, splitext, join | ||
from PIL import Image | ||
from pi_heif import register_heif_opener | ||
from s3sign.utils import ( | ||
DEFAULT_AWS_REGION, | ||
create_presigned_url, s3_config, get_object_name, upload_file | ||
) | ||
from urllib.parse import urlparse | ||
|
||
|
||
s3_sign_view_settings = { | ||
'private': True, | ||
'root': 'private/', | ||
'acl': None, | ||
'expiration_time': 3600 * 8, # 8 hours | ||
'max_file_size': 50000000, # 50mb | ||
} | ||
|
||
|
||
def get_signed_s3_url(url, bucket, aws_key, aws_secret): | ||
s3_client = boto3.client( | ||
def get_s3_private_bucket_name() -> str: | ||
return getattr( | ||
settings, | ||
'S3_PRIVATE_STORAGE_BUCKET_NAME', | ||
'mediathread-private-uploads') | ||
|
||
|
||
def get_s3_client(aws_key, aws_secret): | ||
aws_region_name = DEFAULT_AWS_REGION | ||
if hasattr(settings, 'AWS_S3_REGION_NAME'): | ||
aws_region_name = settings.AWS_S3_REGION_NAME | ||
|
||
return boto3.client( | ||
's3', config=s3_config, | ||
region_name=self.aws_region_name, | ||
aws_access_key_id=aws_key, | ||
aws_secret_access_key=aws_secret) | ||
|
||
|
||
def get_signed_s3_url(url: str, bucket: str, aws_key: str, aws_secret: str): | ||
s3_client = get_s3_client(aws_key, aws_secret) | ||
|
||
url = urlparse(url) | ||
object_name = url.path.lstrip('/') | ||
object_name = object_name.replace(bucket + '/', '') | ||
return create_presigned_url(s3_client, bucket, object_name, 3600) | ||
|
||
|
||
def convert_heic_to_jpg( | ||
url: str, request: object, bucket: str, | ||
aws_key: str, aws_secret: str | ||
) -> str: | ||
""" | ||
Given an heic image url, convert it to a JPEG. This comprises a | ||
few steps: | ||
* Download the file | ||
* Do the conversion | ||
* Upload jpeg to S3 | ||
* Return new url | ||
""" | ||
print('convert_heic_to_jpg', url, bucket) | ||
response = requests.get(url, stream=True) | ||
|
||
# Sort out the new filename | ||
parsed_url = urlparse(url) | ||
filename = splitext(basename(parsed_url.path))[0] | ||
filename = filename + '.jpg' | ||
print(filename) | ||
|
||
# Open the file and convert it | ||
register_heif_opener() | ||
im = Image.open(response.raw) | ||
rgb_im = im.convert('RGB') | ||
tmp_jpeg = join('/tmp/', filename) | ||
rgb_im.save(tmp_jpeg) | ||
print(tmp_jpeg) | ||
|
||
# upload to S3, return source url | ||
s3_client = get_s3_client(aws_key, aws_secret) | ||
object_name = get_object_name(timezone.now(), '.jpg') | ||
print(object_name) | ||
uploaded = upload_file(s3_client, filename, bucket, object_name) | ||
print(uploaded) | ||
|
||
return object_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters