11import re
2- from pathlib import Path
2+ from pathlib import PurePosixPath
33from urllib .parse import urlparse
44
55import requests
6- from django .conf import settings
7- from django .core .files import File
6+ from django .core .files .base import ContentFile
87from django .core .management .base import BaseCommand
98
10- from apps .pages .models import Image , Page , page_image_path
9+ from apps .pages .models import Image , Page
1110
1211HTTP_OK = 200
1312
@@ -18,14 +17,6 @@ class Command(BaseCommand):
1817 def get_success_pages (self ):
1918 return Page .objects .filter (path__startswith = "about/success/" )
2019
21- def image_url (self , path ):
22- """
23- Given a full filesystem path to an image, return the proper media
24- url for it
25- """
26- new_url = path .replace (settings .MEDIA_ROOT , settings .MEDIA_URL )
27- return new_url .replace ("//" , "/" )
28-
2920 def fix_image (self , path , page ):
3021 url = f"http://legacy.python.org{ path } "
3122 # Retrieve the image
@@ -34,27 +25,18 @@ def fix_image(self, path, page):
3425 if r .status_code != HTTP_OK :
3526 return None
3627
37- # Create new associated image and generate ultimate path
28+ # Extract and validate filename (alphanumeric, hyphens, dots only)
29+ raw_name = PurePosixPath (urlparse (url ).path ).name
30+ filename = re .sub (r"[^\w.\-]" , "_" , raw_name )
31+ if not filename or filename .startswith ("." ):
32+ return None
33+
34+ # Use Django's storage API to safely write the file
3835 img = Image ()
3936 img .page = page
37+ img .image .save (filename , ContentFile (r .content ), save = True )
4038
41- filename = Path (urlparse (url ).path ).name
42- output_path = page_image_path (img , filename )
43-
44- # Make sure our directories exist
45- directory = Path (output_path ).parent
46- directory .mkdir (parents = True , exist_ok = True )
47-
48- # Write image data to our location
49- with Path (output_path ).open ("wb" ) as f :
50- f .write (r .content )
51-
52- # Re-open the image as a Django File object
53- with Path (output_path ).open ("rb" ) as reopen :
54- new_file = File (reopen )
55- img .image .save (filename , new_file , save = True )
56-
57- return self .image_url (output_path )
39+ return img .image .url
5840
5941 def find_image_paths (self , page ):
6042 content = page .content .raw
0 commit comments