|
@@ -10,8 +10,10 @@ import org.springframework.web.multipart.MultipartRequest;
|
|
|
import shop.alien.entity.result.R;
|
|
|
import shop.alien.entity.store.vo.StoreImgVo;
|
|
|
import shop.alien.store.service.StoreImgService;
|
|
|
+import shop.alien.util.ali.AliOSSUtil;
|
|
|
import shop.alien.util.common.RandomCreateUtil;
|
|
|
import shop.alien.util.common.VideoUtils;
|
|
|
+import shop.alien.util.file.FileUtil;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.nio.file.Files;
|
|
@@ -34,11 +36,7 @@ public class FileUploadUtil {
|
|
|
@Value("${spring.web.resources.static-locations}")
|
|
|
private String uploadDir;
|
|
|
|
|
|
- @Value("${spring.web.resources.app-locations}")
|
|
|
- private String uploadAppDir;
|
|
|
-
|
|
|
- @Value("${spring.web.resources.url}")
|
|
|
- private String fileUrl;
|
|
|
+ private final AliOSSUtil aliOSSUtil;
|
|
|
|
|
|
List<String> imageFileType = Arrays.asList("jpg", "jpeg", "png", "bmp", "webp", "gif", "svg");
|
|
|
List<String> videoFileType = Arrays.asList("mp4", "avi", "flv", "mkv", "rmvb", "wmv", "3gp", "mov");
|
|
@@ -47,22 +45,19 @@ public class FileUploadUtil {
|
|
|
/**
|
|
|
* 上传文件
|
|
|
*
|
|
|
- * @param file 文件名
|
|
|
+ * @param multipartFile 文件名
|
|
|
* @return 文件路径
|
|
|
*/
|
|
|
- public String uploadOneFile(MultipartFile file) {
|
|
|
+ public String uploadOneFile(MultipartFile multipartFile) {
|
|
|
try {
|
|
|
- String cleanUploadDir = uploadDir.replace("\\", "/");
|
|
|
+ Map<String, String> fileNameAndType = FileUtil.getFileNameAndType(multipartFile);
|
|
|
String prefix = "";
|
|
|
- String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.') + 1);
|
|
|
- if (imageFileType.contains(type.toLowerCase())) {
|
|
|
- cleanUploadDir += "/image";
|
|
|
+ if (imageFileType.contains(fileNameAndType.get("type").toLowerCase())) {
|
|
|
prefix = "image/";
|
|
|
- } else if (videoFileType.contains(type.toLowerCase())) {
|
|
|
- cleanUploadDir += "/video";
|
|
|
+ } else if (videoFileType.contains(fileNameAndType.get("type").toLowerCase())) {
|
|
|
prefix = "video/";
|
|
|
}
|
|
|
- return copyFile(cleanUploadDir, prefix, file);
|
|
|
+ return aliOSSUtil.uploadFile(multipartFile, prefix + fileNameAndType.get("name") + RandomCreateUtil.getRandomNum(6) + "." + fileNameAndType.get("type"));
|
|
|
} catch (Exception e) {
|
|
|
log.error("FileUpload.uploadOneFile ERROR Msg={}", e.getMessage());
|
|
|
throw new RuntimeException(e);
|
|
@@ -72,21 +67,21 @@ public class FileUploadUtil {
|
|
|
/**
|
|
|
* 上传app文件
|
|
|
*
|
|
|
- * @param file 文件名
|
|
|
+ * @param multipartFile 文件名
|
|
|
* @return 文件路径
|
|
|
*/
|
|
|
- public R<Boolean> uploadApp(MultipartFile file) {
|
|
|
+ public String uploadApp(MultipartFile multipartFile) {
|
|
|
try {
|
|
|
- String type = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf('.') + 1);
|
|
|
- if (!appFileType.contains(type)) {
|
|
|
- return R.fail("该文件不是app格式文件");
|
|
|
+ Map<String, String> fileNameAndType = FileUtil.getFileNameAndType(multipartFile);
|
|
|
+ if (!appFileType.contains(fileNameAndType.get("type").toLowerCase())) {
|
|
|
+ log.error("FileUpload.uploadApp ERROR 该文件不是app格式文件");
|
|
|
+ return null;
|
|
|
}
|
|
|
- String cleanUploadDir = uploadAppDir.replace("\\", "/");
|
|
|
String prefix = "app/";
|
|
|
- return R.success(copyFile(cleanUploadDir, prefix, file));
|
|
|
+ return aliOSSUtil.uploadFile(multipartFile, prefix + fileNameAndType.get("name") + RandomCreateUtil.getRandomNum(6) + "." + fileNameAndType.get("type"));
|
|
|
} catch (Exception e) {
|
|
|
log.error("FileUpload.uploadApp ERROR Msg={}", e.getMessage());
|
|
|
- throw new RuntimeException(e);
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -106,28 +101,32 @@ public class FileUploadUtil {
|
|
|
log.info("FileUpload.uploadMoreFile fileName={}", multipartFile.getOriginalFilename());
|
|
|
String uploadDir = this.uploadDir.replace("file:///", "").replace("\\", "/");
|
|
|
String prefix;
|
|
|
- String type = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf('.') + 1);
|
|
|
+ Map<String, String> fileNameAndType = FileUtil.getFileNameAndType(multipartFile);
|
|
|
//区分文件类型
|
|
|
- if (imageFileType.contains(type.toLowerCase())) {
|
|
|
+ if (imageFileType.contains(fileNameAndType.get("type").toLowerCase())) {
|
|
|
uploadDir += "/image";
|
|
|
prefix = "image/";
|
|
|
log.info("FileUpload.uploadMoreFile 获取到图片文件准备复制 {} {} {}", uploadDir, prefix, multipartFile.getOriginalFilename());
|
|
|
- filePathList.add(copyFile(uploadDir, prefix, multipartFile));
|
|
|
- } else if (videoFileType.contains(type.toLowerCase())) {
|
|
|
- uploadDir += "/video";
|
|
|
+ filePathList.add(aliOSSUtil.uploadFile(multipartFile, prefix + fileNameAndType.get("name") + RandomCreateUtil.getRandomNum(6) + "." + fileNameAndType.get("type")));
|
|
|
+ ;
|
|
|
+ } else if (videoFileType.contains(fileNameAndType.get("type").toLowerCase())) {
|
|
|
+ uploadDir += "/video/";
|
|
|
prefix = "video/";
|
|
|
+ //上传视频文件
|
|
|
log.info("FileUpload.uploadMoreFile 获取到视频文件准备复制 {} {} {}", uploadDir, prefix, multipartFile.getOriginalFilename());
|
|
|
- String localFileName = copyFile(uploadDir, prefix, multipartFile);
|
|
|
- filePathList.add(localFileName);
|
|
|
+ String videoFileName = fileNameAndType.get("name") + RandomCreateUtil.getRandomNum(6);
|
|
|
+ String cacheVideoPath = copyFile(uploadDir, multipartFile);
|
|
|
+ filePathList.add(aliOSSUtil.uploadFile(multipartFile, prefix + videoFileName + "." + fileNameAndType.get("type")));
|
|
|
+ //缓存视频截图使用
|
|
|
+ File videoFile = new File(cacheVideoPath);
|
|
|
//获取视频某帧截图
|
|
|
- log.info("FileUpload.uploadMoreFile 视频文件复制完毕, 获取第一秒图片 {}", localFileName);
|
|
|
- String videoPath = videoUtils.getImg(uploadDir + "/" + new File(localFileName).getName());
|
|
|
+ log.info("FileUpload.uploadMoreFile 视频文件复制完毕, 获取第一秒图片 {}", videoFile.getName());
|
|
|
+ String videoPath = videoUtils.getImg(uploadDir + "/" + videoFile.getName());
|
|
|
log.info("FileUpload.uploadMoreFile 视频文件复制完毕, 图片位置 {}", videoPath);
|
|
|
if (!videoPath.isEmpty()) {
|
|
|
File videoImgFile = new File(videoPath);
|
|
|
- String videoImgFileUrl = fileUrl + prefix + videoImgFile.getName();
|
|
|
- log.info("FileUpload.uploadMoreFile 视频图片获取完毕 {}", videoImgFileUrl);
|
|
|
- filePathList.add(videoImgFileUrl);
|
|
|
+ Map<String, String> videoImg = FileUtil.getFileNameAndType(videoImgFile);
|
|
|
+ filePathList.add(aliOSSUtil.uploadFile(videoImgFile, prefix + videoFileName + "." + videoImg.get("type")));
|
|
|
} else {
|
|
|
throw new RuntimeException("视频截图失败");
|
|
|
}
|
|
@@ -165,18 +164,16 @@ public class FileUploadUtil {
|
|
|
for (StoreImgVo storeImgVo : storeImgList) {
|
|
|
//获取文件
|
|
|
MultipartFile multipartFile = multipartRequest.getFileMap().get(storeImgVo.getName());
|
|
|
- String uploadDir = this.uploadDir.replace("file:///", "").replace("\\", "/");
|
|
|
+ Map<String, String> fileInfo = FileUtil.getFileNameAndType(multipartFile);
|
|
|
+ String videoFileName = fileInfo.get("name") + RandomCreateUtil.getRandomNum(6);
|
|
|
String prefix;
|
|
|
- String type = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf('.') + 1);
|
|
|
String filePath = "";
|
|
|
- if (imageFileType.contains(type.toLowerCase())) {
|
|
|
- uploadDir += "/image";
|
|
|
+ if (imageFileType.contains(fileInfo.get("type").toLowerCase())) {
|
|
|
prefix = "image/";
|
|
|
- filePath = copyFile(uploadDir, prefix, multipartFile);
|
|
|
- } else if (videoFileType.contains(type.toLowerCase())) {
|
|
|
- uploadDir += "/video";
|
|
|
+ filePath = aliOSSUtil.uploadFile(multipartFile, prefix + videoFileName + "." + fileInfo.get("type"));
|
|
|
+ } else if (videoFileType.contains(fileInfo.get("type").toLowerCase())) {
|
|
|
prefix = "video/";
|
|
|
- filePath = copyFile(uploadDir, prefix, multipartFile);
|
|
|
+ filePath = aliOSSUtil.uploadFile(multipartFile, prefix + videoFileName + "." + fileInfo.get("type"));
|
|
|
}
|
|
|
storeImgVo.setImgUrl(filePath);
|
|
|
storeImgService.save(storeImgVo);
|
|
@@ -189,10 +186,30 @@ public class FileUploadUtil {
|
|
|
* 复制文件, 返回url链接
|
|
|
*
|
|
|
* @param localFilePath 本地路径
|
|
|
- * @param urlPath 拼接的url路劲
|
|
|
* @param file 文件
|
|
|
* @return 访问url路径
|
|
|
*/
|
|
|
+ private String copyFile(String localFilePath, MultipartFile file) {
|
|
|
+ try {
|
|
|
+ File cacheFilePath = new File(localFilePath);
|
|
|
+ if (!cacheFilePath.exists()) {
|
|
|
+ cacheFilePath.mkdirs();
|
|
|
+ }
|
|
|
+ String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf('.'));
|
|
|
+ log.info("FileUpload.copyFile fileName={}", fileName);
|
|
|
+ String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
|
|
|
+ log.info("FileUpload.copyFile fileType={}", fileType);
|
|
|
+ System.out.println(file.getOriginalFilename());
|
|
|
+ Path path = Paths.get(localFilePath, file.getOriginalFilename());
|
|
|
+ Files.createDirectories(path.getParent());
|
|
|
+ Files.write(path, file.getBytes());
|
|
|
+ return localFilePath + file.getOriginalFilename();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("FileUpload.copyFile ERROR Msg={}", e.getMessage());
|
|
|
+ return e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private String copyFile(String localFilePath, String urlPath, MultipartFile file) {
|
|
|
try {
|
|
|
String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf('.'));
|
|
@@ -206,7 +223,7 @@ public class FileUploadUtil {
|
|
|
Path path = Paths.get(localFilePath, newFileName);
|
|
|
Files.createDirectories(path.getParent());
|
|
|
Files.write(path, file.getBytes());
|
|
|
- return fileUrl + urlPath + newFileName;
|
|
|
+ return urlPath + newFileName;
|
|
|
} catch (Exception e) {
|
|
|
log.error("FileUpload.copyFile ERROR Msg={}", e.getMessage());
|
|
|
return e.getMessage();
|