public void MediaResourceCreateCommandHandler_Handle() { var user = FakeObjects.TestUserWithId(); MediaResource newValue = null; var command = new MediaResourceCreateCommand() { UserId = user.Id, UploadedOn = FakeValues.CreatedDateTime, OriginalFileName = FakeValues.Filename, Stream = null, Usage = FakeValues.Usage }; using (var session = _store.OpenSession()) { session.Store(user); var commandHandler = new MediaResourceCreateCommandHandler(session, _mockMediaFilePathService.Object); commandHandler.Handle(command); session.SaveChanges(); newValue = session.Query <MediaResource>().FirstOrDefault(); } Assert.IsNotNull(newValue); Assert.AreEqual(command.UploadedOn, newValue.UploadedOn); //Assert.AreEqual(user.DenormalisedUserReference(), newValue.CreatedByUser); }
public bool Save(MediaResourceCreateCommand command, User createdByUser, out string failureReason, out MediaResource mediaResource) { failureReason = string.Empty; mediaResource = null; if (!_documentSession.Load <AppRoot>(Constants.AppRootId).AudioServiceStatus) { failureReason = "Audio files cannot be uploaded at the moment. Please try again later."; return(false); } bool returnValue; try { var audio = AudioUtility.Load(command.FileStream, command.FileName, command.FileMimeType); mediaResource = _mediaResourceFactory.MakeContributionAudio( command.Key, createdByUser, command.UploadedOn, command.FileName, new Object(), command.FileMimeType, GetAudioMetadata(audio)); string filePath = _mediaFilePathFactory.MakeMediaFilePath(mediaResource.Id, "Original", MediaTypeUtility.GetStandardExtensionForMimeType(command.FileMimeType)); audio.SaveAs(filePath); returnValue = true; } catch (Exception exception) { _logger.ErrorException("Error saving audio", exception); failureReason = "The file is corrupted or not a valid audio file and could not be saved. Please check the file 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).DocumentServiceStatus) { failureReason = "Word documents and PDF files cannot be uploaded at the moment. Please try again later."; return(false); } bool returnValue; try { //var extension = Path.GetExtension(command.OriginalFileName) ?? string.Empty; //var acceptedFileTypes = new List<string>() // { // "doc", // "docx", // "pdf" // }; //if (acceptedFileTypes.Any(x => x.Contains(extension))) //{ // MakeDocumentMediaResourceFiles(mediaResource, command); //} returnValue = true; } catch (Exception exception) { _logger.ErrorException("Error saving document", exception); failureReason = "The file is corrupted or not a valid Word or PDF document and could not be saved. Please check the file 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).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).ImageServiceStatus) { failureReason = "Image files cannot be uploaded at the moment. Please try again later."; return(false); } ImageUtility image = null; bool returnValue; try { var imageCreationTasks = new List <ImageCreationTask>(); image = ImageUtility.Load(command.FileStream); if (command.Usage == "contribution") { mediaResource = MakeContributionImage( createdByUser, image, imageCreationTasks, command); } else if (command.Usage == "avatar") { mediaResource = MakeAvatarImage( createdByUser, image, imageCreationTasks, command); } else if (command.Usage == "background") { mediaResource = MakeBackgroundImage( createdByUser, image, imageCreationTasks, command); } else { throw new ArgumentException("The specified usage '" + command.Usage + "' is not recognised."); } image.Save(mediaResource, imageCreationTasks, _mediaFilePathFactory); returnValue = true; } catch (Exception exception) { _logger.ErrorException("Error saving images", exception); failureReason = "The file is corrupted or not a valid JPEG and could not be saved. Please check the file and try again."; returnValue = false; } finally { if (image != null) { image.Cleanup(); } } return(returnValue); }
private MediaResource MakeBackgroundImage(User createdByUser, ImageUtility image, List <ImageCreationTask> imageCreationTasks, MediaResourceCreateCommand command) { ImageDimensions imageDimensions = image.GetDimensions(); return(_mediaResourceFactory.MakeBackgroundImage( command.Key, createdByUser, command.UploadedOn, command.FileName, imageDimensions, image.GetMimeType(), imageCreationTasks)); }
private MediaResource MakeContributionImage(User createdByUser, ImageUtility image, List <ImageCreationTask> imageCreationTasks, MediaResourceCreateCommand command) { IDictionary <string, object> exifData = image.GetExifData(); ImageDimensions imageDimensions = image.GetDimensions(); var metadata = new Dictionary <string, string>(); if (exifData.Count > 0) { metadata = GetImageExifMetadata(exifData, createdByUser.Timezone); } return(_mediaResourceFactory.MakeContributionImage( command.Key, createdByUser, command.UploadedOn, command.FileName, imageDimensions, command.FileStream.Length, exifData, image.GetMimeType(), metadata, imageCreationTasks)); }
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); }