From 8491879c2a25c4fc6c405f058ffbd8f4c9456d4c Mon Sep 17 00:00:00 2001 From: yutou Date: Fri, 18 Jun 2021 17:39:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E7=BA=BFOSS=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/yutou/nas/NasApplication.java | 2 +- .../Services/impl/MusicToolsServiceImpl.java | 1 - src/main/java/com/yutou/nas/other/tools.java | 8 +++- .../com/yutou/nas/utils/ApplicationInit.java | 3 ++ .../com/yutou/nas/utils/DepotManager.java | 40 ++++++++++++++++++- .../java/com/yutou/nas/utils/OSSManager.java | 28 ++++++++++--- 6 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/yutou/nas/NasApplication.java b/src/main/java/com/yutou/nas/NasApplication.java index 0288b7c..aa55454 100644 --- a/src/main/java/com/yutou/nas/NasApplication.java +++ b/src/main/java/com/yutou/nas/NasApplication.java @@ -12,7 +12,7 @@ import org.springframework.context.annotation.Import; @Import(BTDownloadManager.class) @SpringBootApplication public class NasApplication { - public static final String version="1.1.3"; + public static final String version="1.1.4"; public static void main(String[] args) { SpringApplication.run(NasApplication.class, args); AppData.defaultMusicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir"); diff --git a/src/main/java/com/yutou/nas/Services/impl/MusicToolsServiceImpl.java b/src/main/java/com/yutou/nas/Services/impl/MusicToolsServiceImpl.java index 7f51a0e..a6f5b69 100644 --- a/src/main/java/com/yutou/nas/Services/impl/MusicToolsServiceImpl.java +++ b/src/main/java/com/yutou/nas/Services/impl/MusicToolsServiceImpl.java @@ -187,7 +187,6 @@ public class MusicToolsServiceImpl implements IMusicToolsService { try { if(getMusicData(file.getAbsolutePath())==null) { musicDataDao.insert(data); - OSSManager.upload(OSSManager.TYPE_MUSIC,musicPath,file); } } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/com/yutou/nas/other/tools.java b/src/main/java/com/yutou/nas/other/tools.java index 2f01d88..8b90a60 100644 --- a/src/main/java/com/yutou/nas/other/tools.java +++ b/src/main/java/com/yutou/nas/other/tools.java @@ -3,6 +3,7 @@ package com.yutou.nas.other; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.yutou.nas.NasApplication; +import com.yutou.nas.utils.DepotManager; import com.yutou.nas.utils.RedisTools; import com.yutou.nas.utils.Tools; import org.springframework.stereotype.Controller; @@ -16,7 +17,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Enumeration; -import java.util.List; @Controller public class tools { @@ -118,5 +118,11 @@ public class tools { } return RedisTools.get("request"); } + @ResponseBody + @RequestMapping("/public/deport.do") + public String testDeport(){ + DepotManager.scan(); + return "ok"; + } } diff --git a/src/main/java/com/yutou/nas/utils/ApplicationInit.java b/src/main/java/com/yutou/nas/utils/ApplicationInit.java index b33fc7a..82adee2 100644 --- a/src/main/java/com/yutou/nas/utils/ApplicationInit.java +++ b/src/main/java/com/yutou/nas/utils/ApplicationInit.java @@ -37,6 +37,9 @@ public class ApplicationInit implements ApplicationRunner { case "00:00": musicTools.scanMusic(); break; + case "01:00": + DepotManager.scan(); + break; case "08:00": case "20:00": QQBotManager.getInstance().reportToDayBangumi(); diff --git a/src/main/java/com/yutou/nas/utils/DepotManager.java b/src/main/java/com/yutou/nas/utils/DepotManager.java index 1c88809..d331590 100644 --- a/src/main/java/com/yutou/nas/utils/DepotManager.java +++ b/src/main/java/com/yutou/nas/utils/DepotManager.java @@ -1,10 +1,48 @@ package com.yutou.nas.utils; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.yutou.nas.interfaces.DownloadInterface; +import org.springframework.util.StringUtils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Objects; + public class DepotManager { private DepotManager(){ } public static void scan(){ - + String str=HttpTools.get("http://tools.yutou233.cn/nas/depot/list.do"); + if(!StringUtils.isEmpty(str)){ + JSONObject json=JSONObject.parseObject(str); + if(json.getInteger("code")==1){ + JSONArray array=json.getJSONArray("data"); + for (Object o : array) { + JSONObject item=JSONObject.parseObject(o.toString()); + scanFile(new File(item.getString("path")), new DownloadInterface() { + @Override + public void onDownload(File file) { + super.onDownload(file); + OSSManager.upload("oss-name-"+item.getString("type"),item.getString("path"),file); + } + }); + } + } + } + } + private static void scanFile(File path, DownloadInterface interfaces){ + if(!path.exists()){ + interfaces.onError(new FileNotFoundException("没有该文件")); + return; + } + for (File file : Objects.requireNonNull(path.listFiles())) { + if(file.isDirectory()){ + scanFile(file,interfaces); + }else{ + interfaces.onDownload(file); + } + } } } diff --git a/src/main/java/com/yutou/nas/utils/OSSManager.java b/src/main/java/com/yutou/nas/utils/OSSManager.java index 8fc402e..9cde526 100644 --- a/src/main/java/com/yutou/nas/utils/OSSManager.java +++ b/src/main/java/com/yutou/nas/utils/OSSManager.java @@ -9,6 +9,7 @@ import com.yutou.nas.interfaces.DownloadInterface; import java.io.File; import java.io.FileNotFoundException; +import java.util.Arrays; public class OSSManager { public static final String TYPE_MUSIC = "oss-name-music"; @@ -27,13 +28,22 @@ public class OSSManager { 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; + boolean music=false,photo=false,depot=false; + if (type.equals(TYPE_MUSIC) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-music", boolean.class, false)) { + music=true; } - if (type.equals(TYPE_PHOTO) && !ConfigTools.load(ConfigTools.CONFIG, TYPE_PHOTO, boolean.class, false)) { - return; + if (type.equals(TYPE_PHOTO) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-photo", boolean.class, false)) { + photo=true; } - if (type.equals(TYPE_DEPOT) && !ConfigTools.load(ConfigTools.CONFIG, TYPE_DEPOT, boolean.class, false)) { + if (type.equals(TYPE_DEPOT) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-depot", boolean.class, false)) { + depot=true; + } + if(music||photo||depot){ + System.out.println("music = " + music); + System.out.println("photo = " + photo); + System.out.println("depot = " + depot); + System.out.println("type = " + type + ", path = " + path + ", files = " + Arrays.deepToString(files)); + System.out.println("------------------------------"); return; } new Thread(() -> { @@ -46,7 +56,13 @@ public class OSSManager { if (uploadPath.startsWith("/")) { uploadPath = uploadPath.substring(1); } - client.putObject(type, uploadPath, file); + if(isExist(ConfigTools.load(ConfigTools.CONFIG,type,String.class),uploadPath)){ + System.out.println("文件已存在:"+file.getName()); + return; + } + + client.putObject(ConfigTools.load(ConfigTools.CONFIG,type,String.class), uploadPath, file); + System.out.println(file.getName()+"已上传"); } closeClient(client); }).start();