zhangjinli 3 years ago
parent 2e40f113c3
commit 220098e947

@ -2,45 +2,32 @@ package com.zh.project0512.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.zh.project0512.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zh.project0512.mapper.QywxDepartmentUserLinkMapper;
import com.zh.project0512.mapper.RankMapper;
import com.zh.project0512.mapper.UserMapper;
import com.zh.project0512.model.Rank;
import com.zh.project0512.model.Task;
import com.zh.project0512.model.UserPointsRecords;
import com.zh.project0512.model.Video;
import com.zh.project0512.model.dto.RankListDTO;
import com.zh.project0512.model.vo.RankListVo;
import com.zh.project0512.service.IRewardRuleService;
import com.zh.project0512.service.ITaskService;
import com.zh.project0512.service.IUserPointsRecordsService;
import com.zh.project0512.service.IVideoService;
import com.zh.project0512.utils.AliyunOss;
import com.zh.project0512.utils.FileTypeUtil;
import com.zh.project0512.utils.result.HttpStatusEnum;
import com.zh.project0512.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jsoup.helper.StringUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.MultimediaObject;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -59,26 +46,33 @@ import java.util.stream.Collectors;
@RequestMapping("/util")
public class UtilsController {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Autowired
private AliyunOss aliyunOss;
// @Autowired
// private RedisTemplate<String, String> redisTemplate;
// @Autowired
// private AliyunOss aliyunOss;
@Value("${web.uploadPath}")
private String path;
@Value("${web.picSizeLimit}")
private long picSizeLimit;
@Value("${web.cdn}")
private String cdn;
@Operation(summary = "上传")
@PostMapping("/upload")
@ResponseBody
public Result upload(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name) {
public Result upload(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name, Boolean compress) {
if (file.isEmpty()) {
return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "请选择文件");
}
String fileName = file.getOriginalFilename();//文件名
String newFileName = UUID.randomUUID().toString().replace("-", "") + LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli() + fileName.substring(fileName.lastIndexOf("."));
String randomName = UUID.randomUUID().toString().replace("-", "") + LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
String newFileName = randomName + fileName.substring(fileName.lastIndexOf("."));
String newFileNameSmall = randomName + "Small" + fileName.substring(fileName.lastIndexOf("."));
JSONObject res = new JSONObject();
int fileTypeNum = FileTypeUtil.getcontentTypeNum((fileName.substring(fileName.lastIndexOf("."))));
//String path = System.getProperty("user.home"); //文件存储位置 我放在了我的项目下
// //获取jar包所在目录
// ApplicationHome h = new ApplicationHome(getClass());
@ -88,14 +82,23 @@ public class UtilsController {
// System.out.println(dirPath);
// System.out.println(path);
// File dest = new File(dirPath+fileName);
File dest = new File(path + "/" + newFileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
String filePath = path + "/" + newFileName;
File dest = new File(filePath);
if (compress != null && compress &&fileTypeNum ==2&& file.getSize() >= picSizeLimit) {
String filePathSmall = path + "/" + newFileNameSmall;
File beforeFile = new File(filePathSmall);
//生成目标图片
Thumbnails.of(file.getInputStream()).scale(1f).toFile(beforeFile);
//压缩图片至指定大小下
commpressPicCycle(filePathSmall, picSizeLimit, 0.8);
res.put("fileUrlSmall", "/upload/" + newFileNameSmall);
}
// if (!dest.getParentFile().exists()) {
// dest.getParentFile().mkdirs();
// }
file.transferTo(dest);
JSONObject res = new JSONObject();
int fileTypeNum = FileTypeUtil.getcontentTypeNum((fileName.substring(fileName.lastIndexOf("."))));
res.put("fileUrl", "/upload/" + newFileName);
res.put("fileName", fileName);
res.put("fileType", FileTypeUtil.getcontentType((fileName.substring(fileName.lastIndexOf(".")))));
@ -156,6 +159,20 @@ public class UtilsController {
return res;
}
private static void commpressPicCycle(String desPath, long desFileSize, double accuracy) throws IOException {
File srcFileJPG = new File(desPath);
//如果小于指定大小不压缩;如果大于等于指定大小压缩
if (srcFileJPG.length() <= desFileSize) {
return;
}
// 计算宽高
BufferedImage bim = ImageIO.read(srcFileJPG);
int desWidth = new BigDecimal(bim.getWidth()).multiply(new BigDecimal(accuracy)).intValue();
int desHeight = new BigDecimal(bim.getHeight()).multiply(new BigDecimal(accuracy)).intValue();
Thumbnails.of(desPath).size(desWidth, desHeight).outputQuality(accuracy).toFile(desPath);
commpressPicCycle(desPath, desFileSize, accuracy);
}
@Autowired
private ITaskService taskService;
@Resource
@ -190,6 +207,7 @@ public class UtilsController {
// int b = StringUtils.countOccurrencesOf(a, "https://v.douyin");
// System.out.println(b);
List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
@ -228,4 +246,7 @@ public class UtilsController {
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out::println);
}
private class LogUtil {
}
}

@ -43,7 +43,7 @@ public class Reference extends Model {
/**
* id(0)
*/
@Schema(title = "素材分组id",description = "0为未分组")
@Schema(title = "素材分组id", description = "0为未分组")
@TableField("groupId")
private Integer groupId;
@ -54,7 +54,7 @@ public class Reference extends Model {
/**
* 12
*/
@Schema(title = "类型",description = "1视频2图片")
@Schema(title = "类型", description = "1视频2图片")
private Integer type;
/**
@ -65,6 +65,13 @@ public class Reference extends Model {
@TableField("fileUrl")
private String fileUrl;
/**
*
*/
@Schema(title = "文件缩略图(图片超高清时方便预加载)")
@TableField("fileUrlSmall")
private String fileUrlSmall;
/**
* ()
*/
@ -75,7 +82,7 @@ public class Reference extends Model {
/**
*
*/
@Schema(title = "封面图片地址",description = "视频文件请上传封面图片")
@Schema(title = "封面图片地址", description = "视频文件请上传封面图片")
@TableField("coverUrl")
private String coverUrl;

@ -53,7 +53,12 @@ mybatis-plus:
logic-delete-value: 0
web:
apiPath: com.zh.project0512.controller
# mac图片地址
# uploadPath: /Users/apple/Pictures
# 线上图片上传位置
uploadPath: /www/wwwroot/project0512/upload/
# 图片压缩尺寸界限(1024*1024)
picSizeLimit: 1048576
cdn: http://cdn.sws010.com
#springdoc:

@ -53,7 +53,12 @@ mybatis-plus:
logic-delete-value: 0
web:
apiPath: com.zh.project0512.controller
uploadPath: /static/sws/upload/
# mac图片地址
# uploadPath: /Users/apple/Pictures
# 线上图片上传位置
uploadPath: /www/wwwroot/project0512/upload/
# 图片压缩尺寸界限(1024*1024)
picSizeLimit: 1048576
cdn: http://cdn.sws010.com
#springdoc:

@ -54,6 +54,7 @@
<result column="title" property="title"/>
<result column="type" property="type"/>
<result column="fileUrl" property="fileUrl"/>
<result column="fileUrlSmall" property="fileUrlSmall"/>
<result column="duration" property="duration"/>
<result column="coverUrl" property="coverUrl"/>
<result column="groupId" property="groupId"/>

Loading…
Cancel
Save