支持分享音乐文件夹
新增在获取音乐数据时,隐藏实际文件路径的功能 调整原播放音乐提供路径改为提供音乐MD5
This commit is contained in:
@@ -2,9 +2,12 @@ package com.yutou.nas.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.nas.mybatis.model.MusicData;
|
||||
import com.yutou.nas.utils.*;
|
||||
import com.yutou.nas.Services.impl.MusicToolsServiceImpl;
|
||||
import com.yutou.nas.mybatis.model.MusicData;
|
||||
import com.yutou.nas.utils.ConfigTools;
|
||||
import com.yutou.nas.utils.RedisTools;
|
||||
import com.yutou.nas.utils.StringUtils;
|
||||
import com.yutou.nas.utils.Tools;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -15,10 +18,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.yutou.nas.Datas.AppData.defaultMusicPath;
|
||||
@@ -36,7 +37,7 @@ 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(true)));
|
||||
return json.toJSONString();
|
||||
}
|
||||
|
||||
@@ -58,7 +59,7 @@ public class MusicController {
|
||||
json.put("code", 0);
|
||||
json.put("scan", musicTools.isScan());
|
||||
json.put("size", musicTools.getLength());
|
||||
json.put("data", JSONArray.toJSON(musicTools.getPath(path, type)));
|
||||
json.put("data", JSONArray.toJSON(musicTools.getPath(path, type, true)));
|
||||
return json.toJSONString();
|
||||
}
|
||||
|
||||
@@ -71,7 +72,7 @@ public class MusicController {
|
||||
if (StringUtils.isEmpty(album)) {
|
||||
json.put("data", JSONArray.toJSON(musicTools.getAllAlbum()));
|
||||
} else {
|
||||
json.put("data", JSONArray.toJSON(musicTools.selectAlbum(album)));
|
||||
json.put("data", JSONArray.toJSON(musicTools.selectAlbum(album, true)));
|
||||
}
|
||||
return json.toJSONString();
|
||||
}
|
||||
@@ -85,7 +86,7 @@ public class MusicController {
|
||||
if (StringUtils.isEmpty(artist)) {
|
||||
json.put("data", JSONArray.toJSON(musicTools.getAllArtist()));
|
||||
} else {
|
||||
json.put("data", JSONArray.toJSON(musicTools.selectArtist(artist)));
|
||||
json.put("data", JSONArray.toJSON(musicTools.selectArtist(artist, true)));
|
||||
}
|
||||
return json.toJSONString();
|
||||
}
|
||||
@@ -104,9 +105,7 @@ 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);
|
||||
@@ -114,7 +113,7 @@ public class MusicController {
|
||||
return json.toJSONString();
|
||||
}
|
||||
json.put("code", 0);
|
||||
json.put("data", musicTools.getMusicData(path));
|
||||
json.put("data", musicTools.getMusicDataOfMd5(path, true));
|
||||
return json.toJSONString();
|
||||
}
|
||||
|
||||
@@ -147,24 +146,9 @@ public class MusicController {
|
||||
@RequestMapping(value = "/nas/music/image.do", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
@ResponseBody
|
||||
public byte[] getImage(@RequestBody JSONObject body) {
|
||||
String fileName = body.getString("fileName");
|
||||
if (!fileName.startsWith(defaultMusicPath)) {
|
||||
fileName = Tools.base64ToString(fileName);
|
||||
}
|
||||
List<MusicData> list = null;
|
||||
if ("album".equals(body.getString("type"))) {
|
||||
list = musicTools.selectAlbum(fileName);
|
||||
}
|
||||
File file;
|
||||
if (list != null && !list.isEmpty()) {
|
||||
fileName = list.get(0).getFile();
|
||||
}
|
||||
if(!fileName.startsWith("/media/yutou/disk_lvm/public/音乐/")
|
||||
||fileName.endsWith("..")
|
||||
||fileName.endsWith("../")){
|
||||
return null;
|
||||
}
|
||||
file = new File(fileName);
|
||||
String md5 = body.getString("fileName");
|
||||
|
||||
File file = musicTools.getMusicOfMd5(md5, false);
|
||||
if (file.exists()) {
|
||||
try {
|
||||
return musicTools.readImage(file.getAbsolutePath());
|
||||
@@ -178,83 +162,87 @@ public class MusicController {
|
||||
@RequestMapping("/nas/music/random.do")
|
||||
@ResponseBody
|
||||
public String random() {
|
||||
List<MusicData> list = musicTools.getMusicList();
|
||||
List<MusicData> list = musicTools.getMusicList(true);
|
||||
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"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
json.put("data", data.getMd5());
|
||||
return json.toJSONString();
|
||||
}
|
||||
|
||||
private String getBase64(String str) {
|
||||
return new String(Base64.getEncoder().encode(str.getBytes()));
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/nas/music/play.do")
|
||||
public ResponseEntity<FileSystemResource> play(String filePath, String random) {
|
||||
String _filePath;
|
||||
boolean _random;
|
||||
_random = !StringUtils.isEmpty(random) && "true".equals(random);
|
||||
_filePath = Tools.base64ToString(filePath);
|
||||
if (_random) {
|
||||
List<MusicData> list = musicTools.getMusicList();
|
||||
List<MusicData> list = musicTools.getMusicList(true);
|
||||
MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]);
|
||||
_filePath = data.getFile();
|
||||
filePath = data.getMd5();
|
||||
}
|
||||
if(!_filePath.startsWith("/media/yutou/disk_lvm/public/音乐/")
|
||||
||_filePath.endsWith("..")
|
||||
||_filePath.endsWith("../")){
|
||||
return null;
|
||||
}
|
||||
File file = new File(_filePath);
|
||||
|
||||
File file = musicTools.getMusicOfMd5(filePath, false);
|
||||
if (file.exists()) {
|
||||
return Tools.getFile(file);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/nas/music/share.do")
|
||||
@ResponseBody
|
||||
public JSONObject share(@RequestBody JSONObject data){
|
||||
public JSONObject share(@RequestBody JSONObject data) {
|
||||
System.out.println(data);
|
||||
String file=data.getString("file");
|
||||
String token=data.getString("token");
|
||||
JSONObject json=new JSONObject();
|
||||
File music=new File(file);
|
||||
if(music.exists()){
|
||||
String key=Tools.getMD5(UUID.randomUUID().toString()+music.getAbsolutePath());
|
||||
RedisTools.set(key,file,3600);
|
||||
JSONObject item=new JSONObject();
|
||||
item.put("share",key);
|
||||
json.put("code",1);
|
||||
json.put("msg","ok");
|
||||
json.put("data",item);
|
||||
}else{
|
||||
json.put("code",-1);
|
||||
json.put("msg","文件不存在");
|
||||
json.put("data","{}");
|
||||
String md5 = data.getString("file");
|
||||
boolean isDir = data.getBoolean("isDir");
|
||||
JSONObject json = new JSONObject();
|
||||
File music;
|
||||
if (isDir) {
|
||||
music = new File(md5);
|
||||
} else {
|
||||
music = musicTools.getMusicOfMd5(md5, false);
|
||||
}
|
||||
if (music.exists()) {
|
||||
String key = Tools.getMD5(UUID.randomUUID() + music.getAbsolutePath());
|
||||
RedisTools.set(key, music.getAbsolutePath(), 3600);
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("share", key);
|
||||
json.put("code", 1);
|
||||
json.put("msg", "ok");
|
||||
json.put("data", item);
|
||||
} else {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "文件不存在");
|
||||
json.put("data", "{}");
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@RequestMapping("/nas/music/playShare.do")
|
||||
@ResponseBody
|
||||
public JSONObject playShare(String token) throws UnsupportedEncodingException {
|
||||
JSONObject json=new JSONObject();
|
||||
String redis=RedisTools.get(token);
|
||||
if(redis!=null&&!"-999".equals(token)){
|
||||
String file=redis;
|
||||
JSONObject item=new JSONObject();
|
||||
item.put("file",file);
|
||||
json.put("code",0);
|
||||
json.put("data",item);
|
||||
}else{
|
||||
json.put("code",-1);
|
||||
json.put("msg","分享已过期");
|
||||
public JSONObject playShare(String token) {
|
||||
JSONObject json = new JSONObject();
|
||||
String redis = RedisTools.get(token);
|
||||
if (redis != null && !"-999".equals(token)) {
|
||||
File file = new File(redis);
|
||||
JSONArray array = new JSONArray();
|
||||
if (file.isFile()) {
|
||||
array.add(musicTools.getMusicData(file.getAbsolutePath(), true));
|
||||
} else {
|
||||
for (File listFile : Objects.requireNonNull(file.listFiles())) {
|
||||
MusicData data=musicTools.getMusicData(listFile.getAbsolutePath(), true);
|
||||
if(data!=null) {
|
||||
array.add(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("file", array);
|
||||
json.put("code", 0);
|
||||
json.put("data", item);
|
||||
} else {
|
||||
json.put("code", -1);
|
||||
json.put("msg", "分享已过期");
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user