支持分享音乐文件夹

新增在获取音乐数据时,隐藏实际文件路径的功能
调整原播放音乐提供路径改为提供音乐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,57 +162,49 @@ 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) {
System.out.println(data);
String file=data.getString("file");
String token=data.getString("token");
String md5 = data.getString("file");
boolean isDir = data.getBoolean("isDir");
JSONObject json = new JSONObject();
File music=new File(file);
File music;
if (isDir) {
music = new File(md5);
} else {
music = musicTools.getMusicOfMd5(md5, false);
}
if (music.exists()) {
String key=Tools.getMD5(UUID.randomUUID().toString()+music.getAbsolutePath());
RedisTools.set(key,file,3600);
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);
@ -241,15 +217,27 @@ public class MusicController {
}
return json;
}
@RequestMapping("/nas/music/playShare.do")
@ResponseBody
public JSONObject playShare(String token) throws UnsupportedEncodingException {
public JSONObject playShare(String token) {
JSONObject json = new JSONObject();
String redis = RedisTools.get(token);
if (redis != null && !"-999".equals(token)) {
String file=redis;
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",file);
item.put("file", array);
json.put("code", 0);
json.put("data", item);
} else {

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);
}

View File

@ -27,7 +27,7 @@
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
@ -43,7 +43,8 @@
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
<foreach close=")" collection="criterion.value" item="listItem" open="("
separator=",">
#{listItem}
</foreach>
</when>
@ -72,7 +73,8 @@
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
<foreach close=")" collection="criterion.value" item="listItem" open="("
separator=",">
#{listItem}
</foreach>
</when>
@ -84,32 +86,50 @@
</where>
</sql>
<sql id="Base_Column_List">
id, artist, album, title, `comment`, `year`, track, disc_no, composer, artist_sort,
id
, artist, album, title, `comment`, `year`, track, disc_no, composer, artist_sort,
`file`, lastDir, isDir, bitRate, sampleRate, noOfSamples, channelCount, encodingType,
durationAsDouble, lossless, variableBitRate, md5
</sql>
<sql id="Base_Column_ListNotFile">
id
, artist, album, title, `comment`, `year`, track, disc_no, composer, artist_sort,
isDir, bitRate, sampleRate, noOfSamples, channelCount, encodingType,
durationAsDouble, lossless, variableBitRate, md5
</sql>
<select id="selectByExample" parameterType="com.yutou.nas.mybatis.model.MusicDataExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
<if test="isDelFile ==true">
<include refid="Base_Column_ListNotFile"/>
</if>
<if test="isDelFile != true">
<include refid="Base_Column_List"/>
</if>
from music_data
<if test="_parameter != null">
<include refid="Example_Where_Clause"/>
</if>
<if test="orderByClause != null">
order by ${orderByClause}
<if test="example.orderByClause != null">
order by ${example.orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<if test="isDelFile ==true">
<include refid="Base_Column_ListNotFile"/>
</if>
<if test="isDelFile != true">
<include refid="Base_Column_List"/>
</if>
from music_data
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from music_data
delete
from music_data
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.yutou.nas.mybatis.model.MusicDataExample">
@ -118,25 +138,25 @@
<include refid="Example_Where_Clause"/>
</if>
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yutou.nas.mybatis.model.MusicData" useGeneratedKeys="true">
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yutou.nas.mybatis.model.MusicData"
useGeneratedKeys="true">
insert into music_data (artist, album, title,
`comment`, `year`, track,
disc_no, composer, artist_sort,
`file`, lastDir, isDir,
bitRate, sampleRate, noOfSamples,
channelCount, encodingType, durationAsDouble,
lossless, variableBitRate, md5
)
lossless, variableBitRate, md5)
values (#{artist,jdbcType=VARCHAR}, #{album,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR},
#{comment,jdbcType=VARCHAR}, #{year,jdbcType=VARCHAR}, #{track,jdbcType=VARCHAR},
#{discNo,jdbcType=VARCHAR}, #{composer,jdbcType=VARCHAR}, #{artistSort,jdbcType=VARCHAR},
#{file,jdbcType=VARCHAR}, #{lastdir,jdbcType=VARCHAR}, #{isdir,jdbcType=INTEGER},
#{bitrate,jdbcType=INTEGER}, #{samplerate,jdbcType=INTEGER}, #{noofsamples,jdbcType=BIGINT},
#{channelcount,jdbcType=INTEGER}, #{encodingtype,jdbcType=VARCHAR}, #{durationasdouble,jdbcType=DOUBLE},
#{lossless,jdbcType=INTEGER}, #{variablebitrate,jdbcType=INTEGER}, #{md5,jdbcType=VARCHAR}
)
#{lossless,jdbcType=INTEGER}, #{variablebitrate,jdbcType=INTEGER}, #{md5,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yutou.nas.mybatis.model.MusicData" useGeneratedKeys="true">
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yutou.nas.mybatis.model.MusicData"
useGeneratedKeys="true">
insert into music_data
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="artist != null">
@ -269,29 +289,55 @@
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.yutou.nas.mybatis.model.MusicDataExample" resultType="java.lang.Long">
<select id="countByExample" parameterType="com.yutou.nas.mybatis.model.MusicDataExample"
resultType="java.lang.Long">
select count(*) from music_data
<if test="_parameter != null">
<include refid="Example_Where_Clause"/>
</if>
</select>
<select id="selectByRegexp" resultType="com.yutou.nas.mybatis.model.MusicData">
select * from music_data where `file` REGEXP #{regexp,jdbcType=VARCHAR}
select
<if test="isDelFile ==true">
<include refid="Base_Column_ListNotFile"/>
</if>
<if test="isDelFile != true">
<include refid="Base_Column_List"/>
</if>
from music_data
where `file` REGEXP #{regexp,jdbcType=VARCHAR}
</select>
<select id="selectAllAlbum" resultType="java.lang.String">
SELECT album FROM music_data group by `album`;
SELECT album
FROM music_data
group by `album`;
</select>
<select id="selectAllArtist" resultType="java.lang.String">
SELECT artist FROM music_data group by `artist`;
SELECT artist
FROM music_data
group by `artist`;
</select>
<select id="selectAlbum" resultType="com.yutou.nas.mybatis.model.MusicData">
select
<if test="isDelFile ==true">
<include refid="Base_Column_ListNotFile"/>
</if>
<if test="isDelFile != true">
<include refid="Base_Column_List"/>
</if>
from music_data where `album`= #{album,jdbcType=VARCHAR}
</select>
<select id="selectArtist" resultType="com.yutou.nas.mybatis.model.MusicData">
select
<if test="isDelFile ==true">
<include refid="Base_Column_ListNotFile"/>
</if>
<if test="isDelFile != true">
<include refid="Base_Column_List"/>
</if>
from music_data where `artist`= #{artist,jdbcType=VARCHAR}
</select>
<update id="updateByExampleSelective" parameterType="map">