private bool UploadFile(string strURL, string strFile, string strCompleteURI, IProgress pProgress) { VimeoLogger.Log("Vimeo::UploadFile"); Debug.Assert(File.Exists(strFile)); Debug.Assert(!String.IsNullOrWhiteSpace(strURL)); m_strClipURI = null; FileInfo fi = new FileInfo(strFile); long lFileSize = fi.Length; FileStream fstream = File.OpenRead(strFile); long lChunkSize = m_lChunkSize; byte[] buffer = new byte[lChunkSize]; long lStart = 0; while (true) { if (pProgress != null && pProgress.GetCanceled()) { fstream.Close(); return false; } long lEnd = Math.Min(lStart + lChunkSize, lFileSize); long lBytes = lEnd - lStart; HttpWebRequest request = WebRequest.CreateDefault(new Uri(strURL)) as HttpWebRequest; request.Method = @"PUT"; request.Accept = @"application/vnd.vimeo.*+json; version=3.2"; request.ContentType = @"video/mp4"; request.Headers = new WebHeaderCollection() { {"Content-Range", String.Format("bytes {0}-{1}/{2}", lStart, lEnd, lFileSize)} }; request.KeepAlive = false; try { { long lRead = (long)fstream.Read(buffer, 0, (int)lBytes); Debug.Assert(lRead == lBytes); request.ContentLength = lRead; Stream requestStream = request.GetRequestStream(); requestStream.Write(buffer, 0, (int)lRead); requestStream.Close(); } //Do call HttpWebResponse response = request.GetResponse() as HttpWebResponse; VimeoLogger.Log(response); HttpStatusCode status = response.StatusCode; //Successful uploads with have a HTTP 200 status code. A 501 error means you did not perform a PUT or the request was malformed. //Not sure exactly what to do if I get an error :( if (status != HttpStatusCode.OK) return false; Stream responseStream = response.GetResponseStream(); StreamReader r = new StreamReader(responseStream); string strResponse = r.ReadToEnd(); VimeoLogger.Log("Vimeo::UploadFile; response: " + strResponse); r.Close(); responseStream.Close(); response.Close(); if (pProgress != null) { double dPercentage = lEnd * 100.0 / lFileSize; pProgress.SetProgress(dPercentage); } lStart += lBytes; if (lEnd >= lFileSize) break; } catch (WebException wex) { VimeoLogger.Log(wex); m_strErrorMessage = wex.Message; fstream.Close(); return false; } } fstream.Close(); //Verify the upload bool bOK = VerifyUpload(strURL); if (bOK) { bOK = CompleteUpload(strCompleteURI); } return bOK; }
public bool Upload(string strFile, string strTitle, string strDescription, string strTags, Privacy ePrivacy, string strPassword, IProgress pProgress) { VimeoLogger.Log("Vimeo::Upload"); Debug.Assert(File.Exists(strFile)); Debug.Assert(!String.IsNullOrEmpty(m_strAccessToken));//Did you call ObtainAccessToken? if (String.IsNullOrEmpty(m_strAccessToken)) { return false; } if (pProgress != null && pProgress.GetCanceled()) return false; UploadTicketResponse ticket = GenerateUploadTicket(); //Make sure ticket is good! :) if (ticket == null || String.IsNullOrWhiteSpace(ticket.ticket_id)) return false; if (pProgress != null && pProgress.GetCanceled()) return false; bool bOK = UploadFile(ticket.upload_link_secure, strFile, ticket.complete_uri, pProgress); if (pProgress != null && pProgress.GetCanceled()) return false; if (bOK) { bOK = SetVideoInformation(m_strClipURI, strTitle, strDescription); } if (pProgress != null && pProgress.GetCanceled()) return false; if( bOK ) { bool bSetPrivacy = SetPrivacyInformation( m_strClipURI, ePrivacy, strPassword ); if ( !bSetPrivacy ) { //TODO: keep note; but not fail upload. } } if (pProgress != null && pProgress.GetCanceled()) return false; if (bOK) { bOK = SetVideoTags(m_strClipURI, strTags); } if (pProgress != null && pProgress.GetCanceled()) return false; return bOK; }