Пример #1
0
def drawer_upload_view(context, request,
                       check_upload_size=check_upload_size,
                       get_image_info=get_image_info,
                       batch_images=batch_images,
                       ):
    """Drawer posts a file with the parameter name "file".
    """

    ## XXX The rest is copied from add_file_view. A common denominator
    ## would be desirable.

    creator = authenticated_userid(request)

    fieldstorage = request.params.get('file')
    if not hasattr(fieldstorage, 'filename'):
        msg = 'You must select a file before clicking Upload.'
        return dict(error = msg)

    # For file objects, OSI's policy is to store the upload file's
    # filename as the objectid, instead of basing __name__ on the
    # title field).
    filename = basename_of_filepath(fieldstorage.filename)

    stream = fieldstorage.file
    # use parameter, as the title (or basename, if missing).
    title = request.params.get('title', filename)
    image = create_content(ICommunityFile,
                          title=title,
                          stream=stream,
                          mimetype=get_upload_mimetype(fieldstorage),
                          filename=fieldstorage.filename,
                          creator=creator,
                          )
    # Check if it's an image.
    if not IImage.providedBy(image):
        msg = 'File %s is not an image' % filename
        return dict(error=msg)

    check_upload_size(context, image, 'file')

    if hasattr(context, 'get_attachments'):
        target_folder = context.get_attachments()
        if not has_permission('create', target_folder, request):
            msg = 'You do not have permission to upload files here.'
            return dict(error=msg)

        image.filename = filename
        name = make_name(target_folder, filename, raise_error=False)
        if not name:
            msg = 'The filename must not be empty'
            return dict(error=msg)

        # Is there a key in context with that filename?
        if name in target_folder:
            msg = 'Filename %s already exists in this folder' % filename
            return dict(error=msg)

        target_folder[name] = image

        workflow = get_workflow(ICommunityFile, 'security', context)
        if workflow is not None:
            workflow.initialize(image)

    # In cases where the image is to live in a piece of content which has not
    # yet been created (like when using an 'Add' form), the context is going
    # to be the eventual parent of the not yet created content, which will be
    # the eventual parent of the just now created image.  Since we have nowhere
    # to put the image, we put it in a tempfolder and later move it over after
    # the content is created.  Normal ContentAdded event handlers are not
    # called at this stage and workflow is not yet initialized.  These will
    # occur when the content is moved over.
    else:
        image.modified = datetime.datetime.now()
        tempfolder = find_tempfolder(context)
        tempfolder.add_document(image)


    # Return info about the image uploaded
    return dict(
        upload_image_info=get_image_info(image, request, thumb_size=LARGE_THUMB_SIZE),
    )
Пример #2
0
 def _callFUT(self, fieldstorage):
     from karl.content.views.utils import get_upload_mimetype
     return get_upload_mimetype(fieldstorage)
Пример #3
0
 def _callFUT(self, fieldstorage):
     from karl.content.views.utils import get_upload_mimetype
     return get_upload_mimetype(fieldstorage)