mirror of
https://github.com/inventree/InvenTree.git
synced 2026-04-13 14:58:47 +00:00
Fix storage helpers (#11697)
* Fix storage helpers - Remove os.path reliance - Move to standard django accessors * Refactor rebuild_thumbnails
This commit is contained in:
@@ -8,6 +8,7 @@ import json
|
|||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
from decimal import Decimal, InvalidOperation
|
from decimal import Decimal, InvalidOperation
|
||||||
|
from pathlib import Path
|
||||||
from typing import Optional, TypeVar
|
from typing import Optional, TypeVar
|
||||||
from wsgiref.util import FileWrapper
|
from wsgiref.util import FileWrapper
|
||||||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||||
@@ -35,9 +36,6 @@ from InvenTree.sanitizer import (
|
|||||||
DEFAULT_TAGS,
|
DEFAULT_TAGS,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .setting.storages import StorageBackends
|
|
||||||
from .settings import MEDIA_URL, STATIC_URL
|
|
||||||
|
|
||||||
logger = structlog.get_logger('inventree')
|
logger = structlog.get_logger('inventree')
|
||||||
|
|
||||||
INT_CLIP_MAX = 0x7FFFFFFF
|
INT_CLIP_MAX = 0x7FFFFFFF
|
||||||
@@ -189,9 +187,8 @@ def getMediaUrl(
|
|||||||
)
|
)
|
||||||
if name is not None:
|
if name is not None:
|
||||||
file = regenerate_imagefile(file, name)
|
file = regenerate_imagefile(file, name)
|
||||||
if settings.STORAGE_TARGET == StorageBackends.S3:
|
|
||||||
return str(file.url)
|
return default_storage.url(file.name)
|
||||||
return os.path.join(MEDIA_URL, str(file.url))
|
|
||||||
|
|
||||||
|
|
||||||
def regenerate_imagefile(_file, _name: str):
|
def regenerate_imagefile(_file, _name: str):
|
||||||
@@ -229,7 +226,7 @@ def image2name(img_obj: StdImageField, do_preview: bool, do_thumbnail: bool):
|
|||||||
|
|
||||||
def getStaticUrl(filename):
|
def getStaticUrl(filename):
|
||||||
"""Return the qualified access path for the given file, under the static media directory."""
|
"""Return the qualified access path for the given file, under the static media directory."""
|
||||||
return os.path.join(STATIC_URL, str(filename))
|
return StaticFilesStorage().url(filename)
|
||||||
|
|
||||||
|
|
||||||
def TestIfImage(img) -> bool:
|
def TestIfImage(img) -> bool:
|
||||||
@@ -261,8 +258,8 @@ def getBlankThumbnail():
|
|||||||
def checkStaticFile(*args) -> bool:
|
def checkStaticFile(*args) -> bool:
|
||||||
"""Check if a file exists in the static storage."""
|
"""Check if a file exists in the static storage."""
|
||||||
static_storage = StaticFilesStorage()
|
static_storage = StaticFilesStorage()
|
||||||
fn = os.path.join(*args)
|
fn = Path(*args)
|
||||||
return static_storage.exists(fn)
|
return static_storage.exists(str(fn))
|
||||||
|
|
||||||
|
|
||||||
def getLogoImage(as_file=False, custom=True):
|
def getLogoImage(as_file=False, custom=True):
|
||||||
|
|||||||
@@ -3,8 +3,7 @@
|
|||||||
- May be required after importing a new dataset, for example
|
- May be required after importing a new dataset, for example
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
from django.core.files.storage import default_storage
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.db.utils import OperationalError, ProgrammingError
|
from django.db.utils import OperationalError, ProgrammingError
|
||||||
|
|
||||||
@@ -35,7 +34,7 @@ class Command(BaseCommand):
|
|||||||
img_paths.append(x.path)
|
img_paths.append(x.path)
|
||||||
|
|
||||||
if len(img_paths) > 0:
|
if len(img_paths) > 0:
|
||||||
if all(os.path.exists(path) for path in img_paths):
|
if all(default_storage.exists(p) for p in img_paths):
|
||||||
# All images exist - skip further work
|
# All images exist - skip further work
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -692,13 +692,16 @@ class TestHelpers(TestCase):
|
|||||||
self.assertFalse(helpers.isNull(s))
|
self.assertFalse(helpers.isNull(s))
|
||||||
|
|
||||||
def testStaticUrl(self):
|
def testStaticUrl(self):
|
||||||
"""Test static url helpers."""
|
"""Test static URL helpers."""
|
||||||
self.assertEqual(helpers.getStaticUrl('test.jpg'), '/static/test.jpg')
|
self.assertEqual(helpers.getStaticUrl('test.jpg'), '/static/test.jpg')
|
||||||
self.assertEqual(helpers.getBlankImage(), '/static/img/blank_image.png')
|
self.assertEqual(helpers.getBlankImage(), '/static/img/blank_image.png')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
helpers.getBlankThumbnail(), '/static/img/blank_image.thumbnail.png'
|
helpers.getBlankThumbnail(), '/static/img/blank_image.thumbnail.png'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.assertFalse(helpers.checkStaticFile('dummy', 'dir', 'test.jpg'))
|
||||||
|
self.assertTrue(helpers.checkStaticFile('img', 'blank_image.png'))
|
||||||
|
|
||||||
def testMediaUrl(self):
|
def testMediaUrl(self):
|
||||||
"""Test getMediaUrl."""
|
"""Test getMediaUrl."""
|
||||||
# Str should not work
|
# Str should not work
|
||||||
|
|||||||
Reference in New Issue
Block a user