Compare commits

...

1 Commits
master ... Dev_

Author SHA1 Message Date
a046ba1bf2 试图加入文件监听功能 2021-11-26 19:37:29 +08:00
7 changed files with 168 additions and 11 deletions

View File

@ -142,6 +142,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>20030203.000550</version>
</dependency>
</dependencies>
<build>

View File

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

View File

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

View File

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

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 {
@ -27,4 +28,7 @@ public interface IMusicToolsService {
String getMusicPath();
byte[] readImage(String path) throws Exception;
void addMusic(File file);
void delMusic(File file);
}

View File

@ -183,6 +183,9 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
}
private void add(File file) {
MusicDataExample example=new MusicDataExample();
example.createCriteria().andFileEqualTo(file.getAbsolutePath());
if(musicDataDao.selectByExample(example).isEmpty()) {
MusicData data = getMetadata(file);
if (data != null) {
try {
@ -197,6 +200,7 @@ public class MusicToolsServiceImpl implements IMusicToolsService {
}
}
}
@Override
public MusicData getMusicData(String path) {
@ -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");

View File

@ -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();