def upload(request, path="uploads"): if request.method == 'POST': path = request.POST.get('path', path) if len(request.FILES) != 1: return HttpResponseBadRequest("Must upload one and only file at a time.") for field_name in request.FILES: uploaded_file = request.FILES[field_name] # write the file into /tmp try: destination_folder = safe_join(settings.MEDIA_ROOT, path) except ValueError: return HttpResponseBadRequest("This usually only happens when you supply an invalid value for the path.") try: destination_path = '%s/%s' % (destination_folder, uploaded_file.name) destination = open(destination_path, 'wb+') for chunk in uploaded_file.chunks(): destination.write(chunk) destination.close() except Exception, inst: raise Exception("Some kind of error: %s" % inst) data = { 'file': uploaded_file.name, 'url': '%s/%s/%s' % (settings.MEDIA_URL, path, uploaded_file.name), 'type': uploaded_file.content_type, 'size': uploaded_file.size } # indicate that everything is OK for SWFUpload return HttpResponse(json.dumps(data))
def FileSystemStorage_path(self, name): try: path = storage.safe_join(os.environ['UPLOAD_ROOT'], name) except ValueError: raise storage.SuspiciousOperation( "Attempted access to '%s' denied." % name) return os.path.normpath(path)