mirror of
https://github.com/ajurna/cbwebreader.git
synced 2025-12-06 06:17:17 +00:00
Changed the way it handles paths to urlencode.
now supports subdirectories added breadcrumbs
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
from django.http import HttpResponse
|
||||
from django.template import RequestContext, loader
|
||||
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
|
||||
|
||||
from comic.models import Setting
|
||||
|
||||
from unrar import rarfile
|
||||
from zipfile import ZipFile
|
||||
import os
|
||||
from os import path
|
||||
|
||||
class Comic:
|
||||
def __init__(self):
|
||||
@@ -15,25 +18,56 @@ class Navigation:
|
||||
self.next = 0
|
||||
self.prev = 0
|
||||
self.cur = 0
|
||||
class DirFile:
|
||||
def __init__(self):
|
||||
self.name = ''
|
||||
self.isdir = False
|
||||
self.icon = ''
|
||||
self.iscb = False
|
||||
self.location = ''
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
# Create your views here.
|
||||
def index(request):
|
||||
def index(request, comic_path=''):
|
||||
base_dir = Setting.objects.get(name='BASE_DIR')
|
||||
files = os.listdir(base_dir.value)
|
||||
comic_path = urlsafe_base64_decode(comic_path)
|
||||
breadcrumbs = generate_breadcrumbs(comic_path)
|
||||
|
||||
|
||||
#list and classify files
|
||||
files = []
|
||||
for fn in os.listdir(path.join(base_dir.value, comic_path)):
|
||||
df = DirFile()
|
||||
df.name = fn
|
||||
if path.isdir(path.join(base_dir.value, comic_path, fn)):
|
||||
df.isdir = True
|
||||
df.icon = 'glyphicon-folder-open'
|
||||
df.location = urlsafe_base64_encode(path.join(comic_path, fn))
|
||||
elif fn.lower().endswith('cbz') or fn.lower().endswith('cbr'):
|
||||
df.iscb = True
|
||||
df.icon = 'glyphicon-book'
|
||||
df.location = urlsafe_base64_encode(path.join(comic_path, fn))
|
||||
files.append(df)
|
||||
template = loader.get_template('comic/index.html')
|
||||
context = RequestContext(request, {
|
||||
'file_list': files,
|
||||
'breadcrumbs': breadcrumbs,
|
||||
})
|
||||
|
||||
return HttpResponse(template.render(context))
|
||||
|
||||
|
||||
def read_comic(request, file_name, page):
|
||||
def read_comic(request, comic_path, page):
|
||||
encoded = comic_path
|
||||
comic_path = urlsafe_base64_decode(comic_path)
|
||||
base_dir = Setting.objects.get(name='BASE_DIR')
|
||||
template = loader.get_template('comic/read_comic.html')
|
||||
if file_name.lower().endswith('cbr'):
|
||||
cbx = rarfile.RarFile(os.path.join(base_dir.value, file_name))
|
||||
elif file_name.lower().endswith('cbz'):
|
||||
cbx = ZipFile(os.path.join(base_dir.value, file_name))
|
||||
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)
|
||||
nav.cur = page
|
||||
@@ -46,21 +80,20 @@ def read_comic(request, file_name, page):
|
||||
comic.index = idx
|
||||
pages.append(comic)
|
||||
|
||||
img_src = '/comic/file/0/' + file_name + '/img'
|
||||
context = RequestContext(request, {
|
||||
'pages': pages,
|
||||
'file_name': file_name,
|
||||
'img_src': img_src,
|
||||
'file_name': encoded,
|
||||
'nav': nav,
|
||||
})
|
||||
return HttpResponse(template.render(context))
|
||||
|
||||
def get_image(request, file_name, page):
|
||||
def get_image(request, comic_path, page):
|
||||
base_dir = Setting.objects.get(name='BASE_DIR')
|
||||
if file_name.lower().endswith('cbr'):
|
||||
cbx = rarfile.RarFile(os.path.join(base_dir.value, file_name))
|
||||
elif file_name.lower().endswith('cbz'):
|
||||
cbx = ZipFile(os.path.join(base_dir.value, file_name))
|
||||
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_file = cbx.namelist()[page]
|
||||
file_name = str(page_file).lower()
|
||||
@@ -79,3 +112,26 @@ def get_image(request, file_name, page):
|
||||
except KeyError:
|
||||
img = cbx.open(page_file)
|
||||
return HttpResponse(img.read(), content_type=content)
|
||||
|
||||
class Breadcrumb:
|
||||
def __init__(self):
|
||||
self.name = 'Home'
|
||||
self.url = '/comic/'
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
def generate_breadcrumbs(comic_path):
|
||||
output = [Breadcrumb()]
|
||||
prefix = '/comic/'
|
||||
last = ''
|
||||
comic_path = path.normpath(comic_path)
|
||||
folders = comic_path.split(os.sep)
|
||||
for item in folders:
|
||||
if item == '.':
|
||||
continue
|
||||
bc = Breadcrumb()
|
||||
bc.name = item
|
||||
bc.url = prefix + urlsafe_base64_encode(item)
|
||||
output.append(bc)
|
||||
last = path.join(last, item)
|
||||
return output
|
||||
Reference in New Issue
Block a user