##// END OF EJS Templates
Download attachments to tmp file, not into memory
neko259 -
r1394:f7e9602f default
parent child Browse files
Show More
@@ -1,7 +1,8 b''
1 import os
1 import os
2 import re
2 import re
3
3
4 from django.core.files.uploadedfile import SimpleUploadedFile
4 from django.core.files.uploadedfile import SimpleUploadedFile, \
5 TemporaryUploadedFile
5 from pytube import YouTube
6 from pytube import YouTube
6 import requests
7 import requests
7
8
@@ -14,7 +15,7 b' HTTP_RESULT_OK = 200'
14 HEADER_CONTENT_LENGTH = 'content-length'
15 HEADER_CONTENT_LENGTH = 'content-length'
15 HEADER_CONTENT_TYPE = 'content-type'
16 HEADER_CONTENT_TYPE = 'content-type'
16
17
17 FILE_DOWNLOAD_CHUNK_BYTES = 100000
18 FILE_DOWNLOAD_CHUNK_BYTES = 200000
18
19
19 YOUTUBE_URL = re.compile(r'https?://www\.youtube\.com/watch\?v=\w+')
20 YOUTUBE_URL = re.compile(r'https?://www\.youtube\.com/watch\?v=\w+')
20
21
@@ -38,17 +39,19 b' class Downloader:'
38
39
39 # Download file, stop if the size exceeds limit
40 # Download file, stop if the size exceeds limit
40 size = 0
41 size = 0
41 content = b''
42
43 # Set a dummy file name that will be replaced
44 # anyway, just keep the valid extension
45 filename = 'file.' + content_type.split('/')[1]
46
47 file = TemporaryUploadedFile(filename, content_type, 0, None, None)
42 for chunk in response.iter_content(FILE_DOWNLOAD_CHUNK_BYTES):
48 for chunk in response.iter_content(FILE_DOWNLOAD_CHUNK_BYTES):
43 size += len(chunk)
49 size += len(chunk)
44 validate_file_size(size)
50 validate_file_size(size)
45 content += chunk
51 file.write(chunk)
46
52
47 if response.status_code == HTTP_RESULT_OK and content:
53 if response.status_code == HTTP_RESULT_OK:
48 # Set a dummy file name that will be replaced
54 return file
49 # anyway, just keep the valid extension
50 filename = 'file.' + content_type.split('/')[1]
51 return SimpleUploadedFile(filename, content, content_type)
52
55
53
56
54 class YouTubeDownloader(Downloader):
57 class YouTubeDownloader(Downloader):
General Comments 0
You need to be logged in to leave comments. Login now