public TorrentFileSystemMonitor(Action <string, string> OnNewTorrent) { string downloadDir = uAL.Program.settings.Dir; string downloadFolder = downloadDir.Substring(downloadDir.LastIndexOf('\\') + 1); FileSystemWatcher w = new FileSystemWatcher(downloadDir); w.Filter = "*.torrent"; w.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName; w.EnableRaisingEvents = true; w.IncludeSubdirectories = true; w.Created += (s, e) => { var isCreated = false; var retries = 0; while (!isCreated && retries++ < MAX_RETRIES_LOCK) { try { Thread.Sleep(500); // FSW workaround, file has not finished, let's wait a bit since they're just torrent files. using (var torrentStream = File.OpenRead(e.FullPath)) { // Just testing } isCreated = true; } catch (FileNotFoundException ex) { LoggingAdapter.Error("Couldn't open the torrent file: " + e.FullPath, ex); } catch (UnauthorizedAccessException ex) { LoggingAdapter.Error("Couldn't access the torrent file: " + e.FullPath, ex); } catch (Exception ex) { LoggingAdapter.Error("Unknown exception occured while accessing the torrent file " + e.FullPath, ex); } } if (retries == MAX_RETRIES_LOCK) { LoggingAdapter.Error("Giving up tring to load torrent " + e.FullPath); return; } var computedLabel = TorrentLabelService.CreateTorrentLabel(downloadDir, e.FullPath); LoggingAdapter.Info(string.Format("Generated label {0} for file {1}", computedLabel, e.FullPath)); if (!string.IsNullOrEmpty(computedLabel)) { OnNewTorrent(e.FullPath, computedLabel); } }; }
public void CreateTorrentLabel_TwoLevel_GetLabel() { var label = TorrentLabelService.CreateTorrentLabel(@"C:\Torrents\", @"C:\Torrents\Movies\SomeMovie.torrent"); Assert.AreEqual(@"Movies", label); }
public void CreateTorrentLabel_SimpleChild_GetSimpleLabel() { var label = TorrentLabelService.CreateTorrentLabel(@"C:\Torrents\", @"C:\Torrents\TV\Arrow\Arrow.torrent"); Assert.AreEqual(@"TV\Arrow", label); }