2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-11-30 01:10:00 +00:00

[bug] Media url fix (#10855)

* Bug fix for getMediaUrl

- Proper check for file type
- Fix "fully_qualified_url" for Attachment model

* Add unit test

* Fix typo

* Fix unit test

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
Oliver
2025-11-19 15:50:05 +11:00
committed by GitHub
parent 7b38fa30bb
commit a8f2a02d69
4 changed files with 25 additions and 6 deletions

View File

@@ -16,7 +16,7 @@ from django.conf import settings
from django.contrib.staticfiles.storage import StaticFilesStorage
from django.core.exceptions import FieldError, ValidationError
from django.core.files.storage import default_storage
from django.db.models.fields.files import ImageFieldFile
from django.db.models.fields.files import FieldFile, ImageFieldFile
from django.http import StreamingHttpResponse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
@@ -176,10 +176,14 @@ def constructPathString(path: list[str], max_chars: int = 250) -> str:
return pathstring
def getMediaUrl(file: StdImageFieldFile | ImageFieldFile, name: str | None = None):
def getMediaUrl(
file: FieldFile | ImageFieldFile | StdImageFieldFile, name: str | None = None
):
"""Return the qualified access path for the given file, under the media directory."""
if not isinstance(file, StdImageFieldFile):
raise ValueError('file_obj must be an instance of StdImageFieldFile')
if not isinstance(file, (FieldFile, ImageFieldFile, StdImageFieldFile)):
raise TypeError(
'file must be one of FileField, ImageFileField, StdImageFieldFile'
)
if name is not None:
file = regenerate_imagefile(file, name)
if settings.STORAGE_TARGET == StorageBackends.S3:

View File

@@ -702,7 +702,7 @@ class TestHelpers(TestCase):
def testMediaUrl(self):
"""Test getMediaUrl."""
# Str should not work
with self.assertRaises(ValueError):
with self.assertRaises(TypeError):
helpers.getMediaUrl('xx/yy.png') # type: ignore
# Correct usage

View File

@@ -2034,7 +2034,7 @@ class Attachment(InvenTree.models.MetadataMixin, InvenTree.models.InvenTreeModel
if self.attachment:
import InvenTree.helpers_model
media_url = InvenTree.helpers.getMediaUrl(self.attachment.url)
media_url = InvenTree.helpers.getMediaUrl(self.attachment)
return InvenTree.helpers_model.construct_absolute_url(media_url)
return ''

View File

@@ -202,6 +202,21 @@ class AttachmentTest(InvenTreeAPITestCase):
self.assignRole('part.delete')
self.delete(url, expected_code=204)
def test_fully_qualified_url(self):
"""Test that the fully qualified URL is returned correctly."""
part = Part.objects.first()
attachment = Attachment.objects.create(
attachment=self.generate_file('test.txt'),
comment='Testing filename: test.txt',
model_type='part',
model_id=part.pk,
)
url = attachment.fully_qualified_url()
self.assertIs(type(url), str)
self.assertIn(f'/media/attachments/part/{part.pk}/test', url)
class SettingsTest(InvenTreeTestCase):
"""Tests for the 'settings' model."""