working URL uploader

This commit is contained in:
ville rantanen
2018-07-01 19:39:40 +03:00
parent 5c225901f3
commit 99fa595cd4
3 changed files with 18 additions and 11 deletions

View File

@@ -186,6 +186,8 @@ def upload_url():
if request.method == 'POST': if request.method == 'POST':
name = request.form['name'] name = request.form['name']
url = request.form['url'] url = request.form['url']
if url == "https://...":
return "", 200
if not is_valid_url(url): if not is_valid_url(url):
return "URL not valid", 400 return "URL not valid", 400
(ok,share) = get_share(name) (ok,share) = get_share(name)
@@ -200,7 +202,7 @@ def upload_url():
filename = os.path.join( filename = os.path.join(
share['path'], share['path'],
secure_filename( secure_filename(
os.path.basename(url) safe_string(url, ".[]()- ", no_repeat = True)
) )
) )
if os.path.exists(filename): if os.path.exists(filename):

View File

@@ -1,3 +1,4 @@
flask flask
gunicorn gunicorn
pycrypto pycrypto
requests

View File

@@ -1,23 +1,24 @@
import os import os
from datetime import datetime from datetime import datetime
from flask import current_app as app from flask import current_app as app
import requests
import re
try: try:
from urllib.request import pathname2url from urllib.request import pathname2url
from urllib.request import URLopener
from urllib.request import urlparse from urllib.request import urlparse
from urllib.error import HTTPError
except ImportError: except ImportError:
from urllib import pathname2url from urllib import pathname2url
from urllib import URLopener
from urlparse import urlparse from urlparse import urlparse
from urllib2 import HTTPError
def download_url(url, filename): def download_url(url, filename):
downloader = URLopener()
try: try:
downloader.retrieve(url, filename) r = requests.get(url, stream=True)
except HTTPError as e: with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024 * 1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
except requests.exceptions.RequestException as e:
return (False, ("%s %s"%(e.code,e.reason), e.code)) return (False, ("%s %s"%(e.code,e.reason), e.code))
return (True, ("OK", 200 )) return (True, ("OK", 200 ))
@@ -144,6 +145,9 @@ def safe_path(s):
return safe_string(s, "-_/") return safe_string(s, "-_/")
def safe_string(s, valid): def safe_string(s, valid, no_repeat = False):
""" return a safe string, replace non alnum characters with _ . all characters in valid are considered valid. """ """ return a safe string, replace non alnum characters with _ . all characters in valid are considered valid. """
return "".join([c if c.isalnum() or c in valid else "_" for c in s]) safe = "".join([c if c.isalnum() or c in valid else "_" for c in s])
if no_repeat:
safe = re.sub(r'_+', '_', safe)
return safe