/// <summary> /// Adds a file to Download Station /// </summary> /// <param name="path">Path of file</param> /// <returns>Success?</returns> public static bool AddFileToDownloadStation(string path) { DownloadStation ds = null; string name = string.Empty; string extention = string.Empty; FileStream file = null; try { UriBuilder uriBuilder = new UriBuilder(Properties.Settings.Default.Address) { Scheme = Uri.UriSchemeHttp }; ds = new DownloadStation(uriBuilder.Uri, Properties.Settings.Default.Username, Encoding.UTF8.GetString(Convert.FromBase64String(Properties.Settings.Default.Password))); if (File.Exists(path)) { name = Path.GetFileName(path); Adapter.ShowBalloonTip("Adding " + name , ToolTipIcon.Info, 30000); // Register file for download string fileDownload = RegisterFileDownload(path); // Login to Download Station if (ds.Login()) { // Add file to Download Station SynologyRestDAL.TResult<object> result = ds.CreateTask(fileDownload); if (!result.Success) { if (result.Error.Code == 406) { throw new Exception("Couldn't add link(s). You have to choose a download folder for your Download Station."); } else { throw new Exception("While adding " + name + " error code " + result.Error.Code + " occurred"); } } Adapter.ShowBalloonTip("Added " + name, ToolTipIcon.Info); return true; } else { throw new Exception("Couldn't login to Download Station"); } } else { Adapter.ShowBalloonTip("Couldn't find file '" + path + "'", ToolTipIcon.Error); return false; } } catch (Exception ex) { Adapter.ShowBalloonTip(ex.Message, ToolTipIcon.Error); return false; } finally { if (ds != null) { ds.Logout(); } if (file != null) { file.Close(); } } }
/// <summary> /// Add links to Download Station /// </summary> /// <param name="links">Links to Download</param> /// <returns>Success?</returns> public static bool AddLinksToDownloadStation(List<string> links) { DownloadStation ds = null; try { UriBuilder uriBuilder = new UriBuilder(Properties.Settings.Default.Address); ds = new DownloadStation(uriBuilder.Uri, Properties.Settings.Default.Username, Encoding.UTF8.GetString(Convert.FromBase64String(Properties.Settings.Default.Password))); links.RemoveAll(str => String.IsNullOrEmpty(str.Trim())); Dictionary<string, List<string>> validHostLinks = new Dictionary<string, List<string>>(); List<string> corruptedLinks = new List<string>(); Uri currentLink = null; int validHostLinkCount = 0; int totalLinkCount = 0; string balloonMsg; foreach (string link in links) { try { currentLink = new Uri(link); if (!validHostLinks.ContainsKey(currentLink.Host)) { validHostLinks.Add(currentLink.Host, new List<string>()); } validHostLinks[currentLink.Host].Add(link); } catch { corruptedLinks.Add(link); } } if (validHostLinks.Keys.Count > 1) { frmSettings.Instance.Invoke((MethodInvoker)(() => { // Run on UI thread frmSelectHoster.Instance.SelectHoster(validHostLinks); } )); } // Get total link count foreach (var validHostLink in validHostLinks) { totalLinkCount += validHostLink.Value.Count; } if (validHostLinks.Count > 0) { balloonMsg = "Adding " + totalLinkCount + " links(s) (" + (validHostLinks.Count > 1 ? validHostLinks.Count + " Hosts)" : validHostLinks.First().Key + ")"); Adapter.ShowBalloonTip(balloonMsg, ToolTipIcon.Info, 30000); // Login to Download Station if (ds.Login()) { foreach (var validHostLink in validHostLinks) { validHostLinkCount += validHostLink.Value.Count; List<List<string>> portions = CreateLinkPortions(validHostLink.Value); foreach (List<string> partionLinks in portions) { // Add links to Download Station SynologyRestDAL.TResult<object> result = ds.CreateTask(string.Join(",", partionLinks.ToArray())); if (!result.Success) { if (result.Error.Code == 406) { throw new Exception("Couldn't add link(s). You have to choose a download folder for your Download Station."); } else { throw new Exception("While adding link(s) the error code " + result.Error.Code + " occurred"); } } } } balloonMsg = totalLinkCount + " link(s) added (" + (validHostLinks.Count > 1 ? validHostLinks.Count + " Hosts)" : validHostLinks.First().Key + ")"); if (corruptedLinks.Count > 0) { balloonMsg += "\r\n" + corruptedLinks.Count + " links(s) were corrupted"; } Adapter.ShowBalloonTip(balloonMsg, ToolTipIcon.Info); return true; } else { throw new Exception("Couldn't login to Download Station"); } } else { return false; } } catch (Exception ex) { Adapter.ShowBalloonTip(ex.Message, ToolTipIcon.Error); return false; } finally { if (ds != null) { try { ds.Logout(); } catch { // Ignore error on logout } } } }