comparison user_photos/forms.py @ 976:f5aa74dcdd7a

Ensure temporary files get deleted during hotlinking.
author Brian Neal <bgneal@gmail.com>
date Mon, 05 Oct 2015 20:07:44 -0500
parents 7138883966b3
children 7fc6c42b2f5b
comparison
equal deleted inserted replaced
975:8c3d52b7cbd1 976:f5aa74dcdd7a
6 6
7 from django import forms 7 from django import forms
8 from django.conf import settings 8 from django.conf import settings
9 9
10 from core.download import download_file 10 from core.download import download_file
11 from core.functions import TemporaryFile 11 from core.functions import remove_file, TemporaryFile
12 from core.images.upload import upload 12 from core.images.upload import upload
13 from core.s3 import S3Bucket 13 from core.s3 import S3Bucket
14 from core.services import get_redis_connection 14 from core.services import get_redis_connection
15 from user_photos.models import Photo 15 from user_photos.models import Photo
16 16
148 if (self.url_parts.scheme == 'https' and 148 if (self.url_parts.scheme == 'https' and
149 self.url_parts.hostname in settings.USER_IMAGES_SOURCES): 149 self.url_parts.hostname in settings.USER_IMAGES_SOURCES):
150 return url 150 return url
151 151
152 # Try to download the file 152 # Try to download the file
153 path = download_file(url) 153 with remove_file(download_file(url)) as path:
154 154
155 # Upload it to our S3 bucket 155 # Upload it to our S3 bucket
156 bucket = S3Bucket(access_key=settings.USER_PHOTOS_ACCESS_KEY, 156 bucket = S3Bucket(access_key=settings.USER_PHOTOS_ACCESS_KEY,
157 secret_key=settings.USER_PHOTOS_SECRET_KEY, 157 secret_key=settings.USER_PHOTOS_SECRET_KEY,
158 base_url=settings.HOT_LINK_PHOTOS_BASE_URL, 158 base_url=settings.HOT_LINK_PHOTOS_BASE_URL,
159 bucket_name=settings.HOT_LINK_PHOTOS_BUCKET) 159 bucket_name=settings.HOT_LINK_PHOTOS_BUCKET)
160 160
161 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') 161 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
162 metadata = {'user': self.user.username, 'date': now} 162 metadata = {'user': self.user.username, 'date': now}
163 163
164 url, _ = upload(filename=path, 164 url, _ = upload(filename=path,
165 bucket=bucket, 165 bucket=bucket,
166 metadata=metadata, 166 metadata=metadata,
167 new_size=settings.USER_PHOTOS_MAX_SIZE) 167 new_size=settings.USER_PHOTOS_MAX_SIZE)
168 168
169 return url 169 return url