From 15cebc3e42af26a5c99886259c7ff3ebfccc3dc9 Mon Sep 17 00:00:00 2001 From: JH <935007339@qq.com> Date: Fri, 2 Feb 2024 22:58:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96=E6=AD=8C?= =?UTF-8?q?=E6=9B=B2=E6=97=B6=E9=95=BF=E5=92=8C=E5=88=A4=E6=96=AD=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=98=AF=E5=90=A6=E5=AD=98=E5=9C=A8s3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/luoo/music/service/S3Service.java | 64 ++++++++++++++----- .../java/com/luoo/music/util/CommonUtil.java | 58 +++++++++++++---- 2 files changed, 92 insertions(+), 30 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/service/S3Service.java b/luoo_music/src/main/java/com/luoo/music/service/S3Service.java index fbc1d0b..a10d69d 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/S3Service.java +++ b/luoo_music/src/main/java/com/luoo/music/service/S3Service.java @@ -12,10 +12,7 @@ import org.springframework.web.multipart.MultipartFile; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.services.s3.S3Client; -import software.amazon.awssdk.services.s3.model.CopyObjectRequest; -import software.amazon.awssdk.services.s3.model.CopyObjectResponse; -import software.amazon.awssdk.services.s3.model.PutObjectRequest; -import software.amazon.awssdk.services.s3.model.PutObjectResponse; +import software.amazon.awssdk.services.s3.model.*; import util.IdWorker; import javax.annotation.Resource; @@ -56,12 +53,14 @@ public class S3Service { item.setImage(destImageSuffix); copy(Constants.BUCKET, srcImage, destImage); } - /*if (StringUtils.isNotBlank(tempLyric)) { + if (StringUtils.isNotBlank(tempLyric)) { String srcLyric = tempLyric.substring(0, tempLyric.lastIndexOf(Constants.DOT)) + ".lyric"; - String destLyricSuffix = format + srcLyric.substring(srcLyric.lastIndexOf(Constants.DOT)); - String destLyric = Constants.MUSIC_KEY_PREFIX + destLyricSuffix; - copy(Constants.BUCKET, srcLyric, destLyric); - }*/ + if (checkFileExist(Constants.BUCKET, srcLyric)) { + String destLyricSuffix = format + srcLyric.substring(srcLyric.lastIndexOf(Constants.DOT)); + String destLyric = Constants.MUSIC_KEY_PREFIX + destLyricSuffix; + copy(Constants.BUCKET, srcLyric, destLyric); + } + } } } @@ -93,6 +92,27 @@ public class S3Service { } } + /** + * 检测一个文件是否在s3上 + * @param bucket + * @param key + * @return + */ + public boolean checkFileExist(String bucket, String key) { + try { + HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() + .bucket(bucket) + .key(key) + .build(); + s3Client.headObject(headObjectRequest); + return true; + } catch (Exception e) { + return false; + } finally { + s3Client.close(); + } + } + /** * 歌词保存至s3 * @param bucket @@ -114,6 +134,8 @@ public class S3Service { } } catch (Exception e) { return -1; + } finally { + s3Client.close(); } return 1; } @@ -137,11 +159,14 @@ public class S3Service { SdkHttpResponse sdkHttpResponse = putObjectResponse.sdkHttpResponse(); if (!sdkHttpResponse.isSuccessful()) { return -1; + } else { + return 1; } } catch (Exception e) { return -1; + } finally { + s3Client.close(); } - return 1; } /** @@ -152,14 +177,21 @@ public class S3Service { * @return */ public int copy(String bucket, String srcKey, String destKey) { - CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder() - .sourceBucket(bucket).sourceKey(srcKey).destinationBucket(bucket).destinationKey(destKey).build(); - CopyObjectResponse copyObjectResponse = s3Client.copyObject(copyObjectRequest); - SdkHttpResponse sdkHttpResponse = copyObjectResponse.sdkHttpResponse(); - if(!sdkHttpResponse.isSuccessful()){ + try { + CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder() + .sourceBucket(bucket).sourceKey(srcKey).destinationBucket(bucket).destinationKey(destKey).build(); + CopyObjectResponse copyObjectResponse = s3Client.copyObject(copyObjectRequest); + SdkHttpResponse sdkHttpResponse = copyObjectResponse.sdkHttpResponse(); + if (!sdkHttpResponse.isSuccessful()) { + return -1; + } else { + return 1; + } + } catch(Exception e) { return -1; + } finally { + s3Client.close(); } - return 1; } public void uploadAvatar(String bucket, String key, byte[] buffer) { diff --git a/luoo_music/src/main/java/com/luoo/music/util/CommonUtil.java b/luoo_music/src/main/java/com/luoo/music/util/CommonUtil.java index 984aeec..f6a83ac 100644 --- a/luoo_music/src/main/java/com/luoo/music/util/CommonUtil.java +++ b/luoo_music/src/main/java/com/luoo/music/util/CommonUtil.java @@ -1,13 +1,13 @@ package com.luoo.music.util; -import org.apache.tika.metadata.Metadata; -import org.apache.tika.parser.AutoDetectParser; -import org.apache.tika.parser.ParseContext; -import org.apache.tika.parser.Parser; -import org.apache.tika.sax.BodyContentHandler; import org.springframework.web.multipart.MultipartFile; +import ws.schild.jave.MultimediaObject; +import java.io.File; +import java.io.FileOutputStream; import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; import java.text.DecimalFormat; /** @@ -22,15 +22,13 @@ public class CommonUtil { */ public static long getSongDuration(MultipartFile file) { try { - InputStream inputStream = file.getInputStream(); - Parser parser = new AutoDetectParser(); - Metadata metadata = new Metadata(); - BodyContentHandler handler = new BodyContentHandler(); - ParseContext parseContext = new ParseContext(); - parser.parse(inputStream, handler, metadata, parseContext); - String durationString = metadata.get("xmpDM:duration"); - long duration = Long.parseLong(durationString); - inputStream.close(); + String originalFilename = file.getOriginalFilename(); + String suffix = originalFilename.substring(originalFilename.lastIndexOf(Constants.DOT)); + File tempFile = File.createTempFile("downloaded_", suffix); + file.transferTo(tempFile); + MultimediaObject multimediaObject = new MultimediaObject(tempFile); + long duration = multimediaObject.getInfo().getDuration(); + tempFile.delete(); return duration; } catch (Exception e) { e.printStackTrace(); @@ -38,6 +36,38 @@ public class CommonUtil { } } + /** + * 获取歌曲时长 + * @param onlineAudioURL + * @return + */ + public static long getSongDuration(String onlineAudioURL) { + try { + URL url = new URL(onlineAudioURL); + URLConnection connection = url.openConnection(); + // 获取后缀 + String suffix = onlineAudioURL.substring(onlineAudioURL.lastIndexOf(Constants.DOT)); + // 创建临时文件 + File tempFile = File.createTempFile("downloaded_", suffix); + try (InputStream inputStream = connection.getInputStream(); + FileOutputStream outputStream = new FileOutputStream(tempFile)) { + // 将网络文件写入临时文件 + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } + MultimediaObject multimediaObject = new MultimediaObject(tempFile); + long duration = multimediaObject.getInfo().getDuration(); + tempFile.delete(); + return duration; + } catch (Exception e) { + e.printStackTrace(); + } + return 0L; + } + /** * 格式化文件大小格式 * @param size