public async Task UploadToYouTube(HttpPostedFile postedFile, string title) { UploadingDispatcher.SetProgress(uploadId, 2); Video video = new Video(); video.Title = title; video.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema)); video.Private = false; video.MediaSource = new MediaFileSource(postedFile.InputStream, postedFile.FileName, postedFile.ContentType); var link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"); link.Rel = ResumableUploader.CreateMediaRelation; video.YouTubeEntry.Links.Add(link); var youtubeApiKey = ConfigurationManager.AppSettings["youtubeApiKey"]; var applicationName = ConfigurationManager.AppSettings["applicationName"]; var youtubeUserName = ConfigurationManager.AppSettings["youtubeUserName"]; var youtubePassword = ConfigurationManager.AppSettings["youtubePassword"]; var youtubeChunksize = int.Parse(ConfigurationManager.AppSettings["youtubeChunksize"]); var resumableUploader = new ResumableUploader(youtubeChunksize); resumableUploader.AsyncOperationCompleted += resumableUploader_AsyncOperationCompleted; resumableUploader.AsyncOperationProgress += resumableUploader_AsyncOperationProgress; var youTubeAuthenticator = new ClientLoginAuthenticator(applicationName, ServiceNames.YouTube, youtubeUserName, youtubePassword); youTubeAuthenticator.DeveloperKey = youtubeApiKey; resumableUploader.InsertAsync(youTubeAuthenticator, video.YouTubeEntry, uploadId); }
public void ClientLoginAuthenticatorTest() { Tracing.TraceMsg("Entering ClientLoginAuthenticatorTest"); ClientLoginAuthenticator auth = new ClientLoginAuthenticator( this.ApplicationName, ServiceNames.Documents, this.userName, this.passWord); HttpWebRequest request = auth.CreateHttpWebRequest("GET", new Uri(DocumentsListQuery.documentsBaseUri)); request.Headers.Set(GDataGAuthRequestFactory.GDataVersion, "3.0"); HttpWebResponse response = request.GetResponse() as HttpWebResponse; if (response.StatusCode == HttpStatusCode.Redirect) { request = WebRequest.Create(response.Headers["Location"]) as HttpWebRequest; auth.ApplyAuthenticationToRequest(request); response = request.GetResponse() as HttpWebResponse; } Assert.IsTrue(response.StatusCode == HttpStatusCode.OK); }
public string Sling(string path) { string token = Guid.NewGuid().ToString("N"); zipPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip(); fz.CreateZip(zipPath, path, true, "", ""); int CHUNK_SIZE = 10485760; ClientLoginAuthenticator cla = new ClientLoginAuthenticator("commanigy-slingshot-v1", ServiceNames.Documents, "username", "password"); // Set up resumable uploader and notifications ResumableUploader ru = new ResumableUploader(CHUNK_SIZE); ru.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(this.OnDone); ru.AsyncOperationProgress += new AsyncOperationProgressEventHandler(this.OnProgress); // Set metadata for our upload. Document entry = new Document(); entry.Title = string.Format("testupload", token); //entry.MediaSource = new MediaFileSource(new FileStream(zipPath, FileMode.Open), "test.zip", "application/zip"); //entry.MediaSource = new MediaFileSource(zipPath, "application/zip"); entry.MediaSource = new MediaFileSource(zipPath, "application/zip"); // Add the upload uri to document entry. Uri createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full"); AtomLink link = new AtomLink(createUploadUrl.AbsoluteUri); link.Rel = ResumableUploader.CreateMediaRelation; entry.DocumentEntry.Links.Add(link); string userObject = "Just a sample text"; ru.InsertAsync(cla, entry.DocumentEntry, userObject); //ru.InsertAsync(cla, createUploadUrl, new FileStream(zipPath, FileMode.Open), "application/zip", "mytest.zip", userObject); return(token); }
public void Execute(string title, string description, string keywords, string category, ID item, string handle) { master = Factory.GetDatabase("master"); Item settings = master.GetItem(Data.YouTubeSettingsID); this.handleID = handle; if (settings == null) { Log.Error("YouTube Settings not found!", this); return; } else { devKey = settings.Fields[Data.DevKeyFieldID].Value; clientID = settings.Fields[Data.ClientIDFieldID].Value; username = settings.Fields[Data.UserNameFieldID].Value; password = settings.Fields[Data.PasswordFieldID].Value; EnsureRU(); UserState u = new UserState(); u.RetryCounter = 0; Authenticator youTubeAuthenticator = new ClientLoginAuthenticator("SitecoreYouTubeUploader", ServiceNames.YouTube, username, password); youTubeAuthenticator.DeveloperKey = devKey; YouTubeEntry newVideo = new YouTubeEntry(); newVideo.Media = new Google.GData.YouTube.MediaGroup(); newVideo.Media.Title = new MediaTitle(title); newVideo.Media.Keywords = new MediaKeywords(keywords); newVideo.Media.Description = new MediaDescription(description); newVideo.Media.Categories.Add(new MediaCategory(category, YouTubeNameTable.CategorySchema)); MediaItem media = master.GetItem(item); Stream mediaStream = media.GetMediaStream(); MemoryStream uploadStream = CopyStream.CopyMediaItemStream(mediaStream); newVideo.MediaSource = new MediaFileSource(uploadStream, media.DisplayName, media.MimeType); AtomLink link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"); link.Rel = ResumableUploader.CreateMediaRelation; newVideo.Links.Add(link); // Async Method // uploader.InsertAsync(youTubeAuthenticator, newVideo, u); try { var response = uploader.Insert(youTubeAuthenticator, newVideo); string id = response.ResponseUri.PathAndQuery; id = id.Substring(id.IndexOf("/uploads/") + 9, id.Length - id.IndexOf("/uploads/") - 9); Sitecore.Context.Job.Status.Messages.Add(id); Sitecore.Context.Job.Status.Failed = false; } catch (WebException ex) { Sitecore.Context.Job.Status.Messages.Add(ex.StackTrace); Sitecore.Context.Job.Status.Failed = true; Log.Error("Youtube Video upload failed", ex, this); } } }
public void uploadFile(string fileName, DocumentEntry parentFolder) { if (cancel()) { return; } string fileExtension = System.IO.Path.GetExtension(fileName).ToLower(); if (!m_fileFilters.ContainsKey(fileExtension)) { return; } int CHUNK_SIZE = 1; ResumableUploader ru = new ResumableUploader(CHUNK_SIZE); ru.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(this.OnDone); ru.AsyncOperationProgress += new AsyncOperationProgressEventHandler(this.OnProgress); // Check if entry exists FolderQuery contentQuery = new FolderQuery(parentFolder.ResourceId); contentQuery.Title = Path.GetFileName(fileName); contentQuery.TitleExact = true; DocumentsFeed contents = m_service.Query(contentQuery); bool fileExists = contents.Entries.Count > 0; DocumentEntry entry = fileExists?contents.Entries[0] as DocumentEntry:new DocumentEntry(); entry.Title.Text = Path.GetFileName(fileName); string mimeType = m_fileFilters[fileExtension]; entry.MediaSource = new MediaFileSource(fileName, mimeType); // Define the resumable upload link string notConvert = "?convert=false"; Uri createUploadUrl; if (parentFolder == null) { createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full" + notConvert); } else { createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full/" + parentFolder.ResourceId + "/contents" + notConvert); } AtomLink link = new AtomLink(createUploadUrl.AbsoluteUri); link.Rel = ResumableUploader.CreateMediaRelation; entry.Links.Add(link); // Set the service to be used to parse the returned entry entry.Service = m_service; // Instantiate the ResumableUploader component. ResumableUploader uploader = new ResumableUploader(); // Set the handlers for the completion and progress events uploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(OnDone); uploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(OnProgress); ClientLoginAuthenticator cla = new ClientLoginAuthenticator("uploader", ServiceNames.Documents, m_username, m_password); // Start the upload process if (cancel()) { return; } if (fileExists) { uploader.UpdateAsync(cla, entry, new Object()); } else { uploader.InsertAsync(cla, entry, new Object()); } }