Пример #1
0
 def clean_name(self):
     "validate name"
     if self.cleaned_data['name']:
         # only letters, numbers, underscores, spaces and hyphens are allowed.
         if not ALNUM_NAME_RE.search(self.cleaned_data['name']):
             raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'))
         # Folder must not already exist.
         if self.site.storage.isdir(os.path.join(self.path, convert_filename(self.cleaned_data['name']))):
             raise forms.ValidationError(_(u'The Folder already exists.'))
     return convert_filename(self.cleaned_data['name'])
Пример #2
0
 def clean_name(self):
     "validate name"
     if self.cleaned_data['name']:
         # only letters, numbers, underscores, spaces and hyphens are allowed.
         if not ALNUM_NAME_RE.search(self.cleaned_data['name']):
             raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'))
         # Folder must not already exist.
         if self.site.storage.isdir(os.path.join(self.path, convert_filename(self.cleaned_data['name']))):
             raise forms.ValidationError(_(u'The Folder already exists.'))
     return convert_filename(self.cleaned_data['name'])
Пример #3
0
    def _upload_file(self, request):
        """
        Upload file to the server.
        """
        if request.method == "POST":
            folder = request.GET.get('folder', '')

            if len(request.FILES) == 0:
                return HttpResponseBadRequest(
                    'Invalid request! No files included.')
            if len(request.FILES) > 1:
                return HttpResponseBadRequest(
                    'Invalid request! Multiple files included.')

            filedata = list(request.FILES.values())[0]

            fb_uploadurl_re = re.compile(
                r'^.*(%s)' %
                reverse("filebrowser:fb_upload", current_app=self.name))
            folder = fb_uploadurl_re.sub('', folder)

            path = os.path.join(self.directory, folder)
            # we convert the filename before uploading in order
            # to check for existing files/folders
            file_name = convert_filename(filedata.name)
            filedata.name = file_name
            file_path = os.path.join(path, file_name)
            file_already_exists = self.storage.exists(file_path)

            # Check for name collision with a directory
            if file_already_exists and self.storage.isdir(file_path):
                ret_json = {'success': False, 'filename': file_name}
                return HttpResponse(json.dumps(ret_json))

            signals.filebrowser_pre_upload.send(sender=request,
                                                path=folder,
                                                file=filedata,
                                                site=self)
            uploadedfile = handle_file_upload(path, filedata, site=self)

            if file_already_exists and OVERWRITE_EXISTING:
                old_file = smart_text(file_path)
                new_file = smart_text(uploadedfile)
                self.storage.move(new_file, old_file, allow_overwrite=True)
            else:
                file_name = smart_text(uploadedfile)
                filedata.name = os.path.relpath(file_name, path)

            signals.filebrowser_post_upload.send(sender=request,
                                                 path=folder,
                                                 file=FileObject(
                                                     smart_text(file_name),
                                                     site=self),
                                                 site=self)

            # let Ajax Upload know whether we saved it or not
            ret_json = {'success': True, 'filename': file_name}
            return HttpResponse(json.dumps(ret_json))
Пример #4
0
    def _upload_file(self, request):
        """
        Upload file to the server.
        """
        if request.method == "POST":
            folder = request.GET.get('folder', '')

            if len(request.FILES) == 0:
                return HttpResponseBadRequest('Invalid request! No files included.')
            if len(request.FILES) > 1:
                return HttpResponseBadRequest('Invalid request! Multiple files included.')

            filedata = list(request.FILES.values())[0]

            fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
            folder = fb_uploadurl_re.sub('', folder)

            path = os.path.join(self.directory, folder)
            # we convert the filename before uploading in order
            # to check for existing files/folders
            file_name = convert_filename(filedata.name)
            filedata.name = file_name
            file_path = os.path.join(path, file_name)
            file_already_exists = self.storage.exists(file_path)
            # Check for name collision with a directory
            if file_already_exists and self.storage.isdir(file_path):
                ret_json = {'success': False, 'filename': file_name}
                return HttpResponse(json.dumps(ret_json))

            signals.filebrowser_pre_upload.send(sender=request, path=folder, file=filedata, site=self)
            uploadedfile = handle_file_upload(path, filedata, site=self)
            local_storage = True
            if file_already_exists and OVERWRITE_EXISTING:
                old_file = smart_text(file_path)
                new_file = smart_text(uploadedfile)
                self.storage.move(new_file, old_file, allow_overwrite=True)
                try:
                    full_path = FileObject(smart_text(old_file), site=self).path_full
                except NotImplementedError:
                    local_storage = False
            else:
                file_name = smart_text(uploadedfile)
                filedata.name = os.path.relpath(file_name, path)
                try:
                    full_path = FileObject(smart_text(file_name), site=self).path_full
                except NotImplementedError:
                    local_storage = False

            # set permissions
            if local_storage and DEFAULT_PERMISSIONS is not None:
                os.chmod(full_path, DEFAULT_PERMISSIONS)

            f = FileObject(smart_text(file_name), site=self)
            signals.filebrowser_post_upload.send(sender=request, path=folder, file=f, site=self)

            # let Ajax Upload know whether we saved it or not
            ret_json = {'success': True, 'filename': f.filename}
            return HttpResponse(json.dumps(ret_json), content_type="application/json")
Пример #5
0
    def _upload_file(self, request):
        if not request.user.has_perm('auth.view_filebrowser'):
            raise PermissionDenied()
        """
        Upload file to the server.
        """
        if request.method == "POST":
            folder = request.GET.get('folder', '')

            if len(request.FILES) == 0:
                return HttpResponseBadRequest('Invalid request! No files included.')
            if len(request.FILES) > 1:
                return HttpResponseBadRequest('Invalid request! Multiple files included.')

            filedata = list(request.FILES.values())[0]

            fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
            folder = fb_uploadurl_re.sub('', folder)

            path = os.path.join(self.directory, folder)
            # we convert the filename before uploading in order
            # to check for existing files/folders
            file_name = convert_filename(filedata.name)
            filedata.name = file_name
            file_path = os.path.join(path, file_name)
            file_already_exists = self.storage.exists(file_path)

            # Check for name collision with a directory
            if file_already_exists and self.storage.isdir(file_path):
                ret_json = {'success': False, 'filename': file_name}
                return HttpResponse(json.dumps(ret_json))

            signals.filebrowser_pre_upload.send(sender=request, path=folder, file=filedata, site=self)
            uploadedfile = handle_file_upload(path, filedata, site=self)

            if file_already_exists and OVERWRITE_EXISTING:
                old_file = smart_text(file_path)
                new_file = smart_text(uploadedfile)
                self.storage.move(new_file, old_file, allow_overwrite=True)
            else:
                file_name = smart_text(uploadedfile)
                filedata.name = os.path.relpath(file_name, path)

            signals.filebrowser_post_upload.send(sender=request, path=folder, file=FileObject(smart_text(file_name), site=self), site=self)

            # Comprimir imagens
            if file_name.split('.')[-1].lower() in ('jpg', 'jpeg'):
                os.system('jpegoptim --strip-all %s' % self.storage.path(file_path))
            elif file_name.split('.')[-1].lower() == 'png':
                os.system('optipng %s' % self.storage.path(file_path))

            os.system('chmod 664 %s' % self.storage.path(file_path))

            # let Ajax Upload know whether we saved it or not
            ret_json = {'success': True, 'filename': file_name}
            return HttpResponse(json.dumps(ret_json))
Пример #6
0
 def clean_name(self):
     "validate name"
     if self.cleaned_data['name']:
         # only letters, numbers, underscores, spaces and hyphens are allowed.
         if not ALNUM_NAME_RE.search(self.cleaned_data['name']):
             raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'))
         #  folder/file must not already exist.
         if not OVERWRITE_EXISTING and self.site.storage.isdir(os.path.join(self.path, convert_filename(self.cleaned_data['name']))) and os.path.join(self.path, convert_filename(self.cleaned_data['name'])) != self.fileobject.path:
             raise forms.ValidationError(_(u'The Folder already exists.'))
         elif not OVERWRITE_EXISTING and self.site.storage.isfile(os.path.join(self.path, convert_filename(self.cleaned_data['name']))) and os.path.join(self.path, convert_filename(self.cleaned_data['name'])) != self.fileobject.path:
             raise forms.ValidationError(_(u'The File already exists.'))
     return convert_filename(self.cleaned_data['name'])
Пример #7
0
 def clean_name(self):
     "validate name"
     if self.cleaned_data['name']:
         # only letters, numbers, underscores, spaces and hyphens are allowed.
         if not ALNUM_NAME_RE.search(self.cleaned_data['name']):
             raise forms.ValidationError(
                 _(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'
                   ))
         #  folder/file must not already exist.
         if not OVERWRITE_EXISTING and self.site.storage.isdir(
                 os.path.join(self.path,
                              convert_filename(self.cleaned_data['name']))
         ) and os.path.join(
                 self.path, convert_filename(
                     self.cleaned_data['name'])) != self.fileobject.path:
             raise forms.ValidationError(_(u'The Folder already exists.'))
         elif not OVERWRITE_EXISTING and self.site.storage.isfile(
                 os.path.join(self.path,
                              convert_filename(self.cleaned_data['name']))
         ) and os.path.join(
                 self.path, convert_filename(
                     self.cleaned_data['name'])) != self.fileobject.path:
             raise forms.ValidationError(_(u'The File already exists.'))
     return convert_filename(self.cleaned_data['name'])
Пример #8
0
def handle_uploaded_file(f, to):
    file_name = convert_filename(str(f))
    to_dir = os.path.join(settings.MEDIA_ROOT, *to.split('/'))
    
    makedir(to_dir)
    
    file_path = os.path.join(to_dir, file_name)
    file_path, file_name = check_existing(file_path)
    
    with open(file_path, 'wb+') as image_file:
        for chunk in f.chunks():
            image_file.write(chunk)

    file_url = to + file_name
    return file_url
    def _upload_file(self, request):
        """
        Upload file to the server.
        """
        if request.method == "POST":
            folder = request.GET.get('folder', '')

            if len(request.FILES) == 0:
                return HttpResponseBadRequest('Invalid request! No files included.')
            if len(request.FILES) > 1:
                return HttpResponseBadRequest('Invalid request! Multiple files included.')

            filedata = request.FILES.values()[0]

            fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
            folder = fb_uploadurl_re.sub('', folder)

            path = os.path.join(self.directory, folder)
            # we convert the filename before uploading in order
            # to check for existing files/folders
            file_name = convert_filename(filedata.name)
            filedata.name = file_name
            file_path = os.path.join(path, file_name)
            file_already_exists = self.storage.exists(file_path)

            # Check for name collision with a directory
            if file_already_exists and self.storage.isdir(file_path):
                ret_json = {'success': False, 'filename': file_name}
                return HttpResponse(json.dumps(ret_json))

            signals.filebrowser_pre_upload.send(sender=request, path=folder, file=filedata, site=self)
            uploadedfile = handle_file_upload(path, filedata, site=self)

            if file_already_exists and OVERWRITE_EXISTING:
                old_file = smart_text(file_path)
                new_file = smart_text(uploadedfile)
                self.storage.move(new_file, old_file, allow_overwrite=True)
                file_name = old_file  # otherwise file_name will be missing self.directory (e.g. 'uploads/')
            else:
                file_name = smart_text(uploadedfile)
                filedata.name = os.path.relpath(file_name, path)

            signals.filebrowser_post_upload.send(sender=request, path=folder, file=FileObject(smart_text(file_name), site=self), site=self)

            # let Ajax Upload know whether we saved it or not
            ret_json = {'success': True, 'filename': file_name}
            return HttpResponse(json.dumps(ret_json))
    def _upload_file(self, request):
        """
        Upload file to the server.

        If temporary is true, we upload to UPLOAD_TEMPDIR, otherwise
        we upload to site.directory
        """
        if request.method == "POST":
            folder = request.GET.get('folder', '')
            temporary = request.GET.get('temporary', '')
            temp_filename = None

            if len(request.FILES) == 0:
                return HttpResponseBadRequest('Invalid request! No files included.')
            if len(request.FILES) > 1:
                return HttpResponseBadRequest('Invalid request! Multiple files included.')

            filedata = list(request.FILES.values())[0]

            fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
            folder = fb_uploadurl_re.sub('', folder)

            # temporary upload folder should be outside self.directory
            if folder == UPLOAD_TEMPDIR and temporary == "true":
                path = folder
            else:
                path = os.path.join(self.directory, folder)
            # we convert the filename before uploading in order
            # to check for existing files/folders
            file_name = convert_filename(filedata.name)
            filedata.name = file_name
            file_path = os.path.join(path, file_name)
            file_already_exists = self.storage.exists(file_path)

            # construct temporary filename by adding the upload folder, because
            # otherwise we don't have any clue if the file has temporary been
            # uploaded or not
            if folder == UPLOAD_TEMPDIR and temporary == "true":
                temp_filename = os.path.join(folder, file_name)

            # Check for name collision with a directory
            if file_already_exists and self.storage.isdir(file_path):
                ret_json = {'success': False, 'filename': file_name}
                return HttpResponse(json.dumps(ret_json))

            signals.filebrowser_pre_upload.send(sender=request, path=folder, file=filedata, site=self)
            uploadedfile = handle_file_upload(path, filedata, site=self)

            if file_already_exists and OVERWRITE_EXISTING:
                old_file = smart_text(file_path)
                new_file = smart_text(uploadedfile)
                self.storage.move(new_file, old_file, allow_overwrite=True)
                full_path = FileObject(smart_text(old_file), site=self).path_full
            else:
                file_name = smart_text(uploadedfile)
                filedata.name = os.path.relpath(file_name, path)
                full_path = FileObject(smart_text(file_name), site=self).path_full

            # set permissions
            if DEFAULT_PERMISSIONS is not None:
                os.chmod(full_path, DEFAULT_PERMISSIONS)

            f = FileObject(smart_text(file_name), site=self)
            signals.filebrowser_post_upload.send(sender=request, path=folder, file=f, site=self)

            # let Ajax Upload know whether we saved it or not
            ret_json = {
                'success': True,
                'filename': f.filename,
                'temp_filename': temp_filename,
                'url': f.url,
            }
            return HttpResponse(json.dumps(ret_json), content_type="application/json")
Пример #11
0
    def _upload_file(self, request):
        """
        Upload file to the server.

        If temporary is true, we upload to UPLOAD_TEMPDIR, otherwise
        we upload to site.directory
        """
        if request.method == "POST":
            folder = request.GET.get('folder', '')
            temporary = request.GET.get('temporary', '')
            temp_filename = None

            if len(request.FILES) == 0:
                return HttpResponseBadRequest('Invalid request! No files included.')
            if len(request.FILES) > 1:
                return HttpResponseBadRequest('Invalid request! Multiple files included.')

            filedata = list(request.FILES.values())[0]

            fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
            folder = fb_uploadurl_re.sub('', folder)

            # temporary upload folder should be outside self.directory
            if folder == UPLOAD_TEMPDIR and temporary == "true":
                path = folder
            else:
                path = os.path.join(self.directory, folder)
            # we convert the filename before uploading in order
            # to check for existing files/folders
            file_name = convert_filename(filedata.name)
            filedata.name = file_name
            file_path = os.path.join(path, file_name)
            file_already_exists = self.storage.exists(file_path)

            # construct temporary filename by adding the upload folder, because
            # otherwise we don't have any clue if the file has temporary been
            # uploaded or not
            if folder == UPLOAD_TEMPDIR and temporary == "true":
                temp_filename = os.path.join(folder, file_name)

            # Check for name collision with a directory
            if file_already_exists and self.storage.isdir(file_path):
                ret_json = {'success': False, 'filename': file_name}
                return HttpResponse(json.dumps(ret_json))

            signals.filebrowser_pre_upload.send(sender=request, path=folder, file=filedata, site=self)
            uploadedfile = handle_file_upload(path, filedata, site=self)

            if file_already_exists and OVERWRITE_EXISTING:
                old_file = smart_text(file_path)
                new_file = smart_text(uploadedfile)
                self.storage.move(new_file, old_file, allow_overwrite=True)
                full_path = FileObject(smart_text(old_file), site=self).path_full
            else:
                file_name = smart_text(uploadedfile)
                filedata.name = os.path.relpath(file_name, path)
                full_path = FileObject(smart_text(file_name), site=self).path_full

            # set permissions
            if DEFAULT_PERMISSIONS is not None:
                os.chmod(full_path, DEFAULT_PERMISSIONS)

            f = FileObject(smart_text(file_name), site=self)
            signals.filebrowser_post_upload.send(sender=request, path=folder, file=f, site=self)

            # let Ajax Upload know whether we saved it or not
            ret_json = {'success': True, 'filename': f.filename, 'temp_filename': temp_filename}
            return HttpResponse(json.dumps(ret_json), content_type="application/json")
Пример #12
0
def normalize_file_name(name):
    """
    Normalize a given file name.
    """
    valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
    return convert_filename(''.join(c for c in name if c in valid_chars))
Пример #13
0
def normalize_file_name(name):
    """
    Normalize a given file name.
    """
    valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
    return convert_filename(''.join(c for c in name if c in valid_chars))