1.reduce music jar size

main
Gary 8 months ago
parent a13ead3fd3
commit b03294dbf4

@ -55,11 +55,11 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 解析音乐文件获取音乐时长 -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.mpatric</groupId>
<artifactId>mp3agic</artifactId>
<version>0.9.1</version>
</dependency>
<!-- s3 sdk -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
@ -103,6 +103,13 @@
</exclusions>
</dependency>
</dependencies>
<repositories>
<repository>
<id>ICM</id>
<name>ICM</name>
<url>http://maven.icm.edu.pl/artifactory/repo/</url>
</repository>
</repositories>
<build>
<finalName>app</finalName>
<plugins>

@ -1,8 +1,10 @@
package com.luoo.music.util;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.MultimediaObject;
import com.mpatric.mp3agic.Mp3File;
import lombok.SneakyThrows;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
@ -26,8 +28,7 @@ public class CommonUtil {
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();
long duration = getDuration(tempFile);
tempFile.delete();
return duration;
} catch (Exception e) {
@ -35,7 +36,10 @@ public class CommonUtil {
return 0;
}
}
@SneakyThrows
public static long getDuration(File file) {
return new Mp3File(file).getLengthInMilliseconds();
}
/**
*
* @param onlineAudioURL
@ -58,8 +62,7 @@ public class CommonUtil {
outputStream.write(buffer, 0, bytesRead);
}
}
MultimediaObject multimediaObject = new MultimediaObject(tempFile);
long duration = multimediaObject.getInfo().getDuration();
long duration = getDuration(tempFile);
tempFile.delete();
return duration;
} catch (Exception e) {

@ -0,0 +1,21 @@
package com.luoo.music.util;
import java.io.File;
import org.junit.Test;
import org.springframework.web.multipart.MultipartFile;
public class CommonUtilTest {
@Test
public void test() {
MultipartFile file = getMultipartFile();
long duration = CommonUtil.getSongDuration(file);
System.out.println(duration);
String format = CommonUtil.formatSongDuration(duration);
System.out.println(format);
}
private MultipartFile getMultipartFile() {
return new FileToMultipartFile(new File("src/test/resources/song_1_1.mp3"));
}
}

@ -0,0 +1,68 @@
package com.luoo.music.util;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
public class FileToMultipartFile implements MultipartFile {
private final File file;
public FileToMultipartFile(File file) {
this.file = file;
}
@Override
public String getName() {
return file.getName();
}
@Override
public String getOriginalFilename() {
return file.getName();
}
@Override
public String getContentType() {
return "application/octet-stream";
}
@Override
public boolean isEmpty() {
return file.length() == 0;
}
@Override
public long getSize() {
return file.length();
}
@Override
public byte[] getBytes() throws IOException {
try (InputStream is = new FileInputStream(file)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return baos.toByteArray();
}
}
@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(file);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
try (InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(dest)) {
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
}
}
}
Loading…
Cancel
Save