修改获取歌曲时长和判断文件是否存在s3

main
JH 10 months ago
parent d92cba6b06
commit 15cebc3e42

@ -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) {

@ -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

Loading…
Cancel
Save