Skip to content

Instantly share code, notes, and snippets.

@buunguyen
Created May 9, 2012 17:39
Show Gist options
  • Save buunguyen/2647100 to your computer and use it in GitHub Desktop.
Save buunguyen/2647100 to your computer and use it in GitHub Desktop.
FTP Submitter for qTrace
namespace FtpSubmitter
{
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Iris.Trackers.Contracts;
[Export(typeof(IBugTracker))]
public class Ftp : IBugTracker
{
public string DisplayName
{
get { return "FTP Submitter"; }
}
public string IconUri
{
get { return "/FtpSubmitter;component/ftp.png"; }
}
public string Verify(BugTrackerAccount bugTrackerAccount,
int timeoutInMillis,
IDictionary<string, string> settings)
{
var ftpRequest = (FtpWebRequest)WebRequest.Create(bugTrackerAccount.Url);
ftpRequest.Credentials = new NetworkCredential(bugTrackerAccount.UserName,
bugTrackerAccount.Password);
ftpRequest.Timeout = timeoutInMillis;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
using (ftpRequest.GetResponse())
{
return "Connected to FTP server successfully!";
}
}
public SubmittedDefect Submit(Window owner,
BugTrackerAccount bugTrackerAccount,
int timeoutInMillis,
Defect defect,
Func<IDictionary<string, byte[]>> attachmentsFunc,
IDictionary<string, string> settings)
{
Mouse.OverrideCursor = Cursors.Wait;
try
{
var tasks = from attachment in attachmentsFunc()
select Task.Factory.StartNew(
() => Upload(bugTrackerAccount, timeoutInMillis,
attachment.Key, attachment.Value));
Task.WaitAll(tasks.ToArray());
return null;
}
finally
{
Mouse.OverrideCursor = null;
}
}
private void Upload(BugTrackerAccount bugTrackerAccount,
int timeoutInMillis,
string filePath,
byte[] fileContent)
{
var uri = bugTrackerAccount.Url +
(bugTrackerAccount.Url.EndsWith("/") ? string.Empty : "/") +
Path.GetFileName(filePath);
var ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Credentials = new NetworkCredential(bugTrackerAccount.UserName,
bugTrackerAccount.Password);
ftpRequest.Timeout = timeoutInMillis;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
using (Stream writer = ftpRequest.GetRequestStream())
{
writer.Write(fileContent, 0, fileContent.Length);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment