1#!/usr/local/bin/python 2# 3# imdm 4# 5# IMage Display Master 6# 7# This uses the affiliated C-cgi "imd" to build a caching image display 8# server with only passive Apache cgis. 9# 10import sys,os,string 11import cgi 12 13# this function should find the first four images inside a 14# nested subdirectory 15 16albumstartfile = "/~jeske/Images/jeskealbum.imd" 17imagestartfile = "/~jeske/Images/jeskealbum.imc" 18 19THUMB_WIDTH = 120 20THUMB_HEIGHT = 90 21 22# ------------------------------------------------------------------------------------ 23# 24# utility functions 25 26def albumoverview(basedir,sub_dir,count = 4,skip = 0): 27 images = [] 28 29 fulldir = os.path.join(basedir,sub_dir) 30 for a_entry in os.listdir(fulldir): 31 fullpath = os.path.join(fulldir,a_entry) 32 if os.path.isfile(fullpath): 33 if string.lower(string.split(a_entry,".")[-1]) in ["jpeg","jpg"]: 34 images.append(os.path.join(sub_dir,a_entry)) 35 elif os.path.isdir(fullpath): 36 images + albumoverview(basedir,os.path.join(sub_dir,a_entry),1) 37 if len(images) >= (count + skip): 38 return images[skip:] 39 40 return images[skip:] 41 42 43def makethumbnailimgtag(filename,width=THUMB_WIDTH,height=THUMB_HEIGHT): 44 global imagestartfile 45 return '<IMG SRC="%s?image=%s&width=%s&height=%s">' % (imagestartfile,filename,width,height) 46 47def makealbumurl(dir): 48 global albumstartfile 49 return "%s?album=%s" % (albumstartfile,dir) 50 51def makepictureurl(dir,picture): 52 global albumstartfile 53 return "%s?album=%s&picture=%s" % (albumstartfile,dir,picture) 54 55# ------------------------------------------------------------------------------------ 56# 57# picturedisplay 58 59def picturedisplay(basedir,album,picture): 60 sys.stdout.write("<A HREF=\"%s?\">top</A> " % albumstartfile) 61 62 sys.stdout.write("-- <A HREF=\"%s\">%s</A>" % (makealbumurl(album),album)) 63 64 imagename = os.path.join(album,picture) 65 66 sys.stdout.write("<br><hr>") 67 68 sys.stdout.write("<TABLE WIDTH=100%><TR><TD ALIGN=CENTER>\n") 69 sys.stdout.write(makethumbnailimgtag(imagename,width=600,height=500)) 70 sys.stdout.write("</TD></TR></TABLE>\n") 71 72 images = albumoverview(basedir,album,count=500) 73 image_index = None 74 for x in range(len(images)): 75 if images[x] == imagename: 76 image_index = x 77 break 78 79 if not image_index is None: 80 sys.stdout.write("<CENTER><TABLE WIDTH=50% BORDER=1><TR>") 81 82 # pre-images 83 for i in range(1,3): 84 pic_index = image_index - i 85 86 picture_path = string.join(string.split(images[pic_index],'/')[1:],'/') 87 sys.stdout.write("<TD ALIGN=CENTER><A HREF=\"%s\">%s</A></TD>" % (makepictureurl(album,picture_path),makethumbnailimgtag(images[pic_index]))) 88 89 sys.stdout.write("<br>") 90 91 # post-images 92 for i in range(1,3): 93 pic_index = image_index + i 94 if pic_index >= len(images): 95 pic_index = pic_index - len(images) 96 97 picture_path = string.join(string.split(images[pic_index],'/')[1:],'/') 98 sys.stdout.write("<TD ALIGN=CENTER><A HREF=\"%s\">%s</A></TD>" % (makepictureurl(album,picture_path), makethumbnailimgtag(images[pic_index]))) 99 sys.stdout.write("</TR></TABLE></CENTER>\n") 100 101 # navigation 102 103 104# ------------------------------------------------------------------------------------ 105# 106# albumdisplay 107 108 109 110def albumdisplay(basedir,album,columns=7,rows=5): 111 next_page = 0 112 113 114 sys.stdout.write("<table border=0 bgcolor=#cccccc width=100%%><tr><td align=center><font size=+2>%s</font></td></tr></table>" % album) 115 116 imgcount = columns * rows 117 images = albumoverview(basedir,album,count=(imgcount + 1),skip=0) 118 119 if len(images) > imgcount: 120 images = images[:-1] 121 next_page = 1 122 123 while images: 124 sys.stdout.write("<CENTER><TABLE WIDTH=90% CELLSPACING=3 BORDER=1><TR>") 125 for a_col in range(columns): 126 if len(images): 127 picture_path = string.join(string.split(images[0],'/')[1:],'/') 128 sys.stdout.write("<TD ALIGN=CENTER><A HREF=\"%s\">%s</A></TD>" % (makepictureurl(album,picture_path),makethumbnailimgtag(images[0]))) 129 images = images[1:] 130 sys.stdout.write("</tr></table></CENTER>") 131 132 if next_page: 133 sys.stdout.write("more...") 134 135 136# ------------------------------------------------------------------------------------ 137# 138# topalbumoverview 139 140def topalbumoverview(dir): 141 for a_dir in os.listdir(dir): 142 if os.path.isdir(os.path.join(dir,a_dir)): 143 sys.stdout.write("<CENTER>") 144 sys.stdout.write("<TABLE BGCOLOR=#ccccc WIDTH=50% BORDER=0 CELLSPACING=1 CELLPADDING=1>") 145 146 sys.stdout.write("<TR><TD> <font size=+2><A HREF=\"%s\">%s</A></font></TD></TR>" % (makealbumurl(a_dir),a_dir)) 147 148 sys.stdout.write("<TR><TD ALIGN=CENTER><TABLE BGCOLOR=#FFFFFF WIDTH=100% BORDER=0 CELSPACING=0 CELLPADDING=0><TR>") 149 150 for a_file in albumoverview(dir,a_dir): 151 picture_path = string.join(string.split(a_file,'/')[1:],'/') 152 sys.stdout.write("<TD ALIGN=CENTER><A HREF=\"%s\">%s</A></TD>\n" % (makepictureurl(a_dir,picture_path),makethumbnailimgtag(a_file))) 153 sys.stdout.write("</TR></TABLE></TD></TR></TABLE></CENTER>\n<p>\n") 154 155# ------------------------------------------------------------------------------------ 156# 157# readvars() -- simple file format reader 158 159def readvars(filename): 160 vars = {} 161 data = open(filename,"rb").read() 162 lines = string.split(data,"\n") 163 for a_line in lines: 164 stripped_line = string.strip(a_line) 165 if not stripped_line or stripped_line[0] == "#": 166 continue 167 try: 168 key,value = string.split(a_line,"=") 169 vars[key] = value 170 except: 171 pass 172 return vars 173 174# ------------------------------------------------------------------------------------ 175# 176# main() 177 178 179def main(): 180 global cgiform 181 cgiform = cgi.FieldStorage() 182 183 sys.stdout.write("Content-Type: text/html\n\n") 184 sys.stdout.write("<h1>HTML Image Viewer!</h1><p>") 185 186 myvars = readvars(os.environ['PATH_TRANSLATED']) 187 188 sys.stderr.write(repr(myvars)) 189 190 global albumstartfile, imagestartfile 191 albumstartfile = os.environ['PATH_INFO'] 192 imagestartfile = myvars['IMGSTARTFILE'] 193 BASEDIR = myvars['BASEDIR'] 194 195 album = cgiform.getvalue('album',None) 196 picture = cgiform.getvalue('picture',None) 197 198 if album is None: 199 topalbumoverview(BASEDIR) 200 elif picture is None: 201 albumdisplay(BASEDIR,album) 202 else: 203 picturedisplay(BASEDIR,album,picture) 204 205if __name__ == "__main__": 206 main() 207 208 209 210