/** * Returns true if the code was executed correctly. False if there was an error trying to share * the file. If the file was not supposed to be shared, and was not shared, true would still be * returned. */ private boolean shareTorrentFile(File torrentFile) { if (torrentManager.isDownloadingTorrent(torrentFile)) { return true; } if (!SharingSettings.SHARE_DOWNLOADED_FILES_IN_NON_SHARED_DIRECTORIES.getValue()) { return true; } BTData btData = null; FileInputStream torrentInputStream = null; try { torrentInputStream = new FileInputStream(torrentFile); Map<?, ?> torrentFileMap = (Map<?, ?>) Token.parse(torrentInputStream.getChannel()); btData = new BTDataImpl(torrentFileMap); } catch (IOException e) { LOG.error("Error reading torrent file: " + torrentFile, e); return false; } finally { FileUtils.close(torrentInputStream); } if (btData.isPrivate()) { gnutellaFileList.remove(torrentFile); return true; } File saveDir = SharingSettings.getSaveDirectory(); File torrentParent = torrentFile.getParentFile(); if (torrentParent.equals(saveDir)) { // already in saveDir gnutellaFileList.add(torrentFile); return true; } final File tFile = getSharedTorrentMetaDataFile(btData); if (tFile.equals(torrentFile)) { gnutellaFileList.add(tFile); return true; } gnutellaFileList.remove(tFile); File backup = null; if (tFile.exists()) { backup = new File(tFile.getParent(), tFile.getName().concat(".bak")); FileUtils.forceRename(tFile, backup); } if (FileUtils.copy(torrentFile, tFile)) { gnutellaFileList.add(tFile); } else { if (backup != null) { // restore backup if (FileUtils.forceRename(backup, tFile)) { gnutellaFileList.add(tFile); } } } return true; }
@Override public void handleEvent(TorrentEvent event) { if (event == TorrentEvent.STOPPED) { if (!finished.get()) { cancel(); } else { remove(); } } else if (event == TorrentEvent.STATUS_CHANGED) { // considered to be finished uploading if seed ratio has been // reached boolean finished = torrent.isFinished(); float seedRatio = torrent.getSeedRatio(); TorrentStatus status = torrent.getStatus(); int seedTime = status != null ? status.getSeedingTime() : 0; float targetSeedRatio = torrentManager.getTorrentManagerSettings().getSeedRatioLimit(); int targetSeedTime = torrentManager.getTorrentManagerSettings().getSeedTimeLimit(); if (finished && (seedRatio >= targetSeedRatio || seedTime >= targetSeedTime)) { this.finished.set(true); torrent.stop(); } } }