Add thumbnail generation

This commit is contained in:
Skia
2016-11-20 11:56:33 +01:00
parent 71d22e367b
commit 869634d6e1
12 changed files with 104 additions and 12 deletions

View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
import core.models
class Migration(migrations.Migration):
dependencies = [
('core', '0008_sithfile_asked_for_removal'),
]
operations = [
migrations.AddField(
model_name='sithfile',
name='compressed',
field=models.FileField(upload_to=core.models.get_compressed_directory, null=True, verbose_name='compressed file', blank=True),
),
migrations.AddField(
model_name='sithfile',
name='thumbnail',
field=models.FileField(upload_to=core.models.get_thumbnail_directory, null=True, verbose_name='thumbnail', blank=True),
),
migrations.AlterField(
model_name='user',
name='home',
field=models.OneToOneField(verbose_name='home', related_name='home_of', on_delete=django.db.models.deletion.SET_NULL, null=True, to='core.SithFile', blank=True),
),
]

View File

@ -115,7 +115,8 @@ class User(AbstractBaseUser):
),
)
groups = models.ManyToManyField(RealGroup, related_name='users', blank=True)
home = models.OneToOneField('SithFile', related_name='home_of', verbose_name=_("home"), null=True, blank=True)
home = models.OneToOneField('SithFile', related_name='home_of', verbose_name=_("home"), null=True, blank=True,
on_delete=models.SET_NULL)
profile_pict = models.OneToOneField('SithFile', related_name='profile_of', verbose_name=_("profile"), null=True,
blank=True, on_delete=models.SET_NULL)
avatar_pict = models.OneToOneField('SithFile', related_name='avatar_of', verbose_name=_("avatar"), null=True,
@ -491,10 +492,18 @@ class Preferences(models.Model):
def get_directory(instance, filename):
return '.{0}/{1}'.format(instance.get_parent_path(), filename)
def get_compressed_directory(instance, filename):
return '.{0}/compressed/{1}'.format(instance.get_parent_path(), filename)
def get_thumbnail_directory(instance, filename):
return '.{0}/thumbnail/{1}'.format(instance.get_parent_path(), filename)
class SithFile(models.Model):
name = models.CharField(_('file name'), max_length=256, blank=False)
parent = models.ForeignKey('self', related_name="children", verbose_name=_("parent"), null=True, blank=True)
file = models.FileField(upload_to=get_directory, verbose_name=_("file"), null=True, blank=True)
compressed = models.FileField(upload_to=get_compressed_directory, verbose_name=_("compressed file"), null=True, blank=True)
thumbnail = models.FileField(upload_to=get_thumbnail_directory, verbose_name=_("thumbnail"), null=True, blank=True)
owner = models.ForeignKey(User, related_name="owned_files", verbose_name=_("owner"))
edit_groups = models.ManyToManyField(Group, related_name="editable_files", verbose_name=_("edit group"), blank=True)
view_groups = models.ManyToManyField(Group, related_name="viewable_files", verbose_name=_("view group"), blank=True)
@ -528,6 +537,10 @@ class SithFile(models.Model):
for c in self.children.all():
c.delete()
self.file.delete()
if self.compressed:
self.compressed.delete()
if self.thumbnail:
self.thumbnail.delete()
return super(SithFile, self).delete()
def clean(self):
@ -605,7 +618,7 @@ class SithFile(models.Model):
return l
def get_parent_path(self):
return '/' + '/'.join([p.name for p in self.get_parent_list()])
return '/' + '/'.join([p.name for p in self.get_parent_list()[::-1]])
def get_display_name(self):
return self.name

View File

@ -13,7 +13,7 @@ def scale_dimension(width, height, long_edge):
ratio = long_edge * 1. / height
return int(width * ratio), int(height * ratio)
def resize_image(im, edge, format): # TODO move that into a utils file
def resize_image(im, edge, format):
(w, h) = im.size
(width, height) = scale_dimension(w, h, long_edge=edge)
content = BytesIO()

View File

@ -19,7 +19,7 @@ import os
from core.models import SithFile
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin, can_view, not_found
def send_file(request, file_id, file_class=SithFile):
def send_file(request, file_id, file_class=SithFile, file_attr="file"):
"""
Send a file through Django without loading the whole file into
memory at once. The FileWrapper will turn the file object into an
@ -35,7 +35,7 @@ def send_file(request, file_id, file_class=SithFile):
Counter.objects.filter(token=request.session['counter_token']).exists())
):
raise PermissionDenied
name = f.file.name
name = f.__getattribute__(file_attr).name
with open(settings.MEDIA_ROOT + name, 'rb') as filename:
wrapper = FileWrapper(filename)
response = HttpResponse(wrapper, content_type=f.mime_type)