diff --git a/mall-admin/src/main/java/com/suisung/mall/admin/oss/service/impl/OssServiceImpl.java b/mall-admin/src/main/java/com/suisung/mall/admin/oss/service/impl/OssServiceImpl.java index e3feff36..0867d7a4 100644 --- a/mall-admin/src/main/java/com/suisung/mall/admin/oss/service/impl/OssServiceImpl.java +++ b/mall-admin/src/main/java/com/suisung/mall/admin/oss/service/impl/OssServiceImpl.java @@ -174,78 +174,92 @@ public class OssServiceImpl implements OssService { String url = null; Integer uploadType = configService.getConfig("upload", 1); - if (uploadType.equals(1)) { - url = ConfigConstant.URL_BASE + "/admin/oss/upload/" + dir + "/" + uploadName; // 文件本地路径 - } else if (uploadType.equals(2)) { - // oss 服务 - try { - url = uploadObject2OSS(new File(uploadPath), ALIYUN_OSS_DIR_PREFIX.concat("/").concat(dir).concat(uploadName)); - } catch (Exception e) { - e.printStackTrace(); - logger.error("文件上传失败!", e); - return null; - } - } else if (uploadType.equals(4)) { - url = uploadObject4OSS(new File(uploadPath), TENGXUN_DEFAULT_DIR.concat("/").concat(dir).concat("/").concat(uploadName)); - } - - String media_duration = ""; try { - media_duration = VideoUtil.getFormatDuration(uploadPath); - } catch (IOException e) { - throw new ApiException(String.format(I18nUtil._("解析音视频时长异常!url【%s】"), uploadPath)); - } - - //截图 - //todo 放入upload中 - String thumb_file_url = ""; - if (VideoUtil.videoAllowFiles.contains(VideoUtil.getVideoFormat(uploadPath))) { - String cover_path = uploadPath.replace("." + VideoUtil.getVideoFormat(uploadPath), ".jpg"); - String cover_upname = uploadName.replace("." + VideoUtil.getVideoFormat(uploadName), ".jpg"); - - if (VideoUtil.getVideoCover(uploadPath, cover_path, 1, "375*667")) { + if (uploadType.equals(1)) { + url = ConfigConstant.URL_BASE + "/admin/oss/upload/" + dir + "/" + uploadName; // 文件本地路径 + } else if (uploadType.equals(2)) { + // oss 服务 try { - thumb_file_url = uploadObject2OSS(new File(cover_path), ALIYUN_OSS_DIR_PREFIX.concat("/").concat(dir).concat("/").concat(cover_upname)); + url = uploadObject2OSS(new File(uploadPath), ALIYUN_OSS_DIR_PREFIX.concat("/").concat(dir).concat(uploadName)); } catch (Exception e) { e.printStackTrace(); + logger.error("文件上传失败!", e); + return null; + } + } else if (uploadType.equals(4)) { + url = uploadObject4OSS(new File(uploadPath), TENGXUN_DEFAULT_DIR.concat("/").concat(dir).concat("/").concat(uploadName)); + } + String media_duration = ""; + try { + media_duration = VideoUtil.getFormatDuration(uploadPath); + } catch (IOException e) { + throw new ApiException(String.format(I18nUtil._("解析音视频时长异常!url【%s】"), uploadPath)); + } + + //截图 + //todo 放入upload中 + String thumb_file_url = ""; + if (VideoUtil.videoAllowFiles.contains(VideoUtil.getVideoFormat(uploadPath))) { + String cover_path = uploadPath.replace("." + VideoUtil.getVideoFormat(uploadPath), ".jpg"); + String cover_upname = uploadName.replace("." + VideoUtil.getVideoFormat(uploadName), ".jpg"); + + if (VideoUtil.getVideoCover(uploadPath, cover_path, 1, "375*667")) { + try { + thumb_file_url = uploadObject2OSS(new File(cover_path), ALIYUN_OSS_DIR_PREFIX.concat("/").concat(dir).concat("/").concat(cover_upname)); + } catch (Exception e) { + e.printStackTrace(); + + thumb_file_url = ""; + } + } else { thumb_file_url = ""; } - } else { - thumb_file_url = ""; + } + + // 保存文件信息 + MediaDTO mediaDTO = new MediaDTO(); + String name = file.getOriginalFilename(); + long size = file.getSize(); + + mediaDTO.setMedia_name(name); + mediaDTO.setMedia_alt(name); + mediaDTO.setMedia_size(size); + mediaDTO.setMedia_url(url); + mediaDTO.setMedia_order(1); + mediaDTO.setMedia_path("/".concat(dir).concat("/").concat(uploadName)); + mediaDTO.setMedia_domain(getOssUrlPrefix().concat(ALIYUN_OSS_DIR_PREFIX)); + mediaDTO.setMedia_time(new Date()); + mediaDTO.setMedia_desc(""); + mediaDTO.setMedia_duration(media_duration); + + shopStoreMediaService.saveMedia(mediaDTO, user); + Map result = new HashMap<>(); + result.put("media_duration", media_duration); + result.put("media_url", url); + result.put("thumb", thumb_file_url); + + return result; + } finally { + // 删除临时文件 + try { + File tempFile = new File(uploadPath); + if (tempFile.exists()) { + tempFile.delete(); + logger.info("临时文件已删除: {}", uploadPath); + } + + // 同时删除可能创建的封面文件 + String coverPath = uploadPath.replace("." + VideoUtil.getVideoFormat(uploadPath), ".jpg"); + File coverFile = new File(coverPath); + if (coverFile.exists()) { + coverFile.delete(); + logger.info("临时封面文件已删除: {}", coverPath); + } + } catch (Exception e) { + logger.warn("删除临时文件失败: {}", uploadPath, e); } } - - // 保存文件信息 - MediaDTO mediaDTO = new MediaDTO(); - String name = file.getOriginalFilename(); - long size = file.getSize(); - - mediaDTO.setMedia_name(name); - mediaDTO.setMedia_alt(name); - mediaDTO.setMedia_size(size); - mediaDTO.setMedia_url(url); - mediaDTO.setMedia_order(1); - mediaDTO.setMedia_path("/".concat(dir).concat("/").concat(fileName)); - - if (uploadType.equals(1)) { - mediaDTO.setMedia_domain(ConfigConstant.URL_BASE); - } else if (uploadType.equals(2)) { - mediaDTO.setMedia_domain(getOssUrlPrefix().concat(ALIYUN_OSS_DIR_PREFIX)); - } else if (uploadType.equals(4)) { - mediaDTO.setMedia_domain(TENGXUN_ENDPOINT.concat("/").concat(TENGXUN_DEFAULT_DIR)); - } - mediaDTO.setMedia_time(new Date()); - mediaDTO.setMedia_desc(""); - mediaDTO.setMedia_duration(media_duration); - - shopStoreMediaService.saveMedia(mediaDTO, user); - Map result = new HashMap<>(); - result.put("media_duration", media_duration); - result.put("media_url", url); - result.put("thumb", thumb_file_url); - - return result; } public Map upload(MultipartFile file, UserDto user) { diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/page/service/impl/OssServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/page/service/impl/OssServiceImpl.java index a8e90b22..c689ae78 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/page/service/impl/OssServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/page/service/impl/OssServiceImpl.java @@ -182,78 +182,91 @@ public class OssServiceImpl implements OssService { String ossUrl = null; Integer uploadType = configService.getConfig("upload", 1); - if (uploadType.equals(1)) { - ossUrl = ConfigConstant.URL_BASE + "/admin/oss/upload/" + dir + "/" + uploadName; // 文件本地路径 - } else if (uploadType.equals(2)) { - // oss 服务 - try { - ossUrl = uploadObject2OSS(new File(uploadPath), ALIYUN_OSS_DIR_PREFIX.concat("/").concat(dir).concat("/").concat(uploadName), null, null, null); - } catch (Exception e) { - e.printStackTrace(); - logger.error("文件上传失败!", e); - return null; - } - } else if (uploadType.equals(4)) { - ossUrl = uploadObject4OSS(new File(uploadPath), TENGXUN_DEFAULT_DIR.concat("/").concat(dir).concat("/").concat(uploadName)); - } - - String media_duration = ""; try { - media_duration = VideoUtil.getFormatDuration(uploadPath); - } catch (IOException e) { - throw new ApiException(String.format(I18nUtil._("解析音视频时长异常!url【%s】"), uploadPath)); - } - - //截图 - //todo 放入upload中 - String thumb_file_url = ""; - if (VideoUtil.videoAllowFiles.contains(VideoUtil.getVideoFormat(uploadPath))) { - // String cover_path = uploadPath.replace("." + VideoUtil.getVideoFormat(uploadPath), ".jpg"); - String cover_upname = uploadName.replace("." + VideoUtil.getVideoFormat(uploadName), ".jpg"); - - String floder = ALIYUN_OSS_DIR_PREFIX.concat("/") + dir + "/video/frame1/"; - try { - thumb_file_url = videoUtil.getVideoCoverV2(ossUrl, floder, cover_upname); - } catch (Exception e) { - logger.error("获取视频封面失败: ", e); - thumb_file_url = ""; - } - /*if (VideoUtil.getVideoCover(uploadPath, cover_path, 1, "375*667")) { + if (uploadType.equals(1)) { + ossUrl = ConfigConstant.URL_BASE + "/admin/oss/upload/" + dir + "/" + uploadName; // 文件本地路径 + } else if (uploadType.equals(2)) { + // oss 服务 try { - thumb_file_url = uploadObject2OSS(new File(cover_path), ALIYUN_OSS_DIR_PREFIX.concat("/").concat(dir).concat(cover_upname), null, null, null); + ossUrl = uploadObject2OSS(new File(uploadPath), ALIYUN_OSS_DIR_PREFIX.concat("/").concat(dir).concat("/").concat(uploadName), null, null, null); } catch (Exception e) { e.printStackTrace(); + logger.error("文件上传失败!", e); + return null; + } + } else if (uploadType.equals(4)) { + ossUrl = uploadObject4OSS(new File(uploadPath), TENGXUN_DEFAULT_DIR.concat("/").concat(dir).concat("/").concat(uploadName)); + } + String media_duration = ""; + try { + media_duration = VideoUtil.getFormatDuration(uploadPath); + } catch (IOException e) { + throw new ApiException(String.format(I18nUtil._("解析音视频时长异常!url【%s】"), uploadPath)); + } + + //截图 + //todo 放入upload中 + String thumb_file_url = ""; + if (VideoUtil.videoAllowFiles.contains(VideoUtil.getVideoFormat(uploadPath))) { + // String cover_path = uploadPath.replace("." + VideoUtil.getVideoFormat(uploadPath), ".jpg"); + String cover_upname = uploadName.replace("." + VideoUtil.getVideoFormat(uploadName), ".jpg"); + + String floder = ALIYUN_OSS_DIR_PREFIX.concat("/") + dir + "/video/frame1/"; + try { + thumb_file_url = videoUtil.getVideoCoverV2(ossUrl, floder, cover_upname); + } catch (Exception e) { + logger.error("获取视频封面失败: ", e); thumb_file_url = ""; } - } else { - thumb_file_url = ""; - }*/ + /*if (VideoUtil.getVideoCover(uploadPath, cover_path, 1, "375*667")) { + try { + thumb_file_url = uploadObject2OSS(new File(cover_path), ALIYUN_OSS_DIR_PREFIX.concat("/").concat(dir).concat(cover_upname), null, null, null); + } catch (Exception e) { + e.printStackTrace(); + + thumb_file_url = ""; + } + } else { + thumb_file_url = ""; + }*/ + } + + // 保存文件信息 + MediaDTO mediaDTO = new MediaDTO(); + String name = file.getOriginalFilename(); + long size = file.getSize(); + + mediaDTO.setMedia_name(name); + mediaDTO.setMedia_alt(name); + mediaDTO.setMedia_size(size); + mediaDTO.setMedia_url(ossUrl); + mediaDTO.setMedia_order(1); + mediaDTO.setMedia_path("/".concat(dir).concat("/").concat(uploadName)); + mediaDTO.setMedia_domain(getOssUrlPrefix().concat(ALIYUN_OSS_DIR_PREFIX)); + mediaDTO.setMedia_time(new Date()); + mediaDTO.setMedia_desc(""); + mediaDTO.setMedia_duration(media_duration); + + shopStoreMediaService.saveMedia(mediaDTO, user); + Map result = new HashMap<>(); + result.put("media_duration", media_duration); + result.put("media_url", ossUrl); + result.put("thumb", thumb_file_url); + + return result; + } finally { + // 删除临时文件 + try { + File tempFile = new File(uploadPath); + if (tempFile.exists()) { + tempFile.delete(); + logger.info("临时文件已删除: {}", uploadPath); + } + } catch (Exception e) { + logger.warn("删除临时文件失败: {}", uploadPath, e); + } } - - // 保存文件信息 - MediaDTO mediaDTO = new MediaDTO(); - String name = file.getOriginalFilename(); - long size = file.getSize(); - - mediaDTO.setMedia_name(name); - mediaDTO.setMedia_alt(name); - mediaDTO.setMedia_size(size); - mediaDTO.setMedia_url(ossUrl); - mediaDTO.setMedia_order(1); - mediaDTO.setMedia_path("/".concat(dir).concat("/").concat(fileName)); - mediaDTO.setMedia_domain(getOssUrlPrefix().concat(ALIYUN_OSS_DIR_PREFIX)); - mediaDTO.setMedia_time(new Date()); - mediaDTO.setMedia_desc(""); - mediaDTO.setMedia_duration(media_duration); - - shopStoreMediaService.saveMedia(mediaDTO, user); - Map result = new HashMap<>(); - result.put("media_duration", media_duration); - result.put("media_url", ossUrl); - result.put("thumb", thumb_file_url); - - return result; } public Map upload(MultipartFile file, UserDto user) {