basicattachmentprovider.java

来自「我想下载一个东西」· Java 代码 · 共 580 行 · 第 1/2 页

JAVA
580
字号
        int latestVersion = findLatestVersion( att );        // System.out.println("Latest version is "+latestVersion);        try        {            int versionNumber = latestVersion+1;            File newfile = new File( attDir, versionNumber+"."+                                     getFileExtension(att.getFileName()) );            log.info("Uploading attachment "+att.getFileName()+" to page "+att.getParentName());            log.info("Saving attachment contents to "+newfile.getAbsolutePath());            out = new FileOutputStream(newfile);            FileUtil.copyContents( data, out );            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 )            {                for( int i = 0; i < attachments.length; i++ )                {                    File f = new File( dir, attachments[i] );                    if( f.isDirectory() )                    {                        String attachmentName = unmangleName( attachments[i] );                        if( attachmentName.endsWith( ATTDIR_EXTENSION ) )                        {                            attachmentName = attachmentName.substring( 0, attachmentName.length()-ATTDIR_EXTENSION.length() );                        }                                                Attachment att = getAttachmentInfo( page, attachmentName,                                                            WikiProvider.LATEST_VERSION );                        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    {        // FIXME: Does nothing yet.    }    /**     *  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 + =
减小字号Ctrl + -
显示快捷键?