PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/eeplat/src/net/fckeditor/tool/UtilsFile.java

http://eeplat.googlecode.com/
Java | 179 lines | 57 code | 23 blank | 99 comment | 7 complexity | 8eed2a6e8c6b624bdda4bcf6b8989bd3 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1
  1. /*
  2. * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  3. * Copyright (C) 2004-2010 Frederico Caldeira Knabben
  4. *
  5. * == BEGIN LICENSE ==
  6. *
  7. * Licensed under the terms of any of the following licenses at your
  8. * choice:
  9. *
  10. * - GNU General Public License Version 2 or later (the "GPL")
  11. * http://www.gnu.org/licenses/gpl.html
  12. *
  13. * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  14. * http://www.gnu.org/licenses/lgpl.html
  15. *
  16. * - Mozilla Public License Version 1.1 or later (the "MPL")
  17. * http://www.mozilla.org/MPL/MPL-1.1.html
  18. *
  19. * == END LICENSE ==
  20. */
  21. package net.fckeditor.tool;
  22. import java.io.File;
  23. import java.io.InputStream;
  24. import java.util.regex.Pattern;
  25. import net.fckeditor.handlers.PropertiesLoader;
  26. import org.apache.commons.io.FilenameUtils;
  27. /**
  28. * Static helper methods for files.
  29. *
  30. * @version $Id: UtilsFile.java 4785 2009-12-21 20:10:28Z mosipov $
  31. */
  32. public class UtilsFile {
  33. protected static final Pattern ILLEGAL_CURRENT_FOLDER_PATTERN = Pattern
  34. .compile("^[^/]|[^/]$|/\\.{1,2}|\\\\|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}");
  35. /**
  36. * Sanitizes a filename from certain chars.<br />
  37. *
  38. * This method enforces the <code>forceSingleExtension</code> property and
  39. * then replaces all occurrences of \, /, |, :, ?, *, &quot;, &lt;, &gt;,
  40. * control chars by _ (underscore).
  41. *
  42. * @param filename
  43. * a potentially 'malicious' filename
  44. * @return sanitized filename
  45. */
  46. public static String sanitizeFileName(final String filename) {
  47. if (Utils.isEmpty(filename))
  48. return filename;
  49. String name = (PropertiesLoader.isForceSingleExtension()) ? UtilsFile
  50. .forceSingleExtension(filename) : filename;
  51. // Remove \ / | : ? * " < > 'Control Chars' with _
  52. return name.replaceAll("\\\\|/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
  53. }
  54. /**
  55. * Sanitizes a folder name from certain chars.<br />
  56. *
  57. * This method replaces all occurrences of \, /, |, :, ?, *, &quot;, &lt;,
  58. * &gt;, control chars by _ (underscore).
  59. *
  60. * @param folderName
  61. * a potentially 'malicious' folder name
  62. * @return sanitized folder name
  63. */
  64. public static String sanitizeFolderName(final String folderName) {
  65. if (Utils.isEmpty(folderName))
  66. return folderName;
  67. // Remove . \ / | : ? * " < > 'Control Chars' with _
  68. return folderName.replaceAll(
  69. "\\.|\\\\|/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
  70. }
  71. /**
  72. * Checks if the underlying input stream contains an image.
  73. *
  74. * @param in
  75. * input stream of an image
  76. * @return <code>true</code> if the underlying input stream contains an
  77. * image, else <code>false</code>
  78. */
  79. public static boolean isImage(final InputStream in) {
  80. // ImageInfo ii = new ImageInfo();
  81. // ii.setInput(in);
  82. // return ii.check();
  83. return true;
  84. }
  85. /**
  86. * 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"
  87. * target="_blank">rules</a>.
  88. *
  89. * @param path
  90. * a potentially 'malicious' path
  91. * @return <code>true</code> if path complies with the rules, else
  92. * <code>false</code>
  93. */
  94. public static boolean isValidPath(final String path) {
  95. if (Utils.isEmpty(path))
  96. return false;
  97. if (ILLEGAL_CURRENT_FOLDER_PATTERN.matcher(path).find())
  98. return false;
  99. return true;
  100. }
  101. /**
  102. * Replaces all dots in a filename with underscores except the last one.
  103. *
  104. * @param filename
  105. * filename to sanitize
  106. * @return string with a single dot only
  107. */
  108. public static String forceSingleExtension(final String filename) {
  109. return filename.replaceAll("\\.(?![^.]+$)", "_");
  110. }
  111. /**
  112. * Checks if a filename contains more than one dot.
  113. *
  114. * @param filename
  115. * filename to check
  116. * @return <code>true</code> if filename contains severals dots, else
  117. * <code>false</code>
  118. */
  119. public static boolean isSingleExtension(final String filename) {
  120. return filename.matches("[^\\.]+\\.[^\\.]+");
  121. }
  122. /**
  123. * Checks a directory for existence and creates it if non-existent.
  124. *
  125. * @param dir
  126. * directory to check/create
  127. */
  128. public static void checkDirAndCreate(File dir) {
  129. if (!dir.exists())
  130. dir.mkdirs();
  131. }
  132. /**
  133. * Iterates over a base name and returns the first non-existent file.<br />
  134. * This method extracts a file's base name, iterates over it until the first
  135. * non-existent appearance with <code>basename(n).ext</code>. Where n is a
  136. * positive integer starting from one.
  137. *
  138. * @param file
  139. * base file
  140. * @return first non-existent file
  141. */
  142. public static File getUniqueFile(final File file) {
  143. if (!file.exists())
  144. return file;
  145. File tmpFile = new File(file.getAbsolutePath());
  146. File parentDir = tmpFile.getParentFile();
  147. int count = 1;
  148. String extension = FilenameUtils.getExtension(tmpFile.getName());
  149. String baseName = FilenameUtils.getBaseName(tmpFile.getName());
  150. do {
  151. tmpFile = new File(parentDir, baseName + "(" + count++ + ")."
  152. + extension);
  153. } while (tmpFile.exists());
  154. return tmpFile;
  155. }
  156. }