Files
privatestudio/privatestudio/settings/base.py
T
Shaun KrugerandClaude Opus 5.5 3bbb57760e Add resumable multipart uploads for large files
Stream uploads and downloads in 1 MB blocks so memory use does not grow
with file size, under both WSGI and ASGI.

- S3 multipart upload API (create, upload part, list parts, complete,
  abort), so boto3 upload_file works for files over 8 MB.
- Browser uploader on the studio page: sends 16 MB parts three at a
  time with retries, and resumes when the same file is picked again.
  Uses session-authenticated, CSRF-protected JSON endpoints under
  /studio/<bucket>/uploads/.
- Request bodies are checked against the signed X-Amz-Content-Sha256
  instead of being read into memory; signed requests must include it.
  boto3's aws-chunked bodies with CRC32 trailers are decoded.
- Objects are written to a temporary file and renamed into place, so a
  failed upload leaves the existing file intact.
- Downloads stream with FileResponse, or an async iterator under ASGI.
- SigV4 verification uses the encoded path, fixing keys with spaces.
- cleanup_uploads command removes abandoned uploads and temp files.
- DATA_UPLOAD_MAX_MEMORY_SIZE is capped at 10 MB again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 17:29:46 -07:00

144 lines
4.1 KiB
Python

"""
Base Django settings for the privatestudio project.
Environment specific settings live in dev.py and production.py.
"""
import os
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
# Application definition
INSTALLED_APPS = [
"privatestudio.studio.app.StudioConfig",
"corsheaders",
"ninja",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"allauth",
"allauth.account",
"allauth.socialaccount",
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"allauth.account.middleware.AccountMiddleware",
]
MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage"
ROOT_URLCONF = "privatestudio.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
os.path.join(PROJECT_DIR, "templates"),
],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = "privatestudio.wsgi.application"
ASGI_APPLICATION = "privatestudio.asgi.application"
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"loggers": {
"privatestudio": {
"handlers": ["console"],
"level": "INFO",
},
},
}
# Database
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
AUTH_PASSWORD_VALIDATORS = [
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]
# Internationalization
LANGUAGE_CODE = "en-us"
TIME_ZONE = os.environ.get("DJANGO_TIME_ZONE", "UTC")
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"
# Object bodies are streamed, so this only limits API and form bodies.
DATA_UPLOAD_MAX_MEMORY_SIZE = 10 * 1024 * 1024
# Private Studio configuration
PRIVATESTUDIO_STORAGE_PATH = os.environ.get("STORAGE_PATH", os.path.join(BASE_DIR, "storage"))
PRIVATESTUDIO_S3_CREDENTIAL_TTL_HOURS = int(os.environ.get("S3_CREDENTIAL_TTL_HOURS", "1"))
# Size of the parts the browser uploader sends. Larger parts are used for files that need over 10,000.
PRIVATESTUDIO_UPLOAD_PART_SIZE = int(os.environ.get("UPLOAD_PART_SIZE_MB", "16")) * 1024 * 1024
# Largest multipart upload in bytes. 0 means no limit.
PRIVATESTUDIO_UPLOAD_MAX_SIZE = int(os.environ.get("UPLOAD_MAX_SIZE", "0"))
# cleanup_uploads removes unfinished uploads without activity for this long.
PRIVATESTUDIO_UPLOAD_EXPIRY_DAYS = int(os.environ.get("UPLOAD_EXPIRY_DAYS", "7"))