准备上传oss做云备份
This commit is contained in:
parent
e9ff6a0207
commit
44e0a866a3
@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.nas.mybatis.model.MusicData;
|
||||
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 org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.MediaType;
|
||||
@ -35,7 +35,6 @@ public class MusicController {
|
||||
@ResponseBody
|
||||
public String getAllMusicList() {
|
||||
JSONObject json = new JSONObject();
|
||||
JSONObject data = new JSONObject();
|
||||
json.put("code", 0);
|
||||
json.put("scan", musicTools.isScan());
|
||||
json.put("size", musicTools.getLength());
|
||||
|
@ -1,6 +1,8 @@
|
||||
package com.yutou.nas.Controllers;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.yutou.nas.utils.HttpTools;
|
||||
import com.yutou.nas.utils.OSSManager;
|
||||
import com.yutou.nas.utils.RedisTools;
|
||||
import com.yutou.nas.utils.Tools;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@ -9,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@Controller
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.yutou.nas.utils.Interfaces;
|
||||
package com.yutou.nas.Services;
|
||||
|
||||
|
||||
import com.yutou.nas.mybatis.model.MusicData;
|
@ -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.mybatis.dao.MusicDataDao;
|
||||
import com.yutou.nas.mybatis.model.MusicData;
|
||||
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.AudioFileIO;
|
||||
import ealvatag.audio.AudioHeader;
|
||||
@ -52,11 +56,10 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
|
||||
if (isScan) {
|
||||
return;
|
||||
}
|
||||
if (ConfigTools.load(ConfigTools.CONFIG, "musicScan").equals("false")) {
|
||||
if (ConfigTools.load(ConfigTools.CONFIG, "musicScan",boolean.class,false)) {
|
||||
return;
|
||||
}
|
||||
musicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir");
|
||||
musicDataDao.truncate();
|
||||
com.yutou.nas.utils.Log.i("执行扫描:" + musicPath);
|
||||
new Thread(() -> {
|
||||
long startTime = System.currentTimeMillis();
|
||||
@ -182,7 +185,10 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
|
||||
MusicData data = getMetadata(file);
|
||||
if (data != null) {
|
||||
try {
|
||||
if(getMusicData(file.getAbsolutePath())==null) {
|
||||
musicDataDao.insert(data);
|
||||
OSSManager.upload(OSSManager.TYPE_MUSIC,musicPath,file);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
QQBotManager.getInstance().sendMessage("音乐文件添加失败:" + data.toString());
|
||||
@ -384,8 +390,9 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
|
||||
data.setDurationasdouble(format.duration);
|
||||
data.setBitrate((int) (format.bit_rate / 1000));
|
||||
if (stream != null) {
|
||||
if (data.getBitrate() == 0)
|
||||
if (data.getBitrate() == 0) {
|
||||
data.setBitrate((int) (stream.bit_rate / 1000));
|
||||
}
|
||||
data.setChannelcount(stream.channels);
|
||||
data.setLossless(0);
|
||||
data.setSamplerate(stream.sample_rate);
|
||||
@ -469,6 +476,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
|
||||
return find(by, FIND_ARTIST);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MusicData> getMusicList() {
|
||||
return musicDataDao.selectByExample(new MusicDataExample());
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.yutou.nas.utils;
|
||||
|
||||
import com.yutou.nas.Services.impl.MusicToolsServiceImpl;
|
||||
import com.yutou.nas.other.QQSetu;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
|
@ -55,21 +55,23 @@ public class BTDownloadManager implements ApplicationContextAware {
|
||||
JSONObject json = JSONObject.parseObject(_json);
|
||||
download(item, json);
|
||||
} else {
|
||||
QQBotManager.getInstance().sendMessage(item.getTitle() + "\n下载失败");
|
||||
QQBotManager.getInstance().sendMessage(item.getTitle() + "\n下载失败\n"+getDmhyUrl(item));
|
||||
Log.i(item.getTitle() + "\n下载失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getRSSUrl(BangumiItem item) {
|
||||
String url = "https://api.rss2json.com/v1/api.json?rss_url=%s&api_key=wtfm5pebya13pnl8rtu51wfgfpte0mb9sap1foll&count=500";
|
||||
private String getDmhyUrl(BangumiItem item){
|
||||
String dmhyUrl = "http://share.dmhy.org/topics/rss/page/1/rss.xml?keyword=%s%s%s&order=date-desc";
|
||||
dmhyUrl = String.format(dmhyUrl,
|
||||
item.getTitlekey().replace(" ", "+"),
|
||||
"-1".equals(item.getCategories()) ? "" : String.format("&sort_id=%s", item.getCategories()),
|
||||
"-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 {
|
||||
return String.format(url, URLEncoder.encode(dmhyUrl, "UTF-8"));
|
||||
return String.format(url, URLEncoder.encode(getDmhyUrl(item), "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
|
@ -3,62 +3,72 @@ package com.yutou.nas.utils;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 配置和参数
|
||||
*/
|
||||
public class ConfigTools {
|
||||
public static final String CONFIG="config.json";
|
||||
public static final String DATA="data.json";
|
||||
public static final String SQLITE="sqlite.json";
|
||||
public static final String CONFIG = "config.json";
|
||||
public static final String DATA = "data.json";
|
||||
public static final String SQLITE = "sqlite.json";
|
||||
|
||||
static {
|
||||
try {
|
||||
File file=new File(CONFIG);
|
||||
if(!file.exists()){
|
||||
File file = new File(CONFIG);
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
file=new File(DATA);
|
||||
if(!file.exists()){
|
||||
file = new File(DATA);
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
file=null;
|
||||
}catch (Exception e){
|
||||
file = null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
public static Object load(String type,String key){
|
||||
File file=new File(type);
|
||||
|
||||
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);
|
||||
//com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath());
|
||||
String src=readFile(file);
|
||||
if(src!=null){
|
||||
String src = readFile(file);
|
||||
if (src != null) {
|
||||
try {
|
||||
JSONObject json=JSONObject.parseObject(src);
|
||||
if(json==null){
|
||||
json=new JSONObject();
|
||||
saveFile(file,json.toJSONString());
|
||||
}
|
||||
return json.getOrDefault(key, "");
|
||||
}catch (Exception e){
|
||||
return "";
|
||||
JSONObject json = JSONObject.parseObject(src);
|
||||
return json.getObject(key, t);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
return "";
|
||||
|
||||
return def;
|
||||
}
|
||||
public static boolean save(String type,String key,Object data){
|
||||
File file=new File(type);
|
||||
String src=readFile(file);
|
||||
if(src==null){
|
||||
src="{}";
|
||||
|
||||
public static boolean save(String type, String key, Object data) {
|
||||
File file = new File(type);
|
||||
String src = readFile(file);
|
||||
if (src == null) {
|
||||
src = "{}";
|
||||
}
|
||||
JSONObject json=JSONObject.parseObject(src);
|
||||
json.put(key,data);
|
||||
saveFile(file,json.toJSONString());
|
||||
JSONObject json = JSONObject.parseObject(src);
|
||||
json.put(key, data);
|
||||
saveFile(file, json.toJSONString());
|
||||
return false;
|
||||
}
|
||||
public static boolean saveFile(File file,String data){
|
||||
|
||||
public static boolean saveFile(File file, String data) {
|
||||
try {
|
||||
FileWriter writer=new FileWriter(file);
|
||||
FileWriter writer = new FileWriter(file);
|
||||
writer.write(data);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
@ -68,12 +78,13 @@ public class ConfigTools {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static String readFile(File file){
|
||||
|
||||
public static String readFile(File file) {
|
||||
try {
|
||||
BufferedReader reader=new BufferedReader(new FileReader(file));
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String tmp;
|
||||
StringBuilder str= new StringBuilder();
|
||||
while ((tmp=reader.readLine())!=null){
|
||||
StringBuilder str = new StringBuilder();
|
||||
while ((tmp = reader.readLine()) != null) {
|
||||
str.append(tmp);
|
||||
}
|
||||
reader.close();
|
||||
|
10
src/main/java/com/yutou/nas/utils/DepotManager.java
Normal file
10
src/main/java/com/yutou/nas/utils/DepotManager.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.yutou.nas.utils;
|
||||
|
||||
public class DepotManager {
|
||||
private DepotManager(){
|
||||
|
||||
}
|
||||
public static void scan(){
|
||||
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ public class 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",
|
||||
AppTools.getToDayNowTimeToString(),
|
||||
log
|
||||
|
87
src/main/java/com/yutou/nas/utils/OSSManager.java
Normal file
87
src/main/java/com/yutou/nas/utils/OSSManager.java
Normal 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;
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package com.yutou.nas.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.*;
|
||||
|
||||
public class StreamTools {
|
||||
public static String streamReadLine(InputStream stream) {
|
||||
@ -19,4 +17,25 @@ public class StreamTools {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user