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

View File

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

View File

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

View File

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

View File

@ -276,23 +276,23 @@
</if>
</select>
<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 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 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 id="selectAlbum" resultType="com.yutou.tools.mybatis.model.MusicData">
select
<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 id="selectArtist" resultType="com.yutou.tools.mybatis.model.MusicData">
select
<include refid="Base_Column_List" />
from web_tools.music_data where `artist`= #{artist,jdbcType=VARCHAR}
from music_data where `artist`= #{artist,jdbcType=VARCHAR}
</select>
<update id="updateByExampleSelective" parameterType="map">
update music_data