def copy_media(self): make_copy = (self.incmedia == 'include') media_manifest = set() missing_media_manifest = set() size = len(self.gen_media) for count, mediafile in enumerate(sorted(self.gen_media), 1): yield "Handling media file %d of %d" % (count, size) media_path = media_mapper._map_path(mediafile) if media_path is None: missing_media_manifest.add(mediafile) continue if not os.path.exists(media_path): # FIXME - warn better. missing_media_manifest.add(mediafile) continue if make_copy: zippath = self.lookup_path('media', (), dict(path=mediafile)) self.copyfile(media_path, zippath) media_manifest.add(mediafile) # Make media preview page self.make_media_view(mediafile) self.addfile('/manifests/media.txt', '\n'.join(sorted(media_manifest))) self.addfile('/manifests/missing_media.txt', '\n'.join(sorted(missing_media_manifest)))
def tile(self, path, tile): if not mapper.exists(path): raise cherrypy.HTTPError(404) mo = tile_re.match(tile) if not mo: raise cherrypy.HTTPError(404) group, z, x, y = map(lambda x: int(x), mo.groups()) cached_path = tilecache.get_cached_path(mapper._map_path(path), group, z, x, y) if cached_path is not None: return serve_file(cached_path) data = tilecache.get_data(mapper._map_path(path), group, z, x, y) cherrypy.response.headers["Content-Type"] = "image/jpeg" return data
def thumbtype(public_path): """Get the type of thumbnail for a given file. Returns: - '' if the file doesn't exist, or is inaccessible. - 'icon' if the thumbnail is an icon. - 'text' if the thumbnail is a snippet of text. - 'image' if the thumbnail is an image. """ if public_path is None: return '' from apps.mediainfo.views import mapper localpath = mapper._map_path(public_path) if localpath is None: return '' if not os.path.exists(localpath): return '' type, encoding = mimetypes.guess_type(localpath) if type is None: return 'icon' # Check if we can open it as an image. if type.startswith('image'): try: img = Image.open(localpath) img.load() return 'image' except IOError, e: pass
def get_file_info(self, public_path): path = media_mapper._map_path(public_path) if path is None: return None filename = os.path.basename(public_path) mtype = mimetype(filename) type = thumbtype(path) summary = None if type == 'text': fd = open(path) try: summary = fd.read(100) + '...' except IOError: pass return dict(name=filename, mimetype=mtype, type=type, thumburl=None, # unused src=public_path, summary=summary, url=self.url("media", path=public_path), size=os.path.getsize(path), mtime=isotime(os.path.getmtime(path)), hidden=_local_is_hidden(path), )
def calc_media_roots(self): self.media_roots = {} for media_path, media in self.media_paths.iteritems(): assert media_path.startswith('/') head_tail = media_path[1:].split('/', 1) if len(head_tail) == 1: head, tail = head_tail, '' else: head, tail = head_tail newhead = self.media_root_mappings.get(head, head) path = mapper._map_path('/' + '/'.join((newhead, tail))) if path is None: exists = False else: exists = os.path.isfile(path) info = self.media_roots.get(head, None) if info is None: info = dict(tail=tail, existing=0, missing=0, known=mapper.known_root(newhead), newroot=newhead, ) self.media_roots[head] = info else: tail = tail.split('/') common_tail = info['tail'].split('/') for i, (a, b) in enumerate(zip(tail, common_tail)): if a != b: break info['tail'] = '/'.join(common_tail[:i]) if exists: info['existing'] += 1 else: info['missing'] += 1
def imgsize(public_path): """Get the size of a image. """ from apps.mediainfo.views import mapper try: return thumbcache.get_size(mapper._map_path(public_path), None, None) except OSError: return None
def thumbsize(public_path, width, height): """Get the size of a thumbnail for the given maximum dimensions. """ from apps.mediainfo.views import mapper try: return thumbcache.get_size(mapper._map_path(public_path), width, height) except OSError: return None
def make_thumbnails(self): count = 0 size = len(self.gen_thumbnails) for path, width, height in sorted(self.gen_thumbnails): count += 1 yield "Generating thumbnail %d of %d" % (count, size) media_path = media_mapper._map_path(path) if media_path is None: continue if not os.path.exists(media_path): # FIXME - warn better. continue thumbdata, thumbmime = thumbcache.get_data(media_path, width, height) assert width is not None zippath = self.lookup_path('thumbnail', (), dict(path=path, width=width, height=height)) self.addfile(zippath, thumbdata)
def thumbnail(self, path, width=64, height=64): if not mapper.exists(path): redirect(url("/static/icons/thumbs/missing.png")) width = int(width) height = int(height) # Note - the path supplied is supplied as a query parameter, not as # part of the path info in the URL, because otherwise cherrypy strips # out any double slashes, making it impossible to tell if the path # starts with a drivename (on windows) or with an absolute path which # should have a / inserted before it (on unix). try: data, ctype = thumbcache.get_data(mapper._map_path(path), width, height) cherrypy.response.headers["Content-Type"] = ctype return data except IOError: redirect(url("/static/icons/thumbs/broken.png"))
def get_image(self, src, width=600, height=450): localpath = mapper._map_path(src) if localpath is None: return None img = pil_Image.open(localpath) img.thumbnail((width, height), pil_Image.ANTIALIAS) out = StringIO.StringIO() ext = src.split('.')[-1].lower() if ext in ('jpg', 'jpeg'): img.save(out, "JPEG") fakename = 'img.jpg' else: img.save(out, "PNG") fakename = 'img.png' out = StringIO.StringIO(out.getvalue()) return rtfng_Image(fakename, fin=out, scale_x=50, scale_y=50)
def path(self): from apps.mediainfo.views import mapper return mapper._map_path(self.src)