Merge pull request #250 from SchrodingersGat/tweaks

Some tweaks
This commit is contained in:
Oliver 2019-05-07 08:13:44 +10:00 committed by GitHub
commit b222897358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import json
import os.path
from datetime import datetime
from PIL import Image
import requests
from wsgiref.util import FileWrapper
from django.http import StreamingHttpResponse
@ -34,6 +35,65 @@ def TestIfImageURL(url):
]
def DownloadExternalFile(url, **kwargs):
""" Attempt to download an external file
Args:
url - External URL
"""
result = {
'status': False,
'url': url,
'file': None,
'status_code': 200,
}
headers = {'User-Agent': 'Mozilla/5.0'}
max_size = kwargs.get('max_size', 1048576) # 1MB default limit
# Get the HEAD for the file
try:
head = requests.head(url, stream=True, headers=headers)
except:
result['error'] = 'Error retrieving HEAD data'
return result
if not head.status_code == 200:
result['error'] = 'Incorrect HEAD status code'
result['status_code'] = head.status_code
return result
try:
filesize = int(head.headers['Content-Length'])
except ValueError:
result['error'] = 'Could not decode filesize'
result['extra'] = head.headers['Content-Length']
return result
if filesize > max_size:
result['error'] = 'File size too large ({s})'.format(s=filesize)
return result
# All checks have passed - download the file
try:
request = requests.get(url, stream=True, headers=headers)
except:
result['error'] = 'Error retriving GET data'
return result
try:
dl_file = io.StringIO(request.text)
result['status'] = True
result['file'] = dl_file
return result
except:
result['error'] = 'Could not convert downloaded data to file'
return result
def str2bool(text, test=True):
""" Test if a string 'looks' like a boolean value.

View File

@ -319,7 +319,7 @@ class UploadPartImage(AjaxView):
return JsonResponse(response, status=status)
elif 'image_url' in request.POST:
image_url = request.POST['image_url']
image_url = str(request.POST['image_url']).split('?')[0]
validator = URLValidator()
@ -334,6 +334,7 @@ class UploadPartImage(AjaxView):
# Test the the URL at least looks like an image
if not TestIfImageURL(image_url):
response['error'] = 'Invalid image URL'
response['url'] = image_url
return JsonResponse(response, status=400)
response['error'] = 'Cannot download cross-site images (yet)'

View File

@ -16,11 +16,17 @@ function inventreeDocReady() {
/* Add drag-n-drop functionality to any element
* marked with the class 'dropzone'
*/
$('.dropzone').on('dragenter', function() {
$(this).addClass('dragover');
$('.dropzone').on('dragenter', function(event) {
// TODO - Only indicate that a drop event will occur if a file is being dragged
var transfer = event.originalEvent.dataTransfer;
if (true || isFileTransfer(transfer)) {
$(this).addClass('dragover');
}
});
$('.dropzone').on('dragleave drop', function() {
$('.dropzone').on('dragleave drop', function(event) {
$(this).removeClass('dragover');
});