准备上传oss做云备份

This commit is contained in:
yutou 2021-06-09 18:31:48 +08:00
parent e9ff6a0207
commit 44e0a866a3
11 changed files with 194 additions and 54 deletions

View File

@ -4,7 +4,7 @@ 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.mybatis.model.MusicData;
import com.yutou.nas.utils.ConfigTools; import com.yutou.nas.utils.ConfigTools;
import com.yutou.nas.utils.MusicToolsServiceImpl; import com.yutou.nas.Services.impl.MusicToolsServiceImpl;
import com.yutou.nas.utils.Tools; 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;
@ -35,7 +35,6 @@ public class MusicController {
@ResponseBody @ResponseBody
public String getAllMusicList() { public String getAllMusicList() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
JSONObject data = new JSONObject();
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());

View File

@ -1,6 +1,8 @@
package com.yutou.nas.Controllers; package com.yutou.nas.Controllers;
import com.aliyun.oss.OSS;
import com.yutou.nas.utils.HttpTools; import com.yutou.nas.utils.HttpTools;
import com.yutou.nas.utils.OSSManager;
import com.yutou.nas.utils.RedisTools; import com.yutou.nas.utils.RedisTools;
import com.yutou.nas.utils.Tools; import com.yutou.nas.utils.Tools;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -9,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException; import java.io.IOException;
@Controller @Controller

View File

@ -1,4 +1,4 @@
package com.yutou.nas.utils.Interfaces; package com.yutou.nas.Services;
import com.yutou.nas.mybatis.model.MusicData; import com.yutou.nas.mybatis.model.MusicData;

View File

@ -1,11 +1,15 @@
package com.yutou.nas.utils; package com.yutou.nas.Services.impl;
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;
import com.yutou.nas.mybatis.model.MusicDataExample; import com.yutou.nas.mybatis.model.MusicDataExample;
import com.yutou.nas.utils.Interfaces.IMusicToolsService; import com.yutou.nas.Services.IMusicToolsService;
import com.yutou.nas.utils.ConfigTools;
import com.yutou.nas.utils.OSSManager;
import com.yutou.nas.utils.QQBotManager;
import com.yutou.nas.utils.Tools;
import ealvatag.audio.AudioFile; import ealvatag.audio.AudioFile;
import ealvatag.audio.AudioFileIO; import ealvatag.audio.AudioFileIO;
import ealvatag.audio.AudioHeader; import ealvatag.audio.AudioHeader;
@ -52,11 +56,10 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
if (isScan) { if (isScan) {
return; return;
} }
if (ConfigTools.load(ConfigTools.CONFIG, "musicScan").equals("false")) { if (ConfigTools.load(ConfigTools.CONFIG, "musicScan",boolean.class,false)) {
return; return;
} }
musicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir"); musicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir");
musicDataDao.truncate();
com.yutou.nas.utils.Log.i("执行扫描:" + musicPath); com.yutou.nas.utils.Log.i("执行扫描:" + musicPath);
new Thread(() -> { new Thread(() -> {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
@ -182,7 +185,10 @@ 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) {
musicDataDao.insert(data); musicDataDao.insert(data);
OSSManager.upload(OSSManager.TYPE_MUSIC,musicPath,file);
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
QQBotManager.getInstance().sendMessage("音乐文件添加失败:" + data.toString()); QQBotManager.getInstance().sendMessage("音乐文件添加失败:" + data.toString());
@ -384,8 +390,9 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
data.setDurationasdouble(format.duration); data.setDurationasdouble(format.duration);
data.setBitrate((int) (format.bit_rate / 1000)); data.setBitrate((int) (format.bit_rate / 1000));
if (stream != null) { if (stream != null) {
if (data.getBitrate() == 0) if (data.getBitrate() == 0) {
data.setBitrate((int) (stream.bit_rate / 1000)); data.setBitrate((int) (stream.bit_rate / 1000));
}
data.setChannelcount(stream.channels); data.setChannelcount(stream.channels);
data.setLossless(0); data.setLossless(0);
data.setSamplerate(stream.sample_rate); data.setSamplerate(stream.sample_rate);
@ -469,6 +476,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
return find(by, FIND_ARTIST); return find(by, FIND_ARTIST);
} }
@Override
public List<MusicData> getMusicList() { public List<MusicData> getMusicList() {
return musicDataDao.selectByExample(new MusicDataExample()); return musicDataDao.selectByExample(new MusicDataExample());
} }

View File

@ -1,5 +1,6 @@
package com.yutou.nas.utils; package com.yutou.nas.utils;
import com.yutou.nas.Services.impl.MusicToolsServiceImpl;
import com.yutou.nas.other.QQSetu; import com.yutou.nas.other.QQSetu;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;

View File

@ -55,21 +55,23 @@ public class BTDownloadManager implements ApplicationContextAware {
JSONObject json = JSONObject.parseObject(_json); JSONObject json = JSONObject.parseObject(_json);
download(item, json); download(item, json);
} else { } else {
QQBotManager.getInstance().sendMessage(item.getTitle() + "\n下载失败"); QQBotManager.getInstance().sendMessage(item.getTitle() + "\n下载失败\n"+getDmhyUrl(item));
Log.i(item.getTitle() + "\n下载失败"); Log.i(item.getTitle() + "\n下载失败");
} }
} }
} }
private String getDmhyUrl(BangumiItem item){
private String getRSSUrl(BangumiItem item) {
String url = "https://api.rss2json.com/v1/api.json?rss_url=%s&api_key=wtfm5pebya13pnl8rtu51wfgfpte0mb9sap1foll&count=500";
String dmhyUrl = "http://share.dmhy.org/topics/rss/page/1/rss.xml?keyword=%s%s%s&order=date-desc"; String dmhyUrl = "http://share.dmhy.org/topics/rss/page/1/rss.xml?keyword=%s%s%s&order=date-desc";
dmhyUrl = String.format(dmhyUrl, dmhyUrl = String.format(dmhyUrl,
item.getTitlekey().replace(" ", "+"), item.getTitlekey().replace(" ", "+"),
"-1".equals(item.getCategories()) ? "" : String.format("&sort_id=%s", item.getCategories()), "-1".equals(item.getCategories()) ? "" : String.format("&sort_id=%s", item.getCategories()),
"-1".equals(item.getAuthor()) ? "" : String.format("&team_id=%s", item.getAuthor())); "-1".equals(item.getAuthor()) ? "" : String.format("&team_id=%s", item.getAuthor()));
return dmhyUrl;
}
private String getRSSUrl(BangumiItem item) {
String url = "https://api.rss2json.com/v1/api.json?rss_url=%s&api_key=wtfm5pebya13pnl8rtu51wfgfpte0mb9sap1foll&count=500";
try { try {
return String.format(url, URLEncoder.encode(dmhyUrl, "UTF-8")); return String.format(url, URLEncoder.encode(getDmhyUrl(item), "UTF-8"));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;

View File

@ -3,6 +3,8 @@ package com.yutou.nas.utils;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import java.io.*; import java.io.*;
import java.util.HashSet;
import java.util.Set;
/** /**
* 配置和参数 * 配置和参数
@ -11,6 +13,7 @@ public class ConfigTools {
public static final String CONFIG = "config.json"; public static final String CONFIG = "config.json";
public static final String DATA = "data.json"; public static final String DATA = "data.json";
public static final String SQLITE = "sqlite.json"; public static final String SQLITE = "sqlite.json";
static { static {
try { try {
File file = new File(CONFIG); File file = new File(CONFIG);
@ -27,24 +30,30 @@ public class ConfigTools {
} }
} }
public static Object load(String type, String key) { public static Object load(String type, String key) {
return load(type, key, Object.class, null);
}
public static <T> T load(String type, String key, Class<T> t) {
return load(type, key, t, null);
}
public static <T> T load(String type, String key, Class<T> t, T def) {
File file = new File(type); File file = new File(type);
//com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath()); //com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath());
String src = readFile(file); String src = readFile(file);
if (src != null) { if (src != null) {
try { try {
JSONObject json = JSONObject.parseObject(src); JSONObject json = JSONObject.parseObject(src);
if(json==null){ return json.getObject(key, t);
json=new JSONObject();
saveFile(file,json.toJSONString());
}
return json.getOrDefault(key, "");
} catch (Exception e) { } catch (Exception e) {
return "";
} }
} }
return "";
return def;
} }
public static boolean save(String type, String key, Object data) { public static boolean save(String type, String key, Object data) {
File file = new File(type); File file = new File(type);
String src = readFile(file); String src = readFile(file);
@ -56,6 +65,7 @@ public class ConfigTools {
saveFile(file, json.toJSONString()); saveFile(file, json.toJSONString());
return false; return false;
} }
public static boolean saveFile(File file, String data) { public static boolean saveFile(File file, String data) {
try { try {
FileWriter writer = new FileWriter(file); FileWriter writer = new FileWriter(file);
@ -68,6 +78,7 @@ public class ConfigTools {
return false; return false;
} }
} }
public static String readFile(File file) { public static String readFile(File file) {
try { try {
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));

View File

@ -0,0 +1,10 @@
package com.yutou.nas.utils;
public class DepotManager {
private DepotManager(){
}
public static void scan(){
}
}

View File

@ -6,7 +6,7 @@ public class Log {
} }
public static void i(Object log) { public static void i(Object log) {
if ("true".equals(ConfigTools.load(ConfigTools.CONFIG, "logout"))) { if (ConfigTools.load(ConfigTools.CONFIG, "logout",boolean.class,false)) {
System.out.printf("[%s]%s%n", System.out.printf("[%s]%s%n",
AppTools.getToDayNowTimeToString(), AppTools.getToDayNowTimeToString(),
log log

View File

@ -0,0 +1,87 @@
package com.yutou.nas.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.StorageClass;
import com.yutou.nas.interfaces.DownloadInterface;
import java.io.File;
import java.io.FileNotFoundException;
public class OSSManager {
public static final String TYPE_MUSIC = "oss-name-music";
public static final String TYPE_PHOTO = "oss-name-photo";
public static final String TYPE_DEPOT = "oss-name-depot";
private static OSS getOssClient() {
return new OSSClientBuilder().build(ConfigTools.load(ConfigTools.CONFIG, "oss-url", String.class),
ConfigTools.load(ConfigTools.CONFIG, "oss-id", String.class),
ConfigTools.load(ConfigTools.CONFIG, "oss-key", String.class));
}
private static void closeClient(OSS oss) {
oss.shutdown();
}
public static void upload(String type, String path, File... files) {
if (type.equals(TYPE_MUSIC) && !ConfigTools.load(ConfigTools.CONFIG, TYPE_MUSIC, boolean.class, false)) {
return;
}
if (type.equals(TYPE_PHOTO) && !ConfigTools.load(ConfigTools.CONFIG, TYPE_PHOTO, boolean.class, false)) {
return;
}
if (type.equals(TYPE_DEPOT) && !ConfigTools.load(ConfigTools.CONFIG, TYPE_DEPOT, boolean.class, false)) {
return;
}
new Thread(() -> {
OSS client = getOssClient();
for (File file : files) {
if (file.isDirectory()) {
continue;
}
String uploadPath = file.getAbsolutePath().replace(path, "").replace(File.separator, "/");
if (uploadPath.startsWith("/")) {
uploadPath = uploadPath.substring(1);
}
client.putObject(type, uploadPath, file);
}
closeClient(client);
}).start();
}
public static void download(String type, String path, DownloadInterface downloadInterface) {
new Thread(() -> {
OSS oss = getOssClient();
ObjectMetadata metadata = oss.getObjectMetadata(type, path);
if (metadata.getObjectStorageClass() == StorageClass.Archive) {
oss.restoreObject(type, path);
do {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
metadata = oss.getObjectMetadata(type, path);
} while (!metadata.isRestoreCompleted());
}
OSSObject obj = oss.getObject(type, path);
File file = StreamTools.streamSave(obj.getObjectContent());
if (file != null) {
downloadInterface.onDownload(file);
} else {
downloadInterface.onError(new FileNotFoundException("没有该文件"));
}
closeClient(oss);
}).start();
}
private static boolean isExist(String type,String path){
OSS oss=getOssClient();
boolean flag=oss.doesObjectExist(type,path,true);
closeClient(oss);
return flag;
}
}

View File

@ -1,8 +1,6 @@
package com.yutou.nas.utils; package com.yutou.nas.utils;
import java.io.BufferedReader; import java.io.*;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StreamTools { public class StreamTools {
public static String streamReadLine(InputStream stream) { public static String streamReadLine(InputStream stream) {
@ -19,4 +17,25 @@ public class StreamTools {
} }
return builder.toString(); return builder.toString();
} }
public static File streamSave(InputStream stream) {
try {
if (stream != null) {
File file = new File("tmp" + File.separator + System.currentTimeMillis());
FileOutputStream outputStream = new FileOutputStream(file);
byte[] bytes = new byte[2048];
int len;
while ((len = stream.read(bytes)) > -1) {
outputStream.write(bytes, 0, len);
}
outputStream.flush();
stream.close();
outputStream.close();
return file;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
} }