|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import os
|
|
|
|
import warnings
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.files.storage import FileSystemStorage
|
|
|
|
from django.utils._os import safe_join
|
|
|
|
from django.utils.six.moves.urllib.parse import urljoin
|
|
|
|
|
|
|
|
|
|
|
|
ORIGINAL_FILE_PREFIX = 'original'
|
|
|
|
CACHED_VARIANT_PREFIX = 'cache'
|
|
|
|
MANUAL_VARIANT_PREFIX = 'manual'
|
|
|
|
|
|
|
|
|
|
|
|
# TODO ? @deconstructible
|
|
|
|
class ProtectedMediaAssetStorage(FileSystemStorage):
|
|
|
|
"""
|
|
|
|
Alllows uploads to /original/-subdirectories, but never provides URLs
|
|
|
|
to those files, instead raising an error.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ORIGINAL_FILE_PREFIX = ORIGINAL_FILE_PREFIX
|
|
|
|
|
|
|
|
def __init__(self, location=None, base_url=None, **kwargs):
|
|
|
|
if location is None:
|
|
|
|
location = getattr(settings, 'PROTECTED_MEDIA_ASSETS_ROOT', settings.PROTECTED_MEDIA_ASSETS_ROOT)
|
|
|
|
super(ProtectedMediaAssetStorage, self).__init__(location=location, base_url=None, **kwargs)
|
|
|
|
self.base_url = None
|
|
|
|
|
|
|
|
def delete(self, name):
|
|
|
|
super(ProtectedMediaAssetStorage, self).delete(name)
|
|
|
|
# FIXME Delete all cached files, too
|
|
|
|
warnings.warn("Cached files for asset \"%s\" are not deleted." % name)
|
|
|
|
|
|
|
|
def path(self, name):
|
|
|
|
"""
|
|
|
|
`name` must already contain the whole asset filesystem path.
|
|
|
|
"""
|
|
|
|
return safe_join(self.location, ORIGINAL_FILE_PREFIX, name)
|
|
|
|
|
|
|
|
def size(self, name):
|
|
|
|
return os.path.getsize(self.path(name))
|
|
|
|
|
|
|
|
def url(self, name):
|
|
|
|
raise ValueError("This file storage is not accessible via a URL.")
|
|
|
|
|
|
|
|
|
|
|
|
# TODO ? @deconstructible
|
|
|
|
# class
|
|
|
|
# return urljoin(self.base_url, ORIGINAL_FILE_PREFIX, filepath_to_uri(name))
|