123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package cn.minbb.evaluationsystemserver.stroage;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.UrlResource;
- import org.springframework.stereotype.Service;
- import org.springframework.util.FileSystemUtils;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.File;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.stream.Stream;
- @Service
- public class StorageServiceFileSystem implements StorageService {
- private final Path rootLocation;
- private Path avatarLocation;
- @Autowired
- public StorageServiceFileSystem(StorageProperties properties) {
- this.rootLocation = Paths.get(properties.getLocation());
- }
- /**
- * 初始化存储空间(各文件夹)
- */
- @Override
- public void init() {
- File rootDir = rootLocation.toFile(); // 根目录
- File avatarDir = new File(rootDir, FileList.AVATARS); // 头像目录
- File downloadDir = new File(rootDir, FileList.DOWNLOAD); // 下载目录
- File apkDir = new File(downloadDir, FileList.APK); // APK 目录
- avatarLocation = avatarDir.toPath();
- try {
- if (!rootDir.exists()) {
- Files.createDirectory(rootLocation);
- }
- if (!avatarDir.exists()) {
- Files.createDirectory(avatarLocation);
- }
- if (!downloadDir.exists()) {
- Files.createDirectory(downloadDir.toPath());
- }
- if (!apkDir.exists()) {
- Files.createDirectory(apkDir.toPath());
- }
- } catch (IOException e) {
- throw new StorageException("初始化存储空间失败!", e);
- }
- }
- /**
- * 存储文件
- *
- * @param file
- */
- @Override
- public void store(MultipartFile file) {
- try {
- if (file.isEmpty()) {
- throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
- }
- Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename()));
- } catch (IOException e) {
- throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
- }
- }
- /**
- * 删除单个文件
- *
- * @param filename 文件名
- */
- @Override
- public void delete(String filename) {
- try {
- Files.delete(load(filename));
- } catch (IOException e) {
- throw new StorageException("Failed to delete file " + filename, e);
- }
- }
- /**
- * 删除存储空间全部文件(递归删除)
- */
- @Override
- public void deleteAll() {
- FileSystemUtils.deleteRecursively(rootLocation.toFile());
- }
- /**
- * 通过文件名加载路径
- *
- * @param filename
- * @return
- */
- @Override
- public Path load(String filename) {
- return rootLocation.resolve(filename);
- }
- /**
- * 通过文件名加载资源
- *
- * @param filename
- * @return
- */
- @Override
- public Resource loadAsResource(String filename) {
- try {
- Path file = load(filename);
- Resource resource = new UrlResource(file.toUri());
- if (resource.exists() || resource.isReadable()) {
- return resource;
- } else {
- throw new StorageFileNotFoundException("Could not read file: " + filename);
- }
- } catch (MalformedURLException e) {
- throw new StorageFileNotFoundException("Could not read file: " + filename, e);
- }
- }
- /**
- * 加载全部文件(通过遍历文件夹而非数据库文件名)
- *
- * @return
- */
- @Override
- public Stream<Path> loadAll() {
- try {
- return Files.walk(this.rootLocation, 1)
- .filter(path -> !path.equals(this.rootLocation))
- .map(path -> rootLocation.relativize(path));
- } catch (IOException e) {
- throw new StorageException("Failed to read stored files", e);
- }
- }
- @Override
- public void storeAvatar(MultipartFile file, String filename) {
- try {
- if (file.isEmpty()) {
- throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
- }
- Files.copy(file.getInputStream(), this.avatarLocation.resolve(filename));
- } catch (IOException e) {
- throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
- }
- }
- @Override
- public Resource loadAvatarsAsResource(String filename) {
- try {
- Path file = avatarLocation.resolve(filename);
- Resource resource = new UrlResource(file.toUri());
- if (resource.exists() || resource.isReadable()) {
- return resource;
- } else {
- throw new StorageFileNotFoundException("Could not read file: " + filename);
- }
- } catch (MalformedURLException e) {
- throw new StorageFileNotFoundException("Could not read file: " + filename, e);
- }
- }
- }
|