支持分享音乐文件夹

新增在获取音乐数据时,隐藏实际文件路径的功能
调整原播放音乐提供路径改为提供音乐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.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;
}

View File

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

View File

@ -12,7 +12,7 @@ import org.springframework.context.annotation.Import;
@Import(BTDownloadManager.class)
@SpringBootApplication
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) {
SpringApplication.run(NasApplication.class, args);
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 java.io.File;
import java.util.List;
public interface IMusicToolsService {
@ -10,15 +11,16 @@ public interface IMusicToolsService {
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();
@ -27,4 +29,6 @@ public interface IMusicToolsService {
String getMusicPath();
byte[] readImage(String path) throws Exception;
File getMusicOfMd5(String md5,boolean isDelFile);
}

View File

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

View File

@ -19,9 +19,9 @@ public interface MusicDataDao {
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);
@ -32,10 +32,11 @@ public interface MusicDataDao {
int updateByPrimaryKey(MusicData record);
void truncate();
List<MusicData> selectByRegexp(String regexp);
List<MusicData> selectByRegexp(@Param("regexp")String regexp,@Param("isDelFile")boolean isDelFile);
List<String> selectAllAlbum();
List<String> selectAllArtist();
List<MusicData> selectAlbum(String album);
List<MusicData> selectArtist(String artist);
List<MusicData> selectAlbum(@Param("album")String album,@Param("isDelFile")boolean isDelFile);
List<MusicData> selectArtist(@Param("artist")String artist,@Param("isDelFile")boolean isDelFile);
}

File diff suppressed because it is too large Load Diff