changeset 20:f1142bcce77a

add ability to log uploads
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 19 Jul 2011 08:08:58 -0700
parents 4da97e040145
children 183224756ded
files setup.py uploader/dispatcher.py uploader/handlers.py
diffstat 3 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py
+++ b/setup.py
@@ -1,14 +1,14 @@
 from setuptools import setup, find_packages
 import sys, os
 
 version = "0.2.6"
 
-setup(name='uploader',
+setup(name='UploadView',
       version=version,
       description="a file uploader app",
       long_description="",
       classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
       author='Ethan Jucovy',
       author_email='',
       url='http://k0s.org/hg/uploader',
       license="GPL",
--- a/uploader/dispatcher.py
+++ b/uploader/dispatcher.py
@@ -1,25 +1,26 @@
 """
-request dispatcher
+request dispatcher for uploader
 """
 
 import os
 from handlers import Get, Post, SubpathGet, SubpathPost
 from webob import Request, exc
 
 class Dispatcher(object):
 
     ### class level variables
     defaults = { 'directory': None,
                  'auth': False,
                  'query_string': None,
                  'subpath': False,
                  'display_contents': False,
-                 'app': None}
+                 'app': None,
+                 'log_file': None}
 
     def __init__(self, **kw):
 
         # set defaults
         for key in self.defaults:
             setattr(self, key, kw.get(key, self.defaults[key]))
         assert os.path.exists(self.directory), "The base directory %s does not exist; uploader needs it!" % self.directory
         if self.app:
--- a/uploader/handlers.py
+++ b/uploader/handlers.py
@@ -1,9 +1,10 @@
 import os
+import sys
 from urlparse import urlparse
 from webob import Response, exc
 
 class Handler(object):
     def __init__(self, app, request):
         self.app = app
         self.request = request
         self.application_path = urlparse(request.application_url)[2]
@@ -40,16 +41,20 @@ class Get(Handler):
         form += self.form
         if self.app.display_contents:
             contents = os.listdir(self.app.directory)
             if contents:
                 contents.sort()
                 form += '<div><i>Currently uploaded:<ul><li>' + '</li><li>'.join(contents) + '</li></ul></i></div>'
             else:
                 form += '<div><i>No files in upload directory</i></div>'
+            log_file = self.app.log_file
+            if log_file and os.path.exists(log_file):
+                log_contents = file(log_file).read()
+                form += '<div>Upload log:<pre>%s</pre></div>' % log_contents
         form += '</body></html>'
         return Response(content_type='text/html', body=form)
 
 class Post(Handler):
 
     @classmethod
     def match(cls, app, request):
         return request.method == 'POST'
@@ -72,16 +77,23 @@ class Post(Handler):
         illegal = ['..', '<', '&', '>']
         illegal.append(os.path.sep)
         for i in illegal:
             _path = _path.replace(i, '_')
 
         # write the file + redirect
         _path = os.path.join(self.app.directory, _path)
         self.write(fin, _path)
+        if self.app.log_file:
+            try:
+                f = file(self.app.log_file, 'a')
+                print >> f, fin.filename
+                f.close()
+            except Exception, e:
+                print >> sys.stderr, e
         return self.redirect(self.link('/?uploaded=' + fin.filename))
 
 def path(directory, request):
     if os.sep == '/':
         return os.path.join(directory, request.path_info.strip('/'))
     return os.path.join(directory, *request.path_info.strip('/').split('/'))
 
 class SubpathGet(Get):