}
catch( WikiException e )
{
// this is a kludge, the exception that is caught here contains the i18n key
// here we have the context available, so we can internationalize it properly :
throw new RedirectException (Preferences.getBundle( context, InternationalizationManager.CORE_BUNDLE )
.getString( e.getMessage() ), errorPage );
}
//
// FIXME: This has the unfortunate side effect that it will receive the
// contents. But we can't figure out the page to redirect to
// before we receive the file, due to the stupid constructor of MultipartRequest.
//
if( !context.hasAdminPermissions() )
{
if( contentLength > m_maxSize )
{
// FIXME: Does not delete the received files.
throw new RedirectException( "File exceeds maximum size ("+m_maxSize+" bytes)",
errorPage );
}
if( !isTypeAllowed(filename) )
{
throw new RedirectException( "Files of this type may not be uploaded to this wiki",
errorPage );
}
}
Principal user = context.getCurrentUser();
AttachmentManager mgr = m_engine.getAttachmentManager();
log.debug("file="+filename);
if( data == null )
{
log.error("File could not be opened.");
throw new RedirectException("File could not be opened.",
errorPage);
}
//
// Check whether we already have this kind of a page.
// If the "page" parameter already defines an attachment
// name for an update, then we just use that file.
// Otherwise we create a new attachment, and use the
// filename given. Incidentally, this will also mean
// that if the user uploads a file with the exact
// same name than some other previous attachment,
// then that attachment gains a new version.
//
Attachment att = mgr.getAttachmentInfo( context.getPage().getName() );
if( att == null )
{
att = new Attachment( m_engine, parentPage, filename );
created = true;
}
att.setSize( contentLength );
//
// Check if we're allowed to do this?
//
Permission permission = PermissionFactory.getPagePermission( att, "upload" );
if( m_engine.getAuthorizationManager().checkPermission( context.getWikiSession(),
permission ) )
{
if( user != null )
{
att.setAuthor( user.getName() );
}
if( changenote != null && changenote.length() > 0 )
{
att.setAttribute( WikiPage.CHANGENOTE, changenote );
}
try
{
m_engine.getAttachmentManager().storeAttachment( att, data );
}
catch( ProviderException pe )
{
// this is a kludge, the exception that is caught here contains the i18n key
// here we have the context available, so we can internationalize it properly :
throw new ProviderException( Preferences.getBundle( context, InternationalizationManager.CORE_BUNDLE )
.getString( pe.getMessage() ) );
}
log.info( "User " + user + " uploaded attachment to " + parentPage +
" called "+filename+", size " + att.getSize() );
}
else
{
throw new RedirectException( "No permission to upload a file", errorPage );
}
return created;
}