from filebrowser.fields import FileBrowseField
from filebrowser.views import filebrowser_post_upload
import sys, zipfile, os, os.path
file = FileBrowseField("File", max_length=200, directory="shells/", blank=True,
null=True,help_text="Upload a video/image/swf, zipped slideshow, etc.")
def post_upload_callback(sender, **kwargs):
Signal receiver called each time an upload has finished.
Triggered by Filebrowser's filebrowser_post_upload signal:
We'll use this to unzip .zip files in place when/if they're uploaded.
if kwargs['file'].extension == ".zip":
# Note: this doesn't test for corrupt zip files.
# If encountered, user will get an HTTP Error
# and file will remain on the server.
# We get returned relative path names from Filebrowser
path = kwargs['path']
thefile = kwargs['file']
# Convert file and dir into absolute paths
fullpath = os.path.join(settings.MEDIA_ROOT,thefile.path_relative)
dirname = os.path.dirname(fullpath)
# Get a real Python file handle on the uploaded file
fullpathhandle = open(fullpath, 'r')
# Unzip the file, creating subdirectories as needed
zfobj = zipfile.ZipFile(fullpathhandle)
for name in zfobj.namelist():
try: # Don't try to create a directory if exists
outfile = open(os.path.join(dirname, name), 'wb')
# Now try and delete the uploaded .zip file and the
# stub __MACOSX dir if they exist.
osxjunk = os.path.join(dirname,'__MACOSX')
# Signal provided by FileBrowser on every successful upload.