示例#1
0
    def ajax_upload(self, request, folder_id=None):
        """
        Receives an upload from the uploader. Receives only one file at a time.
        """
        mimetype = "application/json" if request.is_ajax() else "text/html"
        content_type_key = 'mimetype' if DJANGO_1_4 else 'content_type'
        response_params = {content_type_key: mimetype}
        try:
            upload, filename, is_raw = handle_upload(request)

            # Get clipboad
            clipboard = Clipboard.objects.get_or_create(user=request.user)[0]

            # find the file type
            for filer_class in filer_settings.FILER_FILE_MODELS:
                FileSubClass = load_object(filer_class)
                # TODO: What if there are more than one that qualify?
                if FileSubClass.matches_file_type(filename, upload, request):
                    FileForm = modelform_factory(
                        model=FileSubClass,
                        fields=('original_filename', 'owner', 'file')
                    )
                    break
            uploadform = FileForm({'original_filename': filename,
                                   'owner': request.user.pk},
                                  {'file': upload})
            if uploadform.is_valid():
                file_obj = uploadform.save(commit=False)
                # Enforce the FILER_IS_PUBLIC_DEFAULT
                file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT
                file_obj.save()
                clipboard_item = ClipboardItem(
                    clipboard=clipboard, file=file_obj)
                clipboard_item.save()
                json_response = {
                    'thumbnail': file_obj.icons['32'],
                    'alt_text': '',
                    'label': str(file_obj),
                }
                return HttpResponse(json.dumps(json_response),
                                    **response_params)
            else:
                form_errors = '; '.join(['%s: %s' % (
                    field,
                    ', '.join(errors)) for field, errors in list(
                        uploadform.errors.items())
                ])
                raise UploadException(
                    "AJAX request not valid: form invalid '%s'" % (
                        form_errors,))
        except UploadException as e:
            return HttpResponse(json.dumps({'error': str(e)}),
                                **response_params)
示例#2
0
    def ajax_upload(self, request, folder_id=None):
        """
        Receives an upload from the uploader. Receives only one file at a time.
        """
        mimetype = "application/json" if request.is_ajax() else "text/html"
        content_type_key = 'mimetype' if DJANGO_1_4 else 'content_type'
        response_params = {content_type_key: mimetype}
        try:
            upload, filename, is_raw = handle_upload(request)

            # Get clipboad
            clipboard = Clipboard.objects.get_or_create(user=request.user)[0]

            # find the file type
            for filer_class in filer_settings.FILER_FILE_MODELS:
                FileSubClass = load_object(filer_class)
                # TODO: What if there are more than one that qualify?
                if FileSubClass.matches_file_type(filename, upload, request):
                    FileForm = modelform_factory(model=FileSubClass,
                                                 fields=('original_filename',
                                                         'owner', 'file'))
                    break
            uploadform = FileForm(
                {
                    'original_filename': filename,
                    'owner': request.user.pk
                }, {'file': upload})
            if uploadform.is_valid():
                file_obj = uploadform.save(commit=False)
                # Enforce the FILER_IS_PUBLIC_DEFAULT
                file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT
                file_obj.save()
                clipboard_item = ClipboardItem(clipboard=clipboard,
                                               file=file_obj)
                clipboard_item.save()
                json_response = {
                    'thumbnail': file_obj.icons['32'],
                    'alt_text': '',
                    'label': str(file_obj),
                }
                return HttpResponse(json.dumps(json_response),
                                    **response_params)
            else:
                form_errors = '; '.join([
                    '%s: %s' % (field, ', '.join(errors))
                    for field, errors in list(uploadform.errors.items())
                ])
                raise UploadException(
                    "AJAX request not valid: form invalid '%s'" %
                    (form_errors, ))
        except UploadException as e:
            return HttpResponse(json.dumps({'error': str(e)}),
                                **response_params)
示例#3
0
    def upload_thumbnail(self, request, blog_entry_id=None):
        try:
            blog_entry = BlogEntryPage.objects.get(id=blog_entry_id)
            # hold the initial file name in order to delete it after the
            #   uploaded file is saved. Deletion takes place in the model's
            #   save method
            blog_entry._old_poster_image = blog_entry.poster_image.name
        except BlogEntryPage.DoesNotExist:
            raise UploadException(
                "Blog entry with id %s does not exist" % blog_entry_id)

        mimetype = "application/json" if request.is_ajax() else "text/html"
        upload = None
        try:
            upload, full_filename, _ = handle_upload(request)
            filename, extension = os.path.splitext(
                os.path.basename(full_filename))
            # check if it's an image type we can handle
            extension = (imghdr.what(upload) or extension).lstrip('.')
            if extension not in ALLOWED_THUMBNAIL_IMAGE_TYPES:
                displayed_extension = extension or "Unknown"
                raise UploadException(
                    displayed_extension + " file type not allowed."
                    " Please upload one of the following file types: " +
                    ", ".join(ALLOWED_THUMBNAIL_IMAGE_TYPES))

            if not all(get_image_dimensions(upload)):
                raise UploadException(
                    "Image width and height should be greater than 0px")
            try:
                upload.name = ''.join((filename, os.path.extsep, extension))
                blog_entry.poster_image = resize_image(upload)
            except Exception as e:
                raise UploadException("Cannot resize image: %s" % e.message)
            # save new image
            blog_entry.save()
            json_response = {
                'label': unicode(blog_entry.poster_image.name),
                'url': blog_entry.poster_image.url,
            }
            return HttpResponse(
                json.dumps(json_response), mimetype=mimetype)
        except UploadException as e:
            return HttpResponse(
                json.dumps({'error': unicode(e)}), mimetype=mimetype)
        finally:
            if upload:
                upload.close()
示例#4
0
    def ajax_upload(self, request, folder_id=None):
        """
        receives an upload from the uploader. Receives only one file at the time.
        """
        mimetype = "application/json" if request.is_ajax() else "text/html"
        try:
            upload, filename, is_raw = handle_upload(request)

            # Get clipboad
            clipboard = Clipboard.objects.get_or_create(user=request.user)[0]

            # find the file type
            for filer_class in filer_settings.FILER_FILE_MODELS:
                FileSubClass = load_object(filer_class)
                #TODO: What if there are more than one that qualify?
                if FileSubClass.matches_file_type(filename, upload, request):
                    FileForm = modelform_factory(model=FileSubClass,
                                                 fields=('original_filename',
                                                         'owner', 'file'))
                    break
            uploadform = FileForm(
                {
                    'original_filename': filename,
                    'owner': request.user.pk
                }, {'file': upload})
            if uploadform.is_valid():
                file_obj = uploadform.save(commit=False)
                # Enforce the FILER_IS_PUBLIC_DEFAULT
                file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT
                file_obj.save()
                clipboard_item = ClipboardItem(clipboard=clipboard,
                                               file=file_obj)
                clipboard_item.save()
                try:
                    json_response = {
                        'thumbnail': file_obj.icons['32'],
                        'alt_text': '',
                        'label': unicode(file_obj),
                    }
                except Exception, e:
                    json_response = {
                        'thumbnail': None,
                        'alt_text': '',
                        'label': unicode(file_obj),
                    }
                return HttpResponse(simplejson.dumps(json_response),
                                    mimetype=mimetype)
            else:
    def ajax_upload(self, request, folder_id=None):
        """
        receives an upload from the uploader. Receives only one file at the time.
        """
        mimetype = "application/json" if request.is_ajax() else "text/html"
        try:
            upload, filename, is_raw = handle_upload(request)

            # Get clipboad
            clipboard = Clipboard.objects.get_or_create(user=request.user)[0]

            # find the file type
            for filer_class in filer_settings.FILER_FILE_MODELS:
                FileSubClass = load_object(filer_class)
                #TODO: What if there are more than one that qualify?
                if FileSubClass.matches_file_type(filename, upload, request):
                    FileForm = modelform_factory(
                        model = FileSubClass,
                        fields = ('original_filename', 'owner', 'file')
                    )
                    break
            uploadform = FileForm({'original_filename': filename,
                                   'owner': request.user.pk},
                                  {'file': upload})
            if uploadform.is_valid():
                file_obj = uploadform.save(commit=False)
                # Enforce the FILER_IS_PUBLIC_DEFAULT
                file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT
                file_obj.save()
                clipboard_item = ClipboardItem(
                                    clipboard=clipboard, file=file_obj)
                clipboard_item.save()
                try:
                    json_response = {
                        'thumbnail': file_obj.icons['32'],
                        'alt_text': '',
                        'label': unicode(file_obj),
                    }
                except Exception, e:
                    json_response = {
                        'thumbnail': None,
                        'alt_text': '',
                        'label': unicode(file_obj),
                    }
                return HttpResponse(simplejson.dumps(json_response),
                                    mimetype=mimetype)
            else:
示例#6
0
def ajax_upload(request, folder_id=None):
    """
    Receives an upload from the uploader. Receives only one file at a time.
    """
    mimetype = 'application/json' if request.is_ajax() else 'text/html'
    content_type_key = 'content_type'
    response_params = {content_type_key: mimetype}
    folder = None

    if folder_id:
        try:
            # Get folder
            folder = Folder.objects.get(pk=folder_id)
        except Folder.DoesNotExist:
            return HttpResponse(json.dumps({'error': NO_FOLDER_ERROR}), **response_params)

    # check permissions
    if folder and not folder.has_add_children_permission(request):
        return HttpResponse(
            json.dumps({'error': NO_PERMISSIONS_FOR_FOLDER}), **response_params)
    try:
        if len(request.FILES) == 1:
            # dont check if request is ajax or not, just grab the file
            upload, filename, is_raw = handle_request_files_upload(request)
        else:
            # else process the request as usual
            upload, filename, is_raw = handle_upload(request)

        FileForm = None
        # find the file type
        for filer_class in filer_settings.FILER_FILE_MODELS:
            FileSubClass = load_object(filer_class)
            if FileSubClass.matches_file_type(filename, upload, request):
                FileForm = modelform_factory(
                    model=FileSubClass,
                    fields=('original_filename', 'owner', 'file')
                )
                break
        uploadform = FileForm({'original_filename': filename, 'owner': request.user.pk},
                              {'file': upload})
        if uploadform.is_valid():
            file_obj = uploadform.save(commit=False)
            # Enforce the FILER_IS_PUBLIC_DEFAULT
            file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT
            file_obj.folder = folder
            file_obj.save()

            # Try to generate thumbnails.
            if not file_obj.icons:
                # There is no point to continue, as we can't generate
                # thumbnails for this file. Usual reasons: bad format or
                # filename.
                file_obj.delete()
                # This would be logged in BaseImage._generate_thumbnails()
                # if FILER_ENABLE_LOGGING is on.
                return HttpResponse(
                    json.dumps({'error': 'failed to generate icons for file'}),
                    status=500, **response_params
                )
            thumbnail = None
            # Backwards compatibility: try to get specific icon size (32px)
            # first. Then try medium icon size (they are already sorted),
            # fallback to the first (smallest) configured icon.
            for size in (['32'] + filer_settings.FILER_ADMIN_ICON_SIZES[1::-1]):
                try:
                    thumbnail = file_obj.icons[size]
                    break
                except KeyError:  # pragma: no cover
                    continue

            json_response = {
                'thumbnail': thumbnail,
                'alt_text': '',
                'label': str(file_obj),
                'file_id': file_obj.pk,
            }
            # prepare preview thumbnail
            if type(file_obj) == Image:
                thumbnail_180_options = {
                    'size': (180, 180),
                    'crop': True,
                    'upscale': True,
                }
                thumbnail_180 = get_thumbnail_lazy(file_obj, thumbnail_180_options)
                json_response['thumbnail_180'] = thumbnail_180.url
                json_response['original_image'] = file_obj.url
            return HttpResponse(json.dumps(json_response), **response_params)
        else:
            form_errors = '; '.join(
                ['%s: %s' % (field, ', '.join(errors)) for field, errors in list(
                    uploadform.errors.items())]
            )
            raise UploadException('AJAX request not valid: form invalid "%s"' % (form_errors,))
    except UploadException as e:  # pragma: no cover
        return HttpResponse(json.dumps({'error': str(e)}), status=500, **response_params)
    except Exception as e:  # pragma: no cover
        return HttpResponse(json.dumps({'error': str(e)}), status=500, **response_params)
示例#7
0
    def ajax_upload(self, request, folder_id=None):
        """
        Receives an upload from the uploader. Receives only one file at a time.
        """
        mimetype = "application/json" if request.is_ajax() else "text/html"
        content_type_key = "mimetype" if DJANGO_1_4 else "content_type"
        response_params = {content_type_key: mimetype}
        folder = None
        if folder_id:
            try:
                # Get folder
                folder = Folder.objects.get(pk=folder_id)
            except Folder.DoesNotExist:
                return HttpResponse(json.dumps({"error": NO_FOLDER_ERROR}), **response_params)

        # check permissions
        if folder and not folder.has_add_children_permission(request):
            return HttpResponse(json.dumps({"error": NO_PERMISSIONS_FOR_FOLDER}), **response_params)
        try:
            if len(request.FILES) == 1:
                # dont check if request is ajax or not, just grab the file
                upload, filename, is_raw = handle_request_files_upload(request)
            else:
                # else process the request as usual
                upload, filename, is_raw = handle_upload(request)
            # TODO: Deprecated/refactor
            # Get clipboad
            # clipboard = Clipboard.objects.get_or_create(user=request.user)[0]

            # find the file type
            for filer_class in filer_settings.FILER_FILE_MODELS:
                FileSubClass = load_object(filer_class)
                # TODO: What if there are more than one that qualify?
                if FileSubClass.matches_file_type(filename, upload, request):
                    FileForm = modelform_factory(model=FileSubClass, fields=("original_filename", "owner", "file"))
                    break
            uploadform = FileForm({"original_filename": filename, "owner": request.user.pk}, {"file": upload})
            if uploadform.is_valid():
                file_obj = uploadform.save(commit=False)
                # Enforce the FILER_IS_PUBLIC_DEFAULT
                file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT
                file_obj.folder = folder
                file_obj.save()
                # TODO: Deprecated/refactor
                # clipboard_item = ClipboardItem(
                #     clipboard=clipboard, file=file_obj)
                # clipboard_item.save()

                # Try to generate thumbnails.
                if not file_obj.icons:
                    # There is no point to continue, as we can't generate
                    # thumbnails for this file. Usual reasons: bad format or
                    # filename.
                    file_obj.delete()
                    # This would be logged in BaseImage._generate_thumbnails()
                    # if FILER_ENABLE_LOGGING is on.
                    return HttpResponse(
                        json.dumps({"error": "failed to generate icons for file"}), status=500, **response_params
                    )

                thumbnail = None
                # Backwards compatibility: try to get specific icon size (32px)
                # first. Then try medium icon size (they are already sorted),
                # fallback to the first (smallest) configured icon.
                for size in ["32"] + filer_settings.FILER_ADMIN_ICON_SIZES[1::-1]:
                    try:
                        thumbnail = file_obj.icons[size]
                        break
                    except KeyError:
                        continue

                json_response = {"thumbnail": thumbnail, "alt_text": "", "label": str(file_obj), "file_id": file_obj.pk}
                # prepare preview thumbnail
                if type(file_obj) == Image:
                    thumbnail_180_options = {"size": (180, 180), "crop": True, "upscale": True}
                    thumbnail_180 = file_obj.file.get_thumbnail(thumbnail_180_options)
                    json_response["thumbnail_180"] = thumbnail_180.url
                return HttpResponse(json.dumps(json_response), **response_params)
            else:
                form_errors = "; ".join(
                    ["%s: %s" % (field, ", ".join(errors)) for field, errors in list(uploadform.errors.items())]
                )
                raise UploadException("AJAX request not valid: form invalid '%s'" % (form_errors,))
        except UploadException as e:
            return HttpResponse(json.dumps({"error": str(e)}), **response_params)
示例#8
0
def ajax_upload(request, folder_id=None):
    """
    Receives an upload from the uploader. Receives only one file at a time.
    """
    mimetype = "application/json" if request.is_ajax() else "text/html"
    content_type_key = 'mimetype' if DJANGO_1_4 else 'content_type'
    response_params = {content_type_key: mimetype}
    folder = None
    if folder_id:
        try:
            # Get folder
            folder = Folder.objects.get(pk=folder_id)
        except Folder.DoesNotExist:
            return HttpResponse(json.dumps({'error': NO_FOLDER_ERROR}),
                                **response_params)

    # check permissions
    if folder and not folder.has_add_children_permission(request):
        return HttpResponse(json.dumps({'error': NO_PERMISSIONS_FOR_FOLDER}),
                            **response_params)
    try:
        if len(request.FILES) == 1:
            # dont check if request is ajax or not, just grab the file
            upload, filename, is_raw = handle_request_files_upload(request)
        else:
            # else process the request as usual
            upload, filename, is_raw = handle_upload(request)
        # TODO: Deprecated/refactor
        # Get clipboad
        # clipboard = Clipboard.objects.get_or_create(user=request.user)[0]

        # find the file type
        for filer_class in filer_settings.FILER_FILE_MODELS:
            FileSubClass = load_object(filer_class)
            # TODO: What if there are more than one that qualify?
            if FileSubClass.matches_file_type(filename, upload, request):
                FileForm = modelform_factory(model=FileSubClass,
                                             fields=('original_filename',
                                                     'owner', 'file'))
                break
        uploadform = FileForm(
            {
                'original_filename': filename,
                'owner': request.user.pk
            }, {'file': upload})
        if uploadform.is_valid():
            file_obj = uploadform.save(commit=False)
            # Enforce the FILER_IS_PUBLIC_DEFAULT
            file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT
            file_obj.folder = folder
            file_obj.save()
            # TODO: Deprecated/refactor
            # clipboard_item = ClipboardItem(
            #     clipboard=clipboard, file=file_obj)
            # clipboard_item.save()

            # Try to generate thumbnails.
            if not file_obj.icons:
                # There is no point to continue, as we can't generate
                # thumbnails for this file. Usual reasons: bad format or
                # filename.
                file_obj.delete()
                # This would be logged in BaseImage._generate_thumbnails()
                # if FILER_ENABLE_LOGGING is on.
                return HttpResponse(json.dumps(
                    {'error': 'failed to generate icons for file'}),
                                    status=500,
                                    **response_params)

            thumbnail = None
            # Backwards compatibility: try to get specific icon size (32px)
            # first. Then try medium icon size (they are already sorted),
            # fallback to the first (smallest) configured icon.
            for size in (['32'] +
                         filer_settings.FILER_ADMIN_ICON_SIZES[1::-1]):
                try:
                    thumbnail = file_obj.icons[size]
                    break
                except KeyError:
                    continue

            json_response = {
                'thumbnail': thumbnail,
                'alt_text': '',
                'label': str(file_obj),
                'file_id': file_obj.pk,
            }
            # prepare preview thumbnail
            if type(file_obj) == Image:
                thumbnail_180_options = {
                    'size': (180, 180),
                    'crop': True,
                    'upscale': True,
                }
                thumbnail_180 = file_obj.file.get_thumbnail(
                    thumbnail_180_options)
                json_response['thumbnail_180'] = thumbnail_180.url
                json_response['original_image'] = file_obj.url
            return HttpResponse(json.dumps(json_response), **response_params)
        else:
            form_errors = '; '.join([
                '%s: %s' % (field, ', '.join(errors))
                for field, errors in list(uploadform.errors.items())
            ])
            raise UploadException("AJAX request not valid: form invalid '%s'" %
                                  (form_errors, ))
    except UploadException as e:
        return HttpResponse(json.dumps({'error': str(e)}),
                            status=500,
                            **response_params)
def ajax_upload(request, folder_id=None):
    folder = None
    path = request.POST.get('path')
    path_split = path.split('/') if path else []

    # check permissions and data
    error_msg = None
    if not request.user.is_authenticated:
        # User is not logged in. Return a generic message that gives
        # no data info (such as whether a folder exists or not)
        error_msg = filer.admin.clipboardadmin.NO_PERMISSIONS_FOR_FOLDER
    elif folder_id:
        try:
            folder = Folder.objects.get(pk=folder_id)
        except Folder.DoesNotExist:
            # A folder with id=folder_id does not exist so return
            # an error message specifying this
            error_msg = filer.admin.clipboardadmin.NO_FOLDER_ERROR
        else:
            # Now check if the user has sufficient permissions to
            # upload a file to the folder with id=folder_id and return
            # an error message if not
            no_folder_perms = (not folder.has_add_children_permission(request)
                               or (path and not folder.can_have_subfolders))
            if no_folder_perms:
                error_msg = filer.admin.clipboardadmin.NO_PERMISSIONS_FOR_FOLDER
    elif (not request.user.is_superuser and path_split and
          not filer.settings.FILER_ALLOW_REGULAR_USERS_TO_ADD_ROOT_FOLDERS):
        # If uploading the file to Unsorted Uploads (i.e. no folder_id)
        # but filer is set to disallow regular users to add
        # folders there and the user is not a superuser and is uploading
        # folders that aren't yet created on the server (i.e.
        # specifying the path param with folders that don't yet exist)
        # return an error message
        if not Folder.objects.filter(name=path_split[0], parent=None).exists():
            error_msg = filer.admin.clipboardadmin.NO_PERMISSIONS_FOR_FOLDER

    if error_msg:
        return JsonResponse({'error': error_msg})

    try:
        if len(request.FILES) == 1:
            # dont check if request is ajax or not, just grab the file
            upload, filename, is_raw = handle_request_files_upload(request)
        else:
            # else process the request as usual
            upload, filename, is_raw = handle_upload(request)

        # find the file type
        for filer_class in filer_settings.FILER_FILE_MODELS:
            FileSubClass = load_model(filer_class)
            # TODO: What if there are more than one that qualify?
            if FileSubClass.matches_file_type(filename, upload, request):
                FileForm = modelform_factory(model=FileSubClass,
                                             fields=('original_filename',
                                                     'owner', 'file'))
                break
        uploadform = FileForm(
            {
                'original_filename': filename,
                'owner': request.user.pk
            }, {'file': upload})
        if uploadform.is_valid():
            file_obj = uploadform.save(commit=False)
            # Enforce the FILER_IS_PUBLIC_DEFAULT
            file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT

            # Set the file's folder
            current_folder = folder
            for segment in path_split:
                try:
                    current_folder = Folder.objects.get(name=segment,
                                                        parent=current_folder)
                except Folder.DoesNotExist:
                    # If the current_folder can't have subfolders then
                    # return a permission error
                    if current_folder and not current_folder.can_have_subfolders:
                        error_msg = filer.admin.clipboardadmin.NO_PERMISSIONS_FOR_FOLDER
                        return JsonResponse({'error': error_msg})
                    current_folder = Folder.objects.create(
                        name=segment, parent=current_folder)
                else:
                    # If the folder already exists, check the user is
                    # allowed to upload here
                    if not current_folder.has_add_children_permission(request):
                        error_msg = filer.admin.clipboardadmin.NO_PERMISSIONS_FOR_FOLDER
                        return JsonResponse({'error': error_msg})
            file_obj.folder = current_folder

            same_name_file_qs = get_files_distinct_grouper_queryset().annotate(
                _name=NullIfEmptyStr('name'),
                _original_filename=NullIfEmptyStr('original_filename'),
            ).annotate(
                # seperate annotate is needed to get it work on python<36
                # see PEP 468 for more details
                _label=Coalesce('_name', '_original_filename',
                                Value('unnamed file')), ).filter(
                                    folder=folder, _label=file_obj.label)
            existing_file_obj = same_name_file_qs.first()

            if existing_file_obj:
                file_grouper = existing_file_obj.grouper
                new_file_grouper = False

                existing_file_version = Version.objects.get_for_content(
                    existing_file_obj)
                if existing_file_version.state == DRAFT and not all([
                        existing_file_version.can_be_archived(),
                        existing_file_version.check_archive.as_bool(
                            request.user),
                ]):
                    return JsonResponse({
                        'error':
                        ('Cannot archive existing {} file version'.format(
                            existing_file_obj))
                    })
            else:
                new_file_grouper = True
                file_grouper = FileGrouper.objects.create()

            file_obj.grouper = file_grouper
            file_obj.save()
            create_file_version(file_obj, request.user)

            # Try to generate thumbnails.
            if not file_obj.icons:
                # There is no point to continue, as we can't generate
                # thumbnails for this file. Usual reasons: bad format or
                # filename.
                file_obj.delete()
                if new_file_grouper:
                    file_grouper.delete()
                # This would be logged in BaseImage._generate_thumbnails()
                # if FILER_ENABLE_LOGGING is on.
                return JsonResponse(
                    {'error': 'failed to generate icons for file'},
                    status=500,
                )
            thumbnail = None
            # Backwards compatibility: try to get specific icon size (32px)
            # first. Then try medium icon size (they are already sorted),
            # fallback to the first (smallest) configured icon.
            for size in (['32'] +
                         filer_settings.FILER_ADMIN_ICON_SIZES[1::-1]):
                try:
                    thumbnail = file_obj.icons[size]
                    break
                except KeyError:
                    continue

            data = {
                'thumbnail': thumbnail,
                'alt_text': '',
                'label': str(file_obj),
                'file_id': file_obj.pk,
                'grouper_id': file_grouper.pk,
            }
            # prepare preview thumbnail
            if type(file_obj) == Image:
                thumbnail_180_options = {
                    'size': (180, 180),
                    'crop': True,
                    'upscale': True,
                }
                thumbnail_180 = file_obj.file.get_thumbnail(
                    thumbnail_180_options)
                data['thumbnail_180'] = thumbnail_180.url
                data['original_image'] = file_obj.url
            return JsonResponse(data)
        else:
            form_errors = '; '.join([
                '%s: %s' % (field, ', '.join(errors))
                for field, errors in list(uploadform.errors.items())
            ])
            raise UploadException("AJAX request not valid: form invalid '%s'" %
                                  (form_errors, ))
    except UploadException as e:
        # TODO: Test
        return JsonResponse({'error': str(e)}, status=500)
示例#10
0
def ajax_upload(request, folder_id=None):
    folder = None
    if folder_id:
        try:
            # Get folder
            folder = Folder.objects.get(pk=folder_id)
        except Folder.DoesNotExist:
            return JsonResponse({'error': filer.admin.clipboardadmin.NO_FOLDER_ERROR})

    # check permissions
    if folder and not folder.has_add_children_permission(request):
        return JsonResponse({'error': filer.admin.clipboardadmin.NO_PERMISSIONS_FOR_FOLDER})
    try:
        if len(request.FILES) == 1:
            # dont check if request is ajax or not, just grab the file
            upload, filename, is_raw = handle_request_files_upload(request)
        else:
            # else process the request as usual
            upload, filename, is_raw = handle_upload(request)

        # find the file type
        for filer_class in filer_settings.FILER_FILE_MODELS:
            FileSubClass = load_model(filer_class)
            # TODO: What if there are more than one that qualify?
            if FileSubClass.matches_file_type(filename, upload, request):
                FileForm = modelform_factory(
                    model=FileSubClass,
                    fields=('original_filename', 'owner', 'file')
                )
                break
        uploadform = FileForm({'original_filename': filename,
                               'owner': request.user.pk},
                              {'file': upload})
        if uploadform.is_valid():
            file_obj = uploadform.save(commit=False)
            # Enforce the FILER_IS_PUBLIC_DEFAULT
            file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT
            file_obj.folder = folder

            same_name_file_qs = get_files_distinct_grouper_queryset().annotate(
                _name=NullIfEmptyStr('name'),
                _original_filename=NullIfEmptyStr('original_filename'),
            ).annotate(
                # seperate annotate is needed to get it work on python<36
                # see PEP 468 for more details
                _label=Coalesce('_name', '_original_filename', Value('unnamed file')),
            ).filter(folder=folder, _label=file_obj.label)
            existing_file_obj = same_name_file_qs.first()

            if existing_file_obj:
                file_grouper = existing_file_obj.grouper
                new_file_grouper = False

                existing_file_version = Version.objects.get_for_content(existing_file_obj)
                if existing_file_version.state == DRAFT and not all([
                    existing_file_version.can_be_archived(),
                    existing_file_version.check_archive.as_bool(request.user),
                ]):
                    return JsonResponse({'error': (
                        'Cannot archive existing {} file version'.format(existing_file_obj)
                    )})
            else:
                new_file_grouper = True
                file_grouper = FileGrouper.objects.create()

            file_obj.grouper = file_grouper
            file_obj.save()
            create_file_version(file_obj, request.user)

            # Try to generate thumbnails.
            if not file_obj.icons:
                # There is no point to continue, as we can't generate
                # thumbnails for this file. Usual reasons: bad format or
                # filename.
                file_obj.delete()
                if new_file_grouper:
                    file_grouper.delete()
                # This would be logged in BaseImage._generate_thumbnails()
                # if FILER_ENABLE_LOGGING is on.
                return JsonResponse(
                    {'error': 'failed to generate icons for file'},
                    status=500,
                )
            thumbnail = None
            # Backwards compatibility: try to get specific icon size (32px)
            # first. Then try medium icon size (they are already sorted),
            # fallback to the first (smallest) configured icon.
            for size in (['32'] +
                         filer_settings.FILER_ADMIN_ICON_SIZES[1::-1]):
                try:
                    thumbnail = file_obj.icons[size]
                    break
                except KeyError:
                    continue

            data = {
                'thumbnail': thumbnail,
                'alt_text': '',
                'label': str(file_obj),
                'file_id': file_obj.pk,
                'grouper_id': file_grouper.pk,
            }
            # prepare preview thumbnail
            if type(file_obj) == Image:
                thumbnail_180_options = {
                    'size': (180, 180),
                    'crop': True,
                    'upscale': True,
                }
                thumbnail_180 = file_obj.file.get_thumbnail(
                    thumbnail_180_options)
                data['thumbnail_180'] = thumbnail_180.url
                data['original_image'] = file_obj.url
            return JsonResponse(data)
        else:
            form_errors = '; '.join(['%s: %s' % (
                field,
                ', '.join(errors)) for field, errors in list(
                    uploadform.errors.items())
            ])
            raise UploadException(
                "AJAX request not valid: form invalid '%s'" % (
                    form_errors,))
    except UploadException as e:
        return JsonResponse({'error': str(e)}, status=500)