|
|
@@ -0,0 +1,233 @@
|
|
|
+package top.lvzhiqiang.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.mpatric.mp3agic.InvalidDataException;
|
|
|
+import com.mpatric.mp3agic.Mp3File;
|
|
|
+import com.mpatric.mp3agic.UnsupportedTagException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jsoup.Connection;
|
|
|
+import org.jsoup.Jsoup;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import top.lvzhiqiang.config.InitRunner;
|
|
|
+import top.lvzhiqiang.dto.R;
|
|
|
+import top.lvzhiqiang.entity.FileImage;
|
|
|
+import top.lvzhiqiang.entity.FileMusicCollection;
|
|
|
+import top.lvzhiqiang.enumeration.ResultCodeEnum;
|
|
|
+import top.lvzhiqiang.exception.BusinessException;
|
|
|
+import top.lvzhiqiang.mapper.MusicInfoMapper;
|
|
|
+import top.lvzhiqiang.service.MusicInfoService;
|
|
|
+import top.lvzhiqiang.util.DateUtils;
|
|
|
+import top.lvzhiqiang.util.FtpUtil;
|
|
|
+import top.lvzhiqiang.util.StringUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 音乐信息ServiceImpl
|
|
|
+ *
|
|
|
+ * @author lvzhiqiang
|
|
|
+ * 2024/9/25 10:47
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class MusicInfoServiceImpl extends BaseServiceImpl<Object> implements MusicInfoService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MusicInfoMapper musicInfoMapper;
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
+ private String env;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getMusicInfoPage(Map<String, Object> params) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R insertOrUpdateImg(MultipartFile file, String remark, String categoryId, String id, String title, String singer, String issuingDate, String collectionDate, String qualityType) {
|
|
|
+ String imageUrl = "";
|
|
|
+ String imageSize = "";
|
|
|
+ String ftpBasePath = InitRunner.dicCodeMap.get("ftp_music_basepath").getCodeValue();
|
|
|
+ String ftpBaseUrl = InitRunner.dicCodeMap.get("ftp_baseurl").getCodeValue();
|
|
|
+ if (StringUtils.isEmpty(id)) {
|
|
|
+ FileMusicCollection fileMusicCollection = new FileMusicCollection();
|
|
|
+ String parentPath = StringUtils.isEmpty(issuingDate) ? String.valueOf(LocalDate.now().getYear()) : issuingDate.substring(0, 4);
|
|
|
+ try {
|
|
|
+ if (file != null && "low".equals(qualityType)) {
|
|
|
+ // 1、给上传的图片生成新的文件名
|
|
|
+ // 1.1获取原始文件名
|
|
|
+ String oldName = file.getOriginalFilename();
|
|
|
+ // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
|
|
|
+ String newName = FtpUtil.genImageName();
|
|
|
+ newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
+ // 2、把图片上传到图片服务器
|
|
|
+ // 2.1获取上传的io流
|
|
|
+ InputStream input = file.getInputStream();
|
|
|
+
|
|
|
+ // 2.2调用FtpUtil工具类进行上传
|
|
|
+ boolean result = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
|
|
|
+
|
|
|
+ if (result) {
|
|
|
+ File tempFile = null;
|
|
|
+ try {
|
|
|
+ tempFile = File.createTempFile("tempMp3", ".mp3");
|
|
|
+ file.transferTo(tempFile); // 将 MultipartFile 保存到临时文件
|
|
|
+ Mp3File mp3 = new Mp3File(tempFile.getAbsolutePath());
|
|
|
+ long durationInMillis = mp3.getLengthInMilliseconds(); // 时长(毫秒)
|
|
|
+ // 转换为分:秒格式
|
|
|
+ long seconds = durationInMillis / 1000; // 转换为秒
|
|
|
+ long minutes = seconds / 60; // 分钟
|
|
|
+ seconds = seconds % 60; // 剩余的秒数
|
|
|
+ fileMusicCollection.setDuration(String.format("%02d:%02d", minutes, seconds));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Mp3File process fail", e);
|
|
|
+ } finally {
|
|
|
+ // 删除临时文件
|
|
|
+ if (tempFile != null && tempFile.exists()) {
|
|
|
+ tempFile.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1048576")).setScale(0, RoundingMode.UP).toPlainString().concat("MB");
|
|
|
+ fileMusicCollection.setSize(imageSize);
|
|
|
+ // 返回给前端图片访问路径
|
|
|
+ imageUrl = parentPath + "/" + newName;
|
|
|
+ fileMusicCollection.setLowQualityUrl(imageUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fileMusicCollection.setTitle(title);
|
|
|
+ fileMusicCollection.setCategoryId(categoryId);
|
|
|
+ fileMusicCollection.setSinger(singer);
|
|
|
+ fileMusicCollection.setIssuingDate(StringUtils.isEmpty(issuingDate) ? null : LocalDate.parse(issuingDate, DateUtils.dateFormatter));
|
|
|
+ fileMusicCollection.setRemark(remark);
|
|
|
+ fileMusicCollection.setCollectionDate(StringUtils.isEmpty(collectionDate) ? null : LocalDate.parse(collectionDate, DateUtils.dateFormatter));
|
|
|
+
|
|
|
+ musicInfoMapper.insertFileMusic(fileMusicCollection);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("insertOrUpdateMusic Exception,fileMusicCollection={}", fileMusicCollection, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.put("imageUrl", ftpBaseUrl + ftpBasePath + imageUrl);
|
|
|
+ result.put("imageSize", imageSize);
|
|
|
+
|
|
|
+ return R.ok().data(result);
|
|
|
+ } else {
|
|
|
+ FileMusicCollection fileMusicCollection = musicInfoMapper.findFileMusicCollectionById(Long.valueOf(id));
|
|
|
+ if (fileMusicCollection == null) {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ fileMusicCollection.setCategoryId(categoryId);
|
|
|
+ if (StringUtils.isNotEmpty(remark)) {
|
|
|
+ fileMusicCollection.setRemark(remark);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(title)) {
|
|
|
+ fileMusicCollection.setTitle(title);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(singer)) {
|
|
|
+ fileMusicCollection.setSinger(singer);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(issuingDate)) {
|
|
|
+ fileMusicCollection.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter));
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(collectionDate)) {
|
|
|
+ fileMusicCollection.setCollectionDate(LocalDate.parse(collectionDate, DateUtils.dateFormatter));
|
|
|
+ }
|
|
|
+
|
|
|
+ String parentPath = StringUtils.isEmpty(issuingDate) && fileMusicCollection.getIssuingDate() == null ? String.valueOf(LocalDate.now().getYear()) : issuingDate.substring(0, 4);
|
|
|
+ try {
|
|
|
+ if (file != null && "low".equals(qualityType)) {
|
|
|
+ // 1、给上传的图片生成新的文件名
|
|
|
+ // 1.1获取原始文件名
|
|
|
+ String oldName = file.getOriginalFilename();
|
|
|
+ // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
|
|
|
+ String newName = FtpUtil.genImageName();
|
|
|
+ newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
+ // 2、把图片上传到图片服务器
|
|
|
+ // 2.1获取上传的io流
|
|
|
+ InputStream input = file.getInputStream();
|
|
|
+
|
|
|
+ // 2.2调用FtpUtil工具类进行上传
|
|
|
+ boolean result = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
|
|
|
+ if (result) {
|
|
|
+ FtpUtil.delFile(ftpBasePath + fileMusicCollection.getLowQualityUrl());
|
|
|
+
|
|
|
+ File tempFile = null;
|
|
|
+ try {
|
|
|
+ tempFile = File.createTempFile("tempMp3", ".mp3");
|
|
|
+ file.transferTo(tempFile); // 将 MultipartFile 保存到临时文件
|
|
|
+ Mp3File mp3 = new Mp3File(tempFile.getAbsolutePath());
|
|
|
+ long durationInMillis = mp3.getLengthInMilliseconds(); // 时长(毫秒)
|
|
|
+ // 转换为分:秒格式
|
|
|
+ long seconds = durationInMillis / 1000; // 转换为秒
|
|
|
+ long minutes = seconds / 60; // 分钟
|
|
|
+ seconds = seconds % 60; // 剩余的秒数
|
|
|
+ fileMusicCollection.setDuration(String.format("%02d:%02d", minutes, seconds));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Mp3File process fail", e);
|
|
|
+ } finally {
|
|
|
+ // 删除临时文件
|
|
|
+ if (tempFile != null && tempFile.exists()) {
|
|
|
+ tempFile.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1048576")).setScale(2, RoundingMode.UP).toPlainString().concat("MB");
|
|
|
+ fileMusicCollection.setSize(imageSize);
|
|
|
+ // 返回给前端图片访问路径
|
|
|
+ imageUrl = parentPath + "/" + newName;
|
|
|
+ fileMusicCollection.setLowQualityUrl(imageUrl);
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ musicInfoMapper.updateFileMusicCollection(fileMusicCollection);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("insertOrUpdateMusic Exception,fileMusicCollection={}", fileMusicCollection, e);
|
|
|
+ return R.error().message(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.ok().data("success");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R deleteMusics(Long musicId) {
|
|
|
+ FileMusicCollection fileMusicCollection = musicInfoMapper.findFileMusicCollectionById(musicId);
|
|
|
+ if (fileMusicCollection == null) {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ String ftpBasePath = InitRunner.dicCodeMap.get("ftp_music_basepath").getCodeValue();
|
|
|
+ boolean flag = FtpUtil.delFile(ftpBasePath + fileMusicCollection.getLowQualityUrl());
|
|
|
+ if (flag) {
|
|
|
+ musicInfoMapper.deleteFileMusicCollectionById(musicId);
|
|
|
+ return R.ok();
|
|
|
+ } else {
|
|
|
+ return R.error().message("删除失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return R.error().message(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ String srcUrl = "https://image.baidu.com/search/down?thumburl=https://baidu.com&url=https://tva1.sinaimg.cn/mw690/007Y7SRMly1gmays3w173j30ol16fh8x.jpg";
|
|
|
+ Connection.Response response = Jsoup.connect(srcUrl).method(Connection.Method.GET).ignoreContentType(true).timeout(50 * 1000).execute();
|
|
|
+ byte[] imageBytes = response.bodyAsBytes();
|
|
|
+ System.out.println(imageBytes.length);
|
|
|
+ }
|
|
|
+}
|