2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-03 13:58:47 +00:00

Various fixes to the "Download image from URL" functionality (#4101)

* Various fixes to the "Download image from URL" functionality

This commit adds an user-agent string to the download request wich prevents some sites from
returning a 403 (forbidden) status.
Clicking the download button on the thumbnail, also showed the thumbnail because the event wasn't stopped.
The parts thumbnail didn't update after a succesfull download of the image, reloadImage was never called.

* Fixed copy and paste errors in previous commit

* Reverted back the user-agent  immitation from the previous commit

* Add a user-agent overide setting

Add a setting to allow overiding the default user-agent  that is used for downloading images and files from a URL

* Fixed a typo in the description

* Another typo fixed

* Removed cast to string to prevent using 'None' as the user-agent

* Thumbnail delete button not visible after upload

The delete button on the image thumbnails of both parts and companies
isn't showing when we upload a new image. This is fixed by always
including the element on the page, and use javascript to show and hide
the element.
This commit is contained in:
bloemp 2022-12-24 13:17:04 +01:00 committed by GitHub
parent cd2495c3ef
commit e1d1b51936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 9 deletions

View File

@ -145,12 +145,20 @@ def download_image_from_url(remote_url, timeout=2.5):
# Calculate maximum allowable image size (in bytes) # Calculate maximum allowable image size (in bytes)
max_size = int(InvenTreeSetting.get_setting('INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE')) * 1024 * 1024 max_size = int(InvenTreeSetting.get_setting('INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE')) * 1024 * 1024
# Add user specified user-agent to request (if specified)
user_agent = InvenTreeSetting.get_setting('INVENTREE_DOWNLOAD_FROM_URL_USER_AGENT')
if user_agent:
headers = {"User-Agent": user_agent}
else:
headers = None
try: try:
response = requests.get( response = requests.get(
remote_url, remote_url,
timeout=timeout, timeout=timeout,
allow_redirects=True, allow_redirects=True,
stream=True, stream=True,
headers=headers,
) )
# Throw an error if anything goes wrong # Throw an error if anything goes wrong
response.raise_for_status() response.raise_for_status()

View File

@ -875,6 +875,12 @@ class InvenTreeSetting(BaseInvenTreeSetting):
] ]
}, },
'INVENTREE_DOWNLOAD_FROM_URL_USER_AGENT': {
'name': _('User-agent used to download from URL'),
'description': _('Allow to override the user-agent used to download images and files from external URL (leave blank for the default)'),
'default': '',
},
'INVENTREE_REQUIRE_CONFIRM': { 'INVENTREE_REQUIRE_CONFIRM': {
'name': _('Require confirm'), 'name': _('Require confirm'),
'description': _('Require explicit user confirmation for certain action.'), 'description': _('Require explicit user confirmation for certain action.'),

View File

@ -58,9 +58,7 @@
{% if allow_download %} {% if allow_download %}
<button type='button' class='btn btn-outline-secondary' title="{% trans 'Download image from URL' %}" id='company-image-url'><span class='fas fa-cloud-download-alt'></span></button> <button type='button' class='btn btn-outline-secondary' title="{% trans 'Download image from URL' %}" id='company-image-url'><span class='fas fa-cloud-download-alt'></span></button>
{% endif %} {% endif %}
{% if company.image %}
<button type='button' class='btn btn-outline-secondary' title='{% trans "Delete image" %}' id='company-image-delete'><span class='fas fa-trash-alt icon-red'></span></button> <button type='button' class='btn btn-outline-secondary' title='{% trans "Delete image" %}' id='company-image-delete'><span class='fas fa-trash-alt icon-red'></span></button>
{% endif %}
</div> </div>
</div> </div>
</div> </div>
@ -176,6 +174,7 @@
showModalImage(data.image); showModalImage(data.image);
}); });
$('#company-image-delete').show();
} else { } else {
location.reload(); location.reload();
} }
@ -197,6 +196,9 @@
$('#company-image').click(function() { $('#company-image').click(function() {
showModalImage('{{ company.image.url }}'); showModalImage('{{ company.image.url }}');
}); });
{% else %}
$('#company-image-delete').hide();
{% endif %}
$('#company-image-delete').click(function(event) { $('#company-image-delete').click(function(event) {
event.stopPropagation(); event.stopPropagation();
@ -224,8 +226,6 @@
); );
}); });
{% endif %}
$("#company-image-upload").click(function(event) { $("#company-image-upload").click(function(event) {
event.stopPropagation(); event.stopPropagation();
constructForm( constructForm(
@ -244,8 +244,8 @@
}); });
if (global_settings.INVENTREE_DOWNLOAD_FROM_URL) { if (global_settings.INVENTREE_DOWNLOAD_FROM_URL) {
$('#company-image-url').click(function() { $('#company-image-url').click(function() {
event.stopPropagation();
constructForm( constructForm(
'{% url "api-company-detail" company.pk %}', '{% url "api-company-detail" company.pk %}',
{ {

View File

@ -391,6 +391,8 @@
$('#part-thumb').click(function() { $('#part-thumb').click(function() {
showModalImage('{{ part.image.url }}'); showModalImage('{{ part.image.url }}');
}); });
{% else %}
$('#part-image-delete').hide();
{% endif %} {% endif %}
function reloadImage(data) { function reloadImage(data) {
@ -403,6 +405,7 @@
showModalImage(data.image); showModalImage(data.image);
}); });
$("#part-image-delete").show();
} else { } else {
// Otherwise, reload the page // Otherwise, reload the page
location.reload(); location.reload();
@ -586,8 +589,8 @@
{% if roles.part.change %} {% if roles.part.change %}
if (global_settings.INVENTREE_DOWNLOAD_FROM_URL) { if (global_settings.INVENTREE_DOWNLOAD_FROM_URL) {
$("#part-image-url").click(function() { $("#part-image-url").click(function() {
event.stopPropagation();
constructForm( constructForm(
'{% url "api-part-detail" part.pk %}', '{% url "api-part-detail" part.pk %}',
{ {
@ -596,7 +599,9 @@
fields: { fields: {
remote_image: {}, remote_image: {},
}, },
onSuccess: onSelectImage, onSuccess: function(data) {
reloadImage(data);
},
} }
); );
}); });

View File

@ -13,9 +13,7 @@
{% if allow_download %} {% if allow_download %}
<button type='button' class='btn btn-outline-secondary' title="{% trans 'Download image from URL' %}" id='part-image-url'><span class='fas fa-cloud-download-alt'></span></button> <button type='button' class='btn btn-outline-secondary' title="{% trans 'Download image from URL' %}" id='part-image-url'><span class='fas fa-cloud-download-alt'></span></button>
{% endif %} {% endif %}
{% if part.image %}
<button type='button' class='btn btn-outline-secondary' title='{% trans "Delete image" %}' id='part-image-delete'><span class='fas fa-trash-alt icon-red'></span></button> <button type='button' class='btn btn-outline-secondary' title='{% trans "Delete image" %}' id='part-image-delete'><span class='fas fa-trash-alt icon-red'></span></button>
{% endif %}
</div> </div>
</div> </div>
{% endif %} {% endif %}

View File

@ -21,6 +21,7 @@
<tr><td colspan='5'></td></tr> <tr><td colspan='5'></td></tr>
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL" icon="fa-cloud-download-alt" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL" icon="fa-cloud-download-alt" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE" icon="fa-server" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE" icon="fa-server" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL_USER_AGENT" icon="fa-server" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_REQUIRE_CONFIRM" icon="fa-check" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_REQUIRE_CONFIRM" icon="fa-check" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_TREE_DEPTH" icon="fa-sitemap" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_TREE_DEPTH" icon="fa-sitemap" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_BACKUP_ENABLE" icon="fa-hdd" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_BACKUP_ENABLE" icon="fa-hdd" %}