forked from 0x2620/pandora
add thumbnails of pdf pages #2148
This commit is contained in:
parent
d00b72d9eb
commit
167de4cc97
4 changed files with 31 additions and 17 deletions
|
@ -59,8 +59,6 @@ class Document(models.Model):
|
||||||
if not self.uploading:
|
if not self.uploading:
|
||||||
if self.file:
|
if self.file:
|
||||||
self.size = self.file.size
|
self.size = self.file.size
|
||||||
if self.extension == 'pdf' and not os.path.exists(self.thumbnail()):
|
|
||||||
self.make_thumbnail()
|
|
||||||
self.get_info()
|
self.get_info()
|
||||||
|
|
||||||
self.name_sort = ox.sort_string(self.name or u'')[:255].lower()
|
self.name_sort = ox.sort_string(self.name or u'')[:255].lower()
|
||||||
|
@ -208,15 +206,26 @@ class Document(models.Model):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def thumbnail(self, size=None):
|
def thumbnail(self, size=None, page=None):
|
||||||
src = self.file.path
|
src = self.file.path
|
||||||
if self.extension == 'pdf':
|
folder = os.path.dirname(src)
|
||||||
src = '%s.jpg' % src
|
|
||||||
if size:
|
if size:
|
||||||
size = int(size)
|
size = int(size)
|
||||||
path = src.replace('.jpg', '.%d.jpg'%size)
|
path = os.path.join(folder, '%d.jpg' % size)
|
||||||
else:
|
else:
|
||||||
path = src
|
path = src
|
||||||
|
if self.extension == 'pdf':
|
||||||
|
if page:
|
||||||
|
page = int(page)
|
||||||
|
if page and page > 1 and page <= self.pages:
|
||||||
|
src = os.path.join(folder, '1024p%d.jpg' % page)
|
||||||
|
else:
|
||||||
|
src = os.path.join(folder, '1024p1.jpg')
|
||||||
|
page = 1
|
||||||
|
if not os.path.exists(src):
|
||||||
|
self.extract_page(page)
|
||||||
|
if size:
|
||||||
|
path = os.path.join(folder, '%dp%d.jpg' % (size, page))
|
||||||
if os.path.exists(src) and not os.path.exists(path):
|
if os.path.exists(src) and not os.path.exists(path):
|
||||||
image_size = max(self.width, self.height)
|
image_size = max(self.width, self.height)
|
||||||
if image_size == -1:
|
if image_size == -1:
|
||||||
|
@ -227,16 +236,14 @@ class Document(models.Model):
|
||||||
resize_image(src, path, size=size)
|
resize_image(src, path, size=size)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def make_thumbnail(self, force=False):
|
def extract_page(self, page):
|
||||||
thumb = self.thumbnail()
|
pdf = self.file.path
|
||||||
if not os.path.exists(thumb) or force:
|
image = os.path.join(os.path.dirname(pdf), '1024p%d.jpg' % page)
|
||||||
cmd = ['convert', '%s[0]' % self.file.path,
|
utils.extract_pdfpage(pdf, image, page)
|
||||||
'-background', 'white', '-flatten', '-resize', '1024x1024', thumb]
|
|
||||||
p = subprocess.Popen(cmd)
|
|
||||||
p.wait()
|
|
||||||
|
|
||||||
def get_info(self):
|
def get_info(self):
|
||||||
if self.extension == 'pdf':
|
if self.extension == 'pdf':
|
||||||
|
self.thumbnail()
|
||||||
if self.pages == -1:
|
if self.pages == -1:
|
||||||
self.width = -1
|
self.width = -1
|
||||||
self.height = -1
|
self.height = -1
|
||||||
|
@ -247,7 +254,6 @@ class Document(models.Model):
|
||||||
|
|
||||||
def get_ratio(self):
|
def get_ratio(self):
|
||||||
if self.extension == 'pdf':
|
if self.extension == 'pdf':
|
||||||
self.make_thumbnail()
|
|
||||||
image = self.thumbnail()
|
image = self.thumbnail()
|
||||||
try:
|
try:
|
||||||
size = Image.open(image).size
|
size = Image.open(image).size
|
||||||
|
|
|
@ -17,3 +17,11 @@ def pdfinfo(pdf):
|
||||||
if key:
|
if key:
|
||||||
data[key] = ':'.join(parts[1:]).strip()
|
data[key] = ':'.join(parts[1:]).strip()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def extract_pdfpage(pdf, image, page):
|
||||||
|
page -= 1
|
||||||
|
cmd = ['convert', '%s[%d]' % (pdf, page),
|
||||||
|
'-background', 'white', '-flatten', '-resize', '1024x1024', image]
|
||||||
|
p = subprocess.Popen(cmd)
|
||||||
|
p.wait()
|
||||||
|
return image
|
||||||
|
|
|
@ -253,10 +253,10 @@ def file(request, id, name=None):
|
||||||
document = models.Document.get(id)
|
document = models.Document.get(id)
|
||||||
return HttpFileResponse(document.file.path)
|
return HttpFileResponse(document.file.path)
|
||||||
|
|
||||||
def thumbnail(request, id, size=256):
|
def thumbnail(request, id, size=256, page=None):
|
||||||
size = int(size)
|
size = int(size)
|
||||||
document = models.Document.get(id)
|
document = models.Document.get(id)
|
||||||
return HttpFileResponse(document.thumbnail(size))
|
return HttpFileResponse(document.thumbnail(size, page=page))
|
||||||
|
|
||||||
class ChunkForm(forms.Form):
|
class ChunkForm(forms.Form):
|
||||||
chunk = forms.FileField()
|
chunk = forms.FileField()
|
||||||
|
|
|
@ -30,7 +30,7 @@ urlpatterns = patterns('',
|
||||||
(r'^file/(?P<oshash>.*)$', 'archive.views.lookup_file'),
|
(r'^file/(?P<oshash>.*)$', 'archive.views.lookup_file'),
|
||||||
(r'^api/?$', include(ox.django.api.urls)),
|
(r'^api/?$', include(ox.django.api.urls)),
|
||||||
(r'^resetUI$', 'user.views.reset_ui'),
|
(r'^resetUI$', 'user.views.reset_ui'),
|
||||||
(r'^documents/(?P<id>[A-Z0-9]+)/(?P<size>\d*)p.jpg$', 'document.views.thumbnail'),
|
(r'^documents/(?P<id>[A-Z0-9]+)/(?P<size>\d*)p(?P<page>\d*).jpg$', 'document.views.thumbnail'),
|
||||||
(r'^documents/(?P<id>[A-Z0-9]+)/(?P<name>.*?\..+)$', 'document.views.file'),
|
(r'^documents/(?P<id>[A-Z0-9]+)/(?P<name>.*?\..+)$', 'document.views.file'),
|
||||||
(r'^edit/(?P<id>.*?)/icon(?P<size>\d*).jpg$', 'edit.views.icon'),
|
(r'^edit/(?P<id>.*?)/icon(?P<size>\d*).jpg$', 'edit.views.icon'),
|
||||||
(r'^list/(?P<id>.*?)/icon(?P<size>\d*).jpg$', 'itemlist.views.icon'),
|
(r'^list/(?P<id>.*?)/icon(?P<size>\d*).jpg$', 'itemlist.views.icon'),
|
||||||
|
|
Loading…
Reference in a new issue