2018-03-01 20:32:02 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
|
|
|
import base64
|
2016-12-08 03:50:48 +00:00
|
|
|
import mimetypes
|
|
|
|
import logging
|
2017-01-20 19:38:41 +00:00
|
|
|
import os
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
from django.db.transaction import atomic
|
2017-01-20 21:22:59 +00:00
|
|
|
from django.db.models import Q
|
2016-12-08 03:50:48 +00:00
|
|
|
from django.core.files.base import ContentFile
|
|
|
|
from django.core.files.storage import Storage
|
2018-03-01 20:32:02 +00:00
|
|
|
from django.core.urlresolvers import reverse_lazy
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
from .models import DBFile
|
|
|
|
|
|
|
|
L = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-01-20 21:22:59 +00:00
|
|
|
def _get_object(param):
|
2017-01-27 20:52:02 +00:00
|
|
|
return DBFile.objects.filter(name=param).first()
|
2017-01-20 21:22:59 +00:00
|
|
|
|
|
|
|
|
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'):
|
2017-01-23 19:08:51 +00:00
|
|
|
the_file = _get_object(name)
|
2018-03-01 20:32:02 +00:00
|
|
|
return ContentFile(base64.b64decode(the_file.b64))
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
2018-03-01 20:32:02 +00:00
|
|
|
:param name: file name to save
|
|
|
|
:param content: Content object, where content.file is bytes
|
|
|
|
|
2016-12-08 03:50:48 +00:00
|
|
|
:return str: the name(md5) to look up the file by.
|
|
|
|
"""
|
2018-03-01 20:32:02 +00:00
|
|
|
|
|
|
|
# TODO: Make this conditional better
|
2016-12-08 03:50:48 +00:00
|
|
|
if hasattr(content.file, "read"):
|
|
|
|
read_data = content.file.read()
|
2017-08-17 15:15:44 +00:00
|
|
|
if not read_data:
|
|
|
|
# If it's empty, try to seek once.
|
|
|
|
content.file.seek(0)
|
|
|
|
read_data = content.file.read()
|
2016-12-08 03:50:48 +00:00
|
|
|
else:
|
2018-03-01 20:32:02 +00:00
|
|
|
read_data = content.file
|
|
|
|
|
|
|
|
b64 = base64.b64encode(read_data)
|
2016-12-08 03:50:48 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
# create the file, or just return name if the exact file already exists
|
2017-05-10 14:46:12 +00:00
|
|
|
the_file = DBFile.objects.filter(name=name).first()
|
|
|
|
if not the_file:
|
|
|
|
DBFile.objects.create(
|
2017-01-23 19:08:51 +00:00
|
|
|
name=name,
|
2016-12-08 03:50:48 +00:00
|
|
|
content_type=ct,
|
|
|
|
b64=b64)
|
2017-05-10 14:46:12 +00:00
|
|
|
else:
|
|
|
|
the_file.content_type=ct
|
|
|
|
the_file.b64 = b64
|
|
|
|
the_file.save()
|
|
|
|
|
2017-01-23 19:08:51 +00:00
|
|
|
return name
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
def get_available_name(self, name, max_length=None):
|
|
|
|
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.
|
2017-02-06 20:31:40 +00:00
|
|
|
DBFile.objects.filter(name=name).delete()
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
def exists(self, name):
|
2017-01-27 20:52:02 +00:00
|
|
|
return DBFile.objects.filter(name=name).exists()
|
2016-12-08 03:50:48 +00:00
|
|
|
|
|
|
|
def size(self, name):
|
2017-01-23 19:08:51 +00:00
|
|
|
dbf = _get_object(name)
|
2016-12-08 03:50:48 +00:00
|
|
|
return len(dbf.b64)
|
|
|
|
|
|
|
|
def url(self, name):
|
2017-01-23 19:08:51 +00:00
|
|
|
dbf = _get_object(name)
|
|
|
|
if dbf:
|
2018-03-01 20:32:02 +00:00
|
|
|
return reverse_lazy('dbstorage_file', args=(dbf.name,))
|
|
|
|
return reverse_lazy('dbstorage_file', args=(name,))
|
2017-01-23 20:56:51 +00:00
|
|
|
|
|
|
|
def modified_time(self, name):
|
|
|
|
dbf = _get_object(name)
|
|
|
|
return dbf.mtime
|
2017-08-11 15:30:15 +00:00
|
|
|
|
|
|
|
def listdir(self, path):
|
|
|
|
dirs = [] # this doesn't support dirs, so just empty list
|
|
|
|
files = sorted(DBFile.objects.filter(
|
|
|
|
name__startswith=path
|
|
|
|
).values_list(
|
|
|
|
"name", flat=True
|
|
|
|
))
|
|
|
|
return (dirs, files)
|