Spaces:
Sleeping
Sleeping
zamo commited on
Commit ·
68d341f
1
Parent(s): e28f22c
fixing the uploading image error
Browse files- Dockerfile +5 -3
- config/urls.py +6 -3
- fitting_system/storage.py +17 -5
Dockerfile
CHANGED
|
@@ -3,7 +3,8 @@ FROM python:3.11-slim
|
|
| 3 |
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
PYTHONUNBUFFERED=1 \
|
| 5 |
PIP_NO_CACHE_DIR=1 \
|
| 6 |
-
PORT=7860
|
|
|
|
| 7 |
|
| 8 |
# Install required system packages for OpenCV
|
| 9 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
@@ -33,9 +34,10 @@ RUN pip install --upgrade pip && \
|
|
| 33 |
COPY --chown=user . .
|
| 34 |
|
| 35 |
# Create necessary directories with proper user permissions
|
| 36 |
-
RUN mkdir -p $HOME/app/staticfiles $HOME/app/media
|
| 37 |
|
| 38 |
EXPOSE 7860
|
| 39 |
|
| 40 |
# Run migrations, collect static files, and start Gunicorn (all as user 1000)
|
| 41 |
-
|
|
|
|
|
|
| 3 |
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
PYTHONUNBUFFERED=1 \
|
| 5 |
PIP_NO_CACHE_DIR=1 \
|
| 6 |
+
PORT=7860 \
|
| 7 |
+
MEDIA_ROOT=/data/media
|
| 8 |
|
| 9 |
# Install required system packages for OpenCV
|
| 10 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
|
|
| 34 |
COPY --chown=user . .
|
| 35 |
|
| 36 |
# Create necessary directories with proper user permissions
|
| 37 |
+
RUN mkdir -p $HOME/app/staticfiles $HOME/app/media /data/media
|
| 38 |
|
| 39 |
EXPOSE 7860
|
| 40 |
|
| 41 |
# Run migrations, collect static files, and start Gunicorn (all as user 1000)
|
| 42 |
+
# Catalog reset is optional to avoid deleting products/images on each restart.
|
| 43 |
+
CMD ["sh", "-c", "python manage.py migrate && python manage.py collectstatic --noinput && if [ \"${RUN_RESET_CATALOG:-0}\" = \"1\" ]; then python manage.py reset_catalog; fi && python manage.py create_admin && gunicorn config.wsgi:application --bind 0.0.0.0:${PORT:-7860} --workers 2 --timeout 120"]
|
config/urls.py
CHANGED
|
@@ -15,10 +15,11 @@ Including another URLconf
|
|
| 15 |
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
| 16 |
"""
|
| 17 |
from django.contrib import admin
|
| 18 |
-
from django.urls import path, include
|
| 19 |
from django.conf import settings
|
| 20 |
from django.conf.urls.static import static
|
| 21 |
from django.conf.urls.i18n import i18n_patterns
|
|
|
|
| 22 |
|
| 23 |
urlpatterns = [
|
| 24 |
path('admin/', admin.site.urls),
|
|
@@ -32,7 +33,9 @@ urlpatterns += i18n_patterns(
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# WhiteNoise serves STATIC_URL in production.
|
| 35 |
-
#
|
| 36 |
-
# files may still be referenced.
|
| 37 |
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
| 15 |
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
| 16 |
"""
|
| 17 |
from django.contrib import admin
|
| 18 |
+
from django.urls import path, include, re_path
|
| 19 |
from django.conf import settings
|
| 20 |
from django.conf.urls.static import static
|
| 21 |
from django.conf.urls.i18n import i18n_patterns
|
| 22 |
+
from django.views.static import serve
|
| 23 |
|
| 24 |
urlpatterns = [
|
| 25 |
path('admin/', admin.site.urls),
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
# WhiteNoise serves STATIC_URL in production.
|
| 36 |
+
# Media files need explicit routing under Gunicorn deployments.
|
|
|
|
| 37 |
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
| 38 |
+
urlpatterns += [
|
| 39 |
+
re_path(r"^media/(?P<path>.*)$", serve, {"document_root": settings.MEDIA_ROOT}),
|
| 40 |
+
]
|
| 41 |
|
fitting_system/storage.py
CHANGED
|
@@ -6,8 +6,8 @@ from django.core.files.storage import FileSystemStorage
|
|
| 6 |
|
| 7 |
class ProductImageStorage(FileSystemStorage):
|
| 8 |
"""
|
| 9 |
-
Save
|
| 10 |
-
|
| 11 |
"""
|
| 12 |
|
| 13 |
def __init__(self, *args, **kwargs):
|
|
@@ -15,25 +15,37 @@ class ProductImageStorage(FileSystemStorage):
|
|
| 15 |
media_base_url = settings.MEDIA_URL if settings.MEDIA_URL.endswith("/") else f"{settings.MEDIA_URL}/"
|
| 16 |
|
| 17 |
super().__init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
location=Path(settings.BASE_DIR) / "static",
|
| 19 |
base_url=static_base_url,
|
| 20 |
)
|
| 21 |
-
self.
|
| 22 |
location=Path(settings.BASE_DIR) / "media",
|
| 23 |
base_url=media_base_url,
|
| 24 |
)
|
| 25 |
|
| 26 |
def exists(self, name):
|
| 27 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def open(self, name, mode="rb"):
|
| 30 |
if super().exists(name):
|
| 31 |
return super().open(name, mode)
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def url(self, name):
|
| 35 |
if super().exists(name):
|
| 36 |
return super().url(name)
|
| 37 |
if self.legacy_storage.exists(name):
|
| 38 |
return self.legacy_storage.url(name)
|
|
|
|
|
|
|
| 39 |
return super().url(name)
|
|
|
|
| 6 |
|
| 7 |
class ProductImageStorage(FileSystemStorage):
|
| 8 |
"""
|
| 9 |
+
Save product uploads into MEDIA_ROOT and keep older static/media paths
|
| 10 |
+
readable for backward compatibility.
|
| 11 |
"""
|
| 12 |
|
| 13 |
def __init__(self, *args, **kwargs):
|
|
|
|
| 15 |
media_base_url = settings.MEDIA_URL if settings.MEDIA_URL.endswith("/") else f"{settings.MEDIA_URL}/"
|
| 16 |
|
| 17 |
super().__init__(
|
| 18 |
+
location=settings.MEDIA_ROOT,
|
| 19 |
+
base_url=media_base_url,
|
| 20 |
+
)
|
| 21 |
+
self.legacy_storage = FileSystemStorage(
|
| 22 |
location=Path(settings.BASE_DIR) / "static",
|
| 23 |
base_url=static_base_url,
|
| 24 |
)
|
| 25 |
+
self.legacy_media_storage = FileSystemStorage(
|
| 26 |
location=Path(settings.BASE_DIR) / "media",
|
| 27 |
base_url=media_base_url,
|
| 28 |
)
|
| 29 |
|
| 30 |
def exists(self, name):
|
| 31 |
+
return (
|
| 32 |
+
super().exists(name)
|
| 33 |
+
or self.legacy_storage.exists(name)
|
| 34 |
+
or self.legacy_media_storage.exists(name)
|
| 35 |
+
)
|
| 36 |
|
| 37 |
def open(self, name, mode="rb"):
|
| 38 |
if super().exists(name):
|
| 39 |
return super().open(name, mode)
|
| 40 |
+
if self.legacy_storage.exists(name):
|
| 41 |
+
return self.legacy_storage.open(name, mode)
|
| 42 |
+
return self.legacy_media_storage.open(name, mode)
|
| 43 |
|
| 44 |
def url(self, name):
|
| 45 |
if super().exists(name):
|
| 46 |
return super().url(name)
|
| 47 |
if self.legacy_storage.exists(name):
|
| 48 |
return self.legacy_storage.url(name)
|
| 49 |
+
if self.legacy_media_storage.exists(name):
|
| 50 |
+
return self.legacy_media_storage.url(name)
|
| 51 |
return super().url(name)
|