示例#1
0
        private static async Task <CloudBlockBlob> HandleThumbBlob(string thumbUrl, CloudBlobContainer serverContainer, HttpClient httpClient)
        {
            VideoUrlHelper videoUrlHelper = VideoUrlHelper.Parse(thumbUrl);

            CloudBlockBlob thumbBlob;

            if (videoUrlHelper != null)
            {
                thumbBlob = serverContainer.GetBlockBlobReference(videoUrlHelper.FileName.ToLower());
            }
            else
            {
                throw new InvalidOperationException("Unable to parse thumb url: " + thumbUrl);
            }

            thumbBlob.Properties.CacheControl = "max-age=31536000";
            thumbBlob.Properties.ContentType  = GetContentType(videoUrlHelper.FileName);

            Stream sourceStream = await httpClient.GetStreamAsync(thumbUrl);

            await thumbBlob.UploadFromStreamAsync(sourceStream);

            return(thumbBlob);
        }
示例#2
0
        private static async Task <CloudBlockBlob> HandleVideoBlob(string videoUrl, CloudBlobContainer serverContainer, HttpClient httpClient, TraceWriter log)
        {
            VideoUrlHelper videoUrlHelper = VideoUrlHelper.Parse(videoUrl);

            CloudBlockBlob videoBlob;

            if (videoUrlHelper != null)
            {
                videoBlob = serverContainer.GetBlockBlobReference(videoUrlHelper.FileName.ToLower());
            }
            else
            {
                throw new ArgumentException("Unable to parse: " + videoUrl);
            }

            videoBlob.Properties.CacheControl = "max-age=31536000";
            if (videoUrlHelper.FileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
            {
                videoBlob.Properties.ContentType = "video/mp4";
            }
            else if (videoUrlHelper.FileName.EndsWith(".mov", StringComparison.OrdinalIgnoreCase))
            {
                videoBlob.Properties.ContentType = "video/quicktime";
            }
            else if (videoUrlHelper.Server.Contains("youtube.com"))
            {
                log.Warning("Video from youtube.com skipped: " + videoUrl);
            }
            else if (videoUrlHelper.Server.Contains("instagram.com"))
            {
                log.Warning("Video from instagram.com skipped: " + videoUrl);
            }
            else if (videoUrlHelper.Server.Contains("vimeo.com"))
            {
                log.Warning("Video from vimeo.com skipped: " + videoUrl);
            }
            else
            {
                throw new ArgumentException("Unexpected ending in: " + videoUrl);
            }

            var response = await httpClient.GetAsync(videoUrl);

            response.EnsureSuccessStatusCode();

            Stream          sourceStream = null;
            CloudBlobStream uploadStream = null;

            try
            {
                sourceStream = await httpClient.GetStreamAsync(videoUrl);

                uploadStream = await videoBlob.OpenWriteAsync();

                byte[] bytes = new byte[64 * 1024];
                int    length;
                do
                {
                    length = await sourceStream.ReadAsync(bytes, 0, 64 * 1024);

                    await uploadStream.WriteAsync(bytes, 0, length);
                }while (length > 0);
            }
            finally
            {
                if (sourceStream != null)
                {
                    sourceStream.Dispose();
                }
                if (uploadStream != null)
                {
                    await uploadStream.FlushAsync();

                    uploadStream.Dispose();
                }
            }

            await videoBlob.FetchAttributesAsync();

            return(videoBlob);
        }