import base64
import os
import subprocess
from io import BytesIO
import magic
from PIL import Image
from distribusi.page_template import html_footer, html_head
from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES
MIME_TYPE = magic.Magic(mime=True)
def caption(image):
try:
process = subprocess.Popen(
['exiftool', '-Comment', image], stdout=subprocess.PIPE)
out, err = process.communicate()
except Exception as e:
print(e)
print('Do you have exiftool installed?')
try:
caption = out.decode("utf-8").split(": ", 1)[1]
except Exception as e:
caption = ''
print(e)
return caption
def thumbnail(image, name, args):
try:
size = (450, 450)
im = Image.open(image)
im.thumbnail(size)
if (im.mode == 'RGBA'):
bg = Image.new('RGBA', im.size, (255,255,255))
composite = Image.alpha_composite(bg, im)
im=composite.convert('RGB')
output = BytesIO()
im.save(output, format='JPEG')
im_data = output.getvalue()
data_url = base64.b64encode(im_data).decode()
if args.captions:
cap = caption(image)
else:
cap = name
return (
"
" + open(full_path).read() + "" else: subtype = subtype+' unkown-file' a = "{}" # a = FILE_TYPES[type_] if type_ == 'image': if args.thumbnail: a = thumbnail(full_path, name, args) if args.no_filenames: caption = "" a = FILE_TYPES[type_].format(name, caption) if args.captions: caption = caption(full_path) a = FILE_TYPES[type_].format(name, caption) if subtype in SUB_TYPES: a = SUB_TYPES[subtype] if type_ not in FILE_TYPES and subtype not in SUB_TYPES: # catch exceptions not yet defined in FILE_TYPES or SUB_TYPES a = "{}" if args.verbose: message = 'not in list of file types, adding as plain href: \n' print(type_, subtype, message, name) subtype = subtype + ' unkown-file' a = a.replace('{}', name) html.append(div(args, type_, subtype, a, name)) if root != directory: if args.menu_with_index: html.append('../') else: html.append('../') for name in dirs: if args.menu_with_index: a = "{}".replace('{}', name) else: a = "{}/".replace('{}', name) html.insert(0, div(args, 'dir', 'dir', a, 'folder')) index = os.path.join(root, 'index.html') if os.path.exists(index): if check_distribusi_index(args, index): write_index(args,index,html, html_head, html_footer) elif not os.path.exists(index): write_index(args,index,html, html_head, html_footer) if args.remove_index: index = os.path.join(root, 'index.html') if 'index.html' in files: try: if check_distribusi_index(args, index): if args.verbose: print('Removing index.html from', root) os.remove(index) except Exception as e: print(e)