public bool Save(MediaResourceCreateCommand command, User createdByUser, out string failureReason, out MediaResource mediaResource)
        {
            failureReason = string.Empty;
            mediaResource = null;

            if (!_documentSession.Load <AppRoot>(Constants.AppRootId).VimeoVideoServiceStatus)
            {
                failureReason = "Vimeo video files cannot be imported at the moment. Please try again later.";
                return(false);
            }

            bool returnValue;

            try
            {
                string apiUri = string.Format(_apiUriFormat, command.VideoId);

                dynamic data = GetVideoDataFromApi(apiUri);

                // Get thumbnail URI
                var thumbnailUri = (string)data[0]["thumbnail_large"];
                var videoWidth   = (int)data[0]["width"];
                var videoHeight  = (int)data[0]["height"];

                var imageCreationTasks = new List <ImageCreationTask>();

                using (var stream = new MemoryStream(new WebClient().DownloadData(thumbnailUri)))
                {
                    var image = ImageUtility.Load(stream);

                    mediaResource = _mediaResourceFactory.MakeContributionExternalVideo(
                        command.Key,
                        createdByUser,
                        command.UploadedOn,
                        string.Format(_uriFormat, command.VideoId),
                        "vimeo",
                        data,
                        command.VideoId,
                        ImageDimensions.MakeRectangle(videoWidth, videoHeight),
                        thumbnailUri,
                        image.GetDimensions(),
                        MediaTypeUtility.GetStandardMimeTypeForMimeType(image.GetMimeType()),
                        GetVideoMetadata(data, command.VideoId),
                        imageCreationTasks);

                    image.Save(mediaResource, imageCreationTasks, _mediaFilePathFactory);

                    image.Cleanup();
                }

                returnValue = true;
            }
            catch (Exception exception)
            {
                _logger.ErrorException("Error saving video", exception);

                failureReason = "The video cannot be retrieved from Vimeo. Please check the video and try again.";
                returnValue   = false;
            }

            return(returnValue);
        }
        public bool Save(MediaResourceCreateCommand command, User createdByUser, out string failureReason, out MediaResource mediaResource)
        {
            failureReason = string.Empty;
            mediaResource = null;

            if (!_documentSession.Load <AppRoot>(Constants.AppRootId).YouTubeVideoServiceStatus)
            {
                failureReason = "Youtube video files cannot be imported at the moment. Please try again later.";
                return(false);
            }

            bool returnValue;

            try
            {
                string apiUri = string.Format(_apiUriFormat, command.VideoId);

                JObject data = GetVideoDataFromApi(apiUri);

                // Get thumbnail URI
                var mediaThumbnails = data["entry"]["media$group"]["media$thumbnail"];
                var mediaThumbnail  = mediaThumbnails.Single(x => (string)x["yt$name"] == "hqdefault");
                var thumbnailUri    = (string)mediaThumbnail["url"];

                var imageCreationTasks = new List <ImageCreationTask>();

                using (var stream = new MemoryStream(new WebClient().DownloadData(thumbnailUri)))
                {
                    var image = ImageUtility.Load(stream);

                    mediaResource = _mediaResourceFactory.MakeContributionExternalVideo(
                        command.Key,
                        createdByUser,
                        command.UploadedOn,
                        string.Format(_uriFormat, command.VideoId),
                        "youtube",
                        data,
                        command.VideoId,
                        ImageDimensions.MakeRectangle(1024, 576), // As at 08/2012, Youtube states that videos are encoded in 16:9 ratio. 1024x576px is the max size we present in Bowerbird at that ratio
                        thumbnailUri,
                        image.GetDimensions(),
                        MediaTypeUtility.GetStandardMimeTypeForMimeType(image.GetMimeType()),
                        GetVideoMetadata(data, command.VideoId),
                        imageCreationTasks);

                    image.Save(mediaResource, imageCreationTasks, _mediaFilePathFactory);

                    image.Cleanup();
                }

                returnValue = true;
            }
            catch (Exception exception)
            {
                _logger.ErrorException("Error saving video", exception);

                failureReason = "The video cannot be retrieved from Youtube. Please check the video and try again.";
                returnValue   = false;
            }

            return(returnValue);
        }