mirror of
https://github.com/narrrl/holy-flavor-rater.git
synced 2026-11-03 16:41:27 +01:00
removed cached image from repo
This commit is contained in:
1 parent
244795e887
commit
209af30a43
170 files changed
+32
-11
No files matched your search
@@ -14,15 +14,25 @@ class Command(BaseCommand):
|
||||
if not url:
|
||||
return None
|
||||
try:
|
||||
# Generate a clean, consistent filename
|
||||
ext = url.split('.')[-1].split('?')[0]
|
||||
if len(ext) > 4 or len(ext) < 3: ext = 'png'
|
||||
filename = f"legacy_{flavor_name.replace(' ', '_').lower()}.{ext}"
|
||||
|
||||
# Use a fixed path to check for existing file
|
||||
filepath = os.path.join(settings.MEDIA_ROOT, 'flavors', filename)
|
||||
|
||||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
||||
with urllib.request.urlopen(req) as response:
|
||||
img_temp = NamedTemporaryFile(delete=True)
|
||||
img_temp.write(response.read())
|
||||
img_temp.flush()
|
||||
img_temp.seek(0)
|
||||
|
||||
# If file exists, delete it so Django doesn't add a suffix
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
||||
|
||||
ext = url.split('.')[-1].split('?')[0]
|
||||
if len(ext) > 4: ext = 'png'
|
||||
filename = f"legacy_{flavor_name.replace(' ', '_').lower()}.{ext}"
|
||||
return File(img_temp, name=filename)
|
||||
except Exception as e:
|
||||
self.stdout.write(self.style.WARNING(f"Could not download image for {flavor_name}: {e}"))
|
||||
|
||||
@@ -4,6 +4,7 @@ import re
|
||||
import os
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.core.files import File
|
||||
from django.conf import settings
|
||||
from tempfile import NamedTemporaryFile
|
||||
from api.models import Category, Flavor
|
||||
|
||||
@@ -21,17 +22,27 @@ class Command(BaseCommand):
|
||||
if not url:
|
||||
return None
|
||||
try:
|
||||
img_temp = NamedTemporaryFile(delete=True)
|
||||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
||||
with urllib.request.urlopen(req) as response:
|
||||
img_temp.write(response.read())
|
||||
img_temp.flush()
|
||||
img_temp.seek(0) # IMPORTANT: go back to start
|
||||
|
||||
# Generate a clean, consistent filename
|
||||
ext = url.split('.')[-1].split('?')[0]
|
||||
if len(ext) > 4 or len(ext) < 3: ext = 'png'
|
||||
filename = f"{flavor_name.replace(' ', '_').lower()}.{ext}"
|
||||
return File(img_temp, name=filename)
|
||||
|
||||
# Use a fixed path to check for existing file
|
||||
filepath = os.path.join(settings.MEDIA_ROOT, 'flavors', filename)
|
||||
|
||||
# Download to a temporary file
|
||||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
||||
with urllib.request.urlopen(req) as response:
|
||||
img_temp = NamedTemporaryFile(delete=True)
|
||||
img_temp.write(response.read())
|
||||
img_temp.flush()
|
||||
img_temp.seek(0)
|
||||
|
||||
# If file exists, delete it so Django doesn't add a suffix
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
||||
|
||||
return File(img_temp, name=filename)
|
||||
except Exception as e:
|
||||
self.stdout.write(self.style.WARNING(f"Could not download image for {flavor_name}: {e}"))
|
||||
return None
|
||||
|
||||
Reference in new issue
Block a user