def get_bitmap_by_id(imageid): # select image with matching id and return 404 if no such image found image = Image.select().where(Image.id == imageid) if image.count() == 0: return json.dumps({'message': 'No image with matching ID found!'}), 404 # forward request to image source using proxy return proxy.stream(request, 'GET', BASE_URL_DATASET + image[0].src)
def title(title_id): """ Render title (grid of page thumbnails). """ # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: render_template("error.html", response="Volume " + title_id + " does not exist") # Get list of images for this title thumbs = Image.select( Image, Volume).join(Volume).where(Volume.id == title_id).order_by(Image.page) # Get list of tags for this title tags = Tag.select(Tag, TagRelation, Volume).join(TagRelation).join(Volume).where( Volume.id == title_id).order_by(Tag.name) # pass list of thumbnails to template return render_template("title.html", title=volume.title, id=str(title_id), thumbs=thumbs, tags=tags)
def get_image_by_id(imageid): # select image with matching id and return 404 if no such image found image = Image.select().where(Image.id == imageid) if image.count() == 0: return json.dumps({'message': 'No image with matching ID found!'}), 404 # wrap image object into JSON and return it image_json = {'image': get_image_json_object(image[0])} return json.dumps(image_json), 200
def get_images(): # get options from request # in case of invalid parameters return 400 try: limit = int(request.args.get('limit', default='100')) offset = int(request.args.get('offset', default='0')) except ValueError: return json.dumps({'message': 'Invalid limit or offset passed!'}), 400 if limit < 1 or limit > 500 or offset < 0: return json.dumps({'message': 'Invalid limit or offset passed!'}), 400 count = Image.select().count() # pylint: disable=no-value-for-parameter image_dict = {'images': [], 'count': count} # iterate over all images with given offset and limit # wrap them into json and add them to the list for image in Image.select().limit(limit).offset(offset): image_json = get_image_json_object(image) image_dict['images'].append(image_json) return json.dumps(image_dict), 200
def page_image(title_id, page_num): """Get archive file corresponding to title ID and member file corresponding to page number Returns extracted image to browser""" # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: abort(404) # Get page information try: page = Image.select().join(Volume).where((Volume.id == title_id) & (Image.page == page_num)).get() except Image.DoesNotExist: abort(404) if volume.filetype == 'zip': # TODO: Error check on archive open z = zipfile.ZipFile(INPUT_PATH+volume.filename) # Get page binary data # TODO: Error checking zf = z.read(page.filename) z.close() # Return extracted image to browser return send_file(BytesIO(zf), mimetype=page.mimetype) elif volume.filetype == 'rar': # TODO: Error check on archive open rar = rarfile.RarFile(INPUT_PATH+volume.filename) # Get page binary data # TODO: Error checking rardata = rar.read(page.filename) rar.close() # Return extracted image to browser return send_file(BytesIO(rardata), mimetype=page.mimetype) # unrecognised archive type else: abort(500)
def slide(title_id): """Test template for Unslider display, displays Title title_id""" # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: abort(404) # Get list of images for this title pages = Image.select(Image, Volume).join(Volume).where( Volume.id == title_id).order_by(Image.page) # Pass volume and page details to Unslider template return render_template("slider.html", title_id=title_id, volume=volume, pages=pages)
def title(title_id): """ Render title (grid of thumbnails). Image 0 as blurred CSS background? """ # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: abort(404) # Get list of images for this title thumbs = Image.select(Image, Volume).join(Volume).where(Volume.id == title_id).order_by(Image.page) # Get list of tags for this title tags = Tag.select(Tag, TagRelation, Volume).join(TagRelation).join(Volume).where(Volume.id == title_id).order_by(Tag.name) # pass list of thumbnails to template return render_template("title.html", title=volume.title, id=str(title_id), thumbs=thumbs, tags=tags)
def page_image(title_id, page_num): """ Get archive file corresponding to title ID and member file corresponding to page number Returns extracted image to browser """ # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: return render_template("error.html", response="Volume " + title_id + " does not exist") # Get page information try: page = Image.select().join(Volume).where( (Volume.id == title_id) & (Image.page == page_num)).get() except Image.DoesNotExist: return render_template("error.html", response="Image " + page_num + " does not exist") if volume.filetype == 'zip': # TODO: Error check on archive open z = zipfile.ZipFile(INPUT_PATH + volume.filename) # Get page binary data try: zf = z.read(page.filename) except zipfile.BadZipFile: return render_template("error.html", response="Bad archive file: " + volume.filename) z.close() # Return extracted image to browser return send_file(BytesIO(zf), mimetype=page.mimetype) elif volume.filetype == 'rar': # TODO: Error check on archive open rar = rarfile.RarFile(INPUT_PATH + volume.filename) # Get page binary data # There must be a cleaner way of fixing paths, but pathlib doesn't seem to work try: rarpath = str(page.filename).replace('\\', '/') rardata = rar.read(rarpath) except rarfile.NoRarEntry: return render_template("error.html", response="No such file: " + rarpath + " in " + volume.filename) rar.close() # Return extracted image to browser return send_file(BytesIO(rardata), mimetype=page.mimetype) # unrecognised archive type else: return render_template("error.html", response="Unrecognised archive type: " + volume.filename)