/// <summary>
        /// Performs an API Write (HTTP POST) request, with an optional callback to add extra request params
        /// </summary>
        /// <param name="postJson"></param>
        /// <param name="fileUploadInfo"></param>
        /// <returns></returns>
        protected virtual string GetPostResponseJson(string postJson, FileUploadInfo fileUploadInfo)
        {
            // Build the URL
            string url = Configuration.ApiWriteUrl;

            Debug.WriteLine(String.Format("POSTing request to URL '{0}' with json: {1} ", url, postJson));

            // build our post parameters
            NameValueCollection postParameters = new NameValueCollection();

            postParameters.Add("json", postJson);

            HttpWebRequest request;

            if (fileUploadInfo != null)
            {
                request = RequestBuilder.BuildMultipartFormDataPostRequest(url, postParameters, fileUploadInfo);
            }
            else
            {
                request = RequestBuilder.BuildPostFormRequest(url, postParameters);
            }

            string json = PerformRequest(request);

            Debug.WriteLine(String.Format("JSON Response: \n{0}", json));

            return(json);
        }
		protected virtual void WriteMultipartFormData(HttpWebRequest request, NameValueCollection postParameters, FileUploadInfo fileToUpload, string boundary)
		{
			// keep track of how much data we're sending
			long contentLength = 0;

			// encode the post parameters
			IList<byte[]> parameterBytes = new List<byte[]>();
			foreach (string key in postParameters)
			{
				string paramData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n",
				                                boundary,
				                                key,
				                                postParameters[key]);
				byte[] bytes = Configuration.Encoding.GetBytes(paramData);
				parameterBytes.Add(bytes);
				contentLength += bytes.Length;
			}

			// encode the footer
			string footer = "\r\n--" + boundary + "--\r\n";
			byte[] footerBytes = Configuration.Encoding.GetBytes(footer);
			contentLength += footerBytes.Length;	

			// file header 
			string fileHeader =
				string.Format(
					"--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
					boundary, "file", fileToUpload.FileName, fileToUpload.ContentType);
			byte[] fileHeaderBytes = Configuration.Encoding.GetBytes(fileHeader);
			contentLength += fileHeaderBytes.Length;

			// tell the request how much data to expect
			contentLength += fileToUpload.FileData.Length;
			request.ContentLength = contentLength;

			// write everything to the request stream
			using (Stream requestStream = request.GetRequestStream())
			{
				// params 
				foreach (byte[] bytes in parameterBytes)
				{
					requestStream.Write(bytes, 0, bytes.Length);	
				}
				
				// file header
				requestStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
                               
				// file data
				byte[] buffer = new byte[4096];
				int bytesRead;
				while ((bytesRead = fileToUpload.FileData.Read(buffer, 0, buffer.Length)) > 0)
				{
					requestStream.Write(buffer, 0, bytesRead);
				}
			
				// footer
				requestStream.Write(footerBytes, 0, footerBytes.Length);
			}
		}
		public virtual HttpWebRequest BuildMultipartFormDataPostRequest(string postUrl, NameValueCollection postParameters, FileUploadInfo fileToUpload)
		{
			string boundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x");
			string contentType = "multipart/form-data; boundary=" + boundary;

			HttpWebRequest request = BuildRequest(postUrl);

			// request properties
			request.Method = "POST";
			request.ContentType = contentType;
			
			WriteMultipartFormData(request, postParameters, fileToUpload, boundary);

			return request;
		}
        public void AddCaptioning_FileUploadInfo_VideoId()
        {
            using (var fs = File.OpenRead(FileToUpload))
            {
                var filename = "Test Caption";
                var captionFileUploadInfo = new FileUploadInfo(fs, filename);
                var result = _api.AddCaptioning(captionFileUploadInfo, 4348957026001, filename);

                Assert.That(result != null);
                Assert.That(result.Id > 0);
                Assert.That(result.CaptionSources.Count == 1);

                var captionSource = result.CaptionSources.First();
                Assert.IsFalse(captionSource.IsRemote);
                Assert.That(String.IsNullOrEmpty(captionSource.Url));
                Assert.AreEqual(captionSource.DisplayName, filename);
            }
        }
		/// <summary>
		/// Creates a new video by uploading a file.
		/// </summary>
		/// <param name="video">The metadata for the video you want to create.</param>
		/// <param name="fileUploadInfo">Information for the file to be uploaded.</param>
		/// <param name="encodeTo">If the file requires transcoding, use this parameter to specify the target encoding. 
		/// Valid values are MP4 or FLV, representing the H264 and VP6 codecs respectively. Note that transcoding of 
		/// FLV files to another codec is not currently supported. This parameter is optional and defaults to FLV.</param>
		/// <param name="createMultipleRenditions">If the file is a supported transcodeable type, this optional flag can be 
		/// used to control the number of transcoded renditions. If true (default), multiple renditions at varying encoding 
		/// rates and dimensions are created. Setting this to false will cause a single transcoded VP6 rendition to be created 
		/// at the standard encoding rate and dimensions.</param>
		/// <param name="preserveSourceRendition">If the video file is H.264 encoded and if createMultipleRenditions is true, 
		/// then multiple VP6 renditions are created and in addition the H.264 source is retained as an additional rendition.</param>
		/// <param name="h264NoProcessing">Use this option to prevent H.264 source files from being transcoded. This parameter cannot
		/// be used in combination with create_multiple_renditions. It is optional and defaults to false.</param>
		/// <returns>The numeric ID of the newly created video.</returns>
		public long CreateVideo(BrightcoveVideo video, FileUploadInfo fileUploadInfo, EncodeTo encodeTo, bool createMultipleRenditions, bool preserveSourceRendition, bool h264NoProcessing)
		{
			BrightcoveParamCollection parms = CreateWriteParamCollection("create_video",
																		 methodParams =>
																		 {
																			 methodParams.Add("video", video);
																			 if (encodeTo != EncodeTo.None)
																			 {
																				 methodParams.Add("encode_to", encodeTo.ToString().ToUpper());
																			 }
																			 methodParams.Add("create_multiple_renditions", createMultipleRenditions.ToString().ToLower());
																			 methodParams.Add("preserve_source_rendition", preserveSourceRendition.ToString().ToLower());
																			 methodParams.Add("H264NoProcessing ", h264NoProcessing.ToString().ToLower());
																		 });

			if (fileUploadInfo != null)
			{
				return RunFilePost<BrightcoveResultContainer<long>>(parms, fileUploadInfo).Result;
			}
			
			return RunPost<BrightcoveResultContainer<long>>(parms).Result;
		}
        /// <summary>
        /// Builds the multipart form data post request.
        /// </summary>
        /// <param name="postUrl">The post URL.</param>
        /// <param name="postParameters">The post parameters.</param>
        /// <param name="fileToUpload">The file to upload.</param>
        /// <returns></returns>
        public virtual HttpWebRequest BuildMultipartFormDataPostRequest(string postUrl, NameValueCollection postParameters, FileUploadInfo fileToUpload)
        {
            string boundary    = "-----------------------------" + DateTime.Now.Ticks.ToString("x");
            string contentType = "multipart/form-data; boundary=" + boundary;

            HttpWebRequest request = BuildRequest(postUrl);

            // request properties
            request.Method      = "POST";
            request.ContentType = contentType;

            // Turn off write stream buffering for file uploads to reduce memory usage
            request.AllowWriteStreamBuffering = false;

            WriteMultipartFormData(request, postParameters, fileToUpload, boundary);

            return(request);
        }
        /// <summary>
        /// Writes the multipart form data.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="postParameters">The post parameters.</param>
        /// <param name="fileToUpload">The file to upload.</param>
        /// <param name="boundary">The boundary.</param>
        protected virtual void WriteMultipartFormData(HttpWebRequest request, NameValueCollection postParameters, FileUploadInfo fileToUpload, string boundary)
        {
            // keep track of how much data we're sending
            long contentLength = 0;

            // encode the post parameters
            IList <byte[]> parameterBytes = new List <byte[]>();

            foreach (string key in postParameters)
            {
                string paramData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n",
                                                 boundary,
                                                 key,
                                                 postParameters[key]);
                byte[] bytes = Configuration.Encoding.GetBytes(paramData);
                parameterBytes.Add(bytes);
                contentLength += bytes.Length;
            }

            // encode the footer
            string footer = "\r\n--" + boundary + "--\r\n";

            byte[] footerBytes = Configuration.Encoding.GetBytes(footer);
            contentLength += footerBytes.Length;

            // file header
            string fileHeader =
                string.Format(
                    "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
                    boundary, "file", fileToUpload.FileName, fileToUpload.ContentType);

            byte[] fileHeaderBytes = Configuration.Encoding.GetBytes(fileHeader);
            contentLength += fileHeaderBytes.Length;

            // tell the request how much data to expect
            contentLength        += fileToUpload.FileData.Length;
            request.ContentLength = contentLength;

            // write everything to the request stream
            using (Stream requestStream = request.GetRequestStream())
            {
                // params
                foreach (byte[] bytes in parameterBytes)
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                }

                // file header
                requestStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);

                // file data
                byte[] buffer = new byte[4096];
                int    bytesRead;
                while ((bytesRead = fileToUpload.FileData.Read(buffer, 0, buffer.Length)) > 0)
                {
                    requestStream.Write(buffer, 0, bytesRead);
                }

                // footer
                requestStream.Write(footerBytes, 0, footerBytes.Length);
            }
        }
		/// <summary>
		/// Add a thumbnail asset to the specified audio track.
		/// </summary>
		/// <param name="image">A BrightcoveImage containing the metadata for the image you'd like to create (or update).</param>
		/// <param name="fileUploadInfo">Information for the file to be uploaded.</param>
		/// <param name="audioTrackReferenceId">The reference ID of the audio track to which you'd like to assign the image.</param>
		/// <param name="resize">Set this to false if you don't want your image to be automatically resized to the default size for its type. 
		/// By default images will be resized.</param>
		/// <returns>The image that was added or updated.</returns>
		public BrightcoveImage AddAudioImage(BrightcoveImage image, FileUploadInfo fileUploadInfo, string audioTrackReferenceId, bool resize)
		{
			return DoAddAudioImage(image, fileUploadInfo, -1, audioTrackReferenceId, resize);
		}
 public BrightcoveCaptioning AddCaptioning(FileUploadInfo captionFileUploadInfo, string videoReferenceId, string filename)
 {
     return DoAddCaptioningWithFileUpload(captionFileUploadInfo, -1, videoReferenceId, filename);
 }
		/// <summary>
		/// Adds a logo overlay image to a video.
		/// </summary>
		/// <param name="logoOverlay">The metadata for the logo overlay you'd like to create (or update).</param>
		/// <param name="fileUploadInfo">Information for the file to be uploaded.</param>
		/// <param name="videoReferenceId">The reference ID of the video you want to assign a logo overlay to.</param>
		/// <returns>The newly created or updated BrightcoveLogoOverlay</returns>
		public BrightcoveLogoOverlay AddLogoOverlay(BrightcoveLogoOverlay logoOverlay, FileUploadInfo fileUploadInfo, string videoReferenceId)
		{
			return DoAddLogoOverlay(logoOverlay, fileUploadInfo, -1, videoReferenceId);
		}
		private BrightcoveImage DoAddImage(BrightcoveImage image, FileUploadInfo fileUploadInfo, long videoId, string videoReferenceId, bool resize)
		{
			string propName;
			object propValue;
			GetIdValuesForUpload(videoId, videoReferenceId, "video_id", "video_reference_id", out propName, out propValue);

			BrightcoveParamCollection parms = CreateWriteParamCollection("add_image",
																		 methodParams =>
																		 {
																			 methodParams.Add("image", image);
																			 methodParams.Add("resize", resize.ToString().ToLower());
																			 methodParams.Add(propName, propValue);
																		 });

			return RunFilePost<BrightcoveResultContainer<BrightcoveImage>>(parms, fileUploadInfo).Result;
		}
Esempio n. 12
0
        /// <summary>
        /// Performs an API Write (HTTP POST) request, with an optional callback to add extra request params
        /// </summary>
        /// <param name="postJson"></param>
        /// <param name="fileUploadInfo"></param>
        /// <returns></returns>
        protected virtual string GetPostResponseJson(string postJson, FileUploadInfo fileUploadInfo)
        {
            // Build the URL
            string url = Configuration.ApiWriteUrl;

            Debug.WriteLine(String.Format("POSTing request to URL '{0}' with json: {1} ", url, postJson));

            // build our post parameters
            NameValueCollection postParameters = new NameValueCollection();
            postParameters.Add("json", postJson);

            HttpWebRequest request;
            if (fileUploadInfo != null)
            {
                request = RequestBuilder.BuildMultipartFormDataPostRequest(url, postParameters, fileUploadInfo);
            }
            else
            {
                request = RequestBuilder.BuildPostFormRequest(url, postParameters);
            }

            string json = PerformRequest(request);

            Debug.WriteLine(String.Format("JSON Response: \n{0}", json));

            return json;
        }
 /// <summary>
 /// Performs an API Write (HTTP POST) request that includes file data
 /// </summary>
 /// <param name="postJson"></param>
 /// <param name="fileUploadInfo"></param>
 /// <returns></returns>
 public virtual string GetFilePostResponseJson(string postJson, FileUploadInfo fileUploadInfo)
 {
     return(GetPostResponseJson(postJson, fileUploadInfo));
 }
        private BrightcoveCaptioning DoAddCaptioningWithFileUpload(FileUploadInfo captionFileUploadInfo, long videoId, string videoReferenceid, string filename)
	    {
            string propName;
            object propValue;
            GetIdValuesForUpload(videoId, videoReferenceid, "video_id", "video_reference_id", out propName, out propValue);

            var captionSource = new BrightcoveCaptionSource {
                DisplayName = filename,
            };

            BrightcoveParamCollection parms = CreateWriteParamCollection("add_captioning",
                                                                         methodParams => {
                                                                             methodParams.Add(propName, propValue);
                                                                             methodParams.Add("caption_source", captionSource);
                                                                             methodParams.Add("filename", filename);
                                                                         });

            return RunFilePost<BrightcoveResultContainer<BrightcoveCaptioning>>(parms, captionFileUploadInfo).Result;
        }
		/// <summary>
		/// Add a new thumbnail or video still image to a video, or assign an existing image to another video.
		/// </summary>
		/// <param name="image">The metadata for the image you'd like to create (or update).</param>
		/// <param name="fileUploadInfo">Information for the file to be uploaded.</param>
		/// <param name="videoId">The ID of the video to which you'd like to assign the image.</param>
		/// <param name="resize">Set this to false if you don't want your image to be automatically resized to the default size for its type. 
		/// By default images will be resized.</param>
		/// <returns>The image that was added or updated</returns>
		public BrightcoveImage AddImage(BrightcoveImage image, FileUploadInfo fileUploadInfo, long videoId, bool resize)
		{
			return DoAddImage(image, fileUploadInfo, videoId, null, resize);
		}
Esempio n. 16
0
 /// <summary>
 /// Performs an API Write (HTTP POST) request that includes file data
 /// </summary>
 /// <param name="postJson"></param>
 /// <param name="fileUploadInfo"></param>
 /// <returns></returns>
 public virtual string GetFilePostResponseJson(string postJson, FileUploadInfo fileUploadInfo)
 {
     return GetPostResponseJson(postJson, fileUploadInfo);
 }
		/// <summary>
		/// Add a new thumbnail or video still image to a video, or assign an existing image to another video.
		/// </summary>
		/// <param name="image">The metadata for the image you'd like to create (or update).</param>
		/// <param name="fileUploadInfo">Information for the file to be uploaded.</param>
		/// <param name="videoReferenceId">The reference ID of the video to which you'd like to assign the image.</param>
		/// <param name="resize">Set this to false if you don't want your image to be automatically resized to the default size for its type. 
		/// By default images will be resized.</param>
		/// <returns>The image that was added or updated</returns>
		public BrightcoveImage AddImage(BrightcoveImage image, FileUploadInfo fileUploadInfo, string videoReferenceId, bool resize)
		{
			return DoAddImage(image, fileUploadInfo, -1, videoReferenceId, resize);
		}
		/// <summary>
		/// Creates a new audio track in Brightcove by uploading a file.
		/// </summary>
		/// <param name="audioTrack">The audio track to create</param>
		/// <param name="fileUploadInfo">Information for the file to be uploaded.</param>
		/// <returns>The numeric ID of the uploaded track</returns>
		public long CreateAudioTrack(BrightcoveAudioTrack audioTrack, FileUploadInfo fileUploadInfo)
		{
			BrightcoveParamCollection parms = CreateWriteParamCollection("create_audiotrack",
																		 methodParams => methodParams.Add("audiotrack", audioTrack));
			return RunFilePost<BrightcoveResultContainer<long>>(parms, fileUploadInfo).Result;
		}
		/// <summary>
		/// Adds a logo overlay image to a video.
		/// </summary>
		/// <param name="logoOverlay">The metadata for the logo overlay you'd like to create (or update).</param>
		/// <param name="fileUploadInfo">Information for the file to be uploaded.</param>
		/// <param name="videoId">The ID of the video you want to assign a logo overlay to.</param>
		/// <returns>The newly created or updated BrightcoveLogoOverlay</returns>
		public BrightcoveLogoOverlay AddLogoOverlay(BrightcoveLogoOverlay logoOverlay, FileUploadInfo fileUploadInfo, long videoId)
		{
			return DoAddLogoOverlay(logoOverlay, fileUploadInfo, videoId, null);
		}
		/// <summary>
		/// Add a thumbnail asset to the specified audio track.
		/// </summary>
		/// <param name="image">A BrightcoveImage containing the metadata for the image you'd like to create (or update).</param>
		/// <param name="fileUploadInfo">Information for the file to be uploaded.</param>
		/// <param name="audioTrackId">The ID of the audio track to which you'd like to assign the image.</param>
		/// <param name="resize">Set this to false if you don't want your image to be automatically resized to the default size for its type. 
		/// By default images will be resized.</param>
		/// <returns>The image that was added or updated.</returns>
		public BrightcoveImage AddAudioImage(BrightcoveImage image, FileUploadInfo fileUploadInfo, long audioTrackId, bool resize)
		{
			return DoAddAudioImage(image, fileUploadInfo, audioTrackId, null, resize);
		}
		private BrightcoveLogoOverlay DoAddLogoOverlay(BrightcoveLogoOverlay logoOverlay, FileUploadInfo fileUploadInfo, long videoId, string videoReferenceId)
		{
			string propName;
			object propValue;
			GetIdValuesForUpload(videoId, videoReferenceId, "video_id", "video_reference_id", out propName, out propValue);

			BrightcoveParamCollection parms = CreateWriteParamCollection("add_logo_overlay",
																		 methodParams =>
																		 {
																			 methodParams.Add("logooverlay", logoOverlay);
																			 methodParams.Add(propName, propValue);
																		 });

			return RunFilePost<BrightcoveResultContainer<BrightcoveLogoOverlay>>(parms, fileUploadInfo).Result;
		}
 public BrightcoveCaptioning AddCaptioning(FileUploadInfo captionFileUploadInfo, long videoId, string filename)
 {
     return DoAddCaptioningWithFileUpload(captionFileUploadInfo, videoId, null, filename);
 }