pdf and epub previews
[redakcja.git] / apps / catalogue / ebook_utils.py
1 # -*- coding: utf-8 -*-
2 from catalogue.models import Book
3 from librarian import DocProvider
4 from django.http import HttpResponse
5
6
7 class RedakcjaDocProvider(DocProvider):
8     """Used for getting books' children."""
9
10     def by_slug(self, slug):
11         return Book.objects.get(dc_slug=slug).xml_file
12
13
14 def serve_file(file_path, name, mime_type):
15     def read_chunks(f, size=8192):
16         chunk = f.read(size)
17         while chunk:
18             yield chunk
19             chunk = f.read(size)
20
21     response = HttpResponse(mimetype=mime_type)
22     response['Content-Disposition'] = 'attachment; filename=%s' % name
23     with open(file_path) as f:
24         for chunk in read_chunks(f):
25             response.write(chunk)
26     return response