mirror of
https://github.com/ajurna/cbwebreader.git
synced 2025-12-06 06:17:17 +00:00
changed views to use Database data to read files.
moved alot of the functionality to the models. changed the file access so that it ignores the extension and just attempts rar and zip access.
This commit is contained in:
@@ -1,8 +1,16 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from comic.models import Setting
|
from comic.models import Setting, ComicBook, ComicPage
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
@admin.register(Setting)
|
@admin.register(Setting)
|
||||||
class SettingAdmin(admin.ModelAdmin):
|
class SettingAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name', 'value')
|
list_display = ('name', 'value')
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(ComicBook)
|
||||||
|
class ComicBookAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('file_name', 'last_read_page')
|
||||||
|
|
||||||
|
@admin.register(ComicPage)
|
||||||
|
class ComicPageAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('Comic', 'index', 'page_file_name', 'content_type')
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
from unrar import rarfile
|
||||||
|
import zipfile
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
class Setting(models.Model):
|
class Setting(models.Model):
|
||||||
@@ -7,13 +8,57 @@ class Setting(models.Model):
|
|||||||
value = models.TextField()
|
value = models.TextField()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
||||||
return '"%s":"%s"' % (self.name, self.value)
|
return '"%s":"%s"' % (self.name, self.value)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
class ComicBook(models.Model):
|
class ComicBook(models.Model):
|
||||||
file_name = models.CharField(max_length=100, unique=True)
|
file_name = models.CharField(max_length=100, unique=True)
|
||||||
last_read_page = models.IntegerField()
|
last_read_page = models.IntegerField()
|
||||||
|
|
||||||
|
class Comic:
|
||||||
|
def __init__(self):
|
||||||
|
self.name = ''
|
||||||
|
self.index = 0
|
||||||
|
class Navigation:
|
||||||
|
def __init__(self):
|
||||||
|
self.next_index = 0
|
||||||
|
self.next_path = ''
|
||||||
|
self.prev_index = 0
|
||||||
|
self.prev_path = ''
|
||||||
|
self.cur_index = 0
|
||||||
|
self.cur_path = ''
|
||||||
|
def __str__(self):
|
||||||
|
return self.file_name
|
||||||
|
def get_image(self, archive_path, page):
|
||||||
|
try:
|
||||||
|
archive = rarfile.RarFile(archive_path)
|
||||||
|
except rarfile.BadRarFile:
|
||||||
|
archive = zipfile.ZipFile(archive_path)
|
||||||
|
except zipfile.BadZipfile:
|
||||||
|
return False
|
||||||
|
page_obj = ComicPage.objects.get(Comic=self, index=page)
|
||||||
|
|
||||||
|
return (archive.open(page_obj.page_file_name), page_obj.content_type)
|
||||||
|
|
||||||
|
def nav(self, comic_path, page):
|
||||||
|
out = self.Navigation()
|
||||||
|
out.cur_index = page
|
||||||
|
out.cur_path = comic_path
|
||||||
|
out.prev_index = page - 1
|
||||||
|
out.next_index = page + 1
|
||||||
|
out.prev_path = comic_path
|
||||||
|
out.next_path = comic_path
|
||||||
|
return out
|
||||||
|
def pages(self):
|
||||||
|
out = []
|
||||||
|
for item in ComicPage.objects.filter(Comic=self).order_by('index'):
|
||||||
|
i = self.Comic()
|
||||||
|
i.name = item.page_file_name
|
||||||
|
i.index = item.index
|
||||||
|
out.append(i)
|
||||||
|
return out
|
||||||
class ComicPage(models.Model):
|
class ComicPage(models.Model):
|
||||||
Comic = models.ForeignKey(ComicBook)
|
Comic = models.ForeignKey(ComicBook)
|
||||||
index = models.IntegerField()
|
index = models.IntegerField()
|
||||||
|
|||||||
@@ -11,25 +11,25 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<center>
|
<center>
|
||||||
<br/>
|
<br/>
|
||||||
<a href="/comic/read/{{ file_name }}/{{ nav.next }}/">
|
<a href="/comic/read/{{ nav.next_path }}/{{ nav.next_index }}/">
|
||||||
<img src="/comic/read/{{ file_name }}/{{ nav.cur }}/img" class="img-responsive">
|
<img src="/comic/read/{{ nav.cur_path }}/{{ nav.cur_index }}/img" class="img-responsive">
|
||||||
</a>
|
</a>
|
||||||
<br/>
|
<br/>
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<a href="/comic/read/{{ file_name }}/{{ nav.prev }}/" class="btn btn-default">Prev</a>
|
<a href="/comic/read/{{ nav.prev_path }}/{{ nav.prev_index }}/" class="btn btn-default">Prev</a>
|
||||||
{% if pages %}
|
{% if book.pages %}
|
||||||
<div class="btn-group dropup">
|
<div class="btn-group dropup">
|
||||||
<button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle ">{{ orig_file_name}}<span class="caret"></span></button>
|
<button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle ">{{ orig_file_name }}<span class="caret"></span></button>
|
||||||
<ul class="dropdown-menu" id="dropdown-list">
|
<ul class="dropdown-menu" id="dropdown-list">
|
||||||
{% for file in pages %}
|
{% for file in book.pages %}
|
||||||
<li><a href="/comic/read/{{ file_name }}/{{ file.index }}/">{{ file.name }}</a></li>
|
<li><a href="/comic/read/{{ nav.cur_path }}/{{ file.index }}/">{{ file.name }}</a></li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>No comics.</p>
|
<button class="btn btn-default">No Pages</button>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<a href="/comic/read/{{ file_name }}/{{ nav.next }}/" class="btn btn-default">Next</a>
|
<a href="/comic/read/{{ nav.next_path }}/{{ nav.next_index }}/" class="btn btn-default">Next</a>
|
||||||
</div>
|
</div>
|
||||||
</center>
|
</center>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -4,9 +4,9 @@ from . import views
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', views.index, name='index'),
|
url(r'^$', views.index, name='index'),
|
||||||
url(r'^(?P<comic_path>[\w=]+)\/$', views.index, name='index'),
|
url(r'^(?P<comic_path>[\w]+)\/$', views.index, name='index'),
|
||||||
url(r'^read\/(?P<comic_path>[\w=]+)\/(?P<page>[0-9]+)\/$', views.read_comic, name='read_comic'),
|
url(r'^read\/(?P<comic_path>[\w]+)\/(?P<page>[0-9]+)\/$', views.read_comic, name='read_comic'),
|
||||||
url(r'^read\/(?P<comic_path>[\w=]+)\/(?P<page>[0-9]+)\/img$', views.get_image, name='get_image'),
|
url(r'^read\/(?P<comic_path>[\w]+)\/(?P<page>[0-9]+)\/img$', views.get_image, name='get_image'),
|
||||||
url(r"^file\/(?P<page>[0-9]+)\/(?P<file_name>[\w.\s'\(\)\-]+)\/$", views.read_comic, name='read_comic'),
|
url(r"^file\/(?P<page>[0-9]+)\/(?P<file_name>[\w.\s'\(\)\-]+)\/$", views.read_comic, name='read_comic'),
|
||||||
url(r"^file\/(?P<page>[0-9]+)\/(?P<file_name>[\w.\s'\(\)\-]+)\/img$", views.get_image, name='get_image'),
|
url(r"^file\/(?P<page>[0-9]+)\/(?P<file_name>[\w.\s'\(\)\-]+)\/img$", views.get_image, name='get_image'),
|
||||||
]
|
]
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
from django.utils.http import urlsafe_base64_encode
|
from django.utils.http import urlsafe_base64_encode
|
||||||
|
from comic.models import ComicBook, ComicPage
|
||||||
|
from unrar import rarfile
|
||||||
|
import zipfile
|
||||||
from os import path
|
from os import path
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -11,6 +14,7 @@ class Breadcrumb:
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class DirFile:
|
class DirFile:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.name = ''
|
self.name = ''
|
||||||
@@ -22,6 +26,54 @@ class DirFile:
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def process_comic_book(base_dir, comic_path, comic_file_name):
|
||||||
|
try:
|
||||||
|
cbx = rarfile.RarFile(path.join(base_dir.value, comic_path))
|
||||||
|
except rarfile.BadRarFile:
|
||||||
|
cbx = zipfile.ZipFile(path.join(base_dir.value, comic_path))
|
||||||
|
except zipfile.BadZipfile:
|
||||||
|
return False
|
||||||
|
|
||||||
|
book = ComicBook(file_name=comic_file_name,
|
||||||
|
last_read_page=0)
|
||||||
|
book.save()
|
||||||
|
i = 0
|
||||||
|
for f in cbx.namelist():
|
||||||
|
ext = f.lower()[-3:]
|
||||||
|
if ext in ['jpg', 'jpeg']:
|
||||||
|
page = ComicPage(Comic=book,
|
||||||
|
index=i,
|
||||||
|
page_file_name=f,
|
||||||
|
content_type='image/jpeg')
|
||||||
|
page.save()
|
||||||
|
i += 1
|
||||||
|
elif ext == 'png':
|
||||||
|
page = ComicPage(Comic=book,
|
||||||
|
index=i,
|
||||||
|
page_file_name=f,
|
||||||
|
content_type='image/png')
|
||||||
|
page.save()
|
||||||
|
i += 1
|
||||||
|
elif ext == 'bmp':
|
||||||
|
page = ComicPage(Comic=book,
|
||||||
|
index=i,
|
||||||
|
page_file_name=f,
|
||||||
|
content_type='image/bmp')
|
||||||
|
page.save()
|
||||||
|
i += 1
|
||||||
|
elif ext == 'gif':
|
||||||
|
page = ComicPage(Comic=book,
|
||||||
|
index=i,
|
||||||
|
page_file_name=f,
|
||||||
|
content_type='image/gif')
|
||||||
|
page.save()
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return book
|
||||||
|
|
||||||
|
|
||||||
def generate_breadcrumbs(comic_path):
|
def generate_breadcrumbs(comic_path):
|
||||||
output = [Breadcrumb()]
|
output = [Breadcrumb()]
|
||||||
prefix = '/comic/'
|
prefix = '/comic/'
|
||||||
@@ -33,11 +85,12 @@ def generate_breadcrumbs(comic_path):
|
|||||||
continue
|
continue
|
||||||
bc = Breadcrumb()
|
bc = Breadcrumb()
|
||||||
bc.name = item
|
bc.name = item
|
||||||
bc.url = prefix + urlsafe_base64_encode(item)
|
|
||||||
output.append(bc)
|
|
||||||
last = path.join(last, item)
|
last = path.join(last, item)
|
||||||
|
bc.url = prefix + urlsafe_base64_encode(last)
|
||||||
|
output.append(bc)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def generate_directory(base_dir, comic_path):
|
def generate_directory(base_dir, comic_path):
|
||||||
files = []
|
files = []
|
||||||
for fn in os.listdir(path.join(base_dir.value, comic_path)):
|
for fn in os.listdir(path.join(base_dir.value, comic_path)):
|
||||||
@@ -52,4 +105,4 @@ def generate_directory(base_dir, comic_path):
|
|||||||
df.icon = 'glyphicon-book'
|
df.icon = 'glyphicon-book'
|
||||||
df.location = urlsafe_base64_encode(path.join(comic_path, fn))
|
df.location = urlsafe_base64_encode(path.join(comic_path, fn))
|
||||||
files.append(df)
|
files.append(df)
|
||||||
return files
|
return files
|
||||||
|
|||||||
@@ -1,25 +1,13 @@
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template import RequestContext, loader
|
from django.template import RequestContext
|
||||||
from django.utils.http import urlsafe_base64_decode
|
from django.utils.http import urlsafe_base64_decode
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
|
||||||
from comic.models import Setting
|
from comic.models import Setting, ComicBook
|
||||||
from util import generate_breadcrumbs, generate_directory
|
from util import generate_breadcrumbs, generate_directory, process_comic_book
|
||||||
|
|
||||||
from unrar import rarfile
|
|
||||||
from zipfile import ZipFile
|
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
class Comic:
|
|
||||||
def __init__(self):
|
|
||||||
self.name = ''
|
|
||||||
self.index = 0
|
|
||||||
class Navigation:
|
|
||||||
def __init__(self):
|
|
||||||
self.next = 0
|
|
||||||
self.prev = 0
|
|
||||||
self.cur = 0
|
|
||||||
|
|
||||||
def index(request, comic_path=''):
|
def index(request, comic_path=''):
|
||||||
base_dir = Setting.objects.get(name='BASE_DIR')
|
base_dir = Setting.objects.get(name='BASE_DIR')
|
||||||
comic_path = urlsafe_base64_decode(comic_path)
|
comic_path = urlsafe_base64_decode(comic_path)
|
||||||
@@ -33,30 +21,21 @@ def index(request, comic_path=''):
|
|||||||
|
|
||||||
|
|
||||||
def read_comic(request, comic_path, page):
|
def read_comic(request, comic_path, page):
|
||||||
encoded = comic_path
|
|
||||||
comic_path = urlsafe_base64_decode(comic_path)
|
|
||||||
breadcrumbs = generate_breadcrumbs(comic_path)
|
|
||||||
base_dir = Setting.objects.get(name='BASE_DIR')
|
base_dir = Setting.objects.get(name='BASE_DIR')
|
||||||
if comic_path.lower().endswith('cbr'):
|
|
||||||
cbx = rarfile.RarFile(path.join(base_dir.value, comic_path))
|
|
||||||
elif comic_path.lower().endswith('cbz'):
|
|
||||||
cbx = ZipFile(path.join(base_dir.value, comic_path))
|
|
||||||
nav = Navigation()
|
|
||||||
page = int(page)
|
page = int(page)
|
||||||
nav.cur = page
|
decoded_path = urlsafe_base64_decode(comic_path)
|
||||||
nav.next = page + 1
|
breadcrumbs = generate_breadcrumbs(decoded_path)
|
||||||
nav.prev = page - 1
|
_, comic_file_name = path.split(decoded_path)
|
||||||
pages = []
|
try:
|
||||||
for idx, name in enumerate(cbx.namelist()):
|
book = ComicBook.objects.get(file_name=comic_file_name)
|
||||||
comic = Comic()
|
except ComicBook.DoesNotExist:
|
||||||
comic.name = name
|
book = process_comic_book(base_dir, decoded_path, comic_file_name)
|
||||||
comic.index = idx
|
book.last_read_page = page
|
||||||
pages.append(comic)
|
book.save()
|
||||||
context = RequestContext(request, {
|
context = RequestContext(request, {
|
||||||
'pages': pages,
|
'book': book,
|
||||||
'file_name': encoded,
|
'orig_file_name': book.pages()[page].name,
|
||||||
'orig_file_name': pages[nav.cur].name,
|
'nav': book.nav(comic_path, page),
|
||||||
'nav': nav,
|
|
||||||
'breadcrumbs': breadcrumbs,
|
'breadcrumbs': breadcrumbs,
|
||||||
})
|
})
|
||||||
return render(request, 'comic/read_comic.html', context)
|
return render(request, 'comic/read_comic.html', context)
|
||||||
@@ -64,28 +43,13 @@ def read_comic(request, comic_path, page):
|
|||||||
|
|
||||||
def get_image(request, comic_path, page):
|
def get_image(request, comic_path, page):
|
||||||
base_dir = Setting.objects.get(name='BASE_DIR')
|
base_dir = Setting.objects.get(name='BASE_DIR')
|
||||||
comic_path = urlsafe_base64_decode(comic_path)
|
|
||||||
if comic_path.lower().endswith('cbr'):
|
|
||||||
cbx = rarfile.RarFile(path.join(base_dir.value, comic_path))
|
|
||||||
elif comic_path.lower().endswith('cbz'):
|
|
||||||
cbx = ZipFile(path.join(base_dir.value, comic_path))
|
|
||||||
page = int(page)
|
page = int(page)
|
||||||
page_file = cbx.namelist()[page]
|
decoded_path = urlsafe_base64_decode(comic_path)
|
||||||
file_name = str(page_file).lower()
|
_, comic_file_name = path.split(decoded_path)
|
||||||
if file_name.endswith('jpg') or file_name.endswith('jpeg'):
|
|
||||||
content = 'image/JPEG'
|
|
||||||
elif file_name.endswith('png'):
|
|
||||||
content = 'image/png'
|
|
||||||
elif file_name.endswith('bmp'):
|
|
||||||
content = 'image/bmp'
|
|
||||||
elif file_name.endswith('gif'):
|
|
||||||
content = 'image/gif'
|
|
||||||
else:
|
|
||||||
content = 'text/plain'
|
|
||||||
try:
|
try:
|
||||||
img = cbx.open(file)
|
book = ComicBook.objects.get(file_name=comic_file_name)
|
||||||
except KeyError:
|
except ComicBook.DoesNotExist:
|
||||||
img = cbx.open(page_file)
|
book = process_comic_book(base_dir, decoded_path, comic_file_name)
|
||||||
|
full_path = path.join(base_dir.value, decoded_path)
|
||||||
|
img, content = book.get_image(full_path, page)
|
||||||
return HttpResponse(img.read(), content_type=content)
|
return HttpResponse(img.read(), content_type=content)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user