StorageManager.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.baidu.ueditor.upload;
  2. import com.baidu.ueditor.define.AppInfo;
  3. import com.baidu.ueditor.define.BaseState;
  4. import com.baidu.ueditor.define.State;
  5. import java.io.*;
  6. import net.diaowen.common.plugs.file.FileMagicUtils;
  7. import org.apache.commons.io.FileUtils;
  8. public class StorageManager {
  9. public static final int BUFFER_SIZE = 8192;
  10. public StorageManager() {
  11. }
  12. public static State saveBinaryFile(byte[] data, String path) {
  13. if(!FileMagicUtils.isUserUpFileType(data,path.substring(path.lastIndexOf(".")))){
  14. return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
  15. }
  16. File file = new File(path);
  17. State state = valid(file);
  18. if (!state.isSuccess()) {
  19. return state;
  20. }
  21. try {
  22. BufferedOutputStream bos = new BufferedOutputStream(
  23. new FileOutputStream(file));
  24. bos.write(data);
  25. bos.flush();
  26. bos.close();
  27. } catch (IOException ioe) {
  28. ioe.printStackTrace();
  29. return new BaseState(false, AppInfo.IO_ERROR);
  30. }
  31. state = new BaseState(true, file.getAbsolutePath());
  32. state.putInfo( "size", data.length );
  33. state.putInfo( "title", file.getName() );
  34. return state;
  35. }
  36. public static State saveFileByInputStream(InputStream is, String path,
  37. long maxSize) {
  38. System.out.println("saveFileByInputStream");
  39. BaseState validateState = isUserUpFileType(is,path.substring(path.lastIndexOf(".")));
  40. if(!validateState.isSuccess()) return validateState;
  41. State state = new BaseState(false, AppInfo.IO_ERROR);
  42. File tmpFile = validateState.getTmpFile();
  43. if(tmpFile!=null){
  44. state = saveTmpFile(tmpFile, path);
  45. deleteTmpFile(tmpFile);
  46. return state;
  47. }
  48. return state;
  49. }
  50. public static State saveFileByInputStream(InputStream is, String path) {
  51. BaseState validateState = isUserUpFileType(is,path.substring(path.lastIndexOf(".")));
  52. if(!validateState.isSuccess()) return validateState;
  53. State state = new BaseState(false, AppInfo.IO_ERROR);
  54. File tmpFile = validateState.getTmpFile();
  55. if(tmpFile!=null){
  56. state = saveTmpFile(tmpFile, path);
  57. deleteTmpFile(tmpFile);
  58. return state;
  59. }
  60. return state;
  61. }
  62. private static File getTmpFile() {
  63. File tmpDir = FileUtils.getTempDirectory();
  64. String tmpFileName = (Math.random() * 10000 + "").replace(".", "");
  65. return new File(tmpDir, tmpFileName);
  66. }
  67. private static State saveTmpFile(File tmpFile, String path) {
  68. State state = null;
  69. File targetFile = new File(path);
  70. if (targetFile.canWrite()) {
  71. return new BaseState(false, AppInfo.PERMISSION_DENIED);
  72. }
  73. try {
  74. FileUtils.moveFile(tmpFile, targetFile);
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. return new BaseState(false, AppInfo.IO_ERROR);
  78. }
  79. state = new BaseState(true);
  80. state.putInfo( "size", targetFile.length() );
  81. state.putInfo( "title", targetFile.getName() );
  82. return state;
  83. }
  84. private static State valid(File file) {
  85. File parentPath = file.getParentFile();
  86. if ((!parentPath.exists()) && (!parentPath.mkdirs())) {
  87. return new BaseState(false, AppInfo.FAILED_CREATE_FILE);
  88. }
  89. if (!parentPath.canWrite()) {
  90. return new BaseState(false, AppInfo.PERMISSION_DENIED);
  91. }
  92. return new BaseState(true);
  93. }
  94. public static BaseState isUserUpFileType(InputStream is,String fileSuffix) {
  95. File tmpFile = getTmpFile();
  96. byte[] dataBuf = new byte[ 2048 ];
  97. BufferedInputStream bis = new BufferedInputStream(is, StorageManager.BUFFER_SIZE);
  98. try {
  99. BufferedOutputStream bos = new BufferedOutputStream(
  100. new FileOutputStream(tmpFile), StorageManager.BUFFER_SIZE);
  101. int count = 0;
  102. while ((count = bis.read(dataBuf)) != -1) {
  103. bos.write(dataBuf, 0, count);
  104. }
  105. bis.close();
  106. bos.flush();
  107. bos.close();
  108. if(!FileMagicUtils.isUserUpFileType(tmpFile,fileSuffix)){
  109. tmpFile.delete();
  110. return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
  111. }
  112. // tmpFile.delete();
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. return new BaseState(false, AppInfo.IO_ERROR);
  116. }
  117. return new BaseState(true, AppInfo.SUCCESS, tmpFile);
  118. }
  119. private static void deleteTmpFile(File tmpFile) {
  120. try{
  121. tmpFile.delete();
  122. }catch (Exception e){
  123. e.printStackTrace();
  124. }
  125. }
  126. }