|
|
@@ -0,0 +1,44 @@
|
|
|
+package nju.seec.helper.logic.service.impl;
|
|
|
+
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import nju.seec.helper.config.FileProperties;
|
|
|
+import nju.seec.helper.logic.service.FileService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author cst
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class FileServiceImpl implements FileService {
|
|
|
+ private final FileProperties fileProperties;
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public FileServiceImpl(FileProperties fileProperties) {
|
|
|
+ this.fileProperties = fileProperties;
|
|
|
+
|
|
|
+ Path fileDir = fileProperties.getFileDirPath();
|
|
|
+ if (Files.notExists(fileDir)) {
|
|
|
+ Files.createDirectories(fileDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @Override
|
|
|
+ public String upload(MultipartFile file) {
|
|
|
+ String filename = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
|
|
+ String fileExtension = filename.substring(filename.lastIndexOf('.'));
|
|
|
+ filename = UUID.randomUUID() + fileExtension;
|
|
|
+ Path filepath = fileProperties.getFileDirPath().resolve(filename);
|
|
|
+ file.transferTo(filepath);
|
|
|
+
|
|
|
+ return File.separator + fileProperties.getStoreDir() + filename;
|
|
|
+ }
|
|
|
+}
|