def proxy_file_downloader(request):
if not is_admin_or_root(request.user):
raise PermissionDenied
def download_file(url):
local_filename = url.split('/')[-1]
if local_filename == '':
local_filename = random_string()
r = requests.get(url, stream=True, timeout=30)
with open(path.join(settings.UPLOAD_DIR, local_filename), 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
if request.method == 'POST':
try:
url = request.POST['url']
Thread(target=download_file, args=(url,)).start()
except Exception as e:
raise PermissionDenied(repr(e))
return redirect(reverse('filemanager'))
评论列表
文章目录