歌曲歌词的相关操作

main
JH 10 months ago
parent 0f354723a6
commit 580e31241f

@ -64,6 +64,12 @@ public class CMSSongController {
return songService.updateSongState(id, state); return songService.updateSongState(id, state);
} }
@ApiOperation(value = "获取歌曲歌词", notes = "获取歌曲歌词")
@RequestMapping(value="/lyric/{id}",method= RequestMethod.GET)
public Result<SongVO> querySongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){
return songService.querySongLyric(id);
}
@ApiOperation(value = "更新歌曲歌词", notes = "更新歌曲歌词") @ApiOperation(value = "更新歌曲歌词", notes = "更新歌曲歌词")
@RequestMapping(value="/update/lyric/{id}",method= RequestMethod.PUT) @RequestMapping(value="/update/lyric/{id}",method= RequestMethod.PUT)
public Result updateSongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id, public Result updateSongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id,

@ -29,7 +29,10 @@ public interface JournalSongDao extends JpaRepository<JournalSong,String>, JpaSp
List<String> findJournalBySongId(@Param("songId") String songId); List<String> findJournalBySongId(@Param("songId") String songId);
@Query("SELECT js FROM JournalSong js WHERE js.songId IN :songIds") @Query("SELECT js FROM JournalSong js WHERE js.songId IN :songIds")
List<JournalSong> findBySongId(@Param("songIds") List<String> songIds); List<JournalSong> findBySongIds(@Param("songIds") List<String> songIds);
@Query("SELECT js FROM JournalSong js WHERE js.songId = :songId")
List<JournalSong> findBySongId(@Param("songId") String songId);
@Modifying @Modifying
@Query("DELETE FROM JournalSong js WHERE js.songId = :songId") @Query("DELETE FROM JournalSong js WHERE js.songId = :songId")

@ -224,8 +224,7 @@ public class CMSJournalService {
String image = param.getImage(); String image = param.getImage();
String srcKey = image.substring(image.indexOf(Constants.TEMP_KEY_PREFIX)); String srcKey = image.substring(image.indexOf(Constants.TEMP_KEY_PREFIX));
String suffix = image.substring(image.lastIndexOf(Constants.DOT)); String suffix = image.substring(image.lastIndexOf(Constants.DOT));
String format = String.format("%05d", param.getJournalNo()); String destKeySuffix = String.format("%05d/00%s", param.getJournalNo(), suffix);
String destKeySuffix = format + "/00" + suffix;
String destKey = Constants.IMAGE_KEY_PREFIX + destKeySuffix; String destKey = Constants.IMAGE_KEY_PREFIX + destKeySuffix;
int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey);
if (copy > 0) { if (copy > 0) {
@ -296,6 +295,7 @@ public class CMSJournalService {
journalSongList.add(journalSong); journalSongList.add(journalSong);
} }
} }
s3Service.fromSongToMusic(journalSongList);
journalSongDao.saveAll(journalSongList); journalSongDao.saveAll(journalSongList);
} }
} }
@ -331,10 +331,15 @@ public class CMSJournalService {
for (JournalSong item : journalSongList) { for (JournalSong item : journalSongList) {
SongVO songVO = new SongVO(); SongVO songVO = new SongVO();
songVO.setId(item.getSongId()); songVO.setId(item.getSongId());
songVO.setImage(Constants.RESOURCE_PREFIX + Constants.MUSIC_KEY_PREFIX + item.getImage());
songVO.setName(item.getName()); songVO.setName(item.getName());
songVO.setArtist(item.getArtist()); songVO.setArtist(item.getArtist());
songVO.setAlbum(item.getAlbum()); songVO.setAlbum(item.getAlbum());
if (StringUtils.isNotBlank(item.getUrl())) {
songVO.setUrl(Constants.RESOURCE_PREFIX + Constants.MUSIC_KEY_PREFIX + item.getUrl());
}
if (StringUtils.isNotBlank(item.getImage())) {
songVO.setImage(Constants.RESOURCE_PREFIX + Constants.MUSIC_KEY_PREFIX + item.getImage());
}
list.add(songVO); list.add(songVO);
} }
} }

@ -9,14 +9,8 @@ import com.luoo.music.request.cms.SongQueryModel;
import com.luoo.music.response.cms.SongVO; import com.luoo.music.response.cms.SongVO;
import com.luoo.music.util.CommonUtil; import com.luoo.music.util.CommonUtil;
import com.luoo.music.util.Constants; import com.luoo.music.util.Constants;
import com.luoo.music.util.UploadUtil;
import dto.UserLoginDto; import dto.UserLoginDto;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
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.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@ -31,9 +25,9 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import java.io.File; import java.io.*;
import java.io.FileInputStream; import java.net.HttpURLConnection;
import java.io.InputStream; import java.net.URL;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -146,7 +140,7 @@ public class CMSSongService {
private Map<String, List<String>> buildSongJournalInfo(List<String> ids) { private Map<String, List<String>> buildSongJournalInfo(List<String> ids) {
Map<String, List<String>> result = new HashMap<>(); Map<String, List<String>> result = new HashMap<>();
List<JournalSong> bySongId = journalSongDao.findBySongId(ids); List<JournalSong> bySongId = journalSongDao.findBySongIds(ids);
if (!CollectionUtils.isEmpty(bySongId)) { if (!CollectionUtils.isEmpty(bySongId)) {
Set<String> idSet = bySongId.stream().map(JournalSong::getJournalNo).collect(Collectors.toSet()); Set<String> idSet = bySongId.stream().map(JournalSong::getJournalNo).collect(Collectors.toSet());
List<Journal> journalList = journalDao.findByJournalNoIn(idSet); List<Journal> journalList = journalDao.findByJournalNoIn(idSet);
@ -354,6 +348,28 @@ public class CMSSongService {
return Result.success("更新成功"); return Result.success("更新成功");
} }
/**
*
* @param id
* @return
*/
public Result<SongVO> querySongLyric(String id) {
SongVO result = new SongVO();
SongInfo song = songDao.findById(id).get();
if (StringUtils.isNotBlank(song.getLyric())) {
result.setLyric(song.getLyric());
} else {
if (StringUtils.isNotBlank(song.getUrl())) {
String url = song.getUrl();
String lyricSuffix = url.substring(0, url.lastIndexOf(Constants.DOT)) + ".lyric";
String lyricUrl = Constants.RESOURCE_PREFIX + Constants.SONG_KEY_PREFIX + lyricSuffix;
String lyric = readLyric(lyricUrl);
result.setLyric(lyric);
}
}
return Result.success(result);
}
/** /**
* *
* @param id * @param id
@ -362,6 +378,17 @@ public class CMSSongService {
*/ */
public Result updateSongLyric(String id, String lyric) { public Result updateSongLyric(String id, String lyric) {
songDao.updateSongLyric(id, lyric); songDao.updateSongLyric(id, lyric);
if (StringUtils.isNotBlank(lyric)) {
String key = Constants.SONG_KEY_PREFIX + id + "/00.lyric";
s3Service.uploadText(Constants.BUCKET, key, lyric);
List<JournalSong> bySongId = journalSongDao.findBySongId(id);
for (JournalSong item : bySongId) {
String format = String.format("%05d/%02d", item.getJournalNo(), item.getSongNo());
String destLyricSuffix = format + ".lyric";
String destLyric = Constants.MUSIC_KEY_PREFIX + destLyricSuffix;
s3Service.copy(Constants.BUCKET, key, destLyric);
}
}
return Result.success("更新成功"); return Result.success("更新成功");
} }
@ -407,4 +434,26 @@ public class CMSSongService {
} }
return Result.success("上传成功"); return Result.success("上传成功");
} }
private String readLyric(String lyricUrl) {
try {
// 创建 URL 对象
URL url = new URL(lyricUrl);
// 打开 HTTP 连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法
connection.setRequestMethod("GET");
// 获取输入流并读取文件内容到 StringBuilder
StringBuilder contentBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
contentBuilder.append(line).append("\n");
}
}
return contentBuilder.toString();
} catch (IOException e) {
return null;
}
}
} }

@ -1,9 +1,11 @@
package com.luoo.music.service; package com.luoo.music.service;
import api.Result; import api.Result;
import com.luoo.music.pojo.JournalSong;
import com.luoo.music.response.cms.UploadRespDto; import com.luoo.music.response.cms.UploadRespDto;
import com.luoo.music.util.CommonUtil; import com.luoo.music.util.CommonUtil;
import com.luoo.music.util.Constants; import com.luoo.music.util.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -19,6 +21,7 @@ import util.IdWorker;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List;
@Service @Service
@ -29,6 +32,39 @@ public class S3Service {
@Autowired @Autowired
private IdWorker idWorker; private IdWorker idWorker;
/**
*
* @param journalSongList
*/
public void fromSongToMusic(List<JournalSong> journalSongList) {
for (JournalSong item : journalSongList) {
String tempLyric = null;
String format = String.format("%05d/%02d", item.getJournalNo(), item.getSongNo());
if (StringUtils.isNotBlank(item.getUrl())) {
String srcUrl = item.getUrl();
String destUrlSuffix = format + srcUrl.substring(srcUrl.lastIndexOf(Constants.DOT));
String destUrl = Constants.MUSIC_KEY_PREFIX + destUrlSuffix;
item.setUrl(destUrlSuffix);
copy(Constants.BUCKET, srcUrl, destUrl);
tempLyric = srcUrl;
}
if (StringUtils.isNotBlank(item.getImage())) {
String srcImage = item.getImage();
String destImageSuffix = format + srcImage.substring(srcImage.lastIndexOf(Constants.DOT));
String destImage = Constants.MUSIC_KEY_PREFIX + destImageSuffix;
item.setImage(destImageSuffix);
copy(Constants.BUCKET, srcImage, destImage);
}
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);
}
}
}
/** /**
* @param file * @param file
* @return * @return
@ -57,6 +93,31 @@ public class S3Service {
} }
} }
/**
* s3
* @param bucket
* @param key
* @param content
* @return
*/
public int uploadText(String bucket, String key, String content) {
try {
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucket)
.key(key)
.build();
RequestBody requestBody = RequestBody.fromBytes(content.getBytes());
PutObjectResponse putObjectResponse = s3Client.putObject(putObjectRequest, requestBody);
SdkHttpResponse sdkHttpResponse = putObjectResponse.sdkHttpResponse();
if (!sdkHttpResponse.isSuccessful()) {
return -1;
}
} catch (Exception e) {
return -1;
}
return 1;
}
/** /**
* s3 * s3
* @param bucket * @param bucket
@ -109,4 +170,31 @@ public class S3Service {
RequestBody requestBody = RequestBody.fromInputStream(new ByteArrayInputStream(buffer), buffer.length); RequestBody requestBody = RequestBody.fromInputStream(new ByteArrayInputStream(buffer), buffer.length);
s3Client.putObject(putObjectRequest, requestBody); s3Client.putObject(putObjectRequest, requestBody);
} }
/*public static void main(String[] args) {
final Region region = Region.of("cn-east-1");
AwsBasicCredentials awsBasicCredentials = AwsBasicCredentials.create("GLwHmLTZ05Kw9RyCGJXnIkua", "ynOBIqdNXH5HBgrVA29DTn4cUSh1wAI");
S3Configuration s3Config = S3Configuration.builder().pathStyleAccessEnabled(true).build();
S3Client s3Client = S3Client.builder()
.endpointOverride(URI.create("https://s3.bitiful.net/"))
.credentialsProvider(StaticCredentialsProvider.create(awsBasicCredentials))
.region(region)
.serviceConfiguration(s3Config)
.build();
// 指定 S3 存储桶和文件名
String bucketName = "indie";
String key = "song/1/0.lyric";
String textContent = "Hello, this is some text content.";
try {
RequestBody requestBody = RequestBody.fromBytes(textContent.getBytes());
s3Client.putObject(PutObjectRequest.builder().bucket(bucketName).key(key).build(), requestBody);
System.out.println("Text content successfully stored in " + bucketName + "/" + key);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
}
}*/
} }

Loading…
Cancel
Save