2016-12-08 03:50:48 +00:00
|
|
|
import mimetypes
|
|
|
|
import logging
|
|
|
|
import hashlib
|
2017-01-20 19:38:41 +00:00
|
|
|
import os
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
from django.db.transaction import atomic
|
|
|
|
from django.core.files.base import ContentFile
|
|
|
|
from django.core.files.storage import Storage
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
|
|
from .models import DBFile
|
|
|
|
|
|
|
|
L = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-12-08 20:43:34 +00:00
|
|
|
class DBFileStorage(Storage):
|
2016-12-08 03:50:48 +00:00
|
|
|
"""
|
|
|
|
This is the Test Database file upload storage backend.
|
|
|
|
This is used so that in our test database we always have uploaded
|
|
|
|
files.
|
|
|
|
|
|
|
|
To read more about how to set it up and configure it:
|
|
|
|
https://docs.djangoproject.com/en/1.8/howto/custom-file-storage
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _open(self, name, mode='rb'):
|
|
|
|
the_file = DBFile.objects.get(pk=name)
|
|
|
|
return ContentFile(the_file.b64.decode('base64'))
|
|
|
|
|
|
|
|
@atomic
|
|
|
|
def _save(self, name, content, max_length=None):
|
|
|
|
"""
|
|
|
|
The save method does most of the 'magic'.
|
|
|
|
It stores the contents of the file as a base64 string.
|
2016-12-08 18:43:12 +00:00
|
|
|
It then takes the filename, and tries to get the mimetype from that
|
|
|
|
(for rendering)
|
|
|
|
Then it takes the md5 of the read file and uses that as the "unique"
|
|
|
|
key to access the file.
|
|
|
|
Then it checks if the file exists and if it doesn't, it will create
|
|
|
|
the entry in the database.
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
:return str: the name(md5) to look up the file by.
|
|
|
|
"""
|
|
|
|
if hasattr(content.file, "read"):
|
|
|
|
read_data = content.file.read()
|
|
|
|
else:
|
|
|
|
read_data = content.file.encode('utf8')
|
|
|
|
b64 = read_data.encode('base64')
|
|
|
|
|
2017-01-10 16:29:28 +00:00
|
|
|
# USE mimetypes.guess_type as an attempt at getting the content type.
|
|
|
|
ct = mimetypes.guess_type(name)[0]
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
# After we get the mimetype by name potentially, mangle it.
|
2017-01-20 19:38:41 +00:00
|
|
|
filehash = hashlib.md5(read_data).hexdigest()
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
# create the file, or just return name if the exact file already exists
|
|
|
|
if not DBFile.objects.filter(pk=name).exists():
|
|
|
|
the_file = DBFile(
|
2017-01-20 19:38:41 +00:00
|
|
|
name=os.path.basename(name),
|
|
|
|
filehash=filehash,
|
2016-12-08 03:50:48 +00:00
|
|
|
content_type=ct,
|
|
|
|
b64=b64)
|
|
|
|
the_file.save()
|
2017-01-20 19:38:41 +00:00
|
|
|
return filehash
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
def get_available_name(self, name, max_length=None):
|
|
|
|
return name
|
|
|
|
|
|
|
|
def path(self, name):
|
|
|
|
return name
|
|
|
|
|
|
|
|
def delete(self, name):
|
|
|
|
assert name, "The name argument is not allowed to be empty."
|
2017-01-10 16:29:28 +00:00
|
|
|
# name is the Pk, so it will be unique, deleting on the QS so
|
|
|
|
# that it fails silently if the file doesn't exist.
|
|
|
|
DBFile.objects.filter(pk=name).delete()
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
def exists(self, name):
|
|
|
|
return DBFile.objects.filter(pk=name).exists()
|
|
|
|
|
|
|
|
def size(self, name):
|
|
|
|
dbf = DBFile.objects.get(pk=name)
|
|
|
|
return len(dbf.b64)
|
|
|
|
|
|
|
|
def url(self, name):
|
|
|
|
return reverse('dbstorage_file', args=(name,))
|