#!/usr/bin/python
##############################################################################
# getImages.py #
# #
# Author: Anupam Jain #
# #
# CGI application which will recursively scan the file system starting from #
# the supplied directory, and print an HTML form with a selectable list of #
# image files (GIF, JPG, and PNG format). Upon selection of one of the files #
# the application returns the image file itself using appropriate mime types #
##############################################################################
# Import required modules
import sys, time, os, cgi
# Enable pretty printing of error messages to stdout
import cgitb; cgitb.enable()
# Get the supplied options
form = cgi.FieldStorage()
# If image name is specified
if form.has_key('imageName'):
img = form['imageName'].value
# Try to open the image file
try:
f = open(img, 'rb')
# Print out the contents with the correct mime type
print 'Content-type: image/'+img[-3:]+'\n'
f.seek(0)
print f.read()
# Print error message if the file does not exist
except:
print 'Content-type: text/html\n'
print '<html><head><title>Error!</title></head><body>'
print 'Error: Could not find the requested image file - %s' % img
print '</body></html>'
# If image name is not specified
# Then show the list of available images in the supplied directory
else:
print 'Content-type: text/html\n'
print '<html>'
print '<head><title>Download an image</title></head>'
# javascript function to submit the form
print '<script language="javascript">'
print 'function submitForm(cntl) {'
print ' if(cntl.value==\'none\') return;'
print ' else document.forms[0].submit();'
print '}'
print '</script>'
# print heading and time
print '<body><form action="getImages.py"><h1>Images download</h1>'
print '<b>' + time.ctime() + '</b><br><br>'
# If directory name was specified, get it
if(len(sys.argv)>1):
dirname = sys.argv[1]
else:
dirname = ''
# Search for three types of images
valid_extn = ['png','jpg','gif']
# Function to process files in each directory
def processDir(nothing, dirname, files):
for f in files:
# Avoid directories, process only simple files
if not os.path.isdir(f):
# Process only files with valid extensions
if f.lower()[-3:] in valid_extn:
# print out the options for the <select>
print '<option value="%s">%s</option>' % (os.path.join(dirname,f),f)
# Print error messages if -
# EITHER a) no directory is passed
if not dirname:
print '<b>host: %s<br>directory: %s</b><hr>' % (os.name, 'Not specified')
print 'ERROR: Please specify a valid directory.</form></body></html>'
# OR b) Directory does not exist
elif not os.path.isdir(dirname):
print '<b>HOST: %s<br>DIRECTORY: %s</b><hr>' % (os.name, dirname)
print 'ERROR: Could not find the specified directory.</form></body></html>'
# Directory exists therefore perform recursive search
# and show image files found in a selector
else:
print '<b>HOST: %s<br>DIRECTORY: %s</b><hr>' % (os.name, dirname)
print '<br>Select a file to download:'
print '<select name="imageName" onchange="submitForm(this)">'
print '<option value="none">Select a file...</option>'
# Call recursive directory search using os.path.walk
os.path.walk(dirname,processDir,None)
print '</select>'
print '</form>'
Nice!
No comments:
Post a Comment