|
1 |
| -from io import FileIO, BufferedWriter |
2 | 1 | import os
|
| 2 | +from io import FileIO, BufferedWriter |
3 | 3 |
|
4 |
| -from django.conf import settings |
| 4 | + |
| 5 | +from django.core.files.storage import default_storage |
| 6 | +from django.core.files.base import ContentFile |
| 7 | +from django.template.loader import render_to_string |
5 | 8 |
|
6 | 9 | from ajaxuploader.backends.base import AbstractUploadBackend
|
7 | 10 |
|
| 11 | + |
8 | 12 | class LocalUploadBackend(AbstractUploadBackend):
|
9 |
| - UPLOAD_DIR = "uploads" |
| 13 | + UPLOAD_DIR = 'uploads' |
10 | 14 |
|
11 | 15 | def setup(self, filename):
|
12 |
| - self._path = os.path.join( |
13 |
| - settings.MEDIA_ROOT, self.UPLOAD_DIR, filename) |
14 |
| - try: |
15 |
| - os.makedirs(os.path.realpath(os.path.dirname(self._path))) |
16 |
| - except: |
17 |
| - pass |
18 |
| - self._dest = BufferedWriter(FileIO(self._path, "w")) |
| 16 | + # join UPLOAD_DIR with filename |
| 17 | + new_path = os.path.join(self.UPLOAD_DIR, filename) |
| 18 | + |
| 19 | + # save empty file in default storage with path = new_path |
| 20 | + self._path = default_storage.save(new_path, ContentFile('')) |
| 21 | + |
| 22 | + # get absolute path to new file |
| 23 | + self._abs_path = default_storage.path(self._path) |
| 24 | + |
| 25 | + # create BufferedWriter for new file |
| 26 | + self._dest = BufferedWriter(FileIO(self._abs_path, "w")) |
19 | 27 |
|
20 | 28 | def upload_chunk(self, chunk):
|
21 | 29 | self._dest.write(chunk)
|
22 | 30 |
|
23 | 31 | def upload_complete(self, request, filename):
|
24 |
| - path = settings.MEDIA_URL + self.UPLOAD_DIR + "/" + filename |
25 | 32 | self._dest.close()
|
26 |
| - return {"path": path} |
| 33 | + return {"path": self._path} |
0 commit comments