新增分享音乐文件夹

音乐请求使用md5而不再使用文件
API请求排除拦截public的接口
This commit is contained in:
Yutousama 2022-01-06 02:11:28 +08:00
parent d9a4e4a81a
commit 1497d11754
3 changed files with 304 additions and 218 deletions

View File

@ -44,9 +44,11 @@ public class RoleAccessDecisionManager implements AccessDecisionManager {
case "/login/check.do": case "/login/check.do":
return; return;
} }
if(url.startsWith("/public/")){
return;
}
if(!Tools.isAdminLogin()){ if(!Tools.isAdminLogin()){
String redis=RedisTools.get(musicShare); String redis=RedisTools.get(musicShare);
System.out.println("分享token="+musicShare+" redis="+redis);
if(redis!=null&&!"-999".equals(redis)){ if(redis!=null&&!"-999".equals(redis)){
authentication.setAuthenticated(true); authentication.setAuthenticated(true);
return; return;

View File

@ -29,71 +29,75 @@ public class NasManager {
public static final String NasUrl="http://yutou233.cn"; public static final String NasUrl="http://yutou233.cn";
@Resource @Resource
NasAdminAddressDao adminAddressDao; 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"); @ResponseBody
if(address==null){ @RequestMapping(value = "/auth/nas/address/get.do", method = RequestMethod.GET)
json.put("code",-1); public String getAdminAddress(HttpServletRequest request) {
json.put("msg","暂未设置管理后台"); JSONObject json = new JSONObject();
}else{
JSONObject js=JSONObject.parseObject(address); String address = RedisTools.get("adminAddress");
json.put("code",0); if (address == null) {
json.put("msg","当前状态:"+js.getString("url")+":"+js.getString("port")); 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(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping(value = "/auth/nas/address/list.do",method = RequestMethod.GET) @RequestMapping(value = "/auth/nas/address/list.do", method = RequestMethod.GET)
public String addressList(HttpServletRequest request){ public String addressList(HttpServletRequest request) {
JSONObject json =new JSONObject(); JSONObject json = new JSONObject();
try{ try {
List<NasAdminAddress> list=adminAddressDao.selectByExample(new NasAdminAddressExample()); List<NasAdminAddress> list = adminAddressDao.selectByExample(new NasAdminAddressExample());
json.put("code",0); json.put("code", 0);
json.put("msg","ok"); json.put("msg", "ok");
json.put("count",list.size()); json.put("count", list.size());
if(list.size()==0){ if (list.size() == 0) {
json.put("data",new JSONArray()); json.put("data", new JSONArray());
}else { } else {
json.put("data", JSONArray.toJSON(list)); json.put("data", JSONArray.toJSON(list));
} }
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
json.put("code",-1); json.put("code", -1);
json.put("count",0); json.put("count", 0);
json.put("msg",e.getMessage()); json.put("msg", e.getMessage());
json.put("data","[]"); json.put("data", "[]");
} }
return json.toJSONString(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping(value = "/auth/nas/address/add.do",method = RequestMethod.POST) @RequestMapping(value = "/auth/nas/address/add.do", method = RequestMethod.POST)
public String addAddress(HttpServletRequest request,String url,String port,String title) { public String addAddress(HttpServletRequest request, String url, String port, String title) {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
try { try {
NasAdminAddress address=new NasAdminAddress(); NasAdminAddress address = new NasAdminAddress();
address.setUrl(url); address.setUrl(url);
address.setPort(Integer.parseInt(port)); address.setPort(Integer.parseInt(port));
address.setTitle(title); address.setTitle(title);
adminAddressDao.insert(address); adminAddressDao.insert(address);
json.put("code",0); json.put("code", 0);
json.put("msg","ok"); json.put("msg", "ok");
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
json.put("code",-1); json.put("code", -1);
json.put("msg",e.getMessage()); json.put("msg", e.getMessage());
} }
return json.toJSONString(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping(value = "/auth/nas/address/update.do",method = RequestMethod.POST) @RequestMapping(value = "/auth/nas/address/update.do", method = RequestMethod.POST)
public String updateAddress(HttpServletRequest request,String url,String port,String title,String id){ public String updateAddress(HttpServletRequest request, String url, String port, String title, String id) {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
try { try {
NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id)); NasAdminAddress address = adminAddressDao.selectByPrimaryKey(Integer.parseInt(id));
if(address!=null) { if (address != null) {
if (!StringUtils.isEmpty(url)) { if (!StringUtils.isEmpty(url)) {
address.setUrl(url); address.setUrl(url);
} }
@ -104,58 +108,61 @@ public class NasManager {
address.setTitle(title); address.setTitle(title);
} }
adminAddressDao.updateByPrimaryKey(address); adminAddressDao.updateByPrimaryKey(address);
json.put("code",0); json.put("code", 0);
json.put("msg","ok"); json.put("msg", "ok");
}else { } else {
json.put("code",1); json.put("code", 1);
json.put("msg","无更新"); json.put("msg", "无更新");
} }
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
json.put("code",-1); json.put("code", -1);
json.put("msg",e.getMessage()); json.put("msg", e.getMessage());
} }
return json.toJSONString(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping(value = "/auth/nas/address/remove.do",method = RequestMethod.POST) @RequestMapping(value = "/auth/nas/address/remove.do", method = RequestMethod.POST)
public String removeAddress(HttpServletRequest request,String id){ public String removeAddress(HttpServletRequest request, String id) {
JSONObject json=new JSONObject(); 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();
try { try {
NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id)); int code = adminAddressDao.deleteByPrimaryKey(Integer.parseInt(id));
if(address!=null){ json.put("code", code);
RedisTools.set("adminAddress",JSONObject.toJSONString(address)); json.put("msg", "ok");
} } catch (Exception e) {
json.put("code",0);
json.put("msg","ok");
}catch (Exception e){
e.printStackTrace(); e.printStackTrace();
json.put("code",-1); json.put("code", -1);
json.put("msg",e.getMessage()); json.put("msg", e.getMessage());
} }
return json.toJSONString(); 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 @ResponseBody
@RequestMapping(value = "/jellyfin.do") @RequestMapping(value = "/jellyfin.do")
public void jellyfin(HttpServletResponse response){ public void jellyfin(HttpServletResponse response) {
try { try {
response.sendRedirect("http://"+UpdateIp.nas_ip+":8096"); response.sendRedirect("http://" + UpdateIp.nas_ip + ":8096");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -165,67 +172,119 @@ public class NasManager {
@ResponseBody @ResponseBody
public String getLocalHost() { public String getLocalHost() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
if("dev".equals(ConfigTools.load(ConfigTools.CONFIG, "model"))){ if ("dev".equals(ConfigTools.load(ConfigTools.CONFIG, "model"))) {
json.put("data", NasUrl+":8001"); json.put("data", NasUrl + ":8000");
}else { } else {
if(UpdateIp.nas_ip==null) { if (UpdateIp.nas_ip == null) {
json.put("data", "http://yutou233.cn:8001"); json.put("data", "http://yutou233.cn:8001");
}else { } else {
json.put("data", String.format("http://%s:8000", UpdateIp.nas_ip)); json.put("data", String.format("http://%s:8000", UpdateIp.nas_ip));
} }
} }
json.put("code", 0); json.put("code", 0);
return json.toJSONString(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping("/nas/music/share.do") @RequestMapping("/nas/music/share.do")
public JSONObject share(String file){ public JSONObject share(String file,boolean isDir) {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
String token= Tools.getMD5(UUID.randomUUID() +file); String token = Tools.getMD5(UUID.randomUUID() + file);
json.put("token",token); json.put("token", token);
json.put("file",file); json.put("file", file);
HashMap<String,String> header=new HashMap<>(); json.put("isDir",isDir);
header.put("content-type","application/json"); HashMap<String, String> header = new HashMap<>();
String url=NasUrl+":8001"; header.put("content-type", "application/json");
if(UpdateIp.nas_ip!=null){ String url = NasUrl + ":8000";
url="http://"+UpdateIp.nas_ip+":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); String data = HttpTools.http_post(url + "/nas/music/share.do", json.toJSONString().getBytes(StandardCharsets.UTF_8), 1, header);
JSONObject _data=JSONObject.parseObject(data); JSONObject _data = JSONObject.parseObject(data);
if(_data.getInteger("code")==1) { if (_data.getInteger("code") == 1) {
RedisTools.set(token, data, 3600); RedisTools.set(token, data, 3600);
System.out.println("设置分享token:" + token + " -> " + data); System.out.println("设置分享token:" + token + " -> " + data);
_data.put("token", token); _data.put("token", token);
} }
json.clear(); json.clear();
json.put("code",0); json.put("code", 0);
json.put("msg","ok"); json.put("msg", "ok");
json.put("data",_data); json.put("data", _data);
return json; return json;
} }
@ResponseBody @ResponseBody
@RequestMapping("/nas/music/playShare.do") @RequestMapping("/nas/music/playShare.do")
public JSONObject playShare(String share){ public JSONObject playShare(String share) {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
String redis=RedisTools.get(share); String redis = RedisTools.get(share);
if(redis!=null&&!"-999".equals(redis)) { if (redis != null && !"-999".equals(redis)) {
String url=NasUrl+":8001"; String url = NasUrl + ":8000";
if(UpdateIp.nas_ip!=null){ if (UpdateIp.nas_ip != null) {
url="http://"+UpdateIp.nas_ip+":8000"; 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); System.out.println(_json);
JSONObject item=JSONObject.parseObject(_json); JSONObject item = JSONObject.parseObject(_json);
if(item.getInteger("code")==0) { if (item.getInteger("code") == 0) {
json.put("code", 0); json.put("code", 0);
json.put("data", item.getJSONObject("data").getString("file")); JSONArray array = item.getJSONObject("data").getJSONArray("file");
}else{ json.put("data", array);
json.put("code","-1"); } else {
json.put("msg","分享已过期"); json.put("code", "-1");
json.put("msg", "分享已过期");
} }
}else{ } else {
json.put("code","-2"); json.put("code", "-2");
json.put("msg","连接错误"); 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; return json;
} }

View File

@ -66,7 +66,7 @@
let playNow = "" let playNow = ""
let share=getParam('share'); let share=getParam('share');
if(share!=null){ if(share!=null){
$('#playlist').remove() //$('#playlist').remove()
$('#reload').remove() $('#reload').remove()
$('#next').remove() $('#next').remove()
$('#play_share').remove() $('#play_share').remove()
@ -89,94 +89,128 @@
layui.use(['table', 'element'], function () { layui.use(['table', 'element'], function () {
let table = layui.table; let table = layui.table;
let element = layui.element; let element = layui.element;
let listTable = table.render({ let listTable = undefined;
elem: '#playlist' $.post("/public/nas/music/playlist.do",{share:share},function (json) {
, url: localhost + '/nas/music/list.do?token=' //数据接口 if(json.code!==0){
, method: 'post' return;
, where: {
path: 'root',
type: false
} }
, contentType: 'application/json' listTable=table.render({
, page: true //开启分页 elem: '#playlist'
, cols: [[ , url: json.data //数据接口
{field: 'title', title: '标题', width: 400, sort: true, templet: "#listTemplet"} , method: 'post'
, {field: 'artist', title: '艺术家', width: 200} , where: {
, {field: 'album', title: '专辑', width: 200} path: 'root',
, {field: 'composer', title: '作曲', width: 150} type: false
, {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))
} }
} else if (obj.event === 'play') { , contentType: 'application/json'
$.ajax({ , page: true //开启分页
type: "POST", , cols: [[
url:localhost + "/nas/music/list.do", {field: 'title', title: '标题', width: 400, sort: true, templet: "#listTemplet"}
contentType: "application/json; charset=utf-8", , {field: 'artist', title: '艺术家', width: 200}
data:JSON.stringify({type: true, path: data.file}), , {field: 'album', title: '专辑', width: 200}
dataType: "json", , {field: 'composer', title: '作曲', width: 150}
success:function (json){ , {field: 'track', title: '音轨号', width: 100, sort: true}
try { , {field: "right", width: 200, toolbar: '#music'}
if (json.code === 0) { ]]
playIndex = 0; , done: function (res, curr, count) {
musicLib=[] musicLib = res.data
for (let datum of json.data) { $('#musicSize').html("歌单总数:" + res.size)
if(datum.isdir===0){ },error:function (e,data){
musicLib.push(datum) }
})
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; }catch (e){
playNext()
} }
}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'){ play(json.data[0])
$.post('/nas/music/share.do',{file:data.file},function (json){ })
if(json.code===0){ }
layer.prompt({ if(share!==null){
title:"分享链接", playShare()
value:"http://"+window.location.host+"/html/body/nas/music.html?share="+json.data.token }
})
}
})
}
});
element.on('nav(menus)', function (elem) {
});
}); });
}); });
@ -218,7 +252,7 @@
playNext() playNext()
}); });
$('#play_share').click(function (){ $('#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){ if(json.code===0){
layer.prompt({ layer.prompt({
title:"分享链接", title:"分享链接",
@ -246,7 +280,7 @@
playIndex++ playIndex++
playNext() playNext()
} else { } else {
play(musicLib[playIndex].file); play(musicLib[playIndex]);
playIndex++ playIndex++
} }
} }
@ -268,14 +302,14 @@
}) })
} }
function play(file) { function play(_json) {
let filePath = escape(new Base64().encode(file)) let md5=''
playNow = filePath md5=_json.md5
console.log(localhost + "/nas/music/play.do?random=false&filePath=" + filePath ) playNow = _json.md5
player.updateSource({ 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() player.play()
} }
@ -316,17 +350,8 @@
}) })
$('#img').attr("src",localhost+'/nas/music/web/image.do?fileName='+fileName); $('#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()
}
</script> </script>
<style> <style>