A customed FileSystemStorage of Django providing asynchronous deletion of files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from .storage import AsyncFileSystemStorage | |
a_fs = AsyncFileSystemStorage() | |
class UploadedFile(models.Model): | |
image = models.ImageField('Uploaded image file', upload_to='.', storage=a_fs) | |
description = models.TextField('Description of uploaded image file') | |
def delete(self, *args, **kwargs): | |
self.image.delete() | |
super(File, self).delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.files.storage import FileSystemStorage | |
from .tasks import async_file_delete | |
class AsyncFileSystemStorage(FileSystemStorage): | |
def delete(self, name): | |
file_path = self.path(name) | |
async_file_delete.delay(file_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from celery import task | |
@task | |
def async_file_delete(file_path): | |
if os.path.exists(file_path): | |
try: | |
os.remove(file_path) | |
except OSError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment