GetResourceSqlTest.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import com.baomidou.mybatisplus.core.toolkit.IdWorker;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.io.FileUtils;
  4. import org.apache.commons.lang3.RandomStringUtils;
  5. import org.apache.commons.lang3.exception.ExceptionUtils;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import java.io.File;
  10. import java.nio.file.Files;
  11. import java.nio.file.Path;
  12. import java.nio.file.StandardCopyOption;
  13. import java.text.SimpleDateFormat;
  14. import java.util.ArrayList;
  15. import java.util.Date;
  16. import java.util.List;
  17. /**
  18. * 用于批量生成资源文件的sql
  19. * @author hongyang
  20. * @version 1.0
  21. * @date 2023/5/30 13:55
  22. */
  23. @Slf4j
  24. @RunWith(SpringRunner.class)
  25. public class GetResourceSqlTest {
  26. /**
  27. * 需要处理的文件夹
  28. */
  29. public static final String FOLDER_PATH = "/Users/liuchengbiao/Desktop/大屏资源";
  30. /**
  31. * 服务存储文件的位置,即配置文件中的gc.file.basePath
  32. */
  33. public static final String FILE_BASE_PATH = "/root/bigscreen/file";
  34. /**
  35. * 静态资源接口前缀,即配置文件中的gc.file.urlPrefix
  36. */
  37. public static final String FILE_URL_PREFIX = "http://gcpaas.gccloud.com/bigScreenServer/static";
  38. /**
  39. * 文件所属分组编码
  40. */
  41. public static final String FILE_GROUP_CODE = "other";
  42. @Test
  43. public void getResourceSql() {
  44. List<String> sqlList = new ArrayList<>();
  45. // 需要处理的文件夹
  46. File folder = new File(FOLDER_PATH);
  47. if (!folder.exists() || !folder.isDirectory()) {
  48. log.error("文件夹不存在");
  49. return;
  50. }
  51. File[] subFiles = folder.listFiles();
  52. if (subFiles == null) {
  53. log.error("文件夹为空");
  54. return;
  55. }
  56. for (File subFile : subFiles) {
  57. String typeCode = FILE_GROUP_CODE;
  58. if (subFile.isDirectory()) {
  59. // 获取文件夹名称
  60. String folderName = subFile.getName();
  61. // 生成编码
  62. typeCode = RandomStringUtils.randomAlphanumeric(10).toLowerCase();
  63. // 创建时间
  64. String currentDate = getCurrentDateTime();
  65. String insertTypeSql = "INSERT INTO big_screen_type (name, code, type, order_num, update_date, create_date, del_flag) VALUES ('%s', '%s', '%s', %s, '%s', '%s', %s);";
  66. String insertTypeSqlFormat = String.format(insertTypeSql, folderName, typeCode, "resourceCatalog", 0, currentDate, currentDate, 0);
  67. sqlList.add("# 分组");
  68. sqlList.add(insertTypeSqlFormat);
  69. sqlList.add("# 资源");
  70. }
  71. handleFile(subFile, "", sqlList, typeCode);
  72. }
  73. // 将sql输出到文件
  74. String sql = String.join("\n", sqlList);
  75. String fileName = "big_screen_file.sql";
  76. String filePath = FOLDER_PATH + "/" + fileName;
  77. // 写入文件
  78. try {
  79. FileUtils.write(new File(filePath), sql, "UTF-8");
  80. } catch (Exception e) {
  81. log.error("写入sql文件失败");
  82. log.error(ExceptionUtils.getStackTrace(e));
  83. }
  84. log.info("sql生成到文件:{}", FOLDER_PATH + "/big_screen_file.sql");
  85. log.info("重命名后的文件路径:{}", FOLDER_PATH + "_重命名");
  86. }
  87. /**
  88. * 处理文件/文件夹
  89. * @param file 文件/文件夹
  90. * @param relativePath 相对路径(相对于FOLDER_PATH)
  91. * @param sqlList sql列表
  92. */
  93. private static void handleFile(File file, String relativePath, List<String> sqlList, String typeCode) {
  94. if (file.isDirectory()) {
  95. File[] files = file.listFiles();
  96. if (files == null) {
  97. return;
  98. }
  99. for (File subFile : files) {
  100. String subRelativePath = relativePath + "/" + file.getName();
  101. handleFile(subFile, subRelativePath, sqlList, typeCode);
  102. }
  103. return;
  104. }
  105. // 原始文件名
  106. String originalName = file.getName();
  107. // 文件后缀
  108. String extension = getFileExtension(originalName);
  109. // 新文件名
  110. String newFileName = IdWorker.getIdStr()+ "." + extension;
  111. // 新文件路径
  112. String newPath = FOLDER_PATH + "_重命名" + relativePath + "/" + newFileName;
  113. // 复制文件
  114. Path sourcePath = file.toPath();
  115. Path targetPath = new File(newPath).toPath();
  116. try {
  117. // 创建文件夹
  118. Files.createDirectories(targetPath.getParent());
  119. Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
  120. } catch (Exception e) {
  121. log.error(ExceptionUtils.getStackTrace(e));
  122. }
  123. // 在服务器上的文件路径
  124. String path = FILE_BASE_PATH + relativePath;
  125. // 文件访问地址
  126. String url = FILE_URL_PREFIX + relativePath + "/" + newFileName;
  127. // 替换可能存在的反斜杠
  128. url = url.replace("\\", "/");
  129. // 文件大小
  130. long size = file.length();
  131. // 创建时间
  132. String currentDate = getCurrentDateTime();
  133. // 生成sql
  134. String sql = String.format("INSERT INTO big_screen_file (module, original_name, new_name, extension, path, url, size, download_count, create_date, update_date, del_flag) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', %d);",
  135. typeCode, originalName, newFileName, extension, path, url, size, 0, currentDate, currentDate, 0);
  136. sqlList.add(sql);
  137. }
  138. /**
  139. * 获取文件后缀
  140. * @param fileName
  141. * @return
  142. */
  143. private static String getFileExtension(String fileName) {
  144. int dotIndex = fileName.lastIndexOf('.');
  145. if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
  146. return fileName.substring(dotIndex + 1);
  147. }
  148. return "";
  149. }
  150. /**
  151. * 获取当前时间
  152. * @return
  153. */
  154. private static String getCurrentDateTime() {
  155. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  156. Date currentDate = new Date();
  157. return dateFormat.format(currentDate);
  158. }
  159. }