private void Init() { InitializeComponent(); root.DataContext = this; Id = String_Functions.RandomString(20); }
void UploadProfileImage(EditUserInfo info, string path) { if (path != null) { // Crop and Resize image System.Drawing.Image img = ProcessImage(path); if (img != null) { // Generate random file name for processed/temp image (to be saved in temp folder) string newFilename = String_Functions.RandomString(20); // Get file extension of original file string ext = Path.GetExtension(path); // Make sure Temp directory exists FileLocations.CreateTempDirectory(); // Create new full path of temp file string localPath = Path.Combine(FileLocations.TrakHoundTemp, newFilename); //if (ext != null) localPath += "." + ext; if (ext != null) { localPath = Path.ChangeExtension(localPath, ext); } // Save the processed image to the new temp path img.Save(localPath); // Create a temp UserConfiguration object to pass the current SessionToken to the Files.Upload() method var userConfig = new UserConfiguration(); userConfig.SessionToken = info.SessionToken; // Set the HTTP Content Type based on the type of image string contentType = null; if (ext == "jpg") { contentType = "image/jpeg"; } else if (ext == "png") { contentType = "image/png"; } else if (ext == "gif") { contentType = "image/gif"; } var fileData = new HTTP.FileContentData("uploadimage", localPath, contentType); // Upload File var uploadInfos = TrakHound.API.Files.Upload(userConfig, fileData); if (uploadInfos != null && uploadInfos.Length > 0) { string fileId = uploadInfos[0].Id; info.ImageUrl = fileId; } } } }
public static string CreateTempPath() { CreateTempDirectory(); string filename = String_Functions.RandomString(20); return(Path.Combine(TrakHoundTemp, filename)); }
private void UploadDeviceImage_Worker(object o) { if (o != null) { var info = (ImageInfo)o; string fileId = null; if (info.UserConfig != null) { string contentType = null; try { string ext = Path.GetExtension(info.FileId); if (ext == "jpg" || ext == ".jpg") { contentType = "image/jpeg"; } else if (ext == "png" || ext == ".png") { contentType = "image/png"; } else if (ext == "gif" || ext == ".gif") { contentType = "image/gif"; } var img = System.Drawing.Image.FromFile(info.FileId); if (img != null) { if (img.Width > img.Height) { img = Image_Functions.SetImageSize(img, 300, 300); } else { img = Image_Functions.SetImageSize(img, 0, 150); } string tempPath = Path.ChangeExtension(String_Functions.RandomString(20), ext); tempPath = Path.Combine(FileLocations.TrakHoundTemp, tempPath); // Make sure Temp directory exists FileLocations.CreateTempDirectory(); img.Save(tempPath); img.Dispose(); var fileData = new HTTP.FileContentData("uploadImage", tempPath, contentType); var fileInfos = Files.Upload(currentUser, fileData); if (fileInfos != null && fileInfos.Length > 0) { fileId = fileInfos[0].Id; } } } catch (Exception ex) { logger.Error(ex); } } else { string filename = Path.ChangeExtension(Guid.NewGuid().ToString(), ".image"); string destinationPath = Path.Combine(FileLocations.Storage, filename); FileLocations.CreateStorageDirectory(); File.Copy(info.FileId, destinationPath); fileId = filename; } Dispatcher.BeginInvoke(new Action <string>(UploadDeviceImage_GUI), System.Windows.Threading.DispatcherPriority.Background, new object[] { fileId }); } }
private static ResponseInfo SendData( bool returnBytes, string method, string url, PostContentData[] postDatas = null, FileContentData[] fileDatas = null, HeaderData[] headers = null, string userAgent = null, NetworkCredential credential = null, ProxySettings proxySettings = null, int timeout = TIMEOUT, int maxAttempts = CONNECTION_ATTEMPTS, bool getResponse = true ) { ResponseInfo result = null; int attempts = 0; bool success = false; string message = null; // Try to send data for number of connectionAttempts while (attempts < maxAttempts && !success) { attempts += 1; try { // Create HTTP request and define Header info var request = (HttpWebRequest)WebRequest.Create(url); string boundary = String_Functions.RandomString(10); request.Timeout = timeout; request.ReadWriteTimeout = timeout; if (method == "POST") { request.ContentType = "multipart/form-data; boundary=" + boundary; } else { request.ContentType = "application/x-www-form-urlencoded"; } // Set the Method request.Method = method; // Set the UserAgent if (userAgent != null) { request.UserAgent = userAgent; } else { request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; } // Add Header data to request stream (if present) if (headers != null) { foreach (var header in headers) { request.Headers[header.Id] = header.Text; } } // set NetworkCredentials if (credential != null) { request.Credentials = credential; request.PreAuthenticate = true; } // Get Default System Proxy (Windows Internet Settings -> Proxy Settings) var proxy = WebRequest.GetSystemWebProxy(); // Get Custom Proxy Settings from Argument (overwrite default proxy settings) if (proxySettings != null) { if (proxySettings.Address != null && proxySettings.Port > 0) { var customProxy = new WebProxy(proxySettings.Address, proxySettings.Port); customProxy.BypassProxyOnLocal = false; proxy = customProxy; } } request.Proxy = proxy; var bytes = new List <byte>(); // Add Post Name/Value Pairs if (postDatas != null) { string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (var postData in postDatas) { string formitem = string.Format(formdataTemplate, postData.Name, postData.Value); bytes.AddRange(GetBytes("\r\n--" + boundary + "\r\n")); bytes.AddRange(GetBytes(formitem)); } } // Add File data if (fileDatas != null) { bytes.AddRange(GetFileContents(fileDatas, boundary)); } if (bytes.Count > 0) { // Write Trailer Boundary string trailer = "\r\n--" + boundary + "--\r\n"; bytes.AddRange(GetBytes(trailer)); var byteArray = bytes.ToArray(); // Write Data to Request Stream request.ContentLength = byteArray.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(byteArray, 0, byteArray.Length); } } // Get Response Message from HTTP Request if (getResponse) { result = new ResponseInfo(); using (var response = (HttpWebResponse)request.GetResponse()) { // Get HTTP Response Body using (var responseStream = response.GetResponseStream()) using (var memStream = new MemoryStream()) { byte[] buffer = new byte[10240]; int read; while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) { memStream.Write(buffer, 0, read); } result.Body = memStream.ToArray(); success = true; } var responseHeaders = new List <ReponseHeaderInfo>(); // Get HTTP Response Headers foreach (var key in response.Headers.AllKeys) { var header = new ReponseHeaderInfo(); header.Key = key; header.Value = response.Headers.Get(key); responseHeaders.Add(header); } result.Headers = responseHeaders.ToArray(); } } else { success = true; } } catch (WebException wex) { message = wex.Message; } catch (Exception ex) { message = ex.Message; } if (!success) { System.Threading.Thread.Sleep(500); } } if (!success) { logger.Info("Send :: " + attempts.ToString() + " Attempts :: URL = " + url + " :: " + message); } return(result); }