Spaces:
Sleeping
Sleeping
Commit
·
b223052
1
Parent(s):
4c9108c
update pdf
Browse files- src/asg/settings.py +5 -1
- src/demo/views.py +13 -14
src/asg/settings.py
CHANGED
@@ -123,4 +123,8 @@ STATIC_URL = '/static/'
|
|
123 |
|
124 |
STATICFILES_DIRS = (
|
125 |
os.path.join(BASE_DIR, "static"),
|
126 |
-
)
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
STATICFILES_DIRS = (
|
125 |
os.path.join(BASE_DIR, "static"),
|
126 |
+
)
|
127 |
+
|
128 |
+
# Media files (uploads)
|
129 |
+
MEDIA_URL = '/media/'
|
130 |
+
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
src/demo/views.py
CHANGED
@@ -444,10 +444,12 @@ def upload_refs(request):
|
|
444 |
continue
|
445 |
sanitized_filename = f"{sanitized_filename}{file_extension}"
|
446 |
|
447 |
-
|
|
|
|
|
|
|
448 |
if default_storage.exists(file_path):
|
449 |
default_storage.delete(file_path)
|
450 |
-
|
451 |
saved_file_name = default_storage.save(file_path, file)
|
452 |
file_size = round(float(file.size) / 1024000, 2)
|
453 |
|
@@ -672,31 +674,28 @@ def download_pdfs(request):
|
|
672 |
if not pdf_links:
|
673 |
return JsonResponse({"message": "No PDFs to download."}, status=400)
|
674 |
|
675 |
-
|
676 |
-
os.
|
|
|
|
|
677 |
|
678 |
downloaded_files = []
|
679 |
for i, pdf_url in enumerate(pdf_links):
|
680 |
try:
|
681 |
response = requests.get(pdf_url, stream=True)
|
682 |
if response.status_code == 200:
|
683 |
-
# 处理文件名,确保合法
|
684 |
sanitized_title = clean_filename(pdf_titles[i]) if i < len(pdf_titles) else f"file_{i}"
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
with open(pdf_filename, "wb") as pdf_file:
|
689 |
for chunk in response.iter_content(chunk_size=1024):
|
690 |
pdf_file.write(chunk)
|
691 |
-
|
692 |
-
|
693 |
-
print(f"Success: {pdf_filename}")
|
694 |
else:
|
695 |
print(f"Failed to download {pdf_url}")
|
696 |
-
|
697 |
except Exception as e:
|
698 |
print(f"Error downloading {pdf_url}: {e}")
|
699 |
-
|
700 |
print("Download finished")
|
701 |
return JsonResponse({"message": f"Downloaded {len(downloaded_files)} PDFs successfully!", "files": downloaded_files})
|
702 |
|
|
|
444 |
continue
|
445 |
sanitized_filename = f"{sanitized_filename}{file_extension}"
|
446 |
|
447 |
+
# Use a relative path under MEDIA_ROOT for file uploads
|
448 |
+
relative_dir = os.path.join('pdf', Global_survey_id)
|
449 |
+
os.makedirs(os.path.join(settings.MEDIA_ROOT, relative_dir), exist_ok=True)
|
450 |
+
file_path = os.path.join(relative_dir, sanitized_filename)
|
451 |
if default_storage.exists(file_path):
|
452 |
default_storage.delete(file_path)
|
|
|
453 |
saved_file_name = default_storage.save(file_path, file)
|
454 |
file_size = round(float(file.size) / 1024000, 2)
|
455 |
|
|
|
674 |
if not pdf_links:
|
675 |
return JsonResponse({"message": "No PDFs to download."}, status=400)
|
676 |
|
677 |
+
# Use MEDIA_ROOT/pdf/recommend_pdfs for saving downloaded PDFs
|
678 |
+
relative_dir = os.path.join('pdf', 'recommend_pdfs')
|
679 |
+
abs_dir = os.path.join(settings.MEDIA_ROOT, relative_dir)
|
680 |
+
os.makedirs(abs_dir, exist_ok=True)
|
681 |
|
682 |
downloaded_files = []
|
683 |
for i, pdf_url in enumerate(pdf_links):
|
684 |
try:
|
685 |
response = requests.get(pdf_url, stream=True)
|
686 |
if response.status_code == 200:
|
|
|
687 |
sanitized_title = clean_filename(pdf_titles[i]) if i < len(pdf_titles) else f"file_{i}"
|
688 |
+
relative_path = os.path.join(relative_dir, f"{sanitized_title}.pdf")
|
689 |
+
abs_path = os.path.join(settings.MEDIA_ROOT, relative_path)
|
690 |
+
with open(abs_path, "wb") as pdf_file:
|
|
|
691 |
for chunk in response.iter_content(chunk_size=1024):
|
692 |
pdf_file.write(chunk)
|
693 |
+
downloaded_files.append(relative_path)
|
694 |
+
print(f"Success: {abs_path}")
|
|
|
695 |
else:
|
696 |
print(f"Failed to download {pdf_url}")
|
|
|
697 |
except Exception as e:
|
698 |
print(f"Error downloading {pdf_url}: {e}")
|
|
|
699 |
print("Download finished")
|
700 |
return JsonResponse({"message": f"Downloaded {len(downloaded_files)} PDFs successfully!", "files": downloaded_files})
|
701 |
|