/// <summary> /// Post a picture /// </summary> /// <param name="postData"></param> public virtual Uri UploadAttachment(UploadAttachment Attachment, Twitter.Account account) { if(string.IsNullOrEmpty(account.OAuth_token) || string.IsNullOrEmpty(account.OAuth_token_secret)) throw new InvalidOperationException(Localization.XmlBasedResourceManager.GetString("No credentials supplied")); using (Stream contentStream = Attachment.GetStream()) { if (contentStream == null) throw new InvalidOperationException(Localization.XmlBasedResourceManager.GetString("Unable to open file")); HttpWebRequest request = WebRequestFactory.CreateHttpRequest(API_UPLOAD); request.Timeout = 60000; request.AllowWriteStreamBuffering = false; // don't want to buffer 3MB files, thanks request.AllowAutoRedirect = false; Multipart contents = new Multipart(); contents.UploadPart += new Multipart.UploadPartEvent(contents_UploadPart); contents.Add("source", "PockeTwit"); contents.Add("sourceLink", "http://code.google.com/p/pocketwit/"); if (!string.IsNullOrEmpty(Attachment.Caption)) contents.Add("message", Attachment.Caption); contents.Add("media", contentStream, Path.GetFileName(Attachment.Name), (Attachment.MediaType != null) ? Attachment.MediaType.ContentType : string.Empty ); OAuthAuthorizer.AuthorizeEcho(request, account.OAuth_token, account.OAuth_token_secret); XmlDocument uploadResult = contents.UploadXML(request); if (uploadResult == null) throw new InvalidOperationException("Error parsing result"); return new Uri(uploadResult.SelectSingleNode("//url").InnerText); } }
public AttachmentServiceEventArgs(UploadAttachment attachment) : this(attachment, 0, 0) { }
public AttachmentServiceEventArgs(UploadAttachment attachment, long bytesTotal, long bytesProgress) { _attachment = attachment; _bytesTotal = bytesTotal; _bytesProgress = bytesProgress; }
private bool DoUpload(IUploadService Service, UploadAttachment a) { try { a.Status = UploadAttachment.AttachmentStatus.Uploading; a.UploadedUri = Service.UploadAttachment(a, Account); a.Status = UploadAttachment.AttachmentStatus.Complete; return true; } catch (WebException we) { if (we.Response is HttpWebResponse) { using (HttpWebResponse response = we.Response as HttpWebResponse) { Localization.LocalizedMessageBox.Show( string.Format(Localization.XmlBasedResourceManager.GetString("The media service encountered an error: {0} {1}"), response.StatusCode, response.StatusDescription) ); } } else Localization.LocalizedMessageBox.Show("An unknown error occurred while uploading."); } catch (System.Net.Sockets.SocketException se) { string LocalizedString = null; switch (se.NativeErrorCode) { // Can't contact server case 10050: // Netdown case 10051: // Unreachable case 10061: // Refused case 10062: // Cannot translate name case 10064: // Host down case 10065: // No route to host case 11001: // Hostname not found case 11002: // Host temporarily not found case 11003: // Non-recoverable host lookup error case 11004: // No data record for name - IP address lookup failure LocalizedString = string.Format(Localization.XmlBasedResourceManager.GetString("The media service can not be reached (Error {0})."), se.NativeErrorCode); break; // Interrupted connection case 10004: // Airplane mode case 10052: // Network Reset case 10053: // Connection Aborted case 10054: // Connection Reset LocalizedString = Localization.XmlBasedResourceManager.GetString("Connection to service lost."); break; // Timeout case 10060: // Timeout LocalizedString = Localization.XmlBasedResourceManager.GetString("Connection timed out."); break; default: LocalizedString = string.Format(Localization.XmlBasedResourceManager.GetString("The media service encountered an error: {0} {1}"), se.NativeErrorCode, se.Message); break; } Localization.LocalizedMessageBox.Show( LocalizedString ); } catch (InvalidOperationException) { Localization.LocalizedMessageBox.Show("An internal error occurred while uploading."); } catch (Exception) { Localization.LocalizedMessageBox.Show("An unknown error occurred while uploading."); } a.Status = UploadAttachment.AttachmentStatus.Error; return false; }
private MediaType GetMatchingType(UploadAttachment a) { string ext = Path.GetExtension(a.Name); if (!string.IsNullOrEmpty(ext)) ext = ext.Substring(1); else return null; IPictureService srv = null; MediaTypeGroup[] groups = { MediaTypeGroup.PICTURE, MediaTypeGroup.VIDEO, MediaTypeGroup.AUDIO, MediaTypeGroup.DOCUMENT }; foreach (MediaTypeGroup group in groups) { srv = SelectService(group); if (srv != null) foreach (MediaType r in srv.FileTypes) if (r.MediaGroup == group && r.Extension.Equals(ext, StringComparison.InvariantCultureIgnoreCase)) return r; } return null; }
// Solely designed to convert from AttachmentUploadManager to // IPictureService until we get IMediaServices up and running // Let's do it one step at a time! private bool DoUpload(UploadAttachment a) { MediaType type = GetMatchingType(a); if (type == null) { a.Status = UploadAttachment.AttachmentStatus.Error; return false; } IPictureService Service = SelectService(type.MediaGroup); if (Service == null) { a.Status = UploadAttachment.AttachmentStatus.Error; return false; } IUploadService UService = Service as IUploadService; if (UService != null) return DoUpload(UService, a); else return DoUpload(Service, a); }
private bool DoUpload(IPictureService Service, UploadAttachment a) { PicturePostObject ppo = new PicturePostObject(); if(a.MediaType != null) ppo.ContentType = a.MediaType.ContentType; if (a.Position != null) { ppo.Lat = a.Position.Lat.ToString(System.Globalization.CultureInfo.InvariantCulture); ppo.Lon = a.Position.Lat.ToString(System.Globalization.CultureInfo.InvariantCulture); } ppo.Message = a.Caption; //ppo.PictureStream = a.Contents; ppo.Filename = a.Name; ppo.UseAsync = false; // This is only temporary -> PostUpdate form has event handlers reg'd a.Status = UploadAttachment.AttachmentStatus.Uploading; Service.PostPicture(ppo, Account); if (!string.IsNullOrEmpty(ppo.URL)) { a.UploadedUri = new Uri(ppo.URL); a.Status = UploadAttachment.AttachmentStatus.Complete; } else { a.Status = UploadAttachment.AttachmentStatus.Error; } return true; }
public bool Upload(UploadAttachment a) { if (Attachments.Contains(a)) return DoUpload(a); else throw new InvalidOperationException("Attachment not attached"); }
public UploadAttachment[] GetAttachments() { UploadAttachment[] res = new UploadAttachment[Attachments.Count]; Attachments.CopyTo(res); return res; }
public void DeleteAttachment(UploadAttachment Attachment) { Attachments.Remove(Attachment); }
public void AddAttachment(UploadAttachment Attachment) { if (Attachments.IndexOf(Attachment) < 0) { Attachment.MediaType = GetMatchingType(Attachment); Attachments.Add(Attachment); } }
private void StartUpload(IPictureService mediaService, String fileName, Twitter.Account account) { UploadAttachment Attachment = new UploadAttachment(fileName, txtStatusUpdate.Text, GPSLocation); UploadManager.AddAttachment(Attachment); Cursor.Current = Cursors.WaitCursor; UploadManager.Upload(Attachment); if (Attachment.UploadedUri != null) { picAttachments.Image = Themes.FormColors.GetThemeIcon("insertlink", picAttachments.Height); UpdatePictureData(Attachment.UploadedUri.OriginalString, false); } Cursor.Current = Cursors.Default; }