comparison core/images/upload.py @ 1196:e3a7e876aca7

More changes for local upload path.
author Brian Neal <bgneal@gmail.com>
date Sun, 07 May 2023 19:16:03 -0500
parents 7fc6c42b2f5b
children ba23e79438f4
comparison
equal deleted inserted replaced
1195:7fc6c42b2f5b 1196:e3a7e876aca7
5 """ 5 """
6 from base64 import b64encode 6 from base64 import b64encode
7 import datetime 7 import datetime
8 import logging 8 import logging
9 from io import BytesIO 9 from io import BytesIO
10 import os
10 import os.path 11 import os.path
11 import shutil 12 import shutil
12 import uuid 13 import uuid
13 14
14 from django.conf import settings 15 from django.conf import settings
153 ext = os.path.splitext(filename)[1] 154 ext = os.path.splitext(filename)[1]
154 image_name = '%s%s' % (unique_name, ext) 155 image_name = '%s%s' % (unique_name, ext)
155 thumb_name = '%st%s' % (unique_name, ext) 156 thumb_name = '%st%s' % (unique_name, ext)
156 image_path = os.path.join(upload_dir, image_name) 157 image_path = os.path.join(upload_dir, image_name)
157 thumb_path = os.path.join(upload_dir, thumb_name) 158 thumb_path = os.path.join(upload_dir, thumb_name)
159 perms = 0644
158 160
159 shutil.copy(filename, image_path) 161 shutil.copy(filename, image_path)
162 os.chmod(image_path, perms)
163
160 if thumb: 164 if thumb:
165 thumb.seek(0)
161 shutil.copyfileobj(thumb, thumb_path) 166 shutil.copyfileobj(thumb, thumb_path)
167 os.chmod(thumb_path, perms)
162 168
163 # Generate URLs for the image and thumb. 169 # Generate URLs for the image and thumb.
170 image_url = _user_photo_url(image_name)
171 thumb_url = _user_photo_url(thumb_name)
172
173 return (image_url, thumb_url)
174
175
176 def _user_photo_url(filename):
177 url_pattern = '%s://%s%s%s/%s'
164 site = Site.objects.get_current() 178 site = Site.objects.get_current()
165
166 url_pattern = '%s://%s%s%s/%s'
167 179
168 image_url = url_pattern % ( 180 image_url = url_pattern % (
169 settings.SITE_SCHEME, 181 settings.SITE_SCHEME,
170 site.domain, 182 site.domain,
171 settings.MEDIA_URL, 183 settings.MEDIA_URL,
172 settings.USER_PHOTOS_LOCAL_UPLOAD_DIR, 184 settings.USER_PHOTOS_LOCAL_UPLOAD_DIR,
173 image_name) 185 filename)
174
175 thumb_url = url_pattern % (
176 settings.SITE_SCHEME,
177 site.domain,
178 settings.MEDIA_URL,
179 settings.USER_PHOTOS_LOCAL_UPLOAD_DIR,
180 thumb_name)
181
182 return (image_url, thumb_url)
183