📄 basicattachmentprovider.java
字号:
out.close(); Properties props = getPageProperties( att ); String author = att.getAuthor(); if( author == null ) { author = "unknown"; } props.setProperty( versionNumber+".author", author ); putPageProperties( att, props ); } catch( IOException e ) { log.error( "Could not save attachment data: ", e ); throw (IOException) e.fillInStackTrace(); } finally { if( out != null ) out.close(); } } public String getProviderInfo() { return ""; } private File findFile( File dir, Attachment att ) throws FileNotFoundException, ProviderException { int version = att.getVersion(); if( version == WikiProvider.LATEST_VERSION ) { version = findLatestVersion( att ); } String ext = getFileExtension( att.getFileName() ); File f = new File( dir, version+"."+ext ); if( !f.exists() ) { if ("bin".equals(ext)) { File fOld = new File( dir, version+"." ); if (fOld.exists()) f = fOld; } if( !f.exists() ) { throw new FileNotFoundException("No such file: "+f.getAbsolutePath()+" exists."); } } return f; } public InputStream getAttachmentData( Attachment att ) throws IOException, ProviderException { File attDir = findAttachmentDir( att ); File f = findFile( attDir, att ); return new FileInputStream( f ); } public Collection listAttachments( WikiPage page ) throws ProviderException { Collection result = new ArrayList(); File dir = findPageDir( page.getName() ); if( dir != null ) { String[] attachments = dir.list(); if( attachments != null ) { // // We now have a list of all potential attachments in // the directory. // for( int i = 0; i < attachments.length; i++ ) { File f = new File( dir, attachments[i] ); if( f.isDirectory() ) { String attachmentName = unmangleName( attachments[i] ); // // Is it a new-stylea attachment directory? If yes, // we'll just deduce the name. If not, however, // we'll check if there's a suitable property file // in the directory. // if( attachmentName.endsWith( ATTDIR_EXTENSION ) ) { attachmentName = attachmentName.substring( 0, attachmentName.length()-ATTDIR_EXTENSION.length() ); } else { File propFile = new File( f, PROPERTY_FILE ); if( !propFile.exists() ) { // // This is not obviously a JSPWiki attachment, // so let's just skip it. // continue; } } Attachment att = getAttachmentInfo( page, attachmentName, WikiProvider.LATEST_VERSION ); // // Sanity check - shouldn't really be happening, unless // you mess with the repository directly. // if( att == null ) { throw new ProviderException("Attachment disappeared while reading information:"+ " if you did not touch the repository, there is a serious bug somewhere. "+ "Attachment = "+attachments[i]+ ", decoded = "+attachmentName ); } result.add( att ); } } } } return result; } public Collection findAttachments( QueryItem[] query ) { return null; } // FIXME: Very unoptimized. public List listAllChanged( Date timestamp ) throws ProviderException { File attDir = new File( m_storageDir ); if( !attDir.exists() ) { throw new ProviderException("Specified attachment directory "+m_storageDir+" does not exist!"); } ArrayList list = new ArrayList(); String[] pagesWithAttachments = attDir.list( new AttachmentFilter() ); for( int i = 0; i < pagesWithAttachments.length; i++ ) { String pageId = unmangleName( pagesWithAttachments[i] ); pageId = pageId.substring( 0, pageId.length()-DIR_EXTENSION.length() ); Collection c = listAttachments( new WikiPage(pageId) ); for( Iterator it = c.iterator(); it.hasNext(); ) { Attachment att = (Attachment) it.next(); if( att.getLastModified().after( timestamp ) ) { list.add( att ); } } } Collections.sort( list, new PageTimeComparator() ); return list; } public Attachment getAttachmentInfo( WikiPage page, String name, int version ) throws ProviderException { Attachment att = new Attachment( page.getName(), name ); File dir = findAttachmentDir( att ); if( !dir.exists() ) { // log.debug("Attachment dir not found - thus no attachment can exist."); return null; } if( version == WikiProvider.LATEST_VERSION ) { version = findLatestVersion(att); } att.setVersion( version ); // System.out.println("Fetching info on version "+version); try { Properties props = getPageProperties(att); att.setAuthor( props.getProperty( version+".author" ) ); File f = findFile( dir, att ); att.setSize( f.length() ); att.setLastModified( new Date(f.lastModified()) ); } catch( IOException e ) { log.error("Can't read page properties", e ); throw new ProviderException("Cannot read page properties: "+e.getMessage()); } // FIXME: Check for existence of this particular version. return att; } public List getVersionHistory( Attachment att ) { ArrayList list = new ArrayList(); try { int latest = findLatestVersion( att ); for( int i = latest; i >= 1; i-- ) { Attachment a = getAttachmentInfo( new WikiPage(att.getParentName()), att.getFileName(), i ); if( a != null ) { list.add( a ); } } } catch( ProviderException e ) { log.error("Getting version history failed for page: "+att,e); // FIXME: SHould this fail? } return list; } public void deleteVersion( Attachment att ) throws ProviderException { // FIXME: Does nothing yet. } public void deleteAttachment( Attachment att ) throws ProviderException { File dir = findAttachmentDir( att ); String[] files = dir.list(); for( int i = 0; i < files.length; i++ ) { File file = new File( dir.getAbsolutePath() + "/" + files[i] ); file.delete(); } dir.delete(); } /** * Returns only those directories that contain attachments. */ public class AttachmentFilter implements FilenameFilter { public boolean accept( File dir, String name ) { return name.endsWith( DIR_EXTENSION ); } } /** * Accepts only files that are actual versions, no control files. */ public class AttachmentVersionFilter implements FilenameFilter { public boolean accept( File dir, String name ) { return !name.equals( PROPERTY_FILE ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -