3 # Copyright (C) 2010-2013 W. Trevor King <wking@tremily.us>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 CGI gallery server for a picture directory organized along::
24 | |-- another_picture.jpg
28 | |-- captioned_picture.jpg
29 | |-- captioned_picture.jpg.txt
36 Note that you can store a caption for ``<PICTURE>`` as plain text in
39 See RFC 3875 for more details on the the Common Gateway Interface.
41 This script can also be run as a Simple Common Gateway Interface
42 (SCGI) with the ``--scgi`` option.
45 import collections as _collections
46 import logging as _logging
47 import logging.handlers as _logging_handlers
49 import mimetypes as _mimetypes
51 import os.path as _os_path
52 import random as _random
54 import subprocess as _subprocess
56 import urllib.parse as _urllib_parse
57 except ImportError: # Python 2
58 import urlparse as _urllib_parse
59 import xml.sax.saxutils as _xml_sax_saxutils
65 IMAGE_EXTENSIONS = ['.jpg', '.jpeg', '.tif', '.tiff', '.png', '.gif']
66 VIDEO_EXTENSIONS = ['.mov', '.mp4', '.ogv']
67 STREAMING_TYPES = ['video/ogg']
68 RESPONSES = { # httplib takes half a second to load
72 500: 'Internal Server Error',
75 LOG = _logging.getLogger('gallery.py')
76 LOG.addHandler(_logging.StreamHandler())
77 #LOG.addHandler(_logging_handlers.SysLogHandler())
78 LOG.handlers[0].setFormatter(
79 _logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
80 LOG.setLevel(_logging.DEBUG)
81 #LOG.setLevel(_logging.WARNING)
84 class CommandError(Exception):
85 def __init__(self, command, status, stdout=None, stderr=None):
86 strerror = ['Command failed (%d):\n %s\n' % (status, stderr),
87 'while executing\n %s' % str(command)]
88 super(CommandError, self).__init__('\n'.join(strerror))
89 self.command = command
95 class HTTPError(Exception):
96 def __init__(self, status, message=None, content=None):
98 message = RESPONSES[status]
101 super(HTTPError, self).__init__('{} {}'.format(status, message))
103 self.message = message
104 self.content = content
107 class ProcessingComplete(Exception):
111 def invoke(args, stdin=None, stdout=_subprocess.PIPE, stderr=_subprocess.PIPE,
112 expect=(0,), cwd=None, encoding=None):
114 expect should be a tuple of allowed exit codes. cwd should be
115 the directory from which the command will be executed. When
116 unicode_output == True, convert stdout and stdin strings to
117 unicode before returing them.
121 LOG.debug('{}$ {}'.format(cwd, ' '.join(args)))
123 q = _subprocess.Popen(args, stdin=_subprocess.PIPE, stdout=stdout,
124 stderr=stderr, cwd=cwd)
126 raise CommandError(args, status=e.args[0], stderr=e)
127 stdout,stderr = q.communicate(input=stdin)
129 LOG.debug('{:d}\n{}{}'.format(status, stdout, stderr))
130 if status not in expect:
131 raise CommandError(args, status, stdout, stderr)
132 return status, stdout, stderr
134 def is_image(filename):
135 for extension in IMAGE_EXTENSIONS:
136 if filename.lower().endswith(extension):
140 def is_video(filename):
141 for extension in IMAGE_EXTENSIONS:
142 if filename.lower().endswith(extension):
146 def image_base(filename):
147 parts = filename.rsplit('.', 1)
148 assert len(parts) == 2, parts
152 class CGIGalleryServer (object):
153 def __init__(self, base_path='.',
155 cache_path='/tmp/gallery-cache/',
156 serve_originals=True):
157 self._base_path = _os_path.abspath(base_path)
158 self._base_url = base_url
159 self._cache_path = cache_path
160 self._serve_originals = serve_originals
161 self._text_charset = 'UTF-8'
162 self._url_regexp = _re.compile('^[a-zA-Z0-9._/-]*$')
168 def _get_charset(self, headers):
169 content_type = headers.get('Content-type', '')
170 if 'charset=' in content_type:
171 return content_type.split('charset=', 1)[-1]
173 def _http_headers(self, mime='text/html', status=200):
174 msg = RESPONSES[status]
175 headers = _collections.OrderedDict()
176 headers['Status'] = '{:d} {}'.format(status, msg)
177 if mime.startswith('text/'):
178 charset = '; charset={}'.format(self._text_charset)
181 headers['Content-type'] = '{}{}'.format(mime, charset)
184 def _add_header(self, headers=None, stream=None):
185 if headers and self._write_http_headers:
186 for key, value in headers.items():
188 '{}: {}\r\n'.format(key, value).encode('US-ASCII'))
189 stream.write('\r\n'.encode('US-ASCII'))
191 def _response(self, headers=None, content='<h1>It works!</h1>',
194 headers = self._http_headers()
195 self._add_header(headers=headers, stream=stream)
196 charset = self._get_charset(headers=headers)
198 content = content.encode(charset)
199 stream.write(content)
200 raise ProcessingComplete()
202 def _response_stream(self, headers=None, content=None, stream=None,
204 LOG.debug('streaming response')
206 headers = self._http_headers()
207 charset = self._get_charset(headers=headers)
211 content='charset {} set for streamed response'.format(charset))
212 self._add_header(headers=headers, stream=stream)
213 stream.flush() # flush headers
215 chunk = content.read(chunk_size)
219 raise ProcessingComplete()
221 def _error(self, status=404, content=None, stream=None):
222 headers = self._http_headers(status=status)
224 content = RESPONSES[status]
225 self._response(headers=headers, content=content, stream=stream)
227 def validate_url(self, url, exists=True, directory=False):
228 LOG.debug('validating {} (exists={}, directory={})'.format(
229 repr(url), exists, directory))
232 elif (not self._url_regexp.match(url) or
233 url.startswith('/') or
236 LOG.error('invalid url')
239 path = _os_path.join(self._base_path, url)
241 if not _os_path.isdir(path):
242 LOG.error('nonexistent directory')
245 if not _os_path.isfile(path):
246 raise HTTPError(404, content='nonexistent file')
248 def serve(self, url=None, page=0, stream=None):
249 LOG.info('serving url {} (page {})'.format(url, page))
253 self.index(stream=stream)
254 elif url.endswith('random'):
256 url=url, stream=stream, max_width=500, max_height=500)
257 elif self.is_cacheable(url=url):
258 self.validate_url(url=url, exists=False)
259 self.cached(url=url, stream=stream)
261 self.validate_url(url=url, exists=False, directory=True)
262 self.page(url=url, page=page, stream=stream)
263 raise HTTPError(404, content='unexpected URL type')
264 except HTTPError as e:
266 self._error(e.status, content=e.content, stream=stream)
267 except ProcessingComplete:
270 def page_from_query(self, query=None, query_string=None):
271 """Extract the requested page from a query string
273 This is a helper method for CGIGalleryServer consumers.
274 Specify either query or query_string, but not both.
277 query = _urllib_parse.parse_qs(query_string)
281 if isinstance(pp, list):
289 def relative_url(self, url):
292 if not url.startswith(self._base_url):
293 content = 'cannot convert {} to a relative URL of {}'.format(
295 raise HTTPError(404, content=content)
296 if url == self._base_url:
298 return url[len(self._base_url):]
300 def _url(self, path):
301 relpath = _os_path.relpath(
302 _os_path.join(self._base_path, path), self._base_path)
305 elif path.endswith('/'):
307 return '{}{}'.format(self._base_url, relpath)
309 def _label(self, path):
310 dirname,base = _os_path.split(path)
311 if not base: # directory path ending with '/'
312 dirname,base = _os_path.split(dirname)
313 return base.replace('_', ' ').title()
315 def _link(self, path, text=None):
317 text = self._label(path)
318 return '<a href="{}">{}</a>'.format(self._url(path), text)
320 def _subdirs(self, path):
322 order = [d.strip() for d in
323 open(_os_path.join(path, '_order')).readlines()]
326 dirs = sorted(_os.listdir(path))
332 for d in start + dirs:
333 dirpath = _os_path.join(path, d)
334 if _os_path.isdir(dirpath):
337 def _images(self, path):
338 for p in sorted(_os.listdir(path)):
339 if p.startswith('.') or p.endswith('~'):
341 picture_path = _os_path.join(path, p)
342 if is_image(picture_path):
345 def index(self, stream=None):
346 LOG.debug('index page')
347 return self._directory(self._base_path, stream=stream)
349 def _original_url(self, url):
350 """Reverse thumbnail URL mapping
352 Returns (original_url, generating_callback, callback_kwargs).
354 base,extension = _os_path.splitext(url)
355 if extension in ['.png']:
357 root,width,height = base.rsplit('-', 2)
360 400, content='missing width/height in {}'.format(base))
364 except ValueError as e:
366 400, content='invalid width/height: {}'.format(e))
371 'max_height': height},
373 elif extension in VIDEO_EXTENSIONS:
376 getattr(self, '_{}'.format(extension), None),
379 raise HTTPError(400, content='no original URL for {}'.format(url))
381 def _thumb(self, image, max_width=None, max_height=None):
382 if not _os_path.exists(self._cache_path):
383 _os.makedirs(self._cache_path)
384 dirname,filename = _os_path.split(image)
385 reldir = _os_path.relpath(dirname, self._base_path)
386 cache_dir = _os_path.join(self._cache_path, reldir)
387 if not _os_path.isdir(cache_dir):
388 _os.makedirs(cache_dir)
389 extension = '-{:d}-{:d}.png'.format(max_width, max_height)
390 thumb_filename = image_base(filename)+extension
391 thumb_url = _os_path.join(dirname, thumb_filename)
392 thumb_path = _os_path.join(cache_dir, thumb_filename)
393 image_path = _os_path.join(self._base_path, image)
394 if not _os_path.isfile(image_path):
396 404, content='image path for thumbnail does not exist')
397 if (not _os_path.isfile(thumb_path)
398 or _os_path.getmtime(image_path) > _os_path.getmtime(thumb_path)):
399 invoke(['convert', '-format', 'png', '-strip', '-quality', '95',
401 '-thumbnail', '{:d}x{:d}'.format(max_width, max_height),
403 return (thumb_path, self._url(thumb_url))
405 def _mp4(self, video, *args):
406 if not video.endswith('.mov'):
408 500, content="can't translate {} to MPEGv4".format(video))
409 dirname,filename = _os_path.split(video)
410 mp4_filename = image_base(filename) + '.mp4'
411 reldir = _os_path.relpath(dirname, self._base_path)
412 cache_dir = _os_path.join(self._cache_path, reldir)
413 if not _os_path.isdir(cache_dir):
414 _os.makedirs(cache_dir)
415 mp4_url = _os_path.join(dirname, mp4_filename)
416 mp4_path = _os_path.join(cache_dir, mp4_filename)
417 if not _os_path.isfile(video):
418 raise HTTPError(404, content='source video path does not exist')
419 if (not _os_path.isfile(mp4_path)
420 or _os_path.getmtime(video) > _os_path.getmtime(mp4_path)):
421 arg = ['ffmpeg', '-i', video, '-acodec', 'libfaac', '-aq', '200',
422 '-ac', '1', '-s', '640x480', '-vcodec', 'libx264',
423 '-preset', 'slower', '-vpre', 'ipod640', '-b', '800k',
424 '-bt', '800k', '-aspect', '640:480', '-threads', '0']
428 return (mp4_path, self._url(mp4_url))
430 def _ogv(self, video, *args):
431 if not video.endswith('.mov'):
432 LOG.error("can't translate {} to Ogg Video".format(video))
433 dirname,filename = _os_path.split(video)
434 ogv_filename = image_base(filename) + '.ogv'
435 reldir = _os_path.relpath(dirname, self._base_path)
436 cache_dir = _os_path.join(self._cache_path, reldir)
437 if not _os_path.isdir(cache_dir):
438 _os.makedirs(cache_dir)
439 ogv_url = _os_path.join(dirname, ogv_filename)
440 ogv_path = _os_path.join(cache_dir, ogv_filename)
441 if not _os_path.isfile(video):
442 LOG.error('source video path does not exist')
443 if (not _os_path.isfile(ogv_path)
444 or _os_path.getmtime(video) > _os_path.getmtime(ogv_path)):
445 arg = ['ffmpeg2theora', '--optimize']
447 arg.extend(['--output', ogv_path, video])
449 return (ogv_path, self._url(ogv_url))
451 def _get_image_caption(self, path):
452 caption_path = path + '.txt'
454 return open(caption_path, 'r').read()
458 def _get_image_video(self, path, fallback=None):
459 base_path = image_base(path)
460 for extension in VIDEO_EXTENSIONS:
461 video_path = base_path + extension
462 if _os_path.isfile(video_path):
463 return self._video(video_path, fallback=fallback)
466 def _captioned_video(self, path, href=None):
467 img = self._image(path, max_width=640, max_height=480)
468 caption = self._get_image_caption(path)
469 video = self._get_image_video(path, fallback=[img])
472 content.extend(video)
474 content.append('<p>{}</p>'.format(
475 self._link(path=href, text='gallery page')))
477 content.append(self._link(path=href, text=img))
481 caption = _xml_sax_saxutils.escape(caption)
482 content.append('<p>{}</p>'.format(caption))
485 def _video(self, video, fallback=None, **kwargs):
488 '<p>Your browser does not support the <video> tag, try',
489 'downloading the video and playing it in an external player.',
492 fallback = [' '+line for line in fallback]
493 ogv_path,ogv_url = self._ogv(video)
494 mp4_path,mp4_url = self._mp4(video)
497 (' <video preloads="none" controls="controls" '
498 'width="640" height="480">'),
499 ' <source src="{}"'.format(mp4_url),
500 (''' type='video/mp4; '''
501 '''codecs="avc1.42E01E, mp4a.40.2"' />'''),
502 ' <source src="{}"'.format(ogv_url),
503 ''' type='video/ogg; codecs="theora,vorbis"' />''',
508 ' <a href="{}">Ogg/Theora/Vorbis</a> or'.format(ogv_url),
509 (' <a href="{}">Mpeg4/H.264(ConstrainedBaselineProfile)/AAC</a>.'
514 def _image(self, image, **kwargs):
516 image_path,image_url = self._thumb(image, **kwargs)
519 sections = ['<img src="{}"'.format(image_url)]
520 caption = self._get_image_caption(path=image)
522 caption = _xml_sax_saxutils.quoteattr(
523 caption.replace('\n', ' ').strip())
525 'title={}'.format(caption),
526 'alt={}'.format(caption),
528 sections.append('/>')
529 return ' '.join(sections)
531 def _image_page(self, image):
532 return image_base(image) + '/'
534 def random(self, url=None, stream=None, **kwargs):
535 LOG.debug('random image')
536 if url.endswith('/random'):
537 url = url[:(-len('/random'))]
538 self.validate_url(url=url, directory=True, stream=stream)
539 base_dir = _os_path.join(self._base_path, url)
540 elif url == 'random':
541 base_dir = self._base_path
545 for dirpath,dirnames,filenames in _os.walk(base_dir):
546 for filename in filenames:
547 if is_image(filename):
548 images.append(_os_path.join(dirpath, filename))
550 self._response(content='<p>no images to choose from</p>',
552 image = _random.choice(images)
553 LOG.debug('selected random image {}'.format(image))
554 page = self._image_page(image)
555 content = self._captioned_video(path=image, href=page)
556 self._response(content='\n'.join(content), stream=stream)
558 def is_cacheable(self, url):
559 return is_image(url) or is_video(url)
561 def cached(self, url, stream=None):
562 LOG.debug('retrieving possibly cached item')
563 mime = _mimetypes.guess_type(url)[0]
566 500, content='unknown mime type for {}'.format(url))
567 cache_path = _os_path.join(self._cache_path, url)
568 original_path = _os_path.join(self._base_path, url)
570 if _os_path.isfile(cache_path):
571 LOG.debug('return cached item {}'.format(cache_path))
573 elif self._serve_originals and _os_path.isfile(original_path):
574 LOG.debug('return original item {}'.format(original_path))
577 LOG.debug('possibly create cached item {}'.format(cache_path))
578 original_url,callback,kwargs = self._original_url(url)
579 original_path = _os_path.join(self._base_path, original_url)
580 if callback and _os_path.isfile(original_path):
581 path,cache_url = callback(original_path, **kwargs)
585 content = open(path, 'rb')
588 raise HTTPError(404, content='item not found {}'.format(url))
589 headers = self._http_headers(mime=mime)
590 if mime in STREAMING_TYPES:
591 self._response_stream(
592 headers=headers, content=content, stream=stream)
593 content = content.read()
594 self._response(headers=headers, content=content, stream=stream)
596 def page(self, url, page=0, stream=None):
597 LOG.debug('HTML page {} {}'.format(url, page))
598 if not url.endswith('/'):
600 404, content='HTML page URLs must end with a slash')
601 abspath = _os_path.join(self._base_path, url)
602 if _os_path.isdir(abspath):
603 self._directory(path=abspath, page=page, stream=stream)
604 for extension in IMAGE_EXTENSIONS:
605 file_path = abspath[:-1] + extension
606 if _os_path.isfile(file_path):
607 self._page(path=file_path, stream=stream)
608 raise HTTPError(404, content='unknown HTML page {}'.format(url))
610 def _directory_header(self, path):
611 relpath = _os_path.relpath(path, self._base_path)
615 dirname,base = _os_path.split(dirname)
617 crumbs.insert(0, base)
619 links = [None] * len(crumbs)
620 for i,c in enumerate(crumbs):
621 if i < len(crumbs)-1:
623 links[i] = self._link(self._base_path, 'Gallery')
625 relpath = '/'.join(crumbs[1:i+1]) + '/'
626 fullpath = _os_path.join(self._base_path, relpath)
627 links[i] = self._link(path=fullpath)
632 links[i] = self._label(crumbs[i])
633 content = ['<h1>{}</h1>'.format(' '.join(links))]
636 def _directory_page_navigation(self, path, page, pages):
639 prev_page = path + '?pp={:d}'.format((page - 1) % pages + 1)
640 next_page = path + '?pp={:d}'.format((page + 1) % pages + 1)
642 '<div style="text-align: center;">',
644 self._link(prev_page, 'previous'),
645 '({:d} of {:d})'.format(page+1, pages),
646 self._link(next_page, 'next'),
651 def _directory_subdirs(self, path):
653 dirs = list(self._subdirs(path))
655 content.append('<ul>')
657 content.append(' <li>{}</li>'.format(self._link(d+'/')))
658 content.append('</ul>')
661 def _directory_images(self, path, images):
662 content = ['<table style="margin-left: auto; margin-right: auto;">']
665 page = self._image_page(image)
666 img = self._image(image, max_width=300, max_height=300)
667 link = self._link(page, img)
669 content.append(' <tr>')
671 ' <td style="text-align: center;">',
676 if column == self._columns:
677 content.append(' </tr>')
681 content.append(' </tr>')
682 content.append('</table>')
685 def _directory(self, path, page=0, stream=None):
686 LOG.debug('directory page {} {}'.format(path, page))
687 images = list(self._images(path))
688 images_per_page = self._rows * self._columns
689 pages = int(_math.ceil(float(len(images)) / images_per_page)) or 1
690 if page < 0 or page >= pages:
694 'page out of bounds for this gallery 0 <= {:d} < {:d}'
695 ).format(page, pages))
696 first_image = images_per_page * page
697 images = images[first_image:first_image+images_per_page]
699 content.extend(self.header)
700 content.extend(self._directory_header(path))
701 nav = self._directory_page_navigation(path, page=page, pages=pages)
703 content.extend(self._directory_subdirs(path))
704 content.extend(self._directory_images(path, images=images))
706 content.extend(self.footer)
707 self._response(content='\n'.join(content), stream=stream)
709 def _page(self, path, stream=None):
710 LOG.debug('image page {}'.format(path))
711 gallery = _os_path.dirname(path)
712 images = list(self._images(gallery))
713 images_per_page = self._rows * self._columns
714 i = images.index(path)
715 page = i // images_per_page
716 gallery_page = '{}/?pp={:d}'.format(gallery, page + 1)
717 prev_page = self._image_page(images[i - 1])
718 next_page = self._image_page(images[(i + 1) % len(images)])
720 content.extend(self.header)
722 '<div style="text-align: center;">',
724 self._link(prev_page, 'previous'),
725 self._link(gallery_page, 'all'),
726 self._link(next_page, 'next'),
729 content.extend(self._captioned_video(path))
730 content.append('</div>')
731 content.extend(self.footer)
732 self._response(content='\n'.join(content), stream=stream)
735 def serve_cgi(server):
742 #cgitb.enable(display=0, logdir="/tmp/")
743 data = cgi.FieldStorage()
744 page = server.page_from_query(
745 query={key: data[key].getlist() for key in data.keys()})
748 if isinstance(p, list):
751 server.serve(url=url, page=page, stream=sys.stdout)
753 def serve_scgi(server, host='localhost', port=4000):
755 import scgi.scgi_server
757 class GalleryHandler(scgi.scgi_server.SCGIHandler):
758 def produce(self, env, bodysize, input, output):
759 #LOG.info(HTTP_USER_AGENT REQUEST_METHOD REMOTE_ADDR REQUEST_URI
760 url = env.get('DOCUMENT_URI', None)
761 page = server.page_from_query(
762 query_string=env.get('QUERY_STRING', ''))
765 url = server.relative_url(url=url)
766 except HTTPError as e:
768 server._error(e.status, content=e.content, stream=output)
769 except ProcessingComplete:
772 server.serve(url=url, page=page, stream=output)
774 s = scgi.scgi_server.SCGIServer(
775 handler_class=GalleryHandler, host=host, port=port)
776 LOG.info('serving SCGI on {}:{}'.format(host, port))
780 if __name__ == '__main__':
781 import argparse as _argparse
783 parser = _argparse.ArgumentParser(
784 description=__doc__, version=__version__,
785 formatter_class=_argparse.RawDescriptionHelpFormatter)
787 '--scgi', default=False, action='store_const', const=True,
788 help='Run as a SCGI server (vs. serving a single CGI call)')
790 '--port', default=4000, type=int,
791 help='Port to listen to (if runing as a SCGI server)')
793 '--base-path', default='.',
794 help='Path to the root gallery source')
796 '--base-url', default='/',
797 help='URL for the root gallery source')
799 '--shared-path', default=None,
800 help=('Optional path to the shared directory containing '
801 '`header.shtml` and `footer.shtml`'))
803 '--cache-path', default='/tmp/gallery-cache',
804 help='Path to the thumbnail and movie cache directory')
806 args = parser.parse_args()
808 s = CGIGalleryServer(
809 base_path=args.base_path, base_url=args.base_url,
810 cache_path=args.cache_path)
812 shared = args.shared_path
813 s.header = [open(_os_path.join(shared, 'header.shtml'), 'r').read()]
814 s.footer = [open(_os_path.join(shared, 'footer.shtml'), 'r').read()]
817 serve_scgi(server=s, port=args.port)