001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.providers;
020
021import org.apache.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.api.core.Attachment;
024import org.apache.wiki.api.core.Engine;
025import org.apache.wiki.api.core.Page;
026import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
027import org.apache.wiki.api.exceptions.ProviderException;
028import org.apache.wiki.api.providers.AttachmentProvider;
029import org.apache.wiki.api.providers.WikiProvider;
030import org.apache.wiki.api.search.QueryItem;
031import org.apache.wiki.api.spi.Wiki;
032import org.apache.wiki.pages.PageTimeComparator;
033import org.apache.wiki.util.FileUtil;
034import org.apache.wiki.util.TextUtil;
035
036import java.io.File;
037import java.io.FileNotFoundException;
038import java.io.FilenameFilter;
039import java.io.IOException;
040import java.io.InputStream;
041import java.io.OutputStream;
042import java.nio.file.Files;
043import java.util.ArrayList;
044import java.util.Collection;
045import java.util.Date;
046import java.util.List;
047import java.util.Properties;
048import java.util.regex.Matcher;
049import java.util.regex.Pattern;
050
051/**
052 *  Provides basic, versioning attachments.
053 *
054 *  <PRE>
055 *   Structure is as follows:
056 *      attachment_dir/
057 *         ThisPage/
058 *            attachment.doc/
059 *               attachment.properties
060 *               1.doc
061 *               2.doc
062 *               3.doc
063 *            picture.png/
064 *               attachment.properties
065 *               1.png
066 *               2.png
067 *         ThatPage/
068 *            picture.png/
069 *               attachment.properties
070 *               1.png
071 *             
072 *  </PRE>
073 *
074 *  The names of the directories will be URLencoded.
075 *  <p>
076 *  "attachment.properties" consists of the following items:
077 *  <UL>
078 *   <LI>1.author = author name for version 1 (etc)
079 *  </UL>
080 */
081public class BasicAttachmentProvider implements AttachmentProvider {
082
083    private Engine m_engine;
084    private String m_storageDir;
085    
086    /*
087     * Disable client cache for files with patterns
088     * since 2.5.96
089     */
090    private Pattern m_disableCache;
091    
092    /** The property name for specifying which attachments are not cached.  Value is <tt>{@value}</tt>. */
093    public static final String PROP_DISABLECACHE = "jspwiki.basicAttachmentProvider.disableCache";
094
095    /** The name of the property file. */
096    public static final String PROPERTY_FILE = "attachment.properties";
097
098    /** The default extension for the page attachment directory name. */
099    public static final String DIR_EXTENSION = "-att";
100    
101    /** The default extension for the attachment directory. */
102    public static final String ATTDIR_EXTENSION = "-dir";
103    
104    private static final Logger LOG = LogManager.getLogger( BasicAttachmentProvider.class );
105
106    /**
107     *  {@inheritDoc}
108     */
109    @Override
110    public void initialize( final Engine engine, final Properties properties ) throws NoRequiredPropertyException, IOException {
111        m_engine = engine;
112        m_storageDir = TextUtil.getCanonicalFilePathProperty( properties, PROP_STORAGEDIR,
113                                                       System.getProperty("user.home") + File.separator + "jspwiki-files");
114
115        final String patternString = engine.getWikiProperties().getProperty( PROP_DISABLECACHE );
116        if ( patternString != null ) {
117            m_disableCache = Pattern.compile(patternString);
118        }
119
120        //  Check if the directory exists - if it doesn't, create it.
121        final File f = new File( m_storageDir );
122        if( !f.exists() ) {
123            f.mkdirs();
124        }
125
126        // Some sanity checks
127        if( !f.exists() ) {
128            throw new IOException( "Could not find or create attachment storage directory '" + m_storageDir + "'" );
129        }
130
131        if( !f.canWrite() ) {
132            throw new IOException( "Cannot write to the attachment storage directory '" + m_storageDir + "'" );
133        }
134
135        if( !f.isDirectory() ) {
136            throw new IOException( "Your attachment storage points to a file, not a directory: '" + m_storageDir + "'" );
137        }
138    }
139
140    /**
141     *  Finds storage dir, and if it exists, makes sure that it is valid.
142     *
143     *  @param wikipage Page to which this attachment is attached.
144     */
145    private File findPageDir( String wikipage ) throws ProviderException {
146        wikipage = mangleName( wikipage );
147
148        final File f = new File( m_storageDir, wikipage + DIR_EXTENSION );
149        if( f.exists() && !f.isDirectory() ) {
150            throw new ProviderException( "Storage dir '" + f.getAbsolutePath() + "' is not a directory!" );
151        }
152
153        return f;
154    }
155
156    private static String mangleName( final String wikiname ) {
157        return TextUtil.urlEncodeUTF8( wikiname );
158    }
159
160    private static String unmangleName( final String filename )
161    {
162        return TextUtil.urlDecodeUTF8( filename );
163    }
164    
165    /**
166     *  Finds the dir in which the attachment lives.
167     */
168    private File findAttachmentDir( final Attachment att ) throws ProviderException {
169        final File pageDir = findPageDir( att.getParentName() );
170        File f = new File( pageDir, mangleName( att.getFileName() + ATTDIR_EXTENSION ) );
171
172        //  Migration code for earlier versions of JSPWiki. Originally, we used plain filename.  Then we realized we need
173        //  to urlencode it.  Then we realized that we have to use a postfix to make sure illegal file names are never formed.
174        if( !f.exists() ) {
175            File oldf = new File( pageDir, mangleName( att.getFileName() ) );
176            if( oldf.exists() ) {
177                f = oldf;
178            } else {
179                oldf = new File( pageDir, att.getFileName() );
180                if( oldf.exists() ) {
181                    f = oldf;
182                }
183            }
184        }
185
186        // The directory of an attachment is always a direct child of the directory of its page; a name such as . or .. would leave it.
187        try {
188            if( !pageDir.getCanonicalFile().equals( f.getCanonicalFile().getParentFile() ) ) {
189                throw new ProviderException( "The name '" + att.getFileName() + "' does not give a directory of its own to the attachment" );
190            }
191        } catch( final IOException e ) {
192            throw new ProviderException( "Cannot check the directory of the attachment '" + att.getFileName() + "': " + e.getMessage() );
193        }
194
195        return f;
196    }
197
198    /**
199     * Goes through the repository and decides which version is the newest one in that directory.
200     *
201     * @return Latest version number in the repository, or 0, if there is no page in the repository.
202     */
203    private int findLatestVersion( final Attachment att ) throws ProviderException {
204        final File attDir  = findAttachmentDir( att );
205        final String[] pages = attDir.list( new AttachmentVersionFilter() );
206        if( pages == null ) {
207            return 0; // No such thing found.
208        }
209
210        int version = 0;
211        for( final String page : pages ) {
212            final int cutpoint = page.indexOf( '.' );
213            final String pageNum = ( cutpoint > 0 ) ? page.substring( 0, cutpoint ) : page;
214
215            try {
216                final int res = Integer.parseInt( pageNum );
217
218                if( res > version ) {
219                    version = res;
220                }
221            } catch( final NumberFormatException e ) {
222                LOG.debug(e.getMessage(), e);
223            } // It's okay to skip these.
224        }
225
226        return version;
227    }
228
229    /**
230     *  Returns the file extension.  For example "test.png" returns "png".
231     *  <p>
232     *  If file has no extension, will return "bin"
233     *  
234     *  @param filename The file name to check
235     *  @return The extension.  If no extension is found, returns "bin".
236     */
237    protected static String getFileExtension( final String filename ) {
238        String fileExt = "bin";
239
240        final int dot = filename.lastIndexOf('.');
241        if( dot >= 0 && dot < filename.length()-1 ) {
242            fileExt = mangleName( filename.substring( dot+1 ) );
243        }
244
245        return fileExt;
246    }
247
248    /**
249     *  Writes the page properties back to the file system.
250     *  Note that it WILL overwrite any previous properties.
251     */
252    private void putPageProperties( final Attachment att, final Properties properties ) throws IOException, ProviderException {
253        final File attDir = findAttachmentDir( att );
254        final File propertyFile = new File( attDir, PROPERTY_FILE );
255        try( final OutputStream out = Files.newOutputStream( propertyFile.toPath() ) ) {
256            properties.store( out, " JSPWiki page properties for " + att.getName() + ". DO NOT MODIFY!" );
257        }
258    }
259
260    /**
261     *  Reads page properties from the file system.
262     */
263    private Properties getPageProperties( final Attachment att ) throws IOException, ProviderException {
264        final Properties props = new Properties();
265        final File propertyFile = new File( findAttachmentDir(att), PROPERTY_FILE );
266        if( propertyFile.exists() ) {
267            try( final InputStream in = Files.newInputStream( propertyFile.toPath() ) ) {
268                props.load( in );
269            } catch( final IOException ioe ) {
270                LOG.error( ioe.getMessage() );
271            }
272        }
273        
274        return props;
275    }
276
277    /**
278     *  {@inheritDoc}
279     */
280    @Override
281    public void putAttachmentData( final Attachment att, final InputStream data ) throws ProviderException, IOException {
282        final File attDir = findAttachmentDir( att );
283
284        if( !attDir.exists() ) {
285            attDir.mkdirs();
286        }
287        final int latestVersion = findLatestVersion( att );
288        final int versionNumber = latestVersion + 1;
289
290        final File newfile = new File( attDir, versionNumber + "." + getFileExtension( att.getFileName() ) );
291        try( final OutputStream out = Files.newOutputStream( newfile.toPath() ) ) {
292            LOG.info( "Uploading attachment " + att.getFileName() + " to page " + att.getParentName() );
293            LOG.info( "Saving attachment contents to " + newfile.getAbsolutePath() );
294            FileUtil.copyContents( data, out );
295
296            final Properties props = getPageProperties( att );
297
298            String author = att.getAuthor();
299            if( author == null ) {
300                author = "unknown"; // FIXME: Should be localized, but cannot due to missing WikiContext
301            }
302            props.setProperty( versionNumber + ".author", author );
303
304            final String changeNote = att.getAttribute( Page.CHANGENOTE );
305            if( changeNote != null ) {
306                props.setProperty( versionNumber + ".changenote", changeNote );
307            }
308            
309            putPageProperties( att, props );
310        } catch( final IOException e ) {
311            LOG.error( "Could not save attachment data: ", e );
312            throw (IOException) e.fillInStackTrace();
313        }
314    }
315
316    /**
317     *  {@inheritDoc}
318     */
319    @Override
320    public String getProviderInfo() {
321        return "";
322    }
323
324    private File findFile( final File dir, final Attachment att ) throws FileNotFoundException, ProviderException {
325        int version = att.getVersion();
326        if( version == WikiProvider.LATEST_VERSION ) {
327            version = findLatestVersion( att );
328        }
329
330        final String ext = getFileExtension( att.getFileName() );
331        File f = new File( dir, version + "." + ext );
332
333        if( !f.exists() ) {
334            if( "bin".equals( ext ) ) {
335                final File fOld = new File( dir, version + "." );
336                if( fOld.exists() ) {
337                    f = fOld;
338                }
339            }
340            if( !f.exists() ) {
341                throw new FileNotFoundException( "No such file: " + f.getAbsolutePath() + " exists." );
342            }
343        }
344
345        return f;
346    }
347
348    /**
349     *  {@inheritDoc}
350     */
351    @Override
352    public InputStream getAttachmentData( final Attachment att ) throws IOException, ProviderException {
353        final File attDir = findAttachmentDir( att );
354        try {
355            final File f = findFile( attDir, att );
356            return Files.newInputStream( f.toPath() );
357        } catch( final FileNotFoundException e ) {
358            LOG.error( "File not found: " + e.getMessage() );
359            throw new ProviderException( "No such page was found." );
360        }
361    }
362
363    /**
364     *  {@inheritDoc}
365     */
366    @Override
367    public List< Attachment > listAttachments( final Page page ) throws ProviderException {
368        final List< Attachment > result = new ArrayList<>();
369        final File dir = findPageDir( page.getName() );
370        final String[] attachments = dir.list();
371        if( attachments != null ) {
372            //  We now have a list of all potential attachments in the directory.
373            for( final String attachment : attachments ) {
374                final File f = new File( dir, attachment );
375                if( f.isDirectory() ) {
376                    String attachmentName = unmangleName( attachment );
377
378                    //  Is it a new-stylea attachment directory?  If yes, we'll just deduce the name.  If not, however,
379                    //  we'll check if there's a suitable property file in the directory.
380                    if( attachmentName.endsWith( ATTDIR_EXTENSION ) ) {
381                        attachmentName = attachmentName.substring( 0, attachmentName.length() - ATTDIR_EXTENSION.length() );
382                    } else {
383                        final File propFile = new File( f, PROPERTY_FILE );
384                        if( !propFile.exists() ) {
385                            //  This is not obviously a JSPWiki attachment, so let's just skip it.
386                            continue;
387                        }
388                    }
389
390                    final Attachment att = getAttachmentInfo( page, attachmentName, WikiProvider.LATEST_VERSION );
391                    //  Sanity check - shouldn't really be happening, unless you mess with the repository directly.
392                    if( att == null ) {
393                        LOG.error( "Attachment disappeared while reading information:"
394                                + " if you did not touch the repository, there is a serious bug somewhere or perhaps it"
395                                + " was deleted by antivirus software, etc. " + "Attachment = " + attachment
396                                + ", decoded = " + attachmentName );
397                    } else {
398                        result.add( att );
399                    }
400                }
401            }
402        }
403
404        return result;
405    }
406
407    /**
408     *  {@inheritDoc}
409     */
410    @Override
411    public Collection< Attachment > findAttachments( final QueryItem[] query ) {
412        return new ArrayList<>();
413    }
414
415    /**
416     *  {@inheritDoc}
417     */
418    // FIXME: Very unoptimized.
419    @Override
420    public List< Attachment > listAllChanged( final Date timestamp ) throws ProviderException {
421        final File attDir = new File( m_storageDir );
422        if( !attDir.exists() ) {
423            if (!attDir.mkdirs()) {
424                throw new ProviderException( "Specified attachment directory " + m_storageDir + " does not exist!" );
425            }
426        }
427
428        final ArrayList< Attachment > list = new ArrayList<>();
429        final String[] pagesWithAttachments = attDir.list( new AttachmentFilter() );
430
431        if( pagesWithAttachments != null ) {
432            for( final String pagesWithAttachment : pagesWithAttachments ) {
433                String pageId = unmangleName( pagesWithAttachment );
434                pageId = pageId.substring( 0, pageId.length() - DIR_EXTENSION.length() );
435
436                final Collection< Attachment > c = listAttachments( Wiki.contents().page( m_engine, pageId ) );
437                for( final Attachment att : c ) {
438                    if( att.getLastModified().after( timestamp ) ) {
439                        list.add( att );
440                    }
441                }
442            }
443        }
444
445        list.sort( new PageTimeComparator() );
446
447        return list;
448    }
449
450    /**
451     *  {@inheritDoc}
452     */
453    @Override
454    public Attachment getAttachmentInfo( final Page page, final String name, int version ) throws ProviderException {
455        final Attachment att = new org.apache.wiki.attachment.Attachment( m_engine, page.getName(), name );
456        final File dir = findAttachmentDir( att );
457        if( !dir.exists() ) {
458            // LOG.debug("Attachment dir not found - thus no attachment can exist.");
459            return null;
460        }
461        
462        if( version == WikiProvider.LATEST_VERSION ) {
463            version = findLatestVersion(att);
464        }
465
466        att.setVersion( version );
467        
468        // Should attachment be cachable by the client (browser)?
469        if( m_disableCache != null ) {
470            final Matcher matcher = m_disableCache.matcher( name );
471            if( matcher.matches() ) {
472                att.setCacheable( false );
473            }
474        }
475
476        // System.out.println("Fetching info on version "+version);
477        try {
478            final Properties props = getPageProperties( att );
479            att.setAuthor( props.getProperty( version+".author" ) );
480            final String changeNote = props.getProperty( version+".changenote" );
481            if( changeNote != null ) {
482                att.setAttribute( Page.CHANGENOTE, changeNote );
483            }
484
485            final File f = findFile( dir, att );
486            att.setSize( f.length() );
487            att.setLastModified( new Date( f.lastModified() ) );
488        } catch( final FileNotFoundException e ) {
489            LOG.error( "Can't get attachment properties for " + att, e );
490            return null;
491        } catch( final IOException e ) {
492            LOG.error("Can't read page properties", e );
493            throw new ProviderException("Cannot read page properties: "+e.getMessage());
494        }
495        // FIXME: Check for existence of this particular version.
496
497        return att;
498    }
499
500    /**
501     *  {@inheritDoc}
502     */
503    @Override
504    public List< Attachment > getVersionHistory( final Attachment att ) {
505        final ArrayList< Attachment > list = new ArrayList<>();
506        try {
507            final int latest = findLatestVersion( att );
508            for( int i = latest; i >= 1; i-- ) {
509                final Attachment a = getAttachmentInfo( Wiki.contents().page( m_engine, att.getParentName() ), att.getFileName(), i );
510                if( a != null ) {
511                    list.add( a );
512                }
513            }
514        } catch( final ProviderException e ) {
515            LOG.error( "Getting version history failed for page: " + att, e );
516            // FIXME: Should this fail?
517        }
518
519        return list;
520    }
521
522    /**
523     *  {@inheritDoc}
524     */
525    @Override
526    public void deleteVersion( final Attachment att ) throws ProviderException {
527        // FIXME: Does nothing yet.
528    }
529
530    /**
531     *  {@inheritDoc}
532     */
533    @Override
534    public void deleteAttachment( final Attachment att ) throws ProviderException {
535        final File dir = findAttachmentDir( att );
536        final String[] files = dir.list();
537        for( final String s : files ) {
538            final File file = new File( dir.getAbsolutePath() + "/" + s );
539            file.delete();
540        }
541        dir.delete();
542    }
543
544    /**
545     *  Returns only those directories that contain attachments.
546     */
547    public static class AttachmentFilter implements FilenameFilter {
548        /**
549         *  {@inheritDoc}
550         */
551        @Override
552        public boolean accept( final File dir, final String name )
553        {
554            return name.endsWith( DIR_EXTENSION );
555        }
556    }
557
558    /**
559     *  Accepts only files that are actual versions, no control files.
560     */
561    public static class AttachmentVersionFilter implements FilenameFilter {
562        /**
563         *  {@inheritDoc}
564         */
565        @Override
566        public boolean accept( final File dir, final String name )
567        {
568            return !name.equals( PROPERTY_FILE );
569        }
570    }
571
572    /**
573     *  {@inheritDoc}
574     */
575    @Override
576    public void moveAttachmentsForPage( final String oldParent, final String newParent ) throws ProviderException {
577        final File srcDir = findPageDir( oldParent );
578        final File destDir = findPageDir( newParent );
579
580        LOG.debug( "Trying to move all attachments from " + srcDir + " to " + destDir );
581
582        // If it exists, we're overwriting an old page (this has already been confirmed at a higher level), so delete any existing attachments.
583        if( destDir.exists() ) {
584            LOG.error( "Page rename failed because target directory " + destDir + " exists" );
585        } else {
586            // destDir.getParentFile().mkdir();
587            srcDir.renameTo( destDir );
588        }
589    }
590
591}
592