From a046ba1bf2a74a2653bbc0e26bb29cd6f1f3f517 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Fri, 26 Nov 2021 19:37:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=95=E5=9B=BE=E5=8A=A0=E5=85=A5=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9B=91=E5=90=AC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ++ .../yutou/nas/FileListener/FileListener.java | 63 +++++++++++++++++++ .../nas/FileListener/FileListenerFactory.java | 47 ++++++++++++++ .../nas/FileListener/FileListenerRunner.java | 22 +++++++ .../nas/Services/IMusicToolsService.java | 4 ++ .../Services/impl/MusicToolsServiceImpl.java | 36 ++++++++--- .../com/yutou/nas/utils/ApplicationInit.java | 2 +- 7 files changed, 168 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/yutou/nas/FileListener/FileListener.java create mode 100644 src/main/java/com/yutou/nas/FileListener/FileListenerFactory.java create mode 100644 src/main/java/com/yutou/nas/FileListener/FileListenerRunner.java diff --git a/pom.xml b/pom.xml index 33bca14..48b2167 100644 --- a/pom.xml +++ b/pom.xml @@ -142,6 +142,11 @@ org.springframework.boot spring-boot-starter-security --> + + commons-io + commons-io + 20030203.000550 + diff --git a/src/main/java/com/yutou/nas/FileListener/FileListener.java b/src/main/java/com/yutou/nas/FileListener/FileListener.java new file mode 100644 index 0000000..905eb35 --- /dev/null +++ b/src/main/java/com/yutou/nas/FileListener/FileListener.java @@ -0,0 +1,63 @@ +package com.yutou.nas.FileListener; + +import com.yutou.nas.Services.IMusicToolsService; +import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; +import org.apache.commons.io.monitor.FileAlterationObserver; + +import java.io.File; + +public class FileListener extends FileAlterationListenerAdaptor { + private IMusicToolsService musicToolsService; + // 采用构造函数注入服务 + public FileListener( IMusicToolsService musicToolsService) { + this.musicToolsService=musicToolsService; + } + + // 文件创建执行 + @Override + public void onFileCreate(File file) { + musicToolsService.addMusic(file); + + } + + // 文件创建修改 + @Override + public void onFileChange(File file) { + // 触发业务 + musicToolsService.addMusic(file); + } + + // 文件创建删除 + @Override + public void onFileDelete(File file) { + musicToolsService.delMusic(file); + } + + // 目录创建 + @Override + public void onDirectoryCreate(File directory) { + } + + // 目录修改 + @Override + public void onDirectoryChange(File directory) { + } + + // 目录删除 + @Override + public void onDirectoryDelete(File directory) { + } + + + // 轮询开始 + @Override + public void onStart(FileAlterationObserver observer) { + super.onStart(observer); + } + + // 轮询结束 + @Override + public void onStop(FileAlterationObserver observer) { + super.onStop(observer); + } +} diff --git a/src/main/java/com/yutou/nas/FileListener/FileListenerFactory.java b/src/main/java/com/yutou/nas/FileListener/FileListenerFactory.java new file mode 100644 index 0000000..ba6bc0b --- /dev/null +++ b/src/main/java/com/yutou/nas/FileListener/FileListenerFactory.java @@ -0,0 +1,47 @@ +package com.yutou.nas.FileListener; + +import com.yutou.nas.Services.IMusicToolsService; +import org.apache.commons.io.filefilter.FileFilterUtils; +import org.apache.commons.io.filefilter.HiddenFileFilter; +import org.apache.commons.io.filefilter.IOFileFilter; +import org.apache.commons.io.monitor.FileAlterationMonitor; +import org.apache.commons.io.monitor.FileAlterationObserver; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.io.File; +import java.util.concurrent.TimeUnit; + +@Component +public class FileListenerFactory { + // 设置监听路径 + private final String monitorDir = "/media/yutou/disk_lvm/public/音乐"; + + // 设置轮询间隔 + private final long interval = TimeUnit.SECONDS.toMillis(1); + + @Resource + IMusicToolsService musicToolsService; + + + public FileAlterationMonitor getMonitor() { + // 创建过滤器 + IOFileFilter directories = FileFilterUtils.and( + FileFilterUtils.directoryFileFilter(), + HiddenFileFilter.VISIBLE); + IOFileFilter files = FileFilterUtils.and( + FileFilterUtils.fileFileFilter() + ); + IOFileFilter filter = FileFilterUtils.or(directories, files); + + // 装配过滤器 + // FileAlterationObserver observer = new FileAlterationObserver(new File(monitorDir)); + FileAlterationObserver observer = new FileAlterationObserver(new File(monitorDir), filter); + + // 向监听者添加监听器,并注入业务服务 + observer.addListener(new FileListener(musicToolsService)); + + // 返回监听者 + return new FileAlterationMonitor(interval, observer); + } +} diff --git a/src/main/java/com/yutou/nas/FileListener/FileListenerRunner.java b/src/main/java/com/yutou/nas/FileListener/FileListenerRunner.java new file mode 100644 index 0000000..fc27f6d --- /dev/null +++ b/src/main/java/com/yutou/nas/FileListener/FileListenerRunner.java @@ -0,0 +1,22 @@ +package com.yutou.nas.FileListener; + +import org.apache.commons.io.monitor.FileAlterationMonitor; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +@Component +public class FileListenerRunner implements CommandLineRunner { + @Resource + private FileListenerFactory fileListenerFactory; + @Override + public void run(String... args) throws Exception { + FileAlterationMonitor fileAlterationMonitor = fileListenerFactory.getMonitor(); + try { + fileAlterationMonitor.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/yutou/nas/Services/IMusicToolsService.java b/src/main/java/com/yutou/nas/Services/IMusicToolsService.java index 0071b09..fcd39ff 100644 --- a/src/main/java/com/yutou/nas/Services/IMusicToolsService.java +++ b/src/main/java/com/yutou/nas/Services/IMusicToolsService.java @@ -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 { @@ -27,4 +28,7 @@ public interface IMusicToolsService { String getMusicPath(); byte[] readImage(String path) throws Exception; + + void addMusic(File file); + void delMusic(File file); } 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 5f11ff1..a354ed7 100644 --- a/src/main/java/com/yutou/nas/Services/impl/MusicToolsServiceImpl.java +++ b/src/main/java/com/yutou/nas/Services/impl/MusicToolsServiceImpl.java @@ -183,18 +183,22 @@ public class MusicToolsServiceImpl implements IMusicToolsService { } private void add(File file) { - MusicData data = getMetadata(file); - if (data != null) { - try { - if (getMusicData(file.getAbsolutePath()) == null) { - // System.out.println(data); - musicDataDao.insert(data); + MusicDataExample example=new MusicDataExample(); + example.createCriteria().andFileEqualTo(file.getAbsolutePath()); + if(musicDataDao.selectByExample(example).isEmpty()) { + MusicData data = getMetadata(file); + if (data != null) { + try { + if (getMusicData(file.getAbsolutePath()) == null) { + // System.out.println(data); + musicDataDao.insert(data); + } + } catch (Exception e) { + e.printStackTrace(); + QQBotManager.getInstance().sendMessage("音乐文件添加失败:" + data.toString()); } - } catch (Exception e) { - e.printStackTrace(); - QQBotManager.getInstance().sendMessage("音乐文件添加失败:" + data.toString()); - } + } } } @@ -527,6 +531,18 @@ public class MusicToolsServiceImpl implements IMusicToolsService { return bytes; } + @Override + public void addMusic(File file) { + add(file); + } + + @Override + public void delMusic(File file) { + MusicDataExample example=new MusicDataExample(); + example.createCriteria().andFileEqualTo(file.getAbsolutePath()); + musicDataDao.deleteByExample(example); + } + private byte[] readImageFile(File file) throws Exception { String path = file.getAbsolutePath().replace(file.getName(), ""); File img = new File(path, "cover.jpg"); diff --git a/src/main/java/com/yutou/nas/utils/ApplicationInit.java b/src/main/java/com/yutou/nas/utils/ApplicationInit.java index 74a483b..ed58fea 100644 --- a/src/main/java/com/yutou/nas/utils/ApplicationInit.java +++ b/src/main/java/com/yutou/nas/utils/ApplicationInit.java @@ -58,7 +58,7 @@ public class ApplicationInit implements ApplicationRunner { oldTime = time; switch (time) { case "00:00": - musicTools.scanMusic(); + // musicTools.scanMusic(); break; case "01:00": DepotManager.scan();