All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.fckeditor.tool.UtilsFile Maven / Gradle / Ivy

/*
 * 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;
import org.devlib.schmidt.imageinfo.ImageInfo;

/**
 * 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.
* * This method enforces the forceSingleExtension property and * then replaces all occurrences of \, /, |, :, ?, *, ", <, >, * 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.
* * This method replaces all occurrences of \, /, |, :, ?, *, ", <, * >, 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 true if the underlying input stream contains an * image, else false */ public static boolean isImage(final InputStream in) { ImageInfo ii = new ImageInfo(); ii.setInput(in); return ii.check(); } /** * Checks whether a path complies with the FCKeditor File Browser rules. * * @param path * a potentially 'malicious' path * @return true if path complies with the rules, else * false */ 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 true if filename contains severals dots, else * false */ 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.
* This method extracts a file's base name, iterates over it until the first * non-existent appearance with basename(n).ext. 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; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy