diff --git a/boards/forms/__init__.py b/boards/forms/__init__.py --- a/boards/forms/__init__.py +++ b/boards/forms/__init__.py @@ -2,6 +2,7 @@ import hashlib import logging import re import time +import traceback import pytz from django import forms diff --git a/boards/models/attachment/downloaders.py b/boards/models/attachment/downloaders.py --- a/boards/models/attachment/downloaders.py +++ b/boards/models/attachment/downloaders.py @@ -41,20 +41,20 @@ class Downloader: # Get the actual content into memory response = requests.get(url, verify=False, stream=True) - # Download file, stop if the size exceeds limit - size = 0 - - # Set a dummy file name that will be replaced - # anyway, just keep the valid extension - filename = 'file.' + content_type.split('/')[1] + if response.status_code == HTTP_RESULT_OK: + # Download file, stop if the size exceeds limit + size = 0 - file = TemporaryUploadedFile(filename, content_type, 0, None, None) - for chunk in response.iter_content(FILE_DOWNLOAD_CHUNK_BYTES): - size += len(chunk) - validate_file_size(size) - file.write(chunk) + # Set a dummy file name that will be replaced + # anyway, just keep the valid extension + filename = 'file.' + content_type.split('/')[1] - if response.status_code == HTTP_RESULT_OK: + file = TemporaryUploadedFile(filename, content_type, 0, None, None) + for chunk in response.iter_content(FILE_DOWNLOAD_CHUNK_BYTES): + size += len(chunk) + validate_file_size(size) + file.write(chunk) + return file @@ -85,12 +85,15 @@ class YouTubeDownloader(Downloader): class NothingDownloader(Downloader): @staticmethod def handles(url: str) -> bool: - if REGEX_MAGNET.match(url) or REGEX_YOUTUBE_URL.match(url): + if REGEX_MAGNET.match(url): return True response_head = requests.head(url, verify=False) - content_type = response_head.headers[HEADER_CONTENT_TYPE].split(';')[0] - return content_type in TYPE_URL_ONLY + if response_head.status_code == HTTP_RESULT_OK: + content_type = response_head.headers[HEADER_CONTENT_TYPE].split(';')[0] + return content_type in TYPE_URL_ONLY + else: + return True @staticmethod def download(url: str):