net.fckeditor.tool.UtilsFile.java Source code

Java tutorial

Introduction

Here is the source code for net.fckeditor.tool.UtilsFile.java

Source

/*
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2004-2010 Frederico Caldeira Knabben
 * 
 * == BEGIN LICENSE ==
 * 
 * Licensed under the terms of any of the following licenses at your
 * choice:
 * 
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 * 
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 * 
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * == END LICENSE ==
 */
package net.fckeditor.tool;

import java.io.File;
import java.io.InputStream;
import java.util.regex.Pattern;

import net.fckeditor.handlers.PropertiesLoader;

import org.apache.commons.io.FilenameUtils;

/**
 * Static helper methods for files.
 * 
 * @version $Id: UtilsFile.java 4785 2009-12-21 20:10:28Z mosipov $
 */
public class UtilsFile {

    protected static final Pattern ILLEGAL_CURRENT_FOLDER_PATTERN = Pattern
            .compile("^[^/]|[^/]$|/\\.{1,2}|\\\\|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}");

    /**
     * Sanitizes a filename from certain chars.<br />
     * 
     * This method enforces the <code>forceSingleExtension</code> property and
     * then replaces all occurrences of \, /, |, :, ?, *, &quot;, &lt;, &gt;,
     * control chars by _ (underscore).
     * 
     * @param filename
     *            a potentially 'malicious' filename
     * @return sanitized filename
     */
    public static String sanitizeFileName(final String filename) {

        if (Utils.isEmpty(filename))
            return filename;

        String name = (PropertiesLoader.isForceSingleExtension()) ? UtilsFile.forceSingleExtension(filename)
                : filename;

        // Remove \ / | : ? * " < > 'Control Chars' with _
        return name.replaceAll("\\\\|/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
    }

    /**
     * Sanitizes a folder name from certain chars.<br />
     * 
     * This method replaces all occurrences of \, /, |, :, ?, *, &quot;, &lt;,
     * &gt;, control chars by _ (underscore).
     * 
     * @param folderName
     *            a potentially 'malicious' folder name
     * @return sanitized folder name
     */
    public static String sanitizeFolderName(final String folderName) {

        if (Utils.isEmpty(folderName))
            return folderName;

        // Remove . \ / | : ? * " < > 'Control Chars' with _
        return folderName.replaceAll("\\.|\\\\|/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
    }

    /**
     * Checks if the underlying input stream contains an image.
     * 
     * @param in
     *            input stream of an image
     * @return <code>true</code> if the underlying input stream contains an
     *         image, else <code>false</code>
     */
    public static boolean isImage(final InputStream in) {

        //      ImageInfo ii = new ImageInfo();
        //      ii.setInput(in);
        //      return ii.check();
        return true;
    }

    /**
     * Checks whether a path complies with the FCKeditor File Browser <a href="http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/Server_Side_Integration#File_Browser_Requests"
     * target="_blank">rules</a>.
     * 
     * @param path
     *            a potentially 'malicious' path
     * @return <code>true</code> if path complies with the rules, else
     *         <code>false</code>
     */
    public static boolean isValidPath(final String path) {
        if (Utils.isEmpty(path))
            return false;

        if (ILLEGAL_CURRENT_FOLDER_PATTERN.matcher(path).find())
            return false;

        return true;
    }

    /**
     * Replaces all dots in a filename with underscores except the last one.
     * 
     * @param filename
     *            filename to sanitize
     * @return string with a single dot only
     */
    public static String forceSingleExtension(final String filename) {
        return filename.replaceAll("\\.(?![^.]+$)", "_");
    }

    /**
     * Checks if a filename contains more than one dot.
     * 
     * @param filename
     *            filename to check
     * @return <code>true</code> if filename contains severals dots, else
     *         <code>false</code>
     */
    public static boolean isSingleExtension(final String filename) {
        return filename.matches("[^\\.]+\\.[^\\.]+");
    }

    /**
     * Checks a directory for existence and creates it if non-existent.
     * 
     * @param dir
     *            directory to check/create
     */
    public static void checkDirAndCreate(File dir) {
        if (!dir.exists())
            dir.mkdirs();
    }

    /**
     * Iterates over a base name and returns the first non-existent file.<br />
     * This method extracts a file's base name, iterates over it until the first
     * non-existent appearance with <code>basename(n).ext</code>. Where n is a
     * positive integer starting from one.
     * 
     * @param file
     *            base file
     * @return first non-existent file
     */
    public static File getUniqueFile(final File file) {
        if (!file.exists())
            return file;

        File tmpFile = new File(file.getAbsolutePath());
        File parentDir = tmpFile.getParentFile();
        int count = 1;
        String extension = FilenameUtils.getExtension(tmpFile.getName());
        String baseName = FilenameUtils.getBaseName(tmpFile.getName());
        do {
            tmpFile = new File(parentDir, baseName + "(" + count++ + ")." + extension);
        } while (tmpFile.exists());
        return tmpFile;
    }
}