view uploader/ @ 21:183224756ded

reverse order the log for display
author Jeff Hammel <>
date Mon, 01 Aug 2011 23:53:33 -0700
parents f1142bcce77a
children a60cf57d6628
line wrap: on
line source
import os
import sys
from urlparse import urlparse
from webob import Response, exc

class Handler(object):
    def __init__(self, app, request): = app
        self.request = request
        self.application_path = urlparse(request.application_url)[2]

    def link(self, path=(), permanant=False):
        if isinstance(path, basestring):
            path = [ path ]
        path = [ i.strip('/') for i in path ]
        if permanant:
            application_url = [ self.request.application_url ]
            application_url = [ self.application_path ]
        path = application_url + path
        return '/'.join(path)

    def redirect(self, location):
        return exc.HTTPSeeOther(location=location)

class Get(Handler):

    form = """<form name="upload_form" method="post" enctype="multipart/form-data">
<input type="file" name="file"/><input type="submit" value="upload"/></form></body></html>"""

    def match(cls, app, request):
        if app.query_string and (app.query_string not in request.GET):
            return False
        return request.method == 'GET'

    def __call__(self):
        form = "<html><body>"
        if 'uploaded' in self.request.GET:
            form += '<div>%s uploaded successfully</div>' % self.request.GET['uploaded']
        form += self.form
            contents = os.listdir(
            if contents:
                form += '<div><i>Currently uploaded:<ul><li>' + '</li><li>'.join(contents) + '</li></ul></i></div>'
                form += '<div><i>No files in upload directory</i></div>'
            log_file =
            if log_file and os.path.exists(log_file):
                log_contents = ''.join(reversed(file(log_file).readlines()))
                form += '<div>Upload log:<pre>%s</pre></div>' % log_contents
        form += '</body></html>'
        return Response(content_type='text/html', body=form)

class Post(Handler):

    def match(cls, app, request):
        return request.method == 'POST'

    def write(self, fin, path):
        fout = file(path, 'w')

    def __call__(self):

        # get the file
        fin = self.request.POST['file']
            _path = fin.filename
        except AttributeError: # no file uploaded
            return self.redirect('/'))

        # don't allow bad filenames
        illegal = ['..', '<', '&', '>']
        for i in illegal:
            _path = _path.replace(i, '_')

        # write the file + redirect
        _path = os.path.join(, _path)
        self.write(fin, _path)
                f = file(, 'a')
                print >> f, fin.filename
            except Exception, e:
                print >> sys.stderr, e
        return self.redirect('/?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):
    def match(cls, app, request):
        if not Get.match(app, request):
            return False
        _path = path(, request)
        if os.path.exists(_path) and os.path.isdir(_path):
            return True
class SubpathPost(Post):
    def match(cls, app, request):
        if request.method != 'POST':
            return False
        _path = path(, request)
        if os.path.exists(_path) and os.path.isdir(_path):
            return True

    def __call__(self):
        fin = self.request.POST['file']
        _path = path(, self.request)
        _path = os.path.join(_path, fin.filename)
        self.write(fin, _path)
        return self.redirect(