增加无效图片清理

This commit is contained in:
xiongxiaoyang
2020-01-06 10:06:47 +08:00
parent 1555c5b170
commit ae8b8d47a9
3 changed files with 28 additions and 2 deletions

View File

@ -8,6 +8,8 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import xyz.zinglizingli.books.service.BookService;
import java.io.File;
/**
* 清理数据库中无效数据
*
@ -32,8 +34,23 @@ public class ClearInvilidDataSchedule {
bookService.clearInvilidData();
clearInvilidFile(new File(picSavePath));
}
private void clearInvilidFile(File directory) {
for(File file : directory.listFiles()){
if(file.isDirectory()){
clearInvilidFile(file);
}else{
String fileName = file.getName();
int count = bookService.countByPicName(fileName);
if(count == 0){
file.deleteOnExit();
}
}
}
}
}

View File

@ -433,4 +433,13 @@ public class BookService {
public List<Book> queryNetworkPicBooks(Integer limit, Integer offset) {
return bookMapper.queryNetworkPicBooks(limit,offset);
}
/**
* 通过图片名查询小说数量
* */
public int countByPicName(String fileName) {
BookExample bookExample = new BookExample();
bookExample.createCriteria().andPicUrlLike('%'+fileName+'%');
return bookMapper.countByExample(bookExample);
}
}