Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Commit 9e1ce66

Browse files
committed
replace default storage with storage setting
1 parent 399a657 commit 9e1ce66

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

ckeditor_uploader/image/pillow_backend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from io import BytesIO
55

66
from django.conf import settings
7-
from django.core.files.storage import default_storage
87
from django.core.files.uploadedfile import InMemoryUploadedFile
8+
from ckeditor_uploader.utils import storage
99

1010
from PIL import Image, ImageOps
1111

@@ -25,7 +25,7 @@ def create_thumbnail(file_path):
2525
thumbnail_filename = utils.get_thumb_filename(file_path)
2626
thumbnail_format = utils.get_image_format(os.path.splitext(file_path)[1])
2727

28-
image = default_storage.open(file_path)
28+
image = storage.open(file_path)
2929
image = Image.open(image)
3030
file_format = image.format
3131

@@ -49,11 +49,11 @@ def create_thumbnail(file_path):
4949
None)
5050
thumbnail.seek(0)
5151

52-
return default_storage.save(thumbnail_filename, thumbnail)
52+
return storage.save(thumbnail_filename, thumbnail)
5353

5454

5555
def should_create_thumbnail(file_path):
56-
image = default_storage.open(file_path)
56+
image = storage.open(file_path)
5757
try:
5858
Image.open(image)
5959
except IOError:

ckeditor_uploader/utils.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import string
88

99
from django.conf import settings
10-
from django.core.files.storage import default_storage
1110
from django.template.defaultfilters import slugify
1211
from django.utils.encoding import force_text
12+
from django.utils.module_loading import import_string
1313

1414
# Non-image file icons, matched from top to bottom
15-
fileicons_path = '{0}/file-icons/'.format(getattr(settings, 'CKEDITOR_FILEICONS_PATH', '/static/ckeditor'))
16-
# This allows adding or overriding the default icons used by Gallerific by getting an additional two-tuple list from
17-
# the project settings. If it does not exist, it is ignored. If the same file extension exists twice, the settings
15+
fileicons_path = '{0}/file-icons/'.format(getattr(
16+
settings, 'CKEDITOR_FILEICONS_PATH', '/static/ckeditor'))
17+
# This allows adding or overriding the default icons used by Gallerific by
18+
# getting an additional two-tuple list from
19+
# the project settings. If it does not exist, it is ignored. If the same
20+
# file extension exists twice, the settings
1821
# file version is used instead of the default.
1922
override_icons = getattr(settings, 'CKEDITOR_FILEICONS', [])
2023
ckeditor_icons = [
@@ -32,6 +35,14 @@ class NotAnImageException(Exception):
3235
pass
3336

3437

38+
def get_storage_class():
39+
return import_string(getattr(settings, 'CKEDITOR_STORAGE_BACKEND',
40+
'django.core.files.storage.DefaultStorage'))()
41+
42+
43+
storage = get_storage_class
44+
45+
3546
def slugify_filename(filename):
3647
""" Slugify filename """
3748
name, ext = os.path.splitext(filename)
@@ -74,7 +85,7 @@ def get_media_url(path):
7485
"""
7586
Determine system file's media URL.
7687
"""
77-
return default_storage.url(path)
88+
return storage.url(path)
7889

7990

8091
def is_valid_image_extension(file_path):

ckeditor_uploader/views.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from datetime import datetime
55

66
from django.conf import settings
7-
from django.core.files.storage import default_storage
87
from django.http import HttpResponse, JsonResponse
98
from django.shortcuts import render
109
from django.utils.html import escape
@@ -16,6 +15,7 @@
1615

1716
from ckeditor_uploader import image_processing, utils
1817
from ckeditor_uploader.forms import SearchForm
18+
from ckeditor_uploader.utils import storage
1919

2020

2121
def _get_user_path(user):
@@ -61,7 +61,7 @@ def get_upload_filename(upload_name, user):
6161
generator = import_string(settings.CKEDITOR_FILENAME_GENERATOR)
6262
upload_name = generator(upload_name)
6363

64-
return default_storage.get_available_name(
64+
return storage.get_available_name(
6565
os.path.join(upload_path, upload_name)
6666
)
6767

@@ -118,18 +118,18 @@ def _save_file(request, uploaded_file):
118118

119119
img = Image.open(uploaded_file)
120120
img = img.resize(img.size, Image.ANTIALIAS)
121-
saved_path = default_storage.save("{}.jpg".format(img_name), uploaded_file)
121+
saved_path = storage.save("{}.jpg".format(img_name), uploaded_file)
122122
img.save("{}.jpg".format(img_name), quality=IMAGE_QUALITY, optimize=True)
123123

124124
elif(str(img_format).lower() == "jpg" or str(img_format).lower() == "jpeg"):
125125

126126
img = Image.open(uploaded_file)
127127
img = img.resize(img.size, Image.ANTIALIAS)
128-
saved_path = default_storage.save(filename, uploaded_file)
128+
saved_path = storage.save(filename, uploaded_file)
129129
img.save(saved_path, quality=IMAGE_QUALITY, optimize=True)
130130

131131
else:
132-
saved_path = default_storage.save(filename, uploaded_file)
132+
saved_path = storage.save(filename, uploaded_file)
133133

134134
return saved_path
135135

@@ -162,7 +162,7 @@ def get_image_files(user=None, path=''):
162162
browse_path = os.path.join(settings.CKEDITOR_UPLOAD_PATH, user_path, path)
163163

164164
try:
165-
storage_list = default_storage.listdir(browse_path)
165+
storage_list = storage.listdir(browse_path)
166166
except NotImplementedError:
167167
return
168168
except OSError:

0 commit comments

Comments
 (0)