音乐接口改成了POST请求

更新了收藏夹功能
网页版播放器还没改成POST
This commit is contained in:
yutou 2020-12-03 18:32:32 +08:00
parent ca012000a3
commit 3edfd2b55f
5 changed files with 195 additions and 122 deletions

View File

@ -11,6 +11,7 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@ -25,7 +26,7 @@ import java.util.List;
@RequestMapping("/nas/music/") @RequestMapping("/nas/music/")
public class MusicController { public class MusicController {
public static String defaultMusicPath="/media/yutou/4t/public/音乐"; public static String defaultMusicPath = "/media/yutou/4t/public/音乐";
@Resource @Resource
MusicTools musicTools; MusicTools musicTools;
@ -37,95 +38,103 @@ public class MusicController {
json.put("code", 0); json.put("code", 0);
json.put("scan", musicTools.isScan()); json.put("scan", musicTools.isScan());
json.put("size", musicTools.getLength()); json.put("size", musicTools.getLength());
json.put("data", JSONArray.toJSON(musicTools.getMusicList())); json.put("data", JSONArray.toJSON(musicTools.getMusicList()));
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("list.do") @RequestMapping("list.do")
@ResponseBody @ResponseBody
public String getMusicListOfPath(String path,String type){ public String getMusicListOfPath(@RequestBody JSONObject body) {
if(!StringUtils.isEmpty(path)&&!new File(path).exists()){ String path = body.getString("path");
path=Tools.base64ToString(path); boolean type = body.containsKey("type") ? body.getBoolean("type") : false;
} System.out.println("接收到地址:" + path);
System.out.println("接收到地址:"+path); if (StringUtils.isEmpty(path)
if(StringUtils.isEmpty(path) || path.equals("root")
||path.equals("root") || !path.contains(defaultMusicPath)
||!path.contains(defaultMusicPath) ) {
){ path = defaultMusicPath;
path=defaultMusicPath;
} }
//path=path.replace(defaultMusicPath+File.separator,""); //path=path.replace(defaultMusicPath+File.separator,"");
boolean isDir= !StringUtils.isEmpty(type) && (type.equals("true")); JSONObject json = new JSONObject();
JSONObject json=new JSONObject();
json.put("code", 0); json.put("code", 0);
json.put("scan", musicTools.isScan()); json.put("scan", musicTools.isScan());
json.put("size", musicTools.getLength()); json.put("size", musicTools.getLength());
json.put("data", JSONArray.toJSON(musicTools.getPath(path,isDir))); json.put("data", JSONArray.toJSON(musicTools.getPath(path, type)));
return json.toJSONString(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping("getAlbum.do") @RequestMapping("getAlbum.do")
public String getAlbum(String album){ public String getAlbum(@RequestBody JSONObject body) {
JSONObject json=new JSONObject(); String album = body.getString("album");
json.put("code",0); JSONObject json = new JSONObject();
if(StringUtils.isEmpty(album)){ json.put("code", 0);
json.put("data",JSONArray.toJSON(musicTools.getAllAlbum())); if (StringUtils.isEmpty(album)) {
}else{ json.put("data", JSONArray.toJSON(musicTools.getAllAlbum()));
json.put("data",JSONArray.toJSON(musicTools.selectAlbum(album))); } else {
json.put("data", JSONArray.toJSON(musicTools.selectAlbum(album)));
} }
return json.toJSONString(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping("getArtist.do") @RequestMapping("getArtist.do")
public String getArtist(String artist){ public String getArtist(@RequestBody JSONObject body) {
JSONObject json=new JSONObject(); String artist = body.getString("artist");
json.put("code",0); JSONObject json = new JSONObject();
if(StringUtils.isEmpty(artist)){ json.put("code", 0);
json.put("data",JSONArray.toJSON(musicTools.getAllArtist())); if (StringUtils.isEmpty(artist)) {
}else{ json.put("data", JSONArray.toJSON(musicTools.getAllArtist()));
json.put("data",JSONArray.toJSON(musicTools.selectArtist(artist))); } else {
json.put("data", JSONArray.toJSON(musicTools.selectArtist(artist)));
} }
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("reload.do") @RequestMapping("reload.do")
@ResponseBody @ResponseBody
public String reload(){ public String reload() {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
musicTools.scanMusic(); musicTools.scanMusic();
json.put("msg","ok"); json.put("msg", "ok");
json.put("code",0); json.put("code", 0);
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("find/file.do") @RequestMapping("find/file.do")
@ResponseBody @ResponseBody
public String findFile(String path){ public String findFile(@RequestBody JSONObject body) {
JSONObject json=new JSONObject(); String path = body.getString("path");
if(StringUtils.isEmpty(path)){ JSONObject json = new JSONObject();
json.put("code",-1); if (StringUtils.isEmpty(path)) {
json.put("msg","地址为空"); json.put("code", -1);
json.put("msg", "地址为空");
return json.toJSONString(); return json.toJSONString();
} }
json.put("code",0); json.put("code", 0);
json.put("data",musicTools.getMusicData(path)); json.put("data", musicTools.getMusicData(path));
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("getlocalhost.do") @RequestMapping("getlocalhost.do")
@ResponseBody @ResponseBody
public String getLocalHost(){ public String getLocalHost() {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
if(ConfigTools.load(ConfigTools.CONFIG, "model").equals("dev")){ if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("dev")) {
json.put("data",""); json.put("data", "");
}else{ } else {
json.put("data", "http://"+UpdateIp.nas_ip); json.put("data", "http://" + UpdateIp.nas_ip);
} }
json.put("code",0); json.put("code", 0);
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("image.do") @RequestMapping("image.do")
@ResponseBody @ResponseBody
public String getImage(String fileName) { public String getImage(@RequestBody JSONObject body) {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
String fileName = body.getString("fileName");
json.put("code", 1); json.put("code", 1);
if (StringUtils.isEmpty(fileName)) { if (StringUtils.isEmpty(fileName)) {
json.put("msg", "文件为空"); json.put("msg", "文件为空");
@ -133,7 +142,7 @@ public class MusicController {
json.put("data", ""); json.put("data", "");
return json.toJSONString(); return json.toJSONString();
} }
File file = new File(Tools.base64ToString(fileName)); File file = new File(fileName);
try { try {
if (file.exists()) { if (file.exists()) {
json.put("msg", "ok"); json.put("msg", "ok");
@ -144,7 +153,7 @@ public class MusicController {
json.put("code", -1); json.put("code", -1);
json.put("data", ""); json.put("data", "");
} }
}catch (Exception e){ } catch (Exception e) {
json.put("msg", "图片读取失败"); json.put("msg", "图片读取失败");
json.put("code", -1); json.put("code", -1);
json.put("data", ""); json.put("data", "");
@ -153,33 +162,34 @@ public class MusicController {
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("random.do") @RequestMapping("random.do")
@ResponseBody @ResponseBody
public String random(){ public String random() {
List<MusicData> list=musicTools.getMusicList(); List<MusicData> list = musicTools.getMusicList();
MusicData data=list.get(Tools.randomCommon(0,list.size()-1,1)[0]); MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]);
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
json.put("code",0); json.put("code", 0);
try { try {
json.put("data", URLEncoder.encode(getBase64(data.getFile()),"UTF-8")); json.put("data", URLEncoder.encode(getBase64(data.getFile()), "UTF-8"));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
} }
return json.toJSONString(); return json.toJSONString();
} }
private String getBase64(String str){
private String getBase64(String str) {
return new String(Base64.getEncoder().encode(str.getBytes())); return new String(Base64.getEncoder().encode(str.getBytes()));
} }
@RequestMapping("play.do") @RequestMapping("play.do")
public ResponseEntity<FileSystemResource> play(String filePath,boolean random) { public ResponseEntity<FileSystemResource> play(@RequestBody JSONObject body) {
if(!StringUtils.isEmpty(filePath)){ String filePath = body.getString("filePath");
filePath=Tools.base64ToString(filePath); boolean random = body.containsKey("random") ? body.getBoolean("random") : StringUtils.isEmpty(filePath);
} if (random) {
if(random){ List<MusicData> list = musicTools.getMusicList();
List<MusicData> list=musicTools.getMusicList(); MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]);
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()) { if (file.exists()) {

View File

@ -2,101 +2,164 @@ package com.yutou.tools.home.nas;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.mybatis.dao.MusicDataDao;
import com.yutou.tools.mybatis.dao.MusicFavoritesDao; import com.yutou.tools.mybatis.dao.MusicFavoritesDao;
import com.yutou.tools.mybatis.dao.MusicFavoritesDirDao; import com.yutou.tools.mybatis.dao.MusicFavoritesDirDao;
import com.yutou.tools.mybatis.model.MusicFavorites; import com.yutou.tools.mybatis.model.*;
import com.yutou.tools.mybatis.model.MusicFavoritesDir;
import com.yutou.tools.mybatis.model.MusicFavoritesDirExample;
import com.yutou.tools.mybatis.model.MusicFavoritesExample;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
/** /**
* 收藏夹相关 * 收藏夹相关
*/ */
@Controller @Controller
@RequestMapping("/nas/music/favorite/") @RequestMapping("/nas/music/favorite/")
public class MusicFavoritesController{ public class MusicFavoritesController {
@Resource @Resource
MusicFavoritesDao favoritesDao; MusicFavoritesDao favoritesDao;
@Resource @Resource
MusicFavoritesDirDao favoritesDirDao; MusicFavoritesDirDao favoritesDirDao;
@Resource
MusicDataDao musicDataDao;
@RequestMapping("dir/add.do") @RequestMapping("dir/add.do")
@ResponseBody @ResponseBody
public String addFavoriteDir(String favorite){ public String addFavoriteDir(@RequestBody JSONObject body) {
JSONObject json=new JSONObject(); String favorite = body.getString("favorite");
MusicFavoritesDirExample example=new MusicFavoritesDirExample(); JSONObject json = new JSONObject();
MusicFavoritesDirExample example = new MusicFavoritesDirExample();
example.createCriteria().andTitleEqualTo(favorite); example.createCriteria().andTitleEqualTo(favorite);
if(favoritesDirDao.selectByExample(example).isEmpty()){ if (!favoritesDirDao.selectByExample(example).isEmpty()) {
json.put("code",-1); json.put("code", -1);
json.put("msg","已有该收藏夹"); json.put("msg", "已有该收藏夹");
}else{ } else {
MusicFavoritesDir favoritesDir=new MusicFavoritesDir(); MusicFavoritesDir favoritesDir = new MusicFavoritesDir();
favoritesDir.setTitle(favorite); favoritesDir.setTitle(favorite);
int ret=favoritesDirDao.insert(favoritesDir); int ret = favoritesDirDao.insert(favoritesDir);
json.put("code",ret==0?-1:0); json.put("code", ret == 0 ? -1 : 0);
json.put("msg",ret==0?"添加失败":"添加成功"); json.put("msg", ret == 0 ? "添加失败" : "添加成功");
} }
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("dir/list.do") @RequestMapping("dir/list.do")
@ResponseBody @ResponseBody
public String getAllFavoriteDir(){ public String getAllFavoriteDir() {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
json.put("code",0); json.put("code", 0);
json.put("data", JSONArray.toJSON(favoritesDirDao.selectByExample(new MusicFavoritesDirExample()))); List<MusicFavoritesDir> list = favoritesDirDao.selectByExample(new MusicFavoritesDirExample());
JSONArray array = new JSONArray();
for (MusicFavoritesDir favoritesDir : list) {
JSONObject data = new JSONObject();
data.put("id", favoritesDir.getId());
data.put("title", favoritesDir.getTitle());
MusicFavoritesExample example = new MusicFavoritesExample();
example.createCriteria().andFavoriteidEqualTo(favoritesDir.getId());
data.put("count", favoritesDao.countByExample(example));
array.add(data);
}
json.put("data", array);
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("dir/rename.do") @RequestMapping("dir/rename.do")
@ResponseBody @ResponseBody
public String renameFavoriteDir(String id,String title){ public String renameFavoriteDir(@RequestBody JSONObject body) {
JSONObject json=new JSONObject(); int id = body.getInteger("id");
MusicFavoritesDir favoritesDir=favoritesDirDao.selectByPrimaryKey(Integer.parseInt(id)); String title = body.getString("title");
if(favoritesDir==null){ JSONObject json = new JSONObject();
json.put("code",-1); MusicFavoritesDir favoritesDir = favoritesDirDao.selectByPrimaryKey(id);
json.put("msg","没有该收藏夹"); if (favoritesDir == null) {
}else{ json.put("code", -1);
json.put("msg", "没有该收藏夹");
} else {
favoritesDir.setTitle(title); favoritesDir.setTitle(title);
favoritesDirDao.updateByPrimaryKey(favoritesDir); favoritesDirDao.updateByPrimaryKey(favoritesDir);
json.put("code",0); json.put("code", 0);
json.put("msg","更新成功"); json.put("msg", "更新成功");
} }
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("add.do") @RequestMapping("add.do")
@ResponseBody @ResponseBody
public String addFavorite(int fid,String md5s){ public String addFavorite(@RequestBody JSONObject body) {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
JSONArray array=JSONArray.parseArray(md5s); JSONArray array = body.getJSONArray("md5s");
if(array==null){ int fid = body.getInteger("fid");
json.put("code",-1); if (array == null || fid == -1) {
json.put("msg","参数异常"); json.put("code", -1);
json.put("msg", "参数异常: " + body);
return json.toJSONString(); return json.toJSONString();
} }
for (Object o : array) { for (Object md5 : array) {
String md5= (String) o; MusicFavoritesExample example = new MusicFavoritesExample();
MusicFavorites favorites=new MusicFavorites(); example.createCriteria().andMusisMd5EqualTo((String) md5);
favorites.setMusisMd5(md5); if (favoritesDao.countByExample(example) == 0) {
favorites.setFavoriteid(fid); MusicFavorites favorites = new MusicFavorites();
favorites.setSubTime(new Date()); favorites.setMusisMd5((String) md5);
favoritesDao.insert(favorites); favorites.setFavoriteid(fid);
favorites.setSubTime(new Date());
favoritesDao.insert(favorites);
}
} }
json.put("code",0); json.put("code", 0);
json.put("msg","添加成功"); json.put("msg", "添加成功");
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("remove.do") @RequestMapping("remove.do")
@ResponseBody @ResponseBody
public String removeFavorite(int fid){ public String removeFavorite(@RequestBody JSONObject body) {
JSONObject json=new JSONObject(); JSONArray md5s = body.getJSONArray("data");
int ret=favoritesDao.deleteByPrimaryKey(fid); JSONObject json = new JSONObject();
json.put("code",ret==0?-1:0); for (Object md5 : md5s) {
json.put("msg",ret==0?"移除失败":"已从收藏夹中移除"); LinkedHashMap<String,Object> js= (LinkedHashMap<String, Object>) md5;
MusicFavoritesExample example = new MusicFavoritesExample();
example.createCriteria().andMusisMd5EqualTo((String) js.get("md5"))
.andFavoriteidEqualTo((Integer) js.get("fid"));
favoritesDao.deleteByExample(example);
}
json.put("code", 0);
json.put("msg", "已从收藏夹中移除");
return json.toJSONString();
}
@RequestMapping("get.do")
@ResponseBody
public String getFavorite(@RequestBody JSONObject body) {
int fid = body.getInteger("fid");
JSONObject json = new JSONObject();
try {
MusicFavoritesExample example = new MusicFavoritesExample();
example.createCriteria().andFavoriteidEqualTo(fid);
List<MusicFavorites> list = favoritesDao.selectByExample(example);
List<MusicData> dataList = new ArrayList<>();
for (MusicFavorites favorites : list) {
MusicDataExample dataExample = new MusicDataExample();
dataExample.createCriteria().andMd5EqualTo(favorites.getMusisMd5());
try {
dataList.add(musicDataDao.selectByExample(dataExample).get(0));
} catch (Exception ignored) {
}
}
list.clear();
json.put("code", 0);
json.put("data", JSONArray.toJSON(dataList));
} catch (Exception e) {
e.printStackTrace();
json.put("code", -1);
json.put("msg", e.getLocalizedMessage());
json.put("data", new ArrayList<>());
}
return json.toJSONString(); return json.toJSONString();
} }
} }

View File

@ -29,7 +29,7 @@ public class ConfigTools {
} }
public static Object load(String type,String key){ public static Object load(String type,String key){
File file=new File(type); File file=new File(type);
System.out.println(type+"配置文件地址:"+file.getAbsolutePath()); //System.out.println(type+"配置文件地址:"+file.getAbsolutePath());
String src=readFile(file); String src=readFile(file);
if(src!=null){ if(src!=null){
JSONObject json=JSONObject.parseObject(src); JSONObject json=JSONObject.parseObject(src);

View File

@ -201,7 +201,7 @@ public class MusicTools implements MusicToolsService {
@Override @Override
public MusicData getMusicData(String path) { public MusicData getMusicData(String path) {
MusicDataExample example = new MusicDataExample(); MusicDataExample example = new MusicDataExample();
example.createCriteria().andFileEqualTo(Tools.base64ToString(path)); example.createCriteria().andFileEqualTo(path);
List<MusicData> list = musicDataDao.selectByExample(example); List<MusicData> list = musicDataDao.selectByExample(example);
if (list != null && list.size() > 0) { if (list != null && list.size() > 0) {
return list.get(0); return list.get(0);

View File

@ -276,23 +276,23 @@
</if> </if>
</select> </select>
<select id="selectByRegexp" resultType="com.yutou.tools.mybatis.model.MusicData"> <select id="selectByRegexp" resultType="com.yutou.tools.mybatis.model.MusicData">
select * from web_tools.music_data where `file` REGEXP #{regexp,jdbcType=VARCHAR} select * from music_data where `file` REGEXP #{regexp,jdbcType=VARCHAR}
</select> </select>
<select id="selectAllAlbum" resultType="java.lang.String"> <select id="selectAllAlbum" resultType="java.lang.String">
SELECT album FROM web_tools.music_data group by `album`; SELECT album FROM music_data group by `album`;
</select> </select>
<select id="selectAllArtist" resultType="java.lang.String"> <select id="selectAllArtist" resultType="java.lang.String">
SELECT artist FROM web_tools.music_data group by `artist`; SELECT artist FROM music_data group by `artist`;
</select> </select>
<select id="selectAlbum" resultType="com.yutou.tools.mybatis.model.MusicData"> <select id="selectAlbum" resultType="com.yutou.tools.mybatis.model.MusicData">
select select
<include refid="Base_Column_List" /> <include refid="Base_Column_List" />
from web_tools.music_data where `album`= #{album,jdbcType=VARCHAR} from music_data where `album`= #{album,jdbcType=VARCHAR}
</select> </select>
<select id="selectArtist" resultType="com.yutou.tools.mybatis.model.MusicData"> <select id="selectArtist" resultType="com.yutou.tools.mybatis.model.MusicData">
select select
<include refid="Base_Column_List" /> <include refid="Base_Column_List" />
from web_tools.music_data where `artist`= #{artist,jdbcType=VARCHAR} from music_data where `artist`= #{artist,jdbcType=VARCHAR}
</select> </select>
<update id="updateByExampleSelective" parameterType="map"> <update id="updateByExampleSelective" parameterType="map">
update music_data update music_data