2015-09-06 14:12:34 +02:00
|
|
|
import mimetypes
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2016-01-10 10:52:44 +01:00
|
|
|
from django.conf import settings
|
2015-09-06 14:12:34 +02:00
|
|
|
from django.db import models as dbmodels
|
2016-01-27 09:09:49 +01:00
|
|
|
from PyPDF2 import PdfFileReader
|
2016-11-18 09:40:36 +01:00
|
|
|
from PyPDF2.utils import PdfReadError
|
2015-09-06 14:12:34 +02:00
|
|
|
|
|
|
|
from ..utils.rest_api import FileField, ModelSerializer, SerializerMethodField
|
2015-01-24 16:35:50 +01:00
|
|
|
from .models import Mediafile
|
|
|
|
|
|
|
|
|
2015-09-06 14:12:34 +02:00
|
|
|
class AngularCompatibleFileField(FileField):
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
if data == '':
|
|
|
|
return None
|
|
|
|
return super(AngularCompatibleFileField, self).to_internal_value(data)
|
|
|
|
|
|
|
|
def to_representation(self, value):
|
|
|
|
if value is None:
|
|
|
|
return None
|
2016-01-27 09:09:49 +01:00
|
|
|
filetype = mimetypes.guess_type(value.path)[0]
|
|
|
|
result = {
|
2015-09-06 14:12:34 +02:00
|
|
|
'name': value.name,
|
2016-01-27 09:09:49 +01:00
|
|
|
'type': filetype
|
2015-09-06 14:12:34 +02:00
|
|
|
}
|
2016-01-27 09:09:49 +01:00
|
|
|
if filetype == 'application/pdf':
|
2016-02-23 22:34:50 +01:00
|
|
|
try:
|
|
|
|
result['pages'] = PdfFileReader(open(value.path, 'rb')).getNumPages()
|
|
|
|
except FileNotFoundError:
|
|
|
|
# File was deleted from server. Set 'pages' to 0.
|
|
|
|
result['pages'] = 0
|
2016-11-18 09:40:36 +01:00
|
|
|
except PdfReadError:
|
|
|
|
# File could be encrypted but not be detected by PyPDF.
|
|
|
|
result['pages'] = 0
|
|
|
|
result['encrypted'] = True
|
2016-01-27 09:09:49 +01:00
|
|
|
return result
|
2015-09-06 14:12:34 +02:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MediafileSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for mediafile.models.Mediafile objects.
|
|
|
|
"""
|
2016-01-10 10:52:44 +01:00
|
|
|
media_url_prefix = SerializerMethodField()
|
2015-02-12 18:48:14 +01:00
|
|
|
filesize = SerializerMethodField()
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2015-09-06 14:12:34 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
This constructor overwrites the FileField field serializer to return the file meta data in a way that the
|
|
|
|
angualarjs upload module likes
|
|
|
|
"""
|
|
|
|
super(MediafileSerializer, self).__init__(*args, **kwargs)
|
|
|
|
self.serializer_field_mapping[dbmodels.FileField] = AngularCompatibleFileField
|
2016-01-24 22:58:45 +01:00
|
|
|
if self.instance is not None:
|
|
|
|
self.fields['mediafile'].read_only = True
|
2015-09-06 14:12:34 +02:00
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
class Meta:
|
|
|
|
model = Mediafile
|
2015-02-04 00:08:38 +01:00
|
|
|
fields = (
|
|
|
|
'id',
|
|
|
|
'title',
|
|
|
|
'mediafile',
|
2016-01-10 10:52:44 +01:00
|
|
|
'media_url_prefix',
|
2015-02-04 00:08:38 +01:00
|
|
|
'uploader',
|
|
|
|
'filesize',
|
2016-12-12 16:15:04 +01:00
|
|
|
'hidden',
|
2015-09-06 14:12:34 +02:00
|
|
|
'timestamp',)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
def get_filesize(self, mediafile):
|
|
|
|
return mediafile.get_filesize()
|
2016-01-10 10:52:44 +01:00
|
|
|
|
|
|
|
def get_media_url_prefix(self, mediafile):
|
|
|
|
return settings.MEDIA_URL
|