コード例 #1
0
    public async Task UploadVideo(Video video) {
      var videoServiceUrl = await SpHelper.GetVideoPortalRootUrl();

      // create new video object
      var newVideo = new JsonHelpers.NewVideoPayload {
        Title = video.Title,
        Description = video.Description,
        FileName = video.FileName,
        Metadata = new NewVideoPayloadMetadata { Type = "SP.Publishing.VideoItem" }
      };
      var newVideoJson = JsonConvert.SerializeObject(newVideo, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

      // create video placeholder
      var placeholderRequestQuery = string.Format("{0}/_api/VideoService/Channels('{1}')/Videos", videoServiceUrl, video.ChannelId);
      var placeholderRequestBody = new StringContent(newVideoJson);
      placeholderRequestBody.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");

      // issue request & get response 
      var createPlaceholderResponse = await _client.PostAsync(placeholderRequestQuery, placeholderRequestBody);
      string createPlaceholderResponseString = await createPlaceholderResponse.Content.ReadAsStringAsync();
      // convert response to object
      var jsonResponse = JsonConvert.DeserializeObject<JsonHelpers.ChannelVideosSingle>(createPlaceholderResponseString);


      // upload video
      const int fileUploadChunkSize = 2 * 1024 * 1024; // upload 2MB chunks
      long fileBytesUploaded = 0;
      bool canContinue = true;
      var fileUploadSessionId = Guid.NewGuid().ToString();

      string uploadVideoEndpoint = string.Format("{0}/_api/VideoService/Channels('{1}')/Videos('{2}')/GetFile()/StartUpload(uploadId=guid'{3}')",
                                      videoServiceUrl,
                                      video.ChannelId,
                                      jsonResponse.Data.ID,
                                      fileUploadSessionId);

      using (HttpResponseMessage startResponseMessage = await _client.PostAsync(uploadVideoEndpoint, null)) {
        canContinue = startResponseMessage.IsSuccessStatusCode;
      }

      // upload all but the last chunk
      var totalChunks = Math.Ceiling(video.FileContent.Length / (double)fileUploadChunkSize);
      while (fileBytesUploaded < fileUploadChunkSize * (totalChunks - 1)) {
        if (!canContinue) { break; }

        // read file in
        using (var videoFileReader = new BinaryReader(new MemoryStream(video.FileContent))) {
          // advance to the part of the video to show
          videoFileReader.BaseStream.Seek(fileBytesUploaded, SeekOrigin.Begin);

          // get a slice of the file to upload
          var videoSlice = videoFileReader.ReadBytes(Convert.ToInt32(fileUploadChunkSize));

          // upload slice
          string chunkUploadUrl = string.Format("{0}/_api/VideoService/Channels('{1}')/Videos('{2}')/GetFile()/ContinueUpload(uploadId=guid'{3}',fileOffset='{4}')",
                                    videoServiceUrl,
                                    video.ChannelId,
                                    jsonResponse.Data.ID,
                                    fileUploadSessionId, fileBytesUploaded);
          using (var fileContent = new StreamContent(new MemoryStream(videoSlice))) {
            using (HttpResponseMessage uploadResponseMessage = await _client.PostAsync(chunkUploadUrl, fileContent)) {
              canContinue = uploadResponseMessage.IsSuccessStatusCode;
              fileBytesUploaded += fileUploadChunkSize;
            }
          }
        }
      }

      // upload last chunk
      if (canContinue) {
        var lastBytesToUpload = video.FileContent.Length - fileBytesUploaded;
        using (var videoFileReader = new BinaryReader(new MemoryStream(video.FileContent))) {
          // jump to the part of the file to upload
          videoFileReader.BaseStream.Seek(fileBytesUploaded, SeekOrigin.Begin);

          // get the last slice of file to upload
          var videoSlice = videoFileReader.ReadBytes(Convert.ToInt32(lastBytesToUpload));
          string chunkUploadUrl = string.Format("{0}/_api/VideoService/Channels('{1}')/Videos('{2}')/GetFile()/FinishUpload(uploadId=guid'{3}',fileOffset='{4}')",
                                    videoServiceUrl,
                                    video.ChannelId,
                                    jsonResponse.Data.ID,
                                    fileUploadSessionId, fileBytesUploaded);
          using (var fileContent = new StreamContent(new MemoryStream(videoSlice))) {
            using (HttpResponseMessage uploadResponseMessage = await _client.PostAsync(chunkUploadUrl, fileContent)) {
              canContinue = uploadResponseMessage.IsSuccessStatusCode;
              fileBytesUploaded += fileUploadChunkSize;
            }
          }
        }
      }
    }
コード例 #2
0
        public async Task UploadVideo(Video video)
        {
            var videoServiceUrl = await SpHelper.GetVideoPortalRootUrl();

            // create new video object
            var newVideo = new JsonHelpers.NewVideoPayload {
                Title       = video.Title,
                Description = video.Description,
                FileName    = video.FileName,
                Metadata    = new NewVideoPayloadMetadata {
                    Type = "SP.Publishing.VideoItem"
                }
            };
            var newVideoJson = JsonConvert.SerializeObject(newVideo, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            // create video placeholder
            var placeholderRequestQuery = string.Format("{0}/_api/VideoService/Channels('{1}')/Videos", videoServiceUrl, video.ChannelId);
            var placeholderRequestBody  = new StringContent(newVideoJson);

            placeholderRequestBody.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");

            // issue request & get response
            var createPlaceholderResponse = await _client.PostAsync(placeholderRequestQuery, placeholderRequestBody);

            string createPlaceholderResponseString = await createPlaceholderResponse.Content.ReadAsStringAsync();

            // convert response to object
            var jsonResponse = JsonConvert.DeserializeObject <JsonHelpers.ChannelVideosSingle>(createPlaceholderResponseString);


            // upload video
            const int fileUploadChunkSize = 2 * 1024 * 1024; // upload 2MB chunks
            long      fileBytesUploaded   = 0;
            bool      canContinue         = true;
            var       fileUploadSessionId = Guid.NewGuid().ToString();

            string uploadVideoEndpoint = string.Format("{0}/_api/VideoService/Channels('{1}')/Videos('{2}')/GetFile()/StartUpload(uploadId=guid'{3}')",
                                                       videoServiceUrl,
                                                       video.ChannelId,
                                                       jsonResponse.Data.ID,
                                                       fileUploadSessionId);

            using (HttpResponseMessage startResponseMessage = await _client.PostAsync(uploadVideoEndpoint, null)) {
                canContinue = startResponseMessage.IsSuccessStatusCode;
            }

            // upload all but the last chunk
            var totalChunks = Math.Ceiling(video.FileContent.Length / (double)fileUploadChunkSize);

            while (fileBytesUploaded < fileUploadChunkSize * (totalChunks - 1))
            {
                if (!canContinue)
                {
                    break;
                }

                // read file in
                using (var videoFileReader = new BinaryReader(new MemoryStream(video.FileContent))) {
                    // advance to the part of the video to show
                    videoFileReader.BaseStream.Seek(fileBytesUploaded, SeekOrigin.Begin);

                    // get a slice of the file to upload
                    var videoSlice = videoFileReader.ReadBytes(Convert.ToInt32(fileUploadChunkSize));

                    // upload slice
                    string chunkUploadUrl = string.Format("{0}/_api/VideoService/Channels('{1}')/Videos('{2}')/GetFile()/ContinueUpload(uploadId=guid'{3}',fileOffset='{4}')",
                                                          videoServiceUrl,
                                                          video.ChannelId,
                                                          jsonResponse.Data.ID,
                                                          fileUploadSessionId, fileBytesUploaded);
                    using (var fileContent = new StreamContent(new MemoryStream(videoSlice))) {
                        using (HttpResponseMessage uploadResponseMessage = await _client.PostAsync(chunkUploadUrl, fileContent)) {
                            canContinue        = uploadResponseMessage.IsSuccessStatusCode;
                            fileBytesUploaded += fileUploadChunkSize;
                        }
                    }
                }
            }

            // upload last chunk
            if (canContinue)
            {
                var lastBytesToUpload = video.FileContent.Length - fileBytesUploaded;
                using (var videoFileReader = new BinaryReader(new MemoryStream(video.FileContent))) {
                    // jump to the part of the file to upload
                    videoFileReader.BaseStream.Seek(fileBytesUploaded, SeekOrigin.Begin);

                    // get the last slice of file to upload
                    var    videoSlice     = videoFileReader.ReadBytes(Convert.ToInt32(lastBytesToUpload));
                    string chunkUploadUrl = string.Format("{0}/_api/VideoService/Channels('{1}')/Videos('{2}')/GetFile()/FinishUpload(uploadId=guid'{3}',fileOffset='{4}')",
                                                          videoServiceUrl,
                                                          video.ChannelId,
                                                          jsonResponse.Data.ID,
                                                          fileUploadSessionId, fileBytesUploaded);
                    using (var fileContent = new StreamContent(new MemoryStream(videoSlice))) {
                        using (HttpResponseMessage uploadResponseMessage = await _client.PostAsync(chunkUploadUrl, fileContent)) {
                            canContinue        = uploadResponseMessage.IsSuccessStatusCode;
                            fileBytesUploaded += fileUploadChunkSize;
                        }
                    }
                }
            }
        }