From 7ec461ff291c377d60caa14d2175e619b60cf670 Mon Sep 17 00:00:00 2001 From: yutou Date: Fri, 4 Dec 2020 14:09:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=BD=91=E9=A1=B5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=20=E6=9B=B4=E6=96=B0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yutou/tools/ToolsApplication.java | 2 +- .../yutou/tools/home/nas/MusicController.java | 64 +++++----- .../java/com/yutou/tools/utils/Tools.java | 14 ++- web/html/body/nas/music.html | 111 ++++++++++-------- 4 files changed, 106 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/yutou/tools/ToolsApplication.java b/src/main/java/com/yutou/tools/ToolsApplication.java index 800ded5..f0b44de 100644 --- a/src/main/java/com/yutou/tools/ToolsApplication.java +++ b/src/main/java/com/yutou/tools/ToolsApplication.java @@ -9,7 +9,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ToolsApplication { - public static final String version="1.0.12"; + public static final String version="1.0.13"; public static void main(String[] args) { System.out.println("当前版本号:"+version); diff --git a/src/main/java/com/yutou/tools/home/nas/MusicController.java b/src/main/java/com/yutou/tools/home/nas/MusicController.java index 5508184..e9f955f 100644 --- a/src/main/java/com/yutou/tools/home/nas/MusicController.java +++ b/src/main/java/com/yutou/tools/home/nas/MusicController.java @@ -8,6 +8,7 @@ import com.yutou.tools.utils.ConfigTools; import com.yutou.tools.utils.MusicTools; import com.yutou.tools.utils.Tools; import org.springframework.core.io.FileSystemResource; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; @@ -106,6 +107,9 @@ public class MusicController { @ResponseBody public String findFile(@RequestBody JSONObject body) { String path = body.getString("path"); + if(!path.startsWith(defaultMusicPath)){ + path=Tools.base64ToString(path); + } JSONObject json = new JSONObject(); if (StringUtils.isEmpty(path)) { json.put("code", -1); @@ -129,38 +133,29 @@ public class MusicController { json.put("code", 0); return json.toJSONString(); } - - @RequestMapping("image.do") + @RequestMapping(value = "web/image.do",produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody - public String getImage(@RequestBody JSONObject body) { - JSONObject json = new JSONObject(); + public byte[] getImage(String fileName){ + JSONObject json=new JSONObject(); + json.put("fileName",fileName); + return getImage(json); + } + @RequestMapping(value = "image.do",produces = MediaType.IMAGE_JPEG_VALUE) + @ResponseBody + public byte[] getImage(@RequestBody JSONObject body) { String fileName = body.getString("fileName"); - json.put("code", 1); - if (StringUtils.isEmpty(fileName)) { - json.put("msg", "文件为空"); - json.put("code", -1); - json.put("data", ""); - return json.toJSONString(); + if(!fileName.startsWith(defaultMusicPath)){ + fileName=Tools.base64ToString(fileName); } - File file = new File(fileName); - try { - if (file.exists()) { - json.put("msg", "ok"); - json.put("code", 0); - json.put("data", musicTools.readImage(file.getAbsolutePath())); - } else { - json.put("msg", "文件不存在"); - json.put("code", -1); - json.put("data", ""); + File file=new File(fileName); + if(file.exists()){ + try { + return musicTools.readImage(file.getAbsolutePath()); + } catch (Exception e) { + e.printStackTrace(); } - } catch (Exception e) { - json.put("msg", "图片读取失败"); - json.put("code", -1); - json.put("data", ""); - e.printStackTrace(); } - - return json.toJSONString(); + return null; } @RequestMapping("random.do") @@ -182,16 +177,19 @@ public class MusicController { return new String(Base64.getEncoder().encode(str.getBytes())); } + @RequestMapping("play.do") - public ResponseEntity play(@RequestBody JSONObject body) { - String filePath = body.getString("filePath"); - boolean random = body.containsKey("random") ? body.getBoolean("random") : StringUtils.isEmpty(filePath); - if (random) { + public ResponseEntity play(String filePath,String random) { + String _filePath; + boolean _random; + _random= !StringUtils.isEmpty(random) && random.equals("true"); + _filePath=Tools.base64ToString(filePath); + if (_random) { List list = musicTools.getMusicList(); MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]); - filePath = data.getFile(); + _filePath = data.getFile(); } - File file = new File(filePath); + File file = new File(_filePath); if (file.exists()) { return Tools.getFile(file); } else { diff --git a/src/main/java/com/yutou/tools/utils/Tools.java b/src/main/java/com/yutou/tools/utils/Tools.java index 88db304..2be507e 100644 --- a/src/main/java/com/yutou/tools/utils/Tools.java +++ b/src/main/java/com/yutou/tools/utils/Tools.java @@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLDecoder; import java.net.URLEncoder; import java.security.MessageDigest; import java.util.Base64; @@ -374,7 +375,18 @@ public class Tools { } public static String base64ToString(String base){ base=base.replace(" ","+"); - return new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes())); + try { + base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes())); + }catch (Exception e){ + try { + base=URLDecoder.decode(base,"UTF-8"); + base=base.replace(" ","+"); + base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes())); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + return base; } /** * 异常输出 diff --git a/web/html/body/nas/music.html b/web/html/body/nas/music.html index ffee3f4..d165704 100644 --- a/web/html/body/nas/music.html +++ b/web/html/body/nas/music.html @@ -65,10 +65,10 @@ $.get("/nas/music/getlocalhost.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (obj) { try { let json = JSON.parse(obj); - localhost = json.data+":8000"; - console.log("音频地址:"+localhost) - if(localhost==='http://null:8000'||localhost===":8000"){ - localhost="http://127.0.0.1" + localhost = json.data + ":8000"; + console.log("音频地址:" + localhost) + if (localhost === 'http://null:8000' || localhost === ":8000") { + localhost = "http://127.0.0.1" } } catch (e) { localhost = "" @@ -80,23 +80,29 @@ let listTable = table.render({ elem: '#playlist' , url: localhost + '/nas/music/list.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk' //数据接口 + , method: 'post' + , where: { + path: 'root', + type: false + } + , contentType: 'application/json' , page: true //开启分页 , cols: [[ - {field: 'title', title: '标题', width: 400,sort:true, templet: "#listTemplet"} + {field: 'title', title: '标题', width: 400, sort: true, templet: "#listTemplet"} , {field: 'artist', title: '艺术家', width: 200} , {field: 'album', title: '专辑', width: 200} , {field: 'composer', title: '作曲', width: 200} - , {field: 'track', title: '音轨号', width: 100,sort:true} + , {field: 'track', title: '音轨号', width: 100, sort: true} , {field: "right", width: 150, toolbar: '#music'} ]] , done: function (res, curr, count) { musicLib = res.data - $('#musicSize').html("歌单总数:"+res.size) + $('#musicSize').html("歌单总数:" + res.size) } }) table.on('rowDouble(music)', function (obj) { //obj 同上 - if (obj.data.isdir===1) { + if (obj.data.isdir === 1) { listTable.reload({ where: { path: obj.data.file @@ -116,16 +122,16 @@ table.on('tool(music)', function (obj) { let data = obj.data; if (obj.event === 'download') { - if (data.isdir===0) { + if (data.isdir === 0) { window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + new Base64().encode(data.file)) } } else if (obj.event === 'play') { - $.get(localhost + "/nas/music/list.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&type=all&path=" + new Base64().encode(data.file), function (obj) { + $.post(localhost + "/nas/music/list.do", {type: false, path: data.file}, function (obj) { let json = JSON.parse(obj); - if(json.code===0){ - playIndex=0; - musicLib=json.data; - isRandom=false; + if (json.code === 0) { + playIndex = 0; + musicLib = json.data; + isRandom = false; playNext() } @@ -142,7 +148,7 @@ player.init({ container: '#audioWrap' , source: '' - , imagePath: '/assets/' + , imagePath: '/assets' , debuggers: false , allowSeek: true , endedCallback: function () { @@ -168,25 +174,25 @@ $('#download').click(function () { window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + playNow) }); - $('#next').click(function (){ + $('#next').click(function () { playNext() }); - $('#reload').click(function (){ - $.get(localhost+"/nas/music/reload.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk",function (obj){ + $('#reload').click(function () { + $.post(localhost + "/nas/music/reload.do", function (obj) { layer.msg("提交刷新请求成功,几分钟后生效") }) }); function playNext() { - if(isRandom){ + if (isRandom) { random() - return ; + return; } if (playIndex === musicLib.length) { playIndex = 0; } - if (musicLib[playIndex].isdir===1) { + if (musicLib[playIndex].isdir === 1) { playIndex++ playNext() } else { @@ -196,7 +202,7 @@ } function random() { - $.get(localhost + "/nas/music/random.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (data) { + $.post(localhost + "/nas/music/random.do", function (data) { let json = JSON.parse(data) if (json.code === 0) { playNow = json.data @@ -223,36 +229,41 @@ } function update(fileName) { - $.get(localhost + '/nas/music/find/file.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&path=' + fileName, function (data) { - let json = JSON.parse(data); - if(isRandom){ - $('#playerNowList').html("播放列表: (全列表随机播放)") - }else{ - $('#playerNowList').html("播放列表: (" + playIndex+"/"+musicLib.length+")") + $.ajax({ + type: 'post' + , url: localhost + '/nas/music/find/file.do' + , contentType: "application/json" + , data: JSON.stringify({"path": fileName}) + , success: function (data) { + let json = JSON.parse(data); + if (isRandom) { + $('#playerNowList').html("播放列表: (全列表随机播放)") + } else { + $('#playerNowList').html("播放列表: (" + playIndex + "/" + musicLib.length + ")") + } + $('#title').html("标题:" + json.data.title) + $('#artist').html("艺术家:" + json.data.artist) + $('#album').html("专辑:" + json.data.album) + $('#composer').html("作曲:" + json.data.composer) + navigator.mediaSession.metadata = new MediaMetadata({ + title: json.data.title, + artist: json.data.artist, + album: json.data.album, + artwork:[ + { + src: localhost+'/nas/music/web/image.do?fileName='+fileName, + type: 'image/png', + sizes: '128x128' + } + ] + }); + navigator.mediaSession.setActionHandler('nexttrack', function () { + // 用户点击了“下一首”媒体通知图标 + playNext(); + }); } - $('#title').html("标题:" + json.data.title) - $('#artist').html("艺术家:" + json.data.artist) - $('#album').html("专辑:" + json.data.album) - $('#composer').html("作曲:" + json.data.composer) - navigator.mediaSession.metadata=new MediaMetadata({ - title:json.data.title, - artist:json.data.artist, - album:json.data.album - }); - navigator.mediaSession.setActionHandler('nexttrack', function () { - // 用户点击了“下一首”媒体通知图标 - playNext(); - }); - }); - $.get(localhost + "/nas/music/image.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&fileName=" + fileName, function (data) { - let json = JSON.parse(data); - $('#img').attr("src", "data:image/png;base64," + json.data), - navigator.mediaSession.metadata.artwork=[{ - src:"data:image/png;base64," + json.data, - type: 'image/png', - sizes: '128x128' - }] }) + $('#img').attr("src",'/nas/music/web/image.do?fileName='+fileName); }