支持分享音乐文件夹

新增在获取音乐数据时,隐藏实际文件路径的功能
调整原播放音乐提供路径改为提供音乐MD5
This commit is contained in:
Yutousama 2022-01-06 02:59:08 +08:00
parent 006fb8c836
commit 11534b04f7
7 changed files with 644 additions and 579 deletions

View File

@ -2,9 +2,12 @@ package com.yutou.nas.Controllers;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; 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.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.core.io.FileSystemResource;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -15,10 +18,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import static com.yutou.nas.Datas.AppData.defaultMusicPath; import static com.yutou.nas.Datas.AppData.defaultMusicPath;
@ -36,7 +37,7 @@ 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(true)));
return json.toJSONString(); return json.toJSONString();
} }
@ -58,7 +59,7 @@ 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.getPath(path, type))); json.put("data", JSONArray.toJSON(musicTools.getPath(path, type, true)));
return json.toJSONString(); return json.toJSONString();
} }
@ -71,7 +72,7 @@ public class MusicController {
if (StringUtils.isEmpty(album)) { if (StringUtils.isEmpty(album)) {
json.put("data", JSONArray.toJSON(musicTools.getAllAlbum())); json.put("data", JSONArray.toJSON(musicTools.getAllAlbum()));
} else { } else {
json.put("data", JSONArray.toJSON(musicTools.selectAlbum(album))); json.put("data", JSONArray.toJSON(musicTools.selectAlbum(album, true)));
} }
return json.toJSONString(); return json.toJSONString();
} }
@ -85,7 +86,7 @@ public class MusicController {
if (StringUtils.isEmpty(artist)) { if (StringUtils.isEmpty(artist)) {
json.put("data", JSONArray.toJSON(musicTools.getAllArtist())); json.put("data", JSONArray.toJSON(musicTools.getAllArtist()));
} else { } else {
json.put("data", JSONArray.toJSON(musicTools.selectArtist(artist))); json.put("data", JSONArray.toJSON(musicTools.selectArtist(artist, true)));
} }
return json.toJSONString(); return json.toJSONString();
} }
@ -104,9 +105,7 @@ public class MusicController {
@ResponseBody @ResponseBody
public String findFile(@RequestBody JSONObject body) { public String findFile(@RequestBody JSONObject body) {
String path = body.getString("path"); String path = body.getString("path");
if (!path.startsWith(defaultMusicPath)) {
path = Tools.base64ToString(path);
}
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
if (StringUtils.isEmpty(path)) { if (StringUtils.isEmpty(path)) {
json.put("code", -1); json.put("code", -1);
@ -114,7 +113,7 @@ public class MusicController {
return json.toJSONString(); return json.toJSONString();
} }
json.put("code", 0); json.put("code", 0);
json.put("data", musicTools.getMusicData(path)); json.put("data", musicTools.getMusicDataOfMd5(path, true));
return json.toJSONString(); return json.toJSONString();
} }
@ -147,24 +146,9 @@ public class MusicController {
@RequestMapping(value = "/nas/music/image.do", produces = MediaType.IMAGE_JPEG_VALUE) @RequestMapping(value = "/nas/music/image.do", produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody @ResponseBody
public byte[] getImage(@RequestBody JSONObject body) { public byte[] getImage(@RequestBody JSONObject body) {
String fileName = body.getString("fileName"); String md5 = body.getString("fileName");
if (!fileName.startsWith(defaultMusicPath)) {
fileName = Tools.base64ToString(fileName); File file = musicTools.getMusicOfMd5(md5, false);
}
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);
if (file.exists()) { if (file.exists()) {
try { try {
return musicTools.readImage(file.getAbsolutePath()); return musicTools.readImage(file.getAbsolutePath());
@ -178,83 +162,87 @@ public class MusicController {
@RequestMapping("/nas/music/random.do") @RequestMapping("/nas/music/random.do")
@ResponseBody @ResponseBody
public String random() { 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]); 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 { json.put("data", data.getMd5());
json.put("data", URLEncoder.encode(getBase64(data.getFile()), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return json.toJSONString(); return json.toJSONString();
} }
private String getBase64(String str) {
return new String(Base64.getEncoder().encode(str.getBytes()));
}
@RequestMapping("/nas/music/play.do") @RequestMapping("/nas/music/play.do")
public ResponseEntity<FileSystemResource> play(String filePath, String random) { public ResponseEntity<FileSystemResource> play(String filePath, String random) {
String _filePath;
boolean _random; boolean _random;
_random = !StringUtils.isEmpty(random) && "true".equals(random); _random = !StringUtils.isEmpty(random) && "true".equals(random);
_filePath = Tools.base64ToString(filePath);
if (_random) { 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]); 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("..") File file = musicTools.getMusicOfMd5(filePath, false);
||_filePath.endsWith("../")){
return null;
}
File file = new File(_filePath);
if (file.exists()) { if (file.exists()) {
return Tools.getFile(file); return Tools.getFile(file);
} else { } else {
return null; return null;
} }
} }
@RequestMapping("/nas/music/share.do") @RequestMapping("/nas/music/share.do")
@ResponseBody @ResponseBody
public JSONObject share(@RequestBody JSONObject data){ public JSONObject share(@RequestBody JSONObject data) {
System.out.println(data); System.out.println(data);
String file=data.getString("file"); String md5 = data.getString("file");
String token=data.getString("token"); boolean isDir = data.getBoolean("isDir");
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
File music=new File(file); File music;
if(music.exists()){ if (isDir) {
String key=Tools.getMD5(UUID.randomUUID().toString()+music.getAbsolutePath()); music = new File(md5);
RedisTools.set(key,file,3600); } else {
JSONObject item=new JSONObject(); music = musicTools.getMusicOfMd5(md5, false);
item.put("share",key); }
json.put("code",1); if (music.exists()) {
json.put("msg","ok"); String key = Tools.getMD5(UUID.randomUUID() + music.getAbsolutePath());
json.put("data",item); RedisTools.set(key, music.getAbsolutePath(), 3600);
}else{ JSONObject item = new JSONObject();
json.put("code",-1); item.put("share", key);
json.put("msg","文件不存在"); json.put("code", 1);
json.put("data","{}"); json.put("msg", "ok");
json.put("data", item);
} else {
json.put("code", -1);
json.put("msg", "文件不存在");
json.put("data", "{}");
} }
return json; return json;
} }
@RequestMapping("/nas/music/playShare.do") @RequestMapping("/nas/music/playShare.do")
@ResponseBody @ResponseBody
public JSONObject playShare(String token) throws UnsupportedEncodingException { public JSONObject playShare(String token) {
JSONObject json=new JSONObject(); JSONObject json = new JSONObject();
String redis=RedisTools.get(token); String redis = RedisTools.get(token);
if(redis!=null&&!"-999".equals(token)){ if (redis != null && !"-999".equals(token)) {
String file=redis; File file = new File(redis);
JSONObject item=new JSONObject(); JSONArray array = new JSONArray();
item.put("file",file); if (file.isFile()) {
json.put("code",0); array.add(musicTools.getMusicData(file.getAbsolutePath(), true));
json.put("data",item); } else {
}else{ for (File listFile : Objects.requireNonNull(file.listFiles())) {
json.put("code",-1); MusicData data=musicTools.getMusicData(listFile.getAbsolutePath(), true);
json.put("msg","分享已过期"); 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; return json;
} }

View File

@ -146,7 +146,7 @@ public class MusicFavoritesController {
MusicDataExample dataExample = new MusicDataExample(); MusicDataExample dataExample = new MusicDataExample();
dataExample.createCriteria().andMd5EqualTo(favorites.getMusisMd5()); dataExample.createCriteria().andMd5EqualTo(favorites.getMusisMd5());
try { try {
dataList.add(musicDataDao.selectByExample(dataExample).get(0)); dataList.add(musicDataDao.selectByExample(dataExample,true).get(0));
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }

View File

@ -12,7 +12,7 @@ import org.springframework.context.annotation.Import;
@Import(BTDownloadManager.class) @Import(BTDownloadManager.class)
@SpringBootApplication @SpringBootApplication
public class NasApplication { public class NasApplication {
public static final String version="1.2.4"; public static final String version="1.2.5";
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(NasApplication.class, args); SpringApplication.run(NasApplication.class, args);
AppData.defaultMusicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir"); AppData.defaultMusicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir");

View File

@ -3,6 +3,7 @@ package com.yutou.nas.Services;
import com.yutou.nas.mybatis.model.MusicData; import com.yutou.nas.mybatis.model.MusicData;
import java.io.File;
import java.util.List; import java.util.List;
public interface IMusicToolsService { public interface IMusicToolsService {
@ -10,15 +11,16 @@ public interface IMusicToolsService {
void scanMusic(); void scanMusic();
List<MusicData> getPath(String path, boolean isDir); List<MusicData> getPath(String path, boolean isDir,boolean delPath);
MusicData getMusicData(String md5); MusicData getMusicDataOfMd5(String md5,boolean isDelFile);
MusicData getMusicData(String path,boolean isDelFile);
List<MusicData> findOfTitle(String title); List<MusicData> findOfTitle(String title,boolean isDelFile);
List<MusicData> findOfArtist(String by); List<MusicData> findOfArtist(String by,boolean isDelFile);
List<MusicData> getMusicList(); List<MusicData> getMusicList(boolean isDelFile);
int getLength(); int getLength();
@ -27,4 +29,6 @@ public interface IMusicToolsService {
String getMusicPath(); String getMusicPath();
byte[] readImage(String path) throws Exception; byte[] readImage(String path) throws Exception;
File getMusicOfMd5(String md5,boolean isDelFile);
} }

View File

@ -1,5 +1,6 @@
package com.yutou.nas.Services.impl; package com.yutou.nas.Services.impl;
import com.alibaba.fastjson.JSONObject;
import com.yutou.nas.Datas.AppData; import com.yutou.nas.Datas.AppData;
import com.yutou.nas.mybatis.dao.MusicDataDao; import com.yutou.nas.mybatis.dao.MusicDataDao;
import com.yutou.nas.mybatis.model.MusicData; import com.yutou.nas.mybatis.model.MusicData;
@ -108,7 +109,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
* @return 音乐列表 * @return 音乐列表
*/ */
@Override @Override
public List<MusicData> getPath(String path, boolean isDir) { public List<MusicData> getPath(String path, boolean isDir,boolean delPath) {
List<MusicData> list = new ArrayList<>(); List<MusicData> list = new ArrayList<>();
List<MusicData> main = new ArrayList<>(); List<MusicData> main = new ArrayList<>();
MusicDataExample example = new MusicDataExample(); MusicDataExample example = new MusicDataExample();
@ -119,7 +120,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
} }
if (isDir) { if (isDir) {
example.createCriteria().andFileLike(tmpPath.replace(File.separator, replacement) + "%"); example.createCriteria().andFileLike(tmpPath.replace(File.separator, replacement) + "%");
main = musicDataDao.selectByExample(example); main = musicDataDao.selectByExample(example,delPath);
} }
tmpPath = tmpPath.replace(File.separator, replacement) tmpPath = tmpPath.replace(File.separator, replacement)
.replace("+", "\\+") .replace("+", "\\+")
@ -127,7 +128,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
.replace("(", "\\(") .replace("(", "\\(")
.replace(")", "\\)") .replace(")", "\\)")
.replace("]", "\\]"); .replace("]", "\\]");
main.addAll(musicDataDao.selectByRegexp(tmpPath + replacement + "([^" + replacement + "]+)$")); main.addAll(musicDataDao.selectByRegexp(tmpPath + replacement + "([^" + replacement + "]+)$",delPath));
if (!path.equals(AppData.defaultMusicPath) && !"root".equals(path)) { if (!path.equals(AppData.defaultMusicPath) && !"root".equals(path)) {
MusicData t2 = new MusicData(); MusicData t2 = new MusicData();
@ -147,6 +148,8 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
return list; return list;
} }
public List<String> getAllAlbum() { public List<String> getAllAlbum() {
return musicDataDao.selectAllAlbum(); return musicDataDao.selectAllAlbum();
} }
@ -155,12 +158,12 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
return musicDataDao.selectAllArtist(); return musicDataDao.selectAllArtist();
} }
public List<MusicData> selectAlbum(String album) { public List<MusicData> selectAlbum(String album,boolean isDelFile) {
return musicDataDao.selectAlbum(album); return musicDataDao.selectAlbum(album,isDelFile);
} }
public List<MusicData> selectArtist(String artist) { public List<MusicData> selectArtist(String artist,boolean isDelFile) {
return musicDataDao.selectArtist(artist); return musicDataDao.selectArtist(artist,isDelFile);
} }
private void getDirList(String path, List<MusicData> list) { private void getDirList(String path, List<MusicData> list) {
@ -189,7 +192,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
MusicData data = getMetadata(file); MusicData data = getMetadata(file);
if (data != null) { if (data != null) {
try { try {
if (getMusicData(file.getAbsolutePath()) == null) { if (getMusicData(file.getAbsolutePath(),false) == null) {
// System.out.println(data); // System.out.println(data);
musicDataDao.insert(data); musicDataDao.insert(data);
} }
@ -202,10 +205,20 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
} }
@Override @Override
public MusicData getMusicData(String path) { public MusicData getMusicData(String path,boolean delFile) {
MusicDataExample example = new MusicDataExample(); MusicDataExample example = new MusicDataExample();
example.createCriteria().andFileEqualTo(path); example.createCriteria().andFileEqualTo(path);
List<MusicData> list = musicDataDao.selectByExample(example); List<MusicData> list = musicDataDao.selectByExample(example,delFile);
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
@Override
public MusicData getMusicDataOfMd5(String md5, boolean isDelFile) {
MusicDataExample example = new MusicDataExample();
example.createCriteria().andMd5EqualTo(md5);
List<MusicData> list = musicDataDao.selectByExample(example,isDelFile);
if (list != null && list.size() > 0) { if (list != null && list.size() > 0) {
return list.get(0); return list.get(0);
} }
@ -471,23 +484,23 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
} }
@Override @Override
public List<MusicData> findOfTitle(String title) { public List<MusicData> findOfTitle(String title,boolean delFile) {
return find(title, FIND_TITLE); return find(title, FIND_TITLE,delFile);
} }
@Override @Override
public List<MusicData> findOfArtist(String by) { public List<MusicData> findOfArtist(String by,boolean delFile) {
return find(by, FIND_ARTIST); return find(by, FIND_ARTIST,delFile);
} }
@Override @Override
public List<MusicData> getMusicList() { public List<MusicData> getMusicList(boolean delFile) {
return musicDataDao.selectByExample(new MusicDataExample()); return musicDataDao.selectByExample(new MusicDataExample(),delFile);
} }
@Override @Override
public int getLength() { public int getLength() {
return musicDataDao.selectByExample(new MusicDataExample()).size(); return musicDataDao.selectByExample(new MusicDataExample(),true).size();
} }
@Override @Override
@ -495,7 +508,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
return isScan; return isScan;
} }
private List<MusicData> find(String title, int type) { private List<MusicData> find(String title, int type,boolean delFile) {
List<MusicData> list; List<MusicData> list;
MusicDataExample example = new MusicDataExample(); MusicDataExample example = new MusicDataExample();
if (type == FIND_TITLE) { if (type == FIND_TITLE) {
@ -503,7 +516,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
} else if (type == FIND_ARTIST) { } else if (type == FIND_ARTIST) {
example.createCriteria().andArtistEqualTo(title); example.createCriteria().andArtistEqualTo(title);
} }
list = musicDataDao.selectByExample(example); list = musicDataDao.selectByExample(example,delFile);
return list; return list;
} }
@ -530,6 +543,19 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
return bytes; return bytes;
} }
@Override
public File getMusicOfMd5(String md5,boolean delFile) {
MusicDataExample example=new MusicDataExample();
example.createCriteria().andMd5EqualTo(md5);
List<MusicData> list=musicDataDao.selectByExample(example,delFile);
Log.i("Music Size",list.size());
if(list!=null&&!list.isEmpty()){
Log.i("Music File", list.get(0).toString());
return new File(list.get(0).getFile());
}
return null;
}
private byte[] readImageFile(File file) throws Exception { private byte[] readImageFile(File file) throws Exception {
String path = file.getAbsolutePath().replace(file.getName(), ""); String path = file.getAbsolutePath().replace(file.getName(), "");
File img = new File(path, "cover.jpg"); File img = new File(path, "cover.jpg");

View File

@ -19,9 +19,9 @@ public interface MusicDataDao {
int insertSelective(MusicData record); int insertSelective(MusicData record);
List<MusicData> selectByExample(MusicDataExample example); List<MusicData> selectByExample(@Param("example") MusicDataExample example,@Param("isDelFile") boolean isDelFile);
MusicData selectByPrimaryKey(Integer id); MusicData selectByPrimaryKey(@Param("id")Integer id,@Param("isDelFile")boolean isDelFile);
int updateByExampleSelective(@Param("record") MusicData record, @Param("example") MusicDataExample example); int updateByExampleSelective(@Param("record") MusicData record, @Param("example") MusicDataExample example);
@ -32,10 +32,11 @@ public interface MusicDataDao {
int updateByPrimaryKey(MusicData record); int updateByPrimaryKey(MusicData record);
void truncate(); void truncate();
List<MusicData> selectByRegexp(String regexp); List<MusicData> selectByRegexp(@Param("regexp")String regexp,@Param("isDelFile")boolean isDelFile);
List<String> selectAllAlbum(); List<String> selectAllAlbum();
List<String> selectAllArtist(); List<String> selectAllArtist();
List<MusicData> selectAlbum(String album); List<MusicData> selectAlbum(@Param("album")String album,@Param("isDelFile")boolean isDelFile);
List<MusicData> selectArtist(String artist); List<MusicData> selectArtist(@Param("artist")String artist,@Param("isDelFile")boolean isDelFile);
} }

File diff suppressed because it is too large Load Diff