support more image formats, server avif is supported for larger images
This commit is contained in:
parent
0748265fe6
commit
406237b837
9 changed files with 31 additions and 11 deletions
|
|
@ -19,10 +19,13 @@ import ox.image
|
|||
from ox.utils import json
|
||||
from django.conf import settings
|
||||
from PIL import Image, ImageOps
|
||||
import pillow_avif
|
||||
from pillow_heif import register_heif_opener
|
||||
|
||||
from .chop import Chop, make_keyframe_index
|
||||
|
||||
|
||||
register_heif_opener()
|
||||
logger = logging.getLogger('pandora.' + __name__)
|
||||
|
||||
img_extension = 'jpg'
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from django.conf import settings
|
|||
|
||||
logger = logging.getLogger('pandora.' + __name__)
|
||||
|
||||
IMAGE_EXTENSIONS = ('png', 'jpg', 'webp', 'heic', 'heif')
|
||||
|
||||
def extract_text(pdf, page=None):
|
||||
if page is not None:
|
||||
|
|
@ -53,7 +54,7 @@ class FulltextMixin:
|
|||
if self.file:
|
||||
if self.extension == 'pdf':
|
||||
return extract_text(self.file.path)
|
||||
elif self.extension in ('png', 'jpg'):
|
||||
elif self.extension in IMAGE_EXTENSIONS:
|
||||
return ocr_image(self.file.path)
|
||||
elif self.extension == 'html':
|
||||
return self.data.get('text', '')
|
||||
|
|
@ -180,7 +181,7 @@ class FulltextPageMixin(FulltextMixin):
|
|||
if self.document.file:
|
||||
if self.document.extension == 'pdf':
|
||||
return extract_text(self.document.file.path, self.page)
|
||||
elif self.extension in ('png', 'jpg'):
|
||||
elif self.extension in IMAGE_EXTENSIONS:
|
||||
return ocr_image(self.document.file.path)
|
||||
elif self.extension == 'html':
|
||||
# FIXME: is there a nice way to split that into pages
|
||||
|
|
|
|||
|
|
@ -521,14 +521,17 @@ class Document(models.Model, FulltextMixin):
|
|||
return save_chunk(self, self.file, chunk, offset, name, done_cb)
|
||||
return False, 0
|
||||
|
||||
def thumbnail(self, size=None, page=None):
|
||||
def thumbnail(self, size=None, page=None, accept=None):
|
||||
if not self.file:
|
||||
return os.path.join(settings.STATIC_ROOT, 'png/document.png')
|
||||
src = self.file.path
|
||||
folder = os.path.dirname(src)
|
||||
if size:
|
||||
size = int(size)
|
||||
path = os.path.join(folder, '%d.jpg' % size)
|
||||
ext = 'jpg'
|
||||
if accept and 'image/avif' in accept and size > 512:
|
||||
ext = 'avif'
|
||||
path = os.path.join(folder, '%d.%s' % (size, ext))
|
||||
else:
|
||||
path = src
|
||||
if self.extension == 'pdf':
|
||||
|
|
@ -561,7 +564,7 @@ class Document(models.Model, FulltextMixin):
|
|||
path = os.path.join(folder, '%dp%d,%s.jpg' % (size, page, ','.join(map(str, crop))))
|
||||
if not os.path.exists(path):
|
||||
resize_image(src, path, size=size)
|
||||
elif self.extension in ('jpg', 'png', 'gif'):
|
||||
elif self.extension in ('jpg', 'png', 'gif', 'webp', 'heic', 'heif'):
|
||||
if os.path.exists(src):
|
||||
if size and page:
|
||||
crop = list(map(int, page.split(',')))
|
||||
|
|
@ -574,7 +577,7 @@ class Document(models.Model, FulltextMixin):
|
|||
img = open_image_rgb(path)
|
||||
src = path
|
||||
if size < max(img.size):
|
||||
path = os.path.join(folder, '%sp%s.jpg' % (size, ','.join(map(str, crop))))
|
||||
path = os.path.join(folder, '%sp%s.%s' % (size, ','.join(map(str, crop)), ext))
|
||||
if not os.path.exists(path):
|
||||
resize_image(src, path, size=size)
|
||||
if os.path.exists(src) and not os.path.exists(path):
|
||||
|
|
|
|||
|
|
@ -378,15 +378,22 @@ actions.register(sortDocuments, cache=False)
|
|||
|
||||
def file(request, id, name=None):
|
||||
document = get_document_or_404_json(request, id)
|
||||
accept = request.headers.get("Accept")
|
||||
if accept and 'image/' in accept and document.extension in (
|
||||
'webp', 'heif', 'heic', 'avif'
|
||||
) and document.extension not in accept:
|
||||
image_size = max(document.width, document.height)
|
||||
return HttpFileResponse(document.thumbnail(image_size))
|
||||
return HttpFileResponse(document.file.path)
|
||||
|
||||
def thumbnail(request, id, size=256, page=None):
|
||||
size = int(size)
|
||||
document = get_document_or_404_json(request, id)
|
||||
accept = request.headers.get("Accept")
|
||||
if "q" in request.GET and page:
|
||||
img = document.highlight_page(page, request.GET["q"], size)
|
||||
return HttpResponse(img, content_type="image/jpeg")
|
||||
return HttpFileResponse(document.thumbnail(size, page=page))
|
||||
return HttpFileResponse(document.thumbnail(size, page=page, accept=accept))
|
||||
|
||||
|
||||
@login_required_json
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ urlpatterns += [
|
|||
re_path(r'^resetUI$', user.views.reset_ui),
|
||||
re_path(r'^collection/(?P<id>.*?)/icon(?P<size>\d*).jpg$', documentcollection.views.icon),
|
||||
re_path(r'^documents/(?P<id>[A-Z0-9]+)/(?P<size>\d*)p(?P<page>[\d,]*).jpg$', document.views.thumbnail),
|
||||
re_path(r'^documents/(?P<id>[A-Z0-9]+)/(?P<name>.*?\.[^\d]{3})$', document.views.file),
|
||||
re_path(r'^documents/(?P<id>[A-Z0-9]+)/(?P<name>.*?\.[^\d]{3,4})$', document.views.file),
|
||||
re_path(r'^documents/(?P<fragment>.*?)$', document.views.document),
|
||||
re_path(r'^edit/(?P<id>.*?)/icon(?P<size>\d*).jpg$', edit.views.icon),
|
||||
re_path(r'^list/(?P<id>.*?)/icon(?P<size>\d*).jpg$', itemlist.views.icon),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue