Show More
@@ -39,6 +39,10 b" LABEL_SEARCH = _('Search')" | |||||
39 |
|
39 | |||
40 | TAG_MAX_LENGTH = 20 |
|
40 | TAG_MAX_LENGTH = 20 | |
41 |
|
41 | |||
|
42 | IMAGE_DOWNLOAD_CHUNK_BYTES = 100000 | |||
|
43 | ||||
|
44 | HTTP_RESULT_OK = 200 | |||
|
45 | ||||
42 |
|
46 | |||
43 | class FormatPanel(forms.Textarea): |
|
47 | class FormatPanel(forms.Textarea): | |
44 | """ |
|
48 | """ | |
@@ -250,7 +254,6 b' class PostForm(NeboardForm):' | |||||
250 | content_type = response_head.headers['content-type'] |
|
254 | content_type = response_head.headers['content-type'] | |
251 | if content_type in CONTENT_TYPE_IMAGE: |
|
255 | if content_type in CONTENT_TYPE_IMAGE: | |
252 | length_header = response_head.headers.get('content-length') |
|
256 | length_header = response_head.headers.get('content-length') | |
253 | # TODO Do something if there is no such header |
|
|||
254 | if length_header: |
|
257 | if length_header: | |
255 | length = int(length_header) |
|
258 | length = int(length_header) | |
256 | if length > board_settings.MAX_IMAGE_SIZE: |
|
259 | if length > board_settings.MAX_IMAGE_SIZE: | |
@@ -259,20 +262,28 b' class PostForm(NeboardForm):' | |||||
259 | % str(board_settings.MAX_IMAGE_SIZE)) |
|
262 | % str(board_settings.MAX_IMAGE_SIZE)) | |
260 |
|
263 | |||
261 | # Get the actual content into memory |
|
264 | # Get the actual content into memory | |
262 | response = requests.get(url, verify=False) |
|
265 | response = requests.get(url, verify=False, stream=True) | |
263 |
|
266 | |||
264 | if response.status_code == 200: |
|
267 | # Download image, stop if the size exceeds limit | |
265 | content = response.content |
|
268 | size = 0 | |
|
269 | content = b'' | |||
|
270 | for chunk in response.iter_content(IMAGE_DOWNLOAD_CHUNK_BYTES): | |||
|
271 | size += len(chunk) | |||
|
272 | if size > board_settings.MAX_IMAGE_SIZE: | |||
|
273 | # TODO Dedup this code into a method | |||
|
274 | raise forms.ValidationError( | |||
|
275 | _('Image must be less than %s bytes') | |||
|
276 | % str(board_settings.MAX_IMAGE_SIZE)) | |||
|
277 | content += chunk | |||
266 |
|
278 | |||
|
279 | if response.status_code == HTTP_RESULT_OK and content: | |||
267 | # Set a dummy file name that will be replaced |
|
280 | # Set a dummy file name that will be replaced | |
268 | # anyway, just keep the valid extension |
|
281 | # anyway, just keep the valid extension | |
269 | filename = 'image.' + content_type.split('/')[1] |
|
282 | filename = 'image.' + content_type.split('/')[1] | |
270 | img_temp = SimpleUploadedFile(filename, content, |
|
283 | img_temp = SimpleUploadedFile(filename, content, | |
271 | content_type) |
|
284 | content_type) | |
272 | except Exception: |
|
285 | except Exception: | |
273 |
# Just return no |
|
286 | # Just return no image | |
274 | import traceback |
|
|||
275 | traceback.print_exc() |
|
|||
276 | pass |
|
287 | pass | |
277 |
|
288 | |||
278 | return img_temp |
|
289 | return img_temp |
General Comments 0
You need to be logged in to leave comments.
Login now