using System; using System.IO; using System.Net; using System.Security.Cryptography; using System.Text; using System.Web; namespace PDF_Alchemy { class Upload { const string accessKeyId = "xxxxxxxxxxxxxxxxxxxx"; const string secretAccessKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Uploads a single file to PDF Alchemy. static private void UploadFile(string filename) { // Trim filename to just the basename. string basename = filename; if (basename.LastIndexOf('\\') >= 0) basename = basename.Substring(basename.LastIndexOf('\\') + 1); Console.Write("Uploading '" + basename + "': "); // Generate the HMAC-SHA1 signature. string date = DateTime.UtcNow.ToString("r"); string request = '/' + accessKeyId + "/inqueue/" + HttpUtility.UrlEncode(basename); string clearText = "PUT\n" + accessKeyId + '\n' + date + '\n' + request; HMACSHA1 hmac = new HMACSHA1(ASCIIEncoding.UTF8.GetBytes(secretAccessKey)); string signature = Convert.ToBase64String(hmac.ComputeHash(ASCIIEncoding.UTF8.GetBytes(clearText))); // Upload the file to PDF Alchemy. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.pdfalchemy.com" + request); req.Method = "PUT"; req.Headers.Add("X-PDF-Alchemy-Date", date); req.Headers.Add("X-PDF-Alchemy-Signature", signature); byte[] fileData = File.ReadAllBytes(filename); try { Stream stream = req.GetRequestStream(); stream.Write(fileData, 0, fileData.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)req.GetResponse(); Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd()); response.Close(); } catch (System.Net.WebException e) { Console.Write(e.Message); } } // Uploads all convertable files from a single directory, to PDF Alchemy. static private void UploadFilesInDir(string dirname) { foreach (string filename in Directory.GetFiles(dirname)) if (filename.EndsWith(".doc") || filename.EndsWith(".docx") || filename.EndsWith(".rtf") || filename.EndsWith(".txt")) UploadFile(filename); } static void Main(string[] args) { // Process the command line arguments. foreach (string arg in args) if (Directory.Exists(arg)) UploadFilesInDir(arg); else UploadFile(arg); } } }