From 1497d1175471c6a6addbed9719c13b06f2e6edcb Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Thu, 6 Jan 2022 02:11:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=88=86=E4=BA=AB=E9=9F=B3?= =?UTF-8?q?=E4=B9=90=E6=96=87=E4=BB=B6=E5=A4=B9=20=E9=9F=B3=E4=B9=90?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=BD=BF=E7=94=A8md5=E8=80=8C=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E4=BD=BF=E7=94=A8=E6=96=87=E4=BB=B6=20API=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=8E=92=E9=99=A4=E6=8B=A6=E6=88=AApublic=E7=9A=84?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AuthConfig/RoleAccessDecisionManager.java | 4 +- .../java/com/yutou/tools/nas/NasManager.java | 295 +++++++++++------- web/html/body/nas/music.html | 223 +++++++------ 3 files changed, 304 insertions(+), 218 deletions(-) diff --git a/src/main/java/com/yutou/tools/AuthConfig/RoleAccessDecisionManager.java b/src/main/java/com/yutou/tools/AuthConfig/RoleAccessDecisionManager.java index 21d2b3a..31a7cca 100644 --- a/src/main/java/com/yutou/tools/AuthConfig/RoleAccessDecisionManager.java +++ b/src/main/java/com/yutou/tools/AuthConfig/RoleAccessDecisionManager.java @@ -44,9 +44,11 @@ public class RoleAccessDecisionManager implements AccessDecisionManager { case "/login/check.do": return; } + if(url.startsWith("/public/")){ + return; + } if(!Tools.isAdminLogin()){ String redis=RedisTools.get(musicShare); - System.out.println("分享token="+musicShare+" redis="+redis); if(redis!=null&&!"-999".equals(redis)){ authentication.setAuthenticated(true); return; diff --git a/src/main/java/com/yutou/tools/nas/NasManager.java b/src/main/java/com/yutou/tools/nas/NasManager.java index 9a1b3d9..ed37a1b 100644 --- a/src/main/java/com/yutou/tools/nas/NasManager.java +++ b/src/main/java/com/yutou/tools/nas/NasManager.java @@ -29,71 +29,75 @@ public class NasManager { public static final String NasUrl="http://yutou233.cn"; @Resource NasAdminAddressDao adminAddressDao; - @ResponseBody - @RequestMapping(value = "/auth/nas/address/get.do",method = RequestMethod.GET) - public String getAdminAddress(HttpServletRequest request){ - JSONObject json=new JSONObject(); - String address= RedisTools.get("adminAddress"); - if(address==null){ - json.put("code",-1); - json.put("msg","暂未设置管理后台"); - }else{ - JSONObject js=JSONObject.parseObject(address); - json.put("code",0); - json.put("msg","当前状态:"+js.getString("url")+":"+js.getString("port")); + @ResponseBody + @RequestMapping(value = "/auth/nas/address/get.do", method = RequestMethod.GET) + public String getAdminAddress(HttpServletRequest request) { + JSONObject json = new JSONObject(); + + String address = RedisTools.get("adminAddress"); + if (address == null) { + json.put("code", -1); + json.put("msg", "暂未设置管理后台"); + } else { + JSONObject js = JSONObject.parseObject(address); + json.put("code", 0); + json.put("msg", "当前状态:" + js.getString("url") + ":" + js.getString("port")); } return json.toJSONString(); } + @ResponseBody - @RequestMapping(value = "/auth/nas/address/list.do",method = RequestMethod.GET) - public String addressList(HttpServletRequest request){ - JSONObject json =new JSONObject(); - try{ - List list=adminAddressDao.selectByExample(new NasAdminAddressExample()); - json.put("code",0); - json.put("msg","ok"); - json.put("count",list.size()); - if(list.size()==0){ - json.put("data",new JSONArray()); - }else { + @RequestMapping(value = "/auth/nas/address/list.do", method = RequestMethod.GET) + public String addressList(HttpServletRequest request) { + JSONObject json = new JSONObject(); + try { + List list = adminAddressDao.selectByExample(new NasAdminAddressExample()); + json.put("code", 0); + json.put("msg", "ok"); + json.put("count", list.size()); + if (list.size() == 0) { + json.put("data", new JSONArray()); + } else { json.put("data", JSONArray.toJSON(list)); } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); - json.put("code",-1); - json.put("count",0); - json.put("msg",e.getMessage()); - json.put("data","[]"); + json.put("code", -1); + json.put("count", 0); + json.put("msg", e.getMessage()); + json.put("data", "[]"); } return json.toJSONString(); } + @ResponseBody - @RequestMapping(value = "/auth/nas/address/add.do",method = RequestMethod.POST) - public String addAddress(HttpServletRequest request,String url,String port,String title) { - JSONObject json=new JSONObject(); + @RequestMapping(value = "/auth/nas/address/add.do", method = RequestMethod.POST) + public String addAddress(HttpServletRequest request, String url, String port, String title) { + JSONObject json = new JSONObject(); try { - NasAdminAddress address=new NasAdminAddress(); + NasAdminAddress address = new NasAdminAddress(); address.setUrl(url); address.setPort(Integer.parseInt(port)); address.setTitle(title); adminAddressDao.insert(address); - json.put("code",0); - json.put("msg","ok"); - }catch (Exception e){ + json.put("code", 0); + json.put("msg", "ok"); + } catch (Exception e) { e.printStackTrace(); - json.put("code",-1); - json.put("msg",e.getMessage()); + json.put("code", -1); + json.put("msg", e.getMessage()); } - return json.toJSONString(); + return json.toJSONString(); } + @ResponseBody - @RequestMapping(value = "/auth/nas/address/update.do",method = RequestMethod.POST) - public String updateAddress(HttpServletRequest request,String url,String port,String title,String id){ - JSONObject json=new JSONObject(); + @RequestMapping(value = "/auth/nas/address/update.do", method = RequestMethod.POST) + public String updateAddress(HttpServletRequest request, String url, String port, String title, String id) { + JSONObject json = new JSONObject(); try { - NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id)); - if(address!=null) { + NasAdminAddress address = adminAddressDao.selectByPrimaryKey(Integer.parseInt(id)); + if (address != null) { if (!StringUtils.isEmpty(url)) { address.setUrl(url); } @@ -104,58 +108,61 @@ public class NasManager { address.setTitle(title); } adminAddressDao.updateByPrimaryKey(address); - json.put("code",0); - json.put("msg","ok"); - }else { - json.put("code",1); - json.put("msg","无更新"); + json.put("code", 0); + json.put("msg", "ok"); + } else { + json.put("code", 1); + json.put("msg", "无更新"); } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); - json.put("code",-1); - json.put("msg",e.getMessage()); + json.put("code", -1); + json.put("msg", e.getMessage()); } return json.toJSONString(); } + @ResponseBody - @RequestMapping(value = "/auth/nas/address/remove.do",method = RequestMethod.POST) - public String removeAddress(HttpServletRequest request,String id){ - JSONObject json=new JSONObject(); - try{ - int code=adminAddressDao.deleteByPrimaryKey(Integer.parseInt(id)); - json.put("code",code); - json.put("msg","ok"); - }catch (Exception e){ - e.printStackTrace(); - json.put("code",-1); - json.put("msg",e.getMessage()); - } - return json.toJSONString(); - } - @ResponseBody - @RequestMapping(value = "/auth/nas/address/set.do",method = RequestMethod.POST) - public String setAddress(HttpServletRequest request,String id){ - JSONObject json=new JSONObject(); + @RequestMapping(value = "/auth/nas/address/remove.do", method = RequestMethod.POST) + public String removeAddress(HttpServletRequest request, String id) { + JSONObject json = new JSONObject(); try { - NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id)); - if(address!=null){ - RedisTools.set("adminAddress",JSONObject.toJSONString(address)); - } - json.put("code",0); - json.put("msg","ok"); - }catch (Exception e){ + int code = adminAddressDao.deleteByPrimaryKey(Integer.parseInt(id)); + json.put("code", code); + json.put("msg", "ok"); + } catch (Exception e) { e.printStackTrace(); - json.put("code",-1); - json.put("msg",e.getMessage()); + json.put("code", -1); + json.put("msg", e.getMessage()); } return json.toJSONString(); } + + @ResponseBody + @RequestMapping(value = "/auth/nas/address/set.do", method = RequestMethod.POST) + public String setAddress(HttpServletRequest request, String id) { + JSONObject json = new JSONObject(); + try { + NasAdminAddress address = adminAddressDao.selectByPrimaryKey(Integer.parseInt(id)); + if (address != null) { + RedisTools.set("adminAddress", JSONObject.toJSONString(address)); + } + json.put("code", 0); + json.put("msg", "ok"); + } catch (Exception e) { + e.printStackTrace(); + json.put("code", -1); + json.put("msg", e.getMessage()); + } + return json.toJSONString(); + } + @ResponseBody @RequestMapping(value = "/jellyfin.do") - public void jellyfin(HttpServletResponse response){ + public void jellyfin(HttpServletResponse response) { try { - response.sendRedirect("http://"+UpdateIp.nas_ip+":8096"); + response.sendRedirect("http://" + UpdateIp.nas_ip + ":8096"); } catch (IOException e) { e.printStackTrace(); } @@ -165,67 +172,119 @@ public class NasManager { @ResponseBody public String getLocalHost() { JSONObject json = new JSONObject(); - if("dev".equals(ConfigTools.load(ConfigTools.CONFIG, "model"))){ - json.put("data", NasUrl+":8001"); - }else { - if(UpdateIp.nas_ip==null) { + if ("dev".equals(ConfigTools.load(ConfigTools.CONFIG, "model"))) { + json.put("data", NasUrl + ":8000"); + } else { + if (UpdateIp.nas_ip == null) { json.put("data", "http://yutou233.cn:8001"); - }else { + } else { json.put("data", String.format("http://%s:8000", UpdateIp.nas_ip)); } } json.put("code", 0); return json.toJSONString(); } + @ResponseBody @RequestMapping("/nas/music/share.do") - public JSONObject share(String file){ - JSONObject json=new JSONObject(); - String token= Tools.getMD5(UUID.randomUUID() +file); - json.put("token",token); - json.put("file",file); - HashMap header=new HashMap<>(); - header.put("content-type","application/json"); - String url=NasUrl+":8001"; - if(UpdateIp.nas_ip!=null){ - url="http://"+UpdateIp.nas_ip+":8000"; + public JSONObject share(String file,boolean isDir) { + JSONObject json = new JSONObject(); + String token = Tools.getMD5(UUID.randomUUID() + file); + json.put("token", token); + json.put("file", file); + json.put("isDir",isDir); + HashMap header = new HashMap<>(); + header.put("content-type", "application/json"); + String url = NasUrl + ":8000"; + if (UpdateIp.nas_ip != null) { + url = "http://" + UpdateIp.nas_ip + ":8000"; } - String data=HttpTools.http_post(url+"/nas/music/share.do",json.toJSONString().getBytes(StandardCharsets.UTF_8),1,header); - JSONObject _data=JSONObject.parseObject(data); - if(_data.getInteger("code")==1) { + String data = HttpTools.http_post(url + "/nas/music/share.do", json.toJSONString().getBytes(StandardCharsets.UTF_8), 1, header); + JSONObject _data = JSONObject.parseObject(data); + if (_data.getInteger("code") == 1) { RedisTools.set(token, data, 3600); System.out.println("设置分享token:" + token + " -> " + data); _data.put("token", token); } json.clear(); - json.put("code",0); - json.put("msg","ok"); - json.put("data",_data); + json.put("code", 0); + json.put("msg", "ok"); + json.put("data", _data); return json; } + @ResponseBody @RequestMapping("/nas/music/playShare.do") - public JSONObject playShare(String share){ - JSONObject json=new JSONObject(); - String redis=RedisTools.get(share); - if(redis!=null&&!"-999".equals(redis)) { - String url=NasUrl+":8001"; - if(UpdateIp.nas_ip!=null){ - url="http://"+UpdateIp.nas_ip+":8000"; + public JSONObject playShare(String share) { + JSONObject json = new JSONObject(); + String redis = RedisTools.get(share); + if (redis != null && !"-999".equals(redis)) { + String url = NasUrl + ":8000"; + if (UpdateIp.nas_ip != null) { + url = "http://" + UpdateIp.nas_ip + ":8000"; } - String _json=HttpTools.get(url+"/nas/music/playShare.do?token="+JSONObject.parseObject(redis).getJSONObject("data").getString("share")); + String _json = HttpTools.get(url + "/nas/music/playShare.do?token=" + JSONObject.parseObject(redis).getJSONObject("data").getString("share")); System.out.println(_json); - JSONObject item=JSONObject.parseObject(_json); - if(item.getInteger("code")==0) { + JSONObject item = JSONObject.parseObject(_json); + if (item.getInteger("code") == 0) { json.put("code", 0); - json.put("data", item.getJSONObject("data").getString("file")); - }else{ - json.put("code","-1"); - json.put("msg","分享已过期"); + JSONArray array = item.getJSONObject("data").getJSONArray("file"); + json.put("data", array); + } else { + json.put("code", "-1"); + json.put("msg", "分享已过期"); } - }else{ - json.put("code","-2"); - json.put("msg","连接错误"); + } else { + json.put("code", "-2"); + json.put("msg", "连接错误"); + } + return json; + } + + @ResponseBody + @RequestMapping(value = "/public/nas/music/pplist.do",produces = "application/json") + public JSONObject getPlayList(String share) { + String redis = RedisTools.get(share); + JSONObject json = new JSONObject(); + String url = NasUrl + ":8000"; + if (UpdateIp.nas_ip != null) { + url = "http://" + UpdateIp.nas_ip + ":8000"; + } + String _json = HttpTools.get(url + "/nas/music/playShare.do?token=" + JSONObject.parseObject(redis).getJSONObject("data").getString("share")); + JSONObject item = JSONObject.parseObject(_json); + JSONArray array = item.getJSONObject("data").getJSONArray("file"); + json.put("size", array.size()); + json.put("scan", false); + json.put("data", array); + json.put("code",0); + return json; + } + + @ResponseBody + @RequestMapping("/public/nas/music/playlist.do") + public JSONObject getMusicPlayList(String share) { + JSONObject json = new JSONObject(); + String redis = RedisTools.get(share); + if ((StringUtils.isEmpty(redis) || "-999".equals(redis)) && !Tools.isAdminLogin()) { + json.put("code", -1); + } else { + json.put("code", 0); + if (!StringUtils.isEmpty(share)) { + json.put("data", "/public/nas/music/pplist.do?share=" + share); + } else if (Tools.isAdminLogin()) { + if ("dev".equals(ConfigTools.load(ConfigTools.CONFIG, "model"))) { + json.put("data", NasUrl + ":8000/nas/music/list.do?token="); + } else { + if (UpdateIp.nas_ip == null) { + json.put("data", "http://yutou233.cn:8001/nas/music/list.do?token="); + } else { + json.put("data", String.format("http://%s:8000/nas/music/list.do?token=", UpdateIp.nas_ip)); + } + } + } else { + json.put("code", -1); + } + } return json; } diff --git a/web/html/body/nas/music.html b/web/html/body/nas/music.html index b673dc2..1a51367 100644 --- a/web/html/body/nas/music.html +++ b/web/html/body/nas/music.html @@ -66,7 +66,7 @@ let playNow = "" let share=getParam('share'); if(share!=null){ - $('#playlist').remove() + //$('#playlist').remove() $('#reload').remove() $('#next').remove() $('#play_share').remove() @@ -89,94 +89,128 @@ layui.use(['table', 'element'], function () { let table = layui.table; let element = layui.element; - let listTable = table.render({ - elem: '#playlist' - , url: localhost + '/nas/music/list.do?token=' //数据接口 - , method: 'post' - , where: { - path: 'root', - type: false + let listTable = undefined; + $.post("/public/nas/music/playlist.do",{share:share},function (json) { + if(json.code!==0){ + return; } - , contentType: 'application/json' - , page: true //开启分页 - , cols: [[ - {field: 'title', title: '标题', width: 400, sort: true, templet: "#listTemplet"} - , {field: 'artist', title: '艺术家', width: 200} - , {field: 'album', title: '专辑', width: 200} - , {field: 'composer', title: '作曲', width: 150} - , {field: 'track', title: '音轨号', width: 100, sort: true} - , {field: "right", width: 200, toolbar: '#music'} - ]] - , done: function (res, curr, count) { - musicLib = res.data - $('#musicSize').html("歌单总数:" + res.size) - } - }) - table.on('rowDouble(music)', function (obj) { - console.log("双击:"+obj) - //obj 同上 - if (obj.data.isdir === 1) { - listTable.reload({ - where: { - path: obj.data.file - } - , page: { - curr: 1 - } - }); - } else { - isRandom = false; - playIndex = obj.tr[0].dataset.index; - play(obj.data.file) - } - - }); - - table.on('tool(music)', function (obj) { - let data = obj.data; - if (obj.event === 'download') { - if (data.isdir === 0) { - window.open(localhost + "/nas/music/play.do?token=&random=false&filePath=" + new Base64().encode(data.file)) + listTable=table.render({ + elem: '#playlist' + , url: json.data //数据接口 + , method: 'post' + , where: { + path: 'root', + type: false } - } else if (obj.event === 'play') { - $.ajax({ - type: "POST", - url:localhost + "/nas/music/list.do", - contentType: "application/json; charset=utf-8", - data:JSON.stringify({type: true, path: data.file}), - dataType: "json", - success:function (json){ - try { - if (json.code === 0) { - playIndex = 0; - musicLib=[] - for (let datum of json.data) { - if(datum.isdir===0){ - musicLib.push(datum) + , contentType: 'application/json' + , page: true //开启分页 + , cols: [[ + {field: 'title', title: '标题', width: 400, sort: true, templet: "#listTemplet"} + , {field: 'artist', title: '艺术家', width: 200} + , {field: 'album', title: '专辑', width: 200} + , {field: 'composer', title: '作曲', width: 150} + , {field: 'track', title: '音轨号', width: 100, sort: true} + , {field: "right", width: 200, toolbar: '#music'} + ]] + , done: function (res, curr, count) { + musicLib = res.data + $('#musicSize').html("歌单总数:" + res.size) + },error:function (e,data){ + } + }) + table.on('rowDouble(music)', function (obj) { + //obj 同上 + if (obj.data.isdir === 1) { + let path=obj.data.md5; + if(path===undefined){ + path=obj.data.file; + } + listTable.reload({ + where: { + path:path + } + , page: { + curr: 1 + } + }); + } else { + isRandom = false; + playIndex = obj.tr[0].dataset.index; + play(obj.data) + } + + }); + + table.on('tool(music)', function (obj) { + let data = obj.data; + + if (obj.event === 'download') { + if (data.isdir === 0) { + window.open(localhost + "/nas/music/play.do?token=&random=false&filePath=" + data.md5) + } + } else if (obj.event === 'play') { + if(data.md5!==undefined){ + play(data) + return; + } + $.ajax({ + type: "POST", + url:localhost + "/nas/music/list.do", + contentType: "application/json; charset=utf-8", + data:JSON.stringify({type: true, path: data.file}), + dataType: "json", + success:function (json){ + try { + if (json.code === 0) { + playIndex = 0; + musicLib=[] + for (let datum of json.data) { + if(datum.isdir===0){ + musicLib.push(datum) + } } + isRandom = false; + playNext() } - isRandom = false; - playNext() + }catch (e){ + } - }catch (e){ } - + }) + }else if(obj.event==='share'){ + let isDir=false; + let file=data.md5; + if(data.isdir===1){ + isDir=true; + file=data.file; } + $.post('/nas/music/share.do',{file:file,isDir:isDir},function (json){ + if(json.code===0){ + layer.prompt({ + title:"分享链接", + value:"http://"+window.location.host+"/html/body/nas/music.html?share="+json.data.token + }) + } + }) + } + }); + element.on('nav(menus)', function (elem) { + }); + }) + function playShare(){ + $.get('/nas/music/playShare.do?share='+share,function (json) { + isRandom = false; + playIndex =0; + json.data.forEach(function (item) { + musicLib.push(item) }) - }else if(obj.event==='share'){ - $.post('/nas/music/share.do',{file:data.file},function (json){ - if(json.code===0){ - layer.prompt({ - title:"分享链接", - value:"http://"+window.location.host+"/html/body/nas/music.html?share="+json.data.token - }) - } - }) - } - }); - element.on('nav(menus)', function (elem) { - }); + play(json.data[0]) + }) + } + if(share!==null){ + playShare() + } }); }); @@ -218,7 +252,7 @@ playNext() }); $('#play_share').click(function (){ - $.post('/nas/music/share.do',{file:new Base64().decode(decodeURIComponent(playNow))},function (json){ + $.post('/nas/music/share.do',{file:playNow},function (json){ if(json.code===0){ layer.prompt({ title:"分享链接", @@ -246,7 +280,7 @@ playIndex++ playNext() } else { - play(musicLib[playIndex].file); + play(musicLib[playIndex]); playIndex++ } } @@ -268,14 +302,14 @@ }) } - function play(file) { - let filePath = escape(new Base64().encode(file)) - playNow = filePath - console.log(localhost + "/nas/music/play.do?random=false&filePath=" + filePath ) + function play(_json) { + let md5='' + md5=_json.md5 + playNow = _json.md5 player.updateSource({ - source: localhost + "/nas/music/play.do?random=false&filePath=" + filePath + source: localhost + "/nas/music/play.do?random=false&filePath=" + md5 }); - update(filePath) + update(md5) player.play() } @@ -316,17 +350,8 @@ }) $('#img').attr("src",localhost+'/nas/music/web/image.do?fileName='+fileName); } - function playShare(){ - console.log("播放分享:"+share) - $.get('/nas/music/playShare.do?share='+share,function (json) { - isRandom = false; - playIndex =0; - play(json.data) - }) - } - if(share!==null){ - playShare() - } + +