private void UploadNextItemInQueue() { try { UploadEntry nextUpload = DBHandler.Instance.GetNextUploadEntryToUpload(); if (nextUpload == null) { return; } WatchFolderSettings folderConfiguration = configuration.GetWatchFolderSettings(nextUpload.WatchFolderShortName); FileSystemInfo toUpload; Boolean isDirectory; String fullPath = nextUpload.GetCurrentPath(configuration); try { FileAttributes attributes = File.GetAttributes(fullPath); if (attributes.HasFlag(FileAttributes.Directory)) { isDirectory = true; toUpload = new DirectoryInfo(fullPath); } else { isDirectory = false; toUpload = new FileInfo(fullPath); } } catch (FileNotFoundException) { log.WarnFormat("Can no longer find '{0}', cancelling upload", fullPath); nextUpload.CurrentLocation = Location.None; nextUpload.Cancelled = true; DBHandler.Instance.UpdateUploadEntry(nextUpload); return; } if (nextUpload.UploadAttempts >= configuration.MaxRepostCount) { log.WarnFormat("Cancelling the upload after {0} retry attempts.", nextUpload.UploadAttempts); nextUpload.Cancelled = true; nextUpload.Move(configuration, Location.Failed); DBHandler.Instance.UpdateUploadEntry(nextUpload); return; } PostRelease(folderConfiguration, nextUpload, toUpload, isDirectory); } catch (Exception ex) { log.Error("The upload failed to post. Retrying.", ex); } }
private void PostRelease(WatchFolderSettings folderConfiguration, UploadEntry nextUpload, FileSystemInfo toUpload, Boolean isDirectory) { nextUpload.UploadAttempts++; if (folderConfiguration.CleanName) { nextUpload.CleanedName = ApplyTags(CleanName(folderConfiguration, StripNonAscii(toUpload.NameWithoutExtension())), folderConfiguration); } else { nextUpload.CleanedName = ApplyTags(StripNonAscii(toUpload.NameWithoutExtension()), folderConfiguration); } if (folderConfiguration.UseObfuscation) { nextUpload.ObscuredName = Guid.NewGuid().ToString("N"); nextUpload.NotifiedIndexerAt = null; } DBHandler.Instance.UpdateUploadEntry(nextUpload); UsenetPoster poster = new UsenetPoster(configuration, folderConfiguration); FileSystemInfo toPost = null; try { if (isDirectory) { toPost = PrepareDirectoryForPosting(folderConfiguration, nextUpload, (DirectoryInfo)toUpload); } else { toPost = PrepareFileForPosting(folderConfiguration, nextUpload, (FileInfo)toUpload); } String password = folderConfiguration.RarPassword; if (folderConfiguration.ApplyRandomPassword) { password = Guid.NewGuid().ToString("N"); } FileInfo nfoFile = null; if (nextUpload.HasNfo) { nfoFile = new FileInfo(nextUpload.GetCurrentPath(configuration, toUpload.NameWithoutExtension() + ".nfo")); } var nzbFile = poster.PostToUsenet(toPost, password, false, nfoFile, configuration.KeepProcessingFolderAfterError); nextUpload.NzbContents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine + nzbFile.ToString(); if (configuration.NzbOutputFolder != null) { FileInfo file = new FileInfo(Path.Combine(configuration.NzbOutputFolder.FullName, nextUpload.CleanedName + ".nzb")); File.WriteAllText(file.FullName, nextUpload.NzbContents); } nextUpload.RarPassword = password; nextUpload.UploadedAt = DateTime.UtcNow; nextUpload.Move(configuration, Location.Backup); DBHandler.Instance.UpdateUploadEntry(nextUpload); log.InfoFormat("[{0}] was uploaded as obfuscated release [{1}] to usenet." , nextUpload.CleanedName, nextUpload.ObscuredName); } finally { if (toPost != null) { toPost.Refresh(); if (toPost.Exists) { FileAttributes attributes = File.GetAttributes(toPost.FullName); if (attributes.HasFlag(FileAttributes.Directory)) { Directory.Delete(toPost.FullName, true); } else { File.Delete(toPost.FullName); } } } } }