def _upload_file(request): """ Upload file to the server. """ from django.core.files.move import file_move_safe if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, folder) if request.FILES: filedata = request.FILES['Filedata'] filedata.name = _convert_filename(filedata.name) # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) # HANDLE UPLOAD uploadedfile = _handle_file_upload(abs_path, filedata) # MOVE UPLOADED FILE # if file already exists if _supports_os() and os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, filedata.name)): old_file = os.path.join(abs_path, filedata.name) new_file = os.path.join(abs_path, uploadedfile) file_move_safe(new_file, old_file) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(os.path.join(DIRECTORY, folder, filedata.name))) return HttpResponse('True')
def clean_dir_name(self): if self.cleaned_data['dir_name']: # only letters, numbers, underscores, spaces and hyphens are allowed. if not alnum_name_re.search(self.cleaned_data['dir_name']): raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.')) # directory must not already exist. if _supports_os() and os.path.isdir(os.path.join(self.path, _convert_filename(self.cleaned_data['dir_name']))): raise forms.ValidationError(_(u'The Folder already exists.')) return _convert_filename(self.cleaned_data['dir_name'])
def mkdir(request): """ Make Directory. """ from filebrowser.forms import MakeDirForm # QUERY / PATH CHECK query = request.GET path = _get_path(query.get('dir', '')) if path is None: msg = _('Directory/File does not exist.') request.user.message_set.create(message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) if request.method == 'POST': form = MakeDirForm(abs_path, request.POST) if form.is_valid(): server_path = os.path.join(abs_path, form.cleaned_data['dir_name']) try: dirname = form.cleaned_data['dir_name'] # PRE CREATE SIGNAL filebrowser_pre_createdir.send(sender=request, path=path, dirname=dirname) # CREATE FOLDER if _supports_os(): os.mkdir(server_path) os.chmod(server_path, 0775) # POST CREATE SIGNAL filebrowser_post_createdir.send(sender=request, path=path, dirname=dirname) # MESSAGE & REDIRECT msg = _('The Folder %s was successfully created.') % (dirname) request.user.message_set.create(message=msg) # on redirect, sort by date desc to see the new directory on top of the list # remove filter in order to actually _see_ the new folder from urllib import quote new_path=quote(_normpath(os.path.join(path, dirname))) redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date,dir=%s" %(new_path), "ot,o,filter_type,filter_date,q") return HttpResponseRedirect(redirect_url) except OSError, (errno, strerror): if errno == 13: form.errors['dir_name'] = forms.util.ErrorList([_('Permission denied.')]) else: form.errors['dir_name'] = forms.util.ErrorList([_('Error creating directory.')])