📄 attachmentmanager.java
字号:
// if( atts instanceof List ) { Collections.sort( (List) atts ); } return atts; } /** * Returns true, if the page has any attachments at all. This is * a convinience method. * * * @param wikipage The wiki page from which you are seeking attachments for. * @return True, if the page has attachments, else false. */ public boolean hasAttachments( WikiPage wikipage ) { try { return listAttachments( wikipage ).size() > 0; } catch( Exception e ) {} return false; } /** * Finds a (real) attachment from the repository as a stream. * * @param att Attachment * @return An InputStream to read from. May return null, if * attachments are disabled. * @throws IOException If the stream cannot be opened * @throws ProviderException If the backend fails due to some other reason. */ public InputStream getAttachmentStream( Attachment att ) throws IOException, ProviderException { return getAttachmentStream( null, att ); } /** * Returns an attachment stream using the particular WikiContext. This method * should be used instead of getAttachmentStream(Attachment), since it also allows * the DynamicAttachments to function. * * @param ctx The Wiki Context * @param att The Attachment to find * @return An InputStream. May return null, if attachments are disabled. You must * take care of closing it. * @throws ProviderException If the backend fails due to some reason * @throws IOException If the stream cannot be opened */ public InputStream getAttachmentStream( WikiContext ctx, Attachment att ) throws ProviderException, IOException { if( m_provider == null ) { return null; } if( att instanceof DynamicAttachment ) { return ((DynamicAttachment)att).getProvider().getAttachmentData( ctx, att ); } return m_provider.getAttachmentData( att ); } private Cache m_dynamicAttachments = new Cache( true, false, false ); /** * Stores a dynamic attachment. Unlike storeAttachment(), this just stores * the attachment in the memory. * * @param ctx A WikiContext * @param att An attachment to store */ public void storeDynamicAttachment( WikiContext ctx, DynamicAttachment att ) { m_dynamicAttachments.putInCache( att.getName(), att ); } /** * Finds a DynamicAttachment. Normally, you should just use getAttachmentInfo(), * since that will find also DynamicAttachments. * * @param name The name of the attachment to look for * @return An Attachment, or null. * @see #getAttachmentInfo(String) */ public DynamicAttachment getDynamicAttachment( String name ) { try { return (DynamicAttachment) m_dynamicAttachments.getFromCache( name ); } catch( NeedsRefreshException e ) { // // Remove from cache, it has expired. // m_dynamicAttachments.putInCache( name, null ); return null; } } /** * Stores an attachment that lives in the given file. * If the attachment did not exist previously, this method * will create it. If it did exist, it stores a new version. * * @param att Attachment to store this under. * @param source A file to read from. * * @throws IOException If writing the attachment failed. * @throws ProviderException If something else went wrong. */ public void storeAttachment( Attachment att, File source ) throws IOException, ProviderException { FileInputStream in = null; try { in = new FileInputStream( source ); storeAttachment( att, in ); } finally { if( in != null ) in.close(); } } /** * Stores an attachment directly from a stream. * If the attachment did not exist previously, this method * will create it. If it did exist, it stores a new version. * * @param att Attachment to store this under. * @param in InputStream from which the attachment contents will be read. * * @throws IOException If writing the attachment failed. * @throws ProviderException If something else went wrong. */ public void storeAttachment( Attachment att, InputStream in ) throws IOException, ProviderException { if( m_provider == null ) { return; } // // Checks if the actual, real page exists without any modifications // or aliases. We cannot store an attachment to a non-existant page. // if( !m_engine.getPageManager().pageExists( att.getParentName() ) ) { // the caller should catch the exception and use the exception text as an i18n key throw new ProviderException( "attach.parent.not.exist" ); } m_provider.putAttachmentData( att, in ); m_engine.getReferenceManager().updateReferences( att.getName(), new java.util.Vector() ); WikiPage parent = new WikiPage( m_engine, att.getParentName() ); m_engine.updateReferences( parent ); m_engine.getSearchManager().reindexPage( att ); } /** * Returns a list of versions of the attachment. * * @param attachmentName A fully qualified name of the attachment. * * @return A list of Attachments. May return null, if attachments are * disabled. * @throws ProviderException If the provider fails for some reason. */ public List getVersionHistory( String attachmentName ) throws ProviderException { if( m_provider == null ) { return null; } Attachment att = getAttachmentInfo( (WikiContext)null, attachmentName ); if( att != null ) { return m_provider.getVersionHistory( att ); } return null; } /** * Returns a collection of Attachments, containing each and every attachment * that is in this Wiki. * * @return A collection of attachments. If attachments are disabled, will * return an empty collection. * @throws ProviderException If something went wrong with the backend */ public Collection getAllAttachments() throws ProviderException { if( attachmentsEnabled() ) { return m_provider.listAllChanged( new Date(0L) ); } return new ArrayList<Attachment>(); } /** * Returns the current attachment provider. * * @return The current provider. May be null, if attachments are disabled. */ public WikiAttachmentProvider getCurrentProvider() { return m_provider; } /** * Deletes the given attachment version. * * @param att The attachment to delete * @throws ProviderException If something goes wrong with the backend. */ public void deleteVersion( Attachment att ) throws ProviderException { if( m_provider == null ) return; m_provider.deleteVersion( att ); } /** * Deletes all versions of the given attachment. * @param att The Attachment to delete. * @throws ProviderException if something goes wrong with the backend. */ // FIXME: Should also use events! public void deleteAttachment( Attachment att ) throws ProviderException { if( m_provider == null ) return; m_provider.deleteAttachment( att ); m_engine.getSearchManager().pageRemoved( att ); m_engine.getReferenceManager().clearPageEntries( att.getName() ); } /** * Validates the filename and makes sure it is legal. It trims and splits * and replaces bad characters. * * @param filename * @return A validated name with annoying characters replaced. * @throws WikiException If the filename is not legal (e.g. empty) */ static String validateFileName( String filename ) throws WikiException { if( filename == null || filename.trim().length() == 0 ) { log.error("Empty file name given."); // the caller should catch the exception and use the exception text as an i18n key throw new WikiException( "attach.empty.file" ); } // // Should help with IE 5.22 on OSX // filename = filename.trim(); // If file name ends with .jsp or .jspf, the user is being naughty! if( filename.toLowerCase().endsWith( ".jsp" ) || filename.toLowerCase().endsWith(".jspf") ) { log.info( "Attempt to upload a file with a .jsp/.jspf extension. In certain cases this" + " can trigger unwanted security side effects, so we're preventing it." ); // // the caller should catch the exception and use the exception text as an i18n key throw new WikiException( "attach.unwanted.file" ); } // // Some browser send the full path info with the filename, so we need // to remove it here by simply splitting along slashes and then taking the path. // String[] splitpath = filename.split( "[/\\\\]" ); filename = splitpath[splitpath.length-1]; // // Remove any characters that might be a problem. Most // importantly - characters that might stop processing // of the URL. // filename = StringUtils.replaceChars( filename, "#?\"'", "____" ); return filename; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -