2020-08-23 14:39:31 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
2019-06-10 12:33:46 +00:00
|
|
|
import slugify
|
2018-03-25 13:40:37 +00:00
|
|
|
|
|
|
|
from django.core.files.storage import FileSystemStorage
|
2019-06-10 12:33:46 +00:00
|
|
|
from storages.backends.s3boto3 import S3Boto3Storage
|
2018-03-25 13:40:37 +00:00
|
|
|
|
|
|
|
|
2019-06-10 12:33:46 +00:00
|
|
|
def asciionly(name):
|
2018-03-25 13:40:37 +00:00
|
|
|
"""
|
|
|
|
Convert unicode characters in name to ASCII characters.
|
|
|
|
"""
|
2019-06-10 12:33:46 +00:00
|
|
|
return slugify.slugify(name, ok=slugify.SLUG_OK + ".", only_ascii=True)
|
|
|
|
|
|
|
|
|
|
|
|
class ASCIIFileSystemStorage(FileSystemStorage):
|
|
|
|
def get_valid_name(self, name):
|
|
|
|
return super().get_valid_name(asciionly(name))
|
|
|
|
|
2020-08-23 14:39:31 +00:00
|
|
|
def force_delete(self, name):
|
|
|
|
path = self.path(name)
|
|
|
|
try:
|
|
|
|
if os.path.isdir(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
else:
|
|
|
|
return super().delete(name)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
|
2019-06-10 12:33:46 +00:00
|
|
|
class ASCIIS3Boto3Storage(S3Boto3Storage):
|
2018-03-25 13:40:37 +00:00
|
|
|
def get_valid_name(self, name):
|
2019-06-10 12:33:46 +00:00
|
|
|
return super().get_valid_name(asciionly(name))
|