| | |
| | | import java.net.URLDecoder; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.nio.file.StandardCopyOption; |
| | | import java.util.*; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import javax.imageio.IIOImage; |
| | | import javax.imageio.ImageWriteParam; |
| | | import javax.imageio.ImageWriter; |
| | | import javax.imageio.stream.MemoryCacheImageOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.websocket.server.PathParam; |
| | |
| | | import com.ruoyi.common.utils.RenamedMultipartFile; |
| | | import com.ruoyi.common.utils.uuid.UUID; |
| | | import com.ruoyi.service.DownLoadFileService; |
| | | import com.ruoyi.service.impl.VideoProcessService; |
| | | import lombok.Data; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | import com.ruoyi.common.utils.file.FileUploadUtils; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | | import com.ruoyi.framework.config.ServerConfig; |
| | | import springfox.bean.validators.plugins.schema.NotNullAnnotationPlugin; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | |
| | | import java.util.Base64; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.HashSet; |
| | | import java.util.Set; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | |
| | | /** |
| | | * 通用请求处理 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @RestController |
| | |
| | | @Autowired |
| | | private DownLoadFileService downLoadFileService; |
| | | |
| | | |
| | | @Autowired |
| | | private VideoProcessService videoProcessService; |
| | | |
| | | private static final String FILE_DELIMETER = ","; |
| | | |
| | | private static final Pattern CHINESE_PATTERN = Pattern.compile("[\u4e00-\u9fa5]"); |
| | | |
| | | // 缩略图配置 |
| | | @Value("${thumbnail.default-width:300}") |
| | | private int defaultThumbnailWidth; |
| | | |
| | | // @GetMapping("/downloadFile") |
| | | @Value("${thumbnail.default-height:200}") |
| | | private int defaultThumbnailHeight; |
| | | |
| | | @Value("${thumbnail.quality:0.8}") |
| | | private double thumbnailQuality; |
| | | |
| | | @Value("${thumbnail.cache-dir:./cache/thumbnails}") |
| | | private String thumbnailCacheDir; |
| | | |
| | | |
| | | @Value("${thumbnail.max-width:1920}") |
| | | private int maxThumbnailWidth; |
| | | |
| | | @Value("${thumbnail.max-height:1080}") |
| | | private int maxThumbnailHeight; |
| | | |
| | | @Value("${thumbnail.format:jpg}") |
| | | private String thumbnailFormat; |
| | | |
| | | @Value("${thumbnail.keep-aspect-ratio:true}") |
| | | private boolean keepAspectRatio; |
| | | |
| | | // 支持的图片格式 |
| | | private static final String[] IMAGE_FORMATS = {"jpg", "jpeg", "png", "gif", "bmp", "webp"}; |
| | | |
| | | // 支持的视频格式 |
| | | private static final String[] VIDEO_FORMATS = {"mp4", "avi", "mov", "wmv", "flv", "mkv", "webm"}; |
| | | @Autowired |
| | | private NotNullAnnotationPlugin notNullPlugin; |
| | | |
| | | @Anonymous |
| | | @GetMapping("/generateThumbnail") |
| | | public AjaxResult generateThumbnail(@PathParam(value = "url") String url) throws Exception { |
| | | |
| | | return AjaxResult.success( ); |
| | | } |
| | | // @GetMapping("/downloadFile") |
| | | // public void fileDownload(@PathParam("path") String path, HttpServletResponse response) |
| | | // { |
| | | // path=path.substring(8); |
| | |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 压缩图片或提取视频封面并返回Base64 |
| | | * |
| | | * @param file 图片或视频文件 |
| | | * @param width 目标宽度,默认100 |
| | | * @param height 目标高度,默认100 |
| | | * @param quality 图片质量 0.1-1.0,默认0.8 |
| | | * @return Map包含压缩结果和Base64 |
| | | */ |
| | | @Anonymous |
| | | @PostMapping(value = "/image/zip", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
| | | public Map<String, Object> compressMediaToBase64( |
| | | @RequestParam(value = "file", required = false) MultipartFile file, |
| | | @RequestParam(value = "filePath", required = false) String filePath, |
| | | @RequestParam(value = "width", defaultValue = "0") int width, |
| | | @RequestParam(value = "height", defaultValue = "0") int height, |
| | | @RequestParam(value = "quality", defaultValue = "0.8") float quality) { |
| | | Map<String, Object> result = new HashMap<>(); |
| | | |
| | | File trueFile; |
| | | boolean deleteY = true; |
| | | |
| | | try { |
| | | // 验证文件 |
| | | if ((file == null || file.isEmpty()) && filePath == null ) { |
| | | result.put("success", false); |
| | | result.put("message", "文件不能为空"); |
| | | return result; |
| | | } |
| | | |
| | | if ((file == null || file.isEmpty())){ |
| | | deleteY = false; |
| | | String fileUel = RuoYiConfig.getProfile() + filePath.replace("/profile",""); |
| | | trueFile = new File(fileUel); |
| | | }else { |
| | | trueFile = videoProcessService.convertToFile(file); |
| | | } |
| | | String fileName = trueFile.getName(); |
| | | if (fileName.isEmpty() && filePath == null) { |
| | | result.put("success", false); |
| | | result.put("message", "文件名不能为空"); |
| | | return result; |
| | | } |
| | | |
| | | System.out.println("-----------"+fileName); |
| | | |
| | | // 获取文件扩展名 |
| | | String extension = getFileExtension(fileName).toLowerCase(); |
| | | System.out.println("-----------"+extension); |
| | | |
| | | // 判断文件类型 |
| | | if (isImageFile(extension)) { |
| | | // 处理图片文件 |
| | | return videoProcessService.processImage(trueFile, width, height, quality, extension,deleteY); |
| | | } else if (isVideoFile(extension)) { |
| | | // 处理视频文件 |
| | | return videoProcessService.processVideo(trueFile,width,height,quality,extension,deleteY); |
| | | } else { |
| | | result.put("success", false); |
| | | result.put("message", "不支持的文件格式:" + extension); |
| | | return result; |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("文件处理失败", e); |
| | | result.put("success", false); |
| | | result.put("message", "处理失败: " + e.getMessage()); |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断是否是图片文件 |
| | | */ |
| | | private boolean isImageFile(String extension) { |
| | | for (String format : IMAGE_FORMATS) { |
| | | if (format.equalsIgnoreCase(extension)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 判断是否是视频文件 |
| | | */ |
| | | private boolean isVideoFile(String extension) { |
| | | for (String format : VIDEO_FORMATS) { |
| | | if (format.equalsIgnoreCase(extension)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取文件扩展名 |
| | | */ |
| | | private String getFileExtension(String fileName) { |
| | | if (StringUtils.isEmpty(fileName)) { |
| | | return ""; |
| | | } |
| | | |
| | | int lastDot = fileName.lastIndexOf('.'); |
| | | if (lastDot > 0 && lastDot < fileName.length() - 1) { |
| | | return fileName.substring(lastDot + 1).toLowerCase(); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | |
| | | |
| | | } |