Exemplo n.º 1
0
    def attachfile(self, req, form):
        """
        Process requests received from CKEditor to upload files.
        If the uploaded file is an image, create an icon version
        """
        if not is_html_text_editor_installed():
            return apache.HTTP_NOT_FOUND

        if not form.has_key('type'):
            form['type'] = 'File'

        if not form.has_key('upload') or \
               not form['type'] in \
               ['File', 'Image', 'Flash', 'Media']:
            #return apache.HTTP_NOT_FOUND
            pass
        filetype = form['type'].lower()

        uid = getUid(req)


        # URL where the file can be fetched after upload
        user_files_path = '%(CFG_SITE_URL)s/submit/getattachedfile/%(uid)s' % \
                          {'uid': uid,
                           'CFG_SITE_URL': CFG_SITE_URL,
                           'filetype': filetype}

        # Path to directory where uploaded files are saved
        user_files_absolute_path = '%(CFG_PREFIX)s/var/tmp/attachfile/%(uid)s/%(filetype)s' % \
                                   {'uid': uid,
                                    'CFG_PREFIX': CFG_PREFIX,
                                    'filetype': filetype}
        try:
            os.makedirs(user_files_absolute_path)
        except:
            pass

        user_info = collect_user_info(req)
        (auth_code, auth_message) = acc_authorize_action(user_info, 'attachsubmissionfile')
        msg = ""
        if user_info['email'] == 'guest':
            # User is guest: must login prior to upload
            msg = 'Please login before uploading file.'
        elif auth_code:
            # User cannot submit
            msg = 'Sorry, you are not allowed to submit files.'
        ## elif len(form['upload']) != 1:
        ##     msg = 'Sorry, you must upload one single file'
        else:
            # Process the upload and get the response
            (msg, uploaded_file_path, uploaded_file_name, uploaded_file_url, callback_function) = \
                  process_CKEditor_upload(form, uid, user_files_path, user_files_absolute_path)

            if uploaded_file_path:
                # Create an icon
                if form.get('type','') == 'Image':
                    try:
                        (icon_path, icon_name) = create_icon(
                            { 'input-file'           : uploaded_file_path,
                              'icon-name'            : os.path.splitext(uploaded_file_name)[0],
                              'icon-file-format'     : os.path.splitext(uploaded_file_name)[1][1:] or 'gif',
                              'multipage-icon'       : False,
                              'multipage-icon-delay' : 100,
                              'icon-scale'           : "700>", # Resize only if width > 700
                              'verbosity'            : 0,
                              })

                        # Move original file to /original dir, and replace it with icon file
                        original_user_files_absolute_path = os.path.join(user_files_absolute_path,
                                                                         'original')
                        if not os.path.exists(original_user_files_absolute_path):
                            # Create /original dir if needed
                            os.mkdir(original_user_files_absolute_path)
                        os.rename(uploaded_file_path,
                                  original_user_files_absolute_path + os.sep + uploaded_file_name)
                        os.rename(icon_path + os.sep + icon_name,
                                  uploaded_file_path)
                    except InvenioWebSubmitIconCreatorError, e:
                        pass

                user_files_path += '/' + filetype + '/' + uploaded_file_name

            else:
    def attachfile(self, req, form):
        """
        Process requests received from CKEditor to upload files.
        If the uploaded file is an image, create an icon version
        """
        if not is_html_text_editor_installed():
            return apache.HTTP_NOT_FOUND

        if not form.has_key('type'):
            form['type'] = 'File'

        if not form.has_key('upload') or \
               not form['type'] in \
               ['File', 'Image', 'Flash', 'Media']:
            #return apache.HTTP_NOT_FOUND
            pass
        filetype = form['type'].lower()

        uid = getUid(req)

        # URL where the file can be fetched after upload
        user_files_path = '%(CFG_SITE_URL)s/submit/getattachedfile/%(uid)s' % \
                          {'uid': uid,
                           'CFG_SITE_URL': CFG_SITE_URL,
                           'filetype': filetype}

        # Path to directory where uploaded files are saved
        user_files_absolute_path = '%(CFG_PREFIX)s/var/tmp/attachfile/%(uid)s/%(filetype)s' % \
                                   {'uid': uid,
                                    'CFG_PREFIX': CFG_PREFIX,
                                    'filetype': filetype}
        try:
            os.makedirs(user_files_absolute_path)
        except:
            pass

        user_info = collect_user_info(req)
        (auth_code,
         auth_message) = acc_authorize_action(user_info,
                                              'attachsubmissionfile')
        msg = ""
        if user_info['email'] == 'guest':
            # User is guest: must login prior to upload
            msg = 'Please login before uploading file.'
        elif auth_code:
            # User cannot submit
            msg = 'Sorry, you are not allowed to submit files.'
        ## elif len(form['upload']) != 1:
        ##     msg = 'Sorry, you must upload one single file'
        else:
            # Process the upload and get the response
            (msg, uploaded_file_path, uploaded_file_name, uploaded_file_url, callback_function) = \
                  process_CKEditor_upload(form, uid, user_files_path, user_files_absolute_path)

            if uploaded_file_path:
                # Create an icon
                if form.get('type', '') == 'Image':
                    try:
                        (icon_path, icon_name) = create_icon({
                            'input-file':
                            uploaded_file_path,
                            'icon-name':
                            os.path.splitext(uploaded_file_name)[0],
                            'icon-file-format':
                            os.path.splitext(uploaded_file_name)[1][1:]
                            or 'gif',
                            'multipage-icon':
                            False,
                            'multipage-icon-delay':
                            100,
                            'icon-scale':
                            "700>",  # Resize only if width > 700
                            'verbosity':
                            0,
                        })

                        # Move original file to /original dir, and replace it with icon file
                        original_user_files_absolute_path = os.path.join(
                            user_files_absolute_path, 'original')
                        if not os.path.exists(
                                original_user_files_absolute_path):
                            # Create /original dir if needed
                            os.mkdir(original_user_files_absolute_path)
                        os.rename(
                            uploaded_file_path,
                            original_user_files_absolute_path + os.sep +
                            uploaded_file_name)
                        os.rename(icon_path + os.sep + icon_name,
                                  uploaded_file_path)
                    except InvenioWebSubmitIconCreatorError, e:
                        pass

                user_files_path += '/' + filetype + '/' + uploaded_file_name

            else: