public void DownloadAttachments(Ticket ticket, string downloadFolder)
{
using (var webClient = new WebClient())
{
var downloaded = new Dictionary<string, int>();
foreach (var attachment in ticket.Attachments)
{
onDetailedProcessing("Downloading " + attachment.FileName);
var sourceUri = string.Format("{0}?&os_username={1}&os_password={2}",
attachment.Source, userName, password);
string name = Path.GetFileNameWithoutExtension(attachment.FileName),
extension = Path.GetExtension(attachment.FileName),
downloadedName = attachment.FileName;
// Jira can have more than one of the same file attached to a ticket ...
var nextCopy = 0;
if (downloaded.ContainsKey(attachment.FileName))
{
nextCopy = downloaded[attachment.FileName];
downloadedName = string.Format("{0}_{1}{2}", name, nextCopy, extension);
}
downloaded[attachment.FileName] = ++nextCopy;
try
{
var downloadTo = Path.Combine(downloadFolder, downloadedName);
webClient.DownloadFile(new Uri(sourceUri), downloadTo);
attachment.Source = downloadTo;
attachment.FileName = downloadedName;
attachment.Downloaded = true;
}
catch
{
attachment.Downloaded = false;
}
}
}
}