StorageServiceFileSystem.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package cn.minbb.evaluationsystemserver.stroage;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.core.io.Resource;
  4. import org.springframework.core.io.UrlResource;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.util.FileSystemUtils;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.net.MalformedURLException;
  11. import java.nio.file.Files;
  12. import java.nio.file.Path;
  13. import java.nio.file.Paths;
  14. import java.util.stream.Stream;
  15. @Service
  16. public class StorageServiceFileSystem implements StorageService {
  17. private final Path rootLocation;
  18. private Path avatarLocation;
  19. @Autowired
  20. public StorageServiceFileSystem(StorageProperties properties) {
  21. this.rootLocation = Paths.get(properties.getLocation());
  22. }
  23. /**
  24. * 初始化存储空间(各文件夹)
  25. */
  26. @Override
  27. public void init() {
  28. File rootDir = rootLocation.toFile(); // 根目录
  29. File avatarDir = new File(rootDir, FileList.AVATARS); // 头像目录
  30. File downloadDir = new File(rootDir, FileList.DOWNLOAD); // 下载目录
  31. File apkDir = new File(downloadDir, FileList.APK); // APK 目录
  32. avatarLocation = avatarDir.toPath();
  33. try {
  34. if (!rootDir.exists()) {
  35. Files.createDirectory(rootLocation);
  36. }
  37. if (!avatarDir.exists()) {
  38. Files.createDirectory(avatarLocation);
  39. }
  40. if (!downloadDir.exists()) {
  41. Files.createDirectory(downloadDir.toPath());
  42. }
  43. if (!apkDir.exists()) {
  44. Files.createDirectory(apkDir.toPath());
  45. }
  46. } catch (IOException e) {
  47. throw new StorageException("初始化存储空间失败!", e);
  48. }
  49. }
  50. /**
  51. * 存储文件
  52. *
  53. * @param file
  54. */
  55. @Override
  56. public void store(MultipartFile file) {
  57. try {
  58. if (file.isEmpty()) {
  59. throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
  60. }
  61. Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename()));
  62. } catch (IOException e) {
  63. throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
  64. }
  65. }
  66. /**
  67. * 删除单个文件
  68. *
  69. * @param filename 文件名
  70. */
  71. @Override
  72. public void delete(String filename) {
  73. try {
  74. Files.delete(load(filename));
  75. } catch (IOException e) {
  76. throw new StorageException("Failed to delete file " + filename, e);
  77. }
  78. }
  79. /**
  80. * 删除存储空间全部文件(递归删除)
  81. */
  82. @Override
  83. public void deleteAll() {
  84. FileSystemUtils.deleteRecursively(rootLocation.toFile());
  85. }
  86. /**
  87. * 通过文件名加载路径
  88. *
  89. * @param filename
  90. * @return
  91. */
  92. @Override
  93. public Path load(String filename) {
  94. return rootLocation.resolve(filename);
  95. }
  96. /**
  97. * 通过文件名加载资源
  98. *
  99. * @param filename
  100. * @return
  101. */
  102. @Override
  103. public Resource loadAsResource(String filename) {
  104. try {
  105. Path file = load(filename);
  106. Resource resource = new UrlResource(file.toUri());
  107. if (resource.exists() || resource.isReadable()) {
  108. return resource;
  109. } else {
  110. throw new StorageFileNotFoundException("Could not read file: " + filename);
  111. }
  112. } catch (MalformedURLException e) {
  113. throw new StorageFileNotFoundException("Could not read file: " + filename, e);
  114. }
  115. }
  116. /**
  117. * 加载全部文件(通过遍历文件夹而非数据库文件名)
  118. *
  119. * @return
  120. */
  121. @Override
  122. public Stream<Path> loadAll() {
  123. try {
  124. return Files.walk(this.rootLocation, 1)
  125. .filter(path -> !path.equals(this.rootLocation))
  126. .map(path -> rootLocation.relativize(path));
  127. } catch (IOException e) {
  128. throw new StorageException("Failed to read stored files", e);
  129. }
  130. }
  131. @Override
  132. public void storeAvatar(MultipartFile file, String filename) {
  133. try {
  134. if (file.isEmpty()) {
  135. throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
  136. }
  137. Files.copy(file.getInputStream(), this.avatarLocation.resolve(filename));
  138. } catch (IOException e) {
  139. throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
  140. }
  141. }
  142. @Override
  143. public Resource loadAvatarsAsResource(String filename) {
  144. try {
  145. Path file = avatarLocation.resolve(filename);
  146. Resource resource = new UrlResource(file.toUri());
  147. if (resource.exists() || resource.isReadable()) {
  148. return resource;
  149. } else {
  150. throw new StorageFileNotFoundException("Could not read file: " + filename);
  151. }
  152. } catch (MalformedURLException e) {
  153. throw new StorageFileNotFoundException("Could not read file: " + filename, e);
  154. }
  155. }
  156. }