When a torrent reach 0 leecher, the .torrent file is no longer removed, it is moved to archived folder.

This commit is contained in:
anthonyraymond 2017-03-13 23:34:36 +01:00
parent 219304b6b6
commit ee73fe6a09
2 changed files with 36 additions and 12 deletions

View file

@ -100,7 +100,7 @@ public class SeedManager {
private void handleEvent(final Client.Event event) { private void handleEvent(final Client.Event event) {
if (event == Client.Event.ENCOUNTER_ZERO_PEER) { if (event == Client.Event.ENCOUNTER_ZERO_PEER) {
this.actionOnStopSeeding = ActionOnStopSeeding.RESTART_IMMEDIATLY; this.actionOnStopSeeding = ActionOnStopSeeding.RESTART_IMMEDIATLY;
this.torrentFileProvider.forceRemoveTorrent(this.currentTorrent); this.torrentFileProvider.moveToArchiveFolder(this.currentTorrent);
} }
} }

View file

@ -10,7 +10,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -21,12 +24,14 @@ import java.util.Random;
*/ */
public class TorrentFileProvider { public class TorrentFileProvider {
private static final Logger logger = LoggerFactory.getLogger(TorrentFileProvider.class); private static final Logger logger = LoggerFactory.getLogger(TorrentFileProvider.class);
public static final String ARCHIVE_FOLDER = "archived";
private static final IOFileFilter torrentFileFilter = FileFilterUtils.suffixFileFilter(".torrent"); private static final IOFileFilter torrentFileFilter = FileFilterUtils.suffixFileFilter(".torrent");
private final FileAlterationObserver observer; private final FileAlterationObserver observer;
private final FileAlterationMonitor monitor; private final FileAlterationMonitor monitor;
private final List<File> torrentFiles; private final List<File> torrentFiles;
private final Path archiveFolder;
private final Random rand; private final Random rand;
public TorrentFileProvider(final Path torrentFolder) { public TorrentFileProvider(final Path torrentFolder) {
@ -34,9 +39,24 @@ public class TorrentFileProvider {
} }
public TorrentFileProvider(final Path torrentFolder, final int scanInterval) { public TorrentFileProvider(final Path torrentFolder, final int scanInterval) {
this.archiveFolder = torrentFolder.resolve(ARCHIVE_FOLDER);
this.torrentFiles = Collections.synchronizedList(new ArrayList<>()); this.torrentFiles = Collections.synchronizedList(new ArrayList<>());
this.rand = new Random(); this.rand = new Random();
if (!Files.exists(torrentFolder)) {
logger.error("Folder " + torrentFolder.toAbsolutePath() + " does not exists.");
throw new IllegalStateException("Folder " + torrentFolder.toAbsolutePath() + " does not exists.");
}
if (!Files.exists(archiveFolder)) {
try {
Files.createDirectory(archiveFolder);
} catch (final IOException e) {
logger.error("Failed to create archive folder.", e);
throw new IllegalStateException("Failed to create archive folder.", e);
}
}
FileUtils.listFiles(torrentFolder.toFile(), torrentFileFilter, null) FileUtils.listFiles(torrentFolder.toFile(), torrentFileFilter, null)
.forEach(torrentFiles::add); .forEach(torrentFiles::add);
@ -47,7 +67,7 @@ public class TorrentFileProvider {
@Override @Override
public void onFileChange(final File file) { public void onFileChange(final File file) {
removeFileFromList(file); removeFileFromList(file);
onFileCreate(file); addFileToList(file);
logger.info("Torrent file change detected, hot reloaded file: {}", file.getAbsolutePath()); logger.info("Torrent file change detected, hot reloaded file: {}", file.getAbsolutePath());
} }
@ -82,20 +102,25 @@ public class TorrentFileProvider {
return this.torrentFiles.get(this.rand.nextInt(this.torrentFiles.size())); return this.torrentFiles.get(this.rand.nextInt(this.torrentFiles.size()));
} }
public void forceRemoveTorrent(final File torrent) { public void moveToArchiveFolder(final File torrentFile) {
this.removeFileFromList(torrent); this.removeFileFromList(torrentFile);
if (!torrent.exists()) { if (!torrentFile.exists()) {
return; return;
} }
final boolean deleted = torrent.delete();
if (deleted) { try {
logger.info("Successfully deleted file: {}", torrent.getAbsolutePath()); Files.move(torrentFile.toPath(), archiveFolder.resolve(torrentFile.getName()));
} else { logger.info("Successfully moved file: {} to archive folder", torrentFile.getAbsolutePath());
logger.warn("Failed to delete file: {}, the file won't be used anymore for the current session, but it remains on the disk."); } catch (final IOException e) {
logger.warn("Failed to archive file: {}, the file won't be used anymore for the current session, but it remains on the folder.", e);
} }
} }
public int getTorrentCount() {
return this.torrentFiles.size();
}
public void start() { public void start() {
try { try {
monitor.start(); monitor.start();
@ -106,8 +131,7 @@ public class TorrentFileProvider {
public void stop() { public void stop() {
logger.trace("Call to stop TorrentFileProvider."); logger.trace("Call to stop TorrentFileProvider.");
this.observer.getListeners() this.observer.getListeners().forEach(observer::removeListener);
.forEach(observer::removeListener);
try { try {
this.monitor.stop(10); this.monitor.stop(10);
} catch (final Exception ignored) { } catch (final Exception ignored) {