⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicattachmentprovider.java

📁 wiki建站资源 java编写的 很好用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*     JSPWiki - a JSP-based WikiWiki clone.    Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU Lesser General Public License as published by    the Free Software Foundation; either version 2.1 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU Lesser General Public License for more details.    You should have received a copy of the GNU Lesser General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */package com.ecyrd.jspwiki.providers;import java.io.InputStream;import java.io.OutputStream;import java.io.IOException;import java.io.FileNotFoundException;import java.io.File;import java.io.FilenameFilter;import java.io.FileOutputStream;import java.io.FileInputStream;import java.util.Collection;import java.util.Properties;import java.util.ArrayList;import java.util.Iterator;import java.util.Date;import java.util.List;import java.util.Collections;import org.apache.log4j.Logger;import com.ecyrd.jspwiki.*;import com.ecyrd.jspwiki.attachment.Attachment;/** *  Provides basic, versioning attachments. * *  <PRE> *   Structure is as follows: *      attachment_dir/ *         ThisPage/ *            attachment.doc/ *               attachment.properties *               1.doc *               2.doc *               3.doc *            picture.png/ *               attachment.properties *               1.png *               2.png *         ThatPage/ *            picture.png/ *               attachment.properties *               1.png *              *  </PRE> * *  The names of the directories will be URLencoded. *  <p> *  "attachment.properties" consists of the following items: *  <UL> *   <LI>1.author = author name for version 1 (etc) *  </UL> */public class BasicAttachmentProvider    implements WikiAttachmentProvider{    private String m_storageDir;    public static final String PROP_STORAGEDIR = "jspwiki.basicAttachmentProvider.storageDir";    public static final String PROPERTY_FILE   = "attachment.properties";    public static final String DIR_EXTENSION   = "-att";    public static final String ATTDIR_EXTENSION = "-dir";        static final Logger log = Logger.getLogger( BasicAttachmentProvider.class );    public void initialize( WikiEngine engine, Properties properties )         throws NoRequiredPropertyException,               IOException    {        m_storageDir = WikiEngine.getRequiredProperty( properties, PROP_STORAGEDIR );        //        //  Check if the directory exists - if it doesn't, create it.        //        File f = new File( m_storageDir );        if( !f.exists() )        {            f.mkdirs();        }    }    /**     *  Finds storage dir, and if it exists, makes sure that it is valid.     *     *  @param wikipage Page to which this attachment is attached.     */    private File findPageDir( String wikipage )        throws ProviderException    {        wikipage = mangleName( wikipage );        File f = new File( m_storageDir, wikipage+DIR_EXTENSION );        if( f.exists() && !f.isDirectory() )        {            throw new ProviderException("Storage dir '"+f.getAbsolutePath()+"' is not a directory!");        }        return f;    }    private static String mangleName( String wikiname )    {        String res = TextUtil.urlEncodeUTF8( wikiname );        return res;    }    private static String unmangleName( String filename )    {        return TextUtil.urlDecodeUTF8( filename );    }        /**     *  Finds the dir in which the attachment lives.     */    private File findAttachmentDir( Attachment att )        throws ProviderException    {        File f = new File( findPageDir(att.getParentName()),                            mangleName(att.getFileName()+ATTDIR_EXTENSION) );        //        //  Migration code for earlier versions of JSPWiki.        //  Originally, we used plain filename.  Then we realized we need        //  to urlencode it.  Then we realized that we have to use a        //  postfix to make sure illegal file names are never formed.        //        if( !f.exists() )        {            File oldf = new File( findPageDir( att.getParentName() ),                                  mangleName( att.getFileName() ) );            if( oldf.exists() )            {                f = oldf;            }            else            {                oldf = new File( findPageDir( att.getParentName() ),                                 att.getFileName() );                if( oldf.exists() )                {                    f = oldf;                }            }        }        return f;    }    /**     *  Goes through the repository and decides which version is     *  the newest one in that directory.     *     *  @return Latest version number in the repository, or 0, if     *          there is no page in the repository.     */    private int findLatestVersion( Attachment att )        throws ProviderException    {        // File pageDir = findPageDir( att.getName() );        File attDir  = findAttachmentDir( att );        // log.debug("Finding pages in "+attDir.getAbsolutePath());        String[] pages = attDir.list( new AttachmentVersionFilter() );        if( pages == null )        {            return 0; // No such thing found.        }        int version = 0;        for( int i = 0; i < pages.length; i++ )        {            // log.debug("Checking: "+pages[i]);            int cutpoint = pages[i].indexOf( '.' );                String pageNum = ( cutpoint > 0 ) ? pages[i].substring( 0, cutpoint ) : pages[i] ;                try                {                    int res = Integer.parseInt( pageNum );                    if( res > version )                    {                        version = res;                    }                }                catch( NumberFormatException e ) {} // It's okay to skip these.            }        return version;    }    /**     *  Returns the file extension.  For example "test.png" returns "png".     *  <p>     *  If file has no extension, will return "bin"     */    protected static String getFileExtension( String filename )    {        String fileExt = "bin";        int dot = filename.lastIndexOf('.');        if( dot >= 0 && dot < filename.length()-1 )        {            fileExt = mangleName( filename.substring( dot+1 ) );        }        return fileExt;    }    /**     *  Writes the page properties back to the file system.     *  Note that it WILL overwrite any previous properties.     */    private void putPageProperties( Attachment att, Properties properties )        throws IOException,               ProviderException    {        File attDir = findAttachmentDir( att );        File propertyFile = new File( attDir, PROPERTY_FILE );        OutputStream out = new FileOutputStream( propertyFile );        properties.store( out,                           " JSPWiki page properties for "+                          att.getName()+                          ". DO NOT MODIFY!" );        out.close();    }    /**     *  Reads page properties from the file system.     */    private Properties getPageProperties( Attachment att )        throws IOException,               ProviderException    {        Properties props = new Properties();        File propertyFile = new File( findAttachmentDir(att), PROPERTY_FILE );        if( propertyFile != null && propertyFile.exists() )        {            InputStream in = new FileInputStream( propertyFile );            props.load(in);            in.close();        }                return props;    }    public void putAttachmentData( Attachment att, InputStream data )        throws ProviderException,               IOException    {        OutputStream out = null;        File attDir = findAttachmentDir( att );        if(!attDir.exists())        {            attDir.mkdirs();        }        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 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -