comparison user_photos/forms.py @ 1195:7fc6c42b2f5b

Adding a local user photo upload option.
author Brian Neal <bgneal@gmail.com>
date Sun, 07 May 2023 16:22:13 -0500
parents f5aa74dcdd7a
children
comparison
equal deleted inserted replaced
1194:d437d5b88c27 1195:7fc6c42b2f5b
1 """Forms for the user_photos application.""" 1 """Forms for the user_photos application."""
2 import datetime
3 import hashlib 2 import hashlib
4 import os.path 3 import os.path
5 import urlparse 4 import urlparse
6 5
7 from django import forms 6 from django import forms
8 from django.conf import settings 7 from django.conf import settings
9 8
10 from core.download import download_file 9 from core.download import download_file
11 from core.functions import remove_file, TemporaryFile 10 from core.functions import remove_file, TemporaryFile
12 from core.images.upload import upload 11 from core.images.upload import process_upload
13 from core.s3 import S3Bucket
14 from core.services import get_redis_connection 12 from core.services import get_redis_connection
15 from user_photos.models import Photo 13 from user_photos.models import Photo
16 14
17 15
18 def rate_limit(key, limit, seconds): 16 def rate_limit(key, limit, seconds):
68 try: 66 try:
69 return Photo.objects.get(user=self.user, signature=signature) 67 return Photo.objects.get(user=self.user, signature=signature)
70 except Photo.DoesNotExist: 68 except Photo.DoesNotExist:
71 pass 69 pass
72 70
73 # This must not be a duplicate, proceed with upload to S3
74 bucket = S3Bucket(access_key=settings.USER_PHOTOS_ACCESS_KEY,
75 secret_key=settings.USER_PHOTOS_SECRET_KEY,
76 base_url=settings.USER_PHOTOS_BASE_URL,
77 bucket_name=settings.USER_PHOTOS_BUCKET)
78
79 # Trying to use PIL (or Pillow) on a Django UploadedFile is often 71 # Trying to use PIL (or Pillow) on a Django UploadedFile is often
80 # problematic because the file is often an in-memory file if it is under 72 # problematic because the file is often an in-memory file if it is under
81 # a certain size. This complicates matters and many of the operations we try 73 # a certain size. This complicates matters and many of the operations we try
82 # to perform on it fail if this is the case. To get around these issues, 74 # to perform on it fail if this is the case. To get around these issues,
83 # we make a copy of the file on the file system and operate on the copy. 75 # we make a copy of the file on the file system and operate on the copy.
89 with TemporaryFile(suffix=ext) as t: 81 with TemporaryFile(suffix=ext) as t:
90 for chunk in fp.chunks(): 82 for chunk in fp.chunks():
91 t.file.write(chunk) 83 t.file.write(chunk)
92 t.file.close() 84 t.file.close()
93 85
94 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') 86 url, thumb_url = process_upload(self.user, t.filename)
95 metadata = {'user': self.user.username, 'date': now}
96
97 url, thumb_url = upload(filename=t.filename,
98 bucket=bucket,
99 metadata=metadata,
100 new_size=settings.USER_PHOTOS_MAX_SIZE,
101 thumb_size=settings.USER_PHOTOS_THUMB_SIZE)
102 87
103 photo = Photo(user=self.user, url=url, thumb_url=thumb_url, 88 photo = Photo(user=self.user, url=url, thumb_url=thumb_url,
104 signature=signature) 89 signature=signature)
105 photo.save() 90 photo.save()
106 return photo 91 return photo
149 self.url_parts.hostname in settings.USER_IMAGES_SOURCES): 134 self.url_parts.hostname in settings.USER_IMAGES_SOURCES):
150 return url 135 return url
151 136
152 # Try to download the file 137 # Try to download the file
153 with remove_file(download_file(url)) as path: 138 with remove_file(download_file(url)) as path:
154 139 url, _ = process_upload(self.user, path)
155 # Upload it to our S3 bucket
156 bucket = S3Bucket(access_key=settings.USER_PHOTOS_ACCESS_KEY,
157 secret_key=settings.USER_PHOTOS_SECRET_KEY,
158 base_url=settings.HOT_LINK_PHOTOS_BASE_URL,
159 bucket_name=settings.HOT_LINK_PHOTOS_BUCKET)
160
161 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
162 metadata = {'user': self.user.username, 'date': now}
163
164 url, _ = upload(filename=path,
165 bucket=bucket,
166 metadata=metadata,
167 new_size=settings.USER_PHOTOS_MAX_SIZE)
168 140
169 return url 141 return url