private void HandleServerError(UploadEntry upload, String responseBody) { if (responseBody.IndexOf("ALREADY EXISTS") >= 0) { log.InfoFormat("The release {0} already exists.", upload.CleanedName); if (upload.IsRepost) { log.WarnFormat("This release was already a repost. Cancelling."); upload.Cancelled = true; upload.Move(Configuration, Location.Failed); } else { WatchFolderSettings wfConfig = Configuration.GetWatchFolderSettings(upload.WatchFolderShortName); upload.CleanedName = upload.CleanedName.Remove(upload.CleanedName.Length - wfConfig.PostTag.Length - 1) + "-REPOST" + wfConfig.PostTag; upload.IsRepost = true; log.InfoFormat("Reuploading as {0}", upload.CleanedName); upload.UploadedAt = null; upload.Move(Configuration, Location.Queue); } } else { log.WarnFormat("Fatal exception on the server side: {0}", responseBody); log.InfoFormat("Reposting {0}", upload.CleanedName); upload.UploadedAt = null; upload.Move(Configuration, Location.Queue); } DBHandler.Instance.UpdateUploadEntry(upload); }
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); } }
protected virtual void RepostIfRequired(UploadEntry upload) { var ageInMinutes = (DateTime.UtcNow - upload.UploadedAt.Value).TotalMinutes; if (ageInMinutes > Configuration.RepostAfterMinutes) { log.WarnFormat("Could not find [{0}] after {1} minutes, reposting, attempt {2}", upload.CleanedName, Configuration.RepostAfterMinutes, upload.UploadAttempts); upload.UploadedAt = null; upload.Move(Configuration, Location.Queue); DBHandler.Instance.UpdateUploadEntry(upload); } else { log.InfoFormat("A repost of [{0}] is not required as {1} minutes have not passed since upload.", upload.CleanedName, Configuration.RepostAfterMinutes); } }
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"); } var nzbFile = poster.PostToUsenet(toPost, password, false); 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); } } } } }