From 580e31241f5c7c618b1b8c564ffbfdd8c18bcae5 Mon Sep 17 00:00:00 2001 From: JH <935007339@qq.com> Date: Thu, 25 Jan 2024 23:27:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=8C=E6=9B=B2=E6=AD=8C=E8=AF=8D=E7=9A=84?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../music/controller/CMSSongController.java | 6 ++ .../com/luoo/music/dao/JournalSongDao.java | 5 +- .../luoo/music/service/CMSJournalService.java | 11 ++- .../luoo/music/service/CMSSongService.java | 69 ++++++++++++--- .../com/luoo/music/service/S3Service.java | 88 +++++++++++++++++++ 5 files changed, 165 insertions(+), 14 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/controller/CMSSongController.java b/luoo_music/src/main/java/com/luoo/music/controller/CMSSongController.java index f4907bc..d2d5698 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/CMSSongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/CMSSongController.java @@ -64,6 +64,12 @@ public class CMSSongController { return songService.updateSongState(id, state); } + @ApiOperation(value = "获取歌曲歌词", notes = "获取歌曲歌词") + @RequestMapping(value="/lyric/{id}",method= RequestMethod.GET) + public Result querySongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ + return songService.querySongLyric(id); + } + @ApiOperation(value = "更新歌曲歌词", notes = "更新歌曲歌词") @RequestMapping(value="/update/lyric/{id}",method= RequestMethod.PUT) public Result updateSongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id, diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java index 2567c31..a66fd04 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java @@ -29,7 +29,10 @@ public interface JournalSongDao extends JpaRepository, JpaSp List findJournalBySongId(@Param("songId") String songId); @Query("SELECT js FROM JournalSong js WHERE js.songId IN :songIds") - List findBySongId(@Param("songIds") List songIds); + List findBySongIds(@Param("songIds") List songIds); + + @Query("SELECT js FROM JournalSong js WHERE js.songId = :songId") + List findBySongId(@Param("songId") String songId); @Modifying @Query("DELETE FROM JournalSong js WHERE js.songId = :songId") diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java index c94d4c8..ac3f3ff 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java @@ -224,8 +224,7 @@ public class CMSJournalService { String image = param.getImage(); String srcKey = image.substring(image.indexOf(Constants.TEMP_KEY_PREFIX)); String suffix = image.substring(image.lastIndexOf(Constants.DOT)); - String format = String.format("%05d", param.getJournalNo()); - String destKeySuffix = format + "/00" + suffix; + String destKeySuffix = String.format("%05d/00%s", param.getJournalNo(), suffix); String destKey = Constants.IMAGE_KEY_PREFIX + destKeySuffix; int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); if (copy > 0) { @@ -296,6 +295,7 @@ public class CMSJournalService { journalSongList.add(journalSong); } } + s3Service.fromSongToMusic(journalSongList); journalSongDao.saveAll(journalSongList); } } @@ -331,10 +331,15 @@ public class CMSJournalService { for (JournalSong item : journalSongList) { SongVO songVO = new SongVO(); songVO.setId(item.getSongId()); - songVO.setImage(Constants.RESOURCE_PREFIX + Constants.MUSIC_KEY_PREFIX + item.getImage()); songVO.setName(item.getName()); songVO.setArtist(item.getArtist()); 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); } } diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSSongService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSSongService.java index 876dff1..9548cb8 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSSongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSSongService.java @@ -9,14 +9,8 @@ import com.luoo.music.request.cms.SongQueryModel; import com.luoo.music.response.cms.SongVO; import com.luoo.music.util.CommonUtil; import com.luoo.music.util.Constants; -import com.luoo.music.util.UploadUtil; import dto.UserLoginDto; 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.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -31,9 +25,9 @@ import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDateTime; @@ -146,7 +140,7 @@ public class CMSSongService { private Map> buildSongJournalInfo(List ids) { Map> result = new HashMap<>(); - List bySongId = journalSongDao.findBySongId(ids); + List bySongId = journalSongDao.findBySongIds(ids); if (!CollectionUtils.isEmpty(bySongId)) { Set idSet = bySongId.stream().map(JournalSong::getJournalNo).collect(Collectors.toSet()); List journalList = journalDao.findByJournalNoIn(idSet); @@ -354,6 +348,28 @@ public class CMSSongService { return Result.success("更新成功"); } + /** + * 获取歌曲歌词 + * @param id + * @return + */ + public Result 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 @@ -362,6 +378,17 @@ public class CMSSongService { */ public Result updateSongLyric(String id, String 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 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("更新成功"); } @@ -407,4 +434,26 @@ public class CMSSongService { } 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; + } + } } 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 72fe111..1dbbf44 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 @@ -1,9 +1,11 @@ package com.luoo.music.service; import api.Result; +import com.luoo.music.pojo.JournalSong; import com.luoo.music.response.cms.UploadRespDto; import com.luoo.music.util.CommonUtil; import com.luoo.music.util.Constants; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -19,6 +21,7 @@ import util.IdWorker; import javax.annotation.Resource; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.List; @Service @@ -29,6 +32,39 @@ public class S3Service { @Autowired private IdWorker idWorker; + + /** + * 期刊添加音乐时复制音乐的文件 + * @param journalSongList + */ + public void fromSongToMusic(List 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 * @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 * @param bucket @@ -109,4 +170,31 @@ public class S3Service { RequestBody requestBody = RequestBody.fromInputStream(new ByteArrayInputStream(buffer), buffer.length); 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()); + } + }*/ }