Summary
Attached is a utility written in C# that you can use to upload files to a code.google.com project.
Full Source Code
You can get the source code at my Google code project.
Usage
GCUpload.exe -u:<username> -p:<password> -n:<projectname>
-s:<summary> -f:<filename> [-t:<targetfilename>]
[-l:<label1>[,<label2>,...]] [--url:<uploadurl>] [--quiet-q]
Partial Source
The main part of this application is the class below:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace GCUpload
{
public class FileUpload
{
private static readonly string BOUNDARY = "BASSHAMMAHSSAB";
private static readonly string URL_FORMAT =
"https://{0}.googlecode.com/files";
private static readonly string METHOD = "POST";
private static readonly string CONTENT_TYPE =
string.Format("multipart/form-data; boundary={0}", BOUNDARY);
private static readonly string USER_AGENT =
"Bassham GoogleCode Uploader v1.0";
public FileUpload()
{
}
private static Uri GetUploadUrl(string url, string projectName)
{
if (!string.IsNullOrEmpty(url))
{
return new Uri(url);
}
else
{
if (string.IsNullOrEmpty(projectName))
{
throw new ArgumentException(
"You must supply either a url or a projectName");
}
return new Uri(string.Format(URL_FORMAT, projectName));
}
}
private static string GetAuthToken(string userName, string password)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(
string.Format("{0}:{1}", userName, password)));
}
private static void WriteToStream(Stream dest, string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
dest.Write(buffer, 0, buffer.Length);
}
public static void UploadFile(
string userName,
string password,
string projectName,
string fileName,
string summary)
{
UploadFile(userName, password, projectName, fileName, null,
summary, null, null);
}
public static void UploadFile(
string userName,
string password,
string projectName,
string fileName,
string targetFileName,
string summary,
List<string> labels,
string uploadUrl)
{
if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentException("You must specify a file to upload.",
"fileName");
}
if (string.IsNullOrEmpty(summary))
{
throw new ArgumentException("You must specify a description " +
"of the file.", "summary");
}
if (string.IsNullOrEmpty(targetFileName))
{
targetFileName = Path.GetFileName(fileName);
}
Uri uploadUri = GetUploadUrl(uploadUrl, projectName);
System.Diagnostics.Trace.WriteLine(uploadUri.ToString());
HttpWebRequest req = WebRequest.Create(uploadUri) as HttpWebRequest;
req.Method = METHOD;
req.ContentType = CONTENT_TYPE;
req.UserAgent = USER_AGENT;
req.Headers.Add(HttpRequestHeader.Authorization,
string.Format("Basic {0}", GetAuthToken(userName, password)));
System.Diagnostics.Trace.WriteLine("Connecting...");
using (Stream reqStream = req.GetRequestStream())
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("--{0}", BOUNDARY));
sb.AppendLine("Content-Disposition: form-data; name=\"summary\"");
sb.AppendLine();
sb.AppendLine(summary);
if (labels != null && labels.Count > 0)
{
foreach (string label in labels)
{
sb.AppendLine(string.Format("--{0}", BOUNDARY));
sb.AppendLine("Content-Disposition: form-data; name=\"label\"");
sb.AppendLine();
sb.AppendLine(label);
}
}
sb.AppendLine(string.Format("--{0}", BOUNDARY));
sb.AppendLine(string.Format("Content-Disposition: form-data; " +
"name=\"filename\"; filename=\"{0}\"", fileName));
sb.AppendLine("Content-Type: application/octet-stream");
sb.AppendLine();
WriteToStream(reqStream, sb.ToString());
using (FileStream f = File.OpenRead(fileName))
{
int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int count = 0;
while ((count = f.Read(buffer, 0, bufferSize)) > 0)
{
reqStream.Write(buffer, 0, count);
}
}
sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine(string.Format("--{0}--", BOUNDARY));
WriteToStream(reqStream, sb.ToString());
}
try
{
using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
{
System.Diagnostics.Trace.WriteLine(res.Headers.ToString());
System.Diagnostics.Trace.WriteLine(
new StreamReader(res.GetResponseStream()).ReadToEnd());
}
}
catch (WebException ex)
{
if (ex.Response != null)
{
HttpWebResponse res = ex.Response as HttpWebResponse;
System.Diagnostics.Trace.WriteLine(res.StatusDescription);
string responseBody;
using (StreamReader rdr = new StreamReader(res.GetResponseStream()))
{
responseBody = rdr.ReadToEnd();
responseBody = Regex.Replace(
Regex.Replace(responseBody, "<br(\\s*/*)>", "\\n"),
"(<.*?>)", string.Empty).Replace(" ", " ");
System.Diagnostics.Trace.WriteLine(responseBody);
}
throw new Exception(responseBody, ex);
}
}
}
public void UploadFile()
{
FileUpload.UploadFile(this.UserName, this.Password, this.ProjectName,
this.FileName, this.TargetFileName,
this.Summary, this.Labels, this.UploadUrl);
}
public string UserName { get; set; }
public string Password { get; set; }
public string ProjectName { get; set; }
public string FileName { get; set; }
public string TargetFileName { get; set; }
public string Summary { get; set; }
public List<string> Labels { get; set; }
public string UploadUrl { get; set; }
}
}