private void SavingMedia(IMediaService sender, SaveEventArgs <IMedia> e) { MediaFileSystem mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>(); IContentSection contentSection = UmbracoConfig.For.UmbracoSettings().Content; IEnumerable <string> supportedTypes = contentSection.ImageFileTypes.ToList(); foreach (var entity in e.SavedEntities) { if (!entity.HasProperty("umbracoFile")) { continue; } var path = entity.GetValue <string>("umbracoFile"); var extension = Path.GetExtension(path)?.Substring(1); if (!supportedTypes.InvariantContains(extension)) { continue; } var fullPath = mediaFileSystem.GetFullPath(path); using (ImageFactory imageFactory = new ImageFactory(true)) { ResizeLayer layer = new ResizeLayer(new Size(1920, 0), ResizeMode.Max) { Upscale = false }; imageFactory.Load(fullPath) .Resize(layer) .Save(fullPath); } } }
public void CopyFileStream(IMedia mediaToCopy, IMedia copiedMedia) { string path = mediaToCopy?.GetUrl(); if (string.IsNullOrEmpty(path)) { return; } string fullPath = _mediaFileSystem?.GetFullPath(path); if (string.IsNullOrEmpty(fullPath)) { return; } try { using (var inStream = _mediaFileSystem.OpenFile(WebUtility.UrlDecode(fullPath))) { inStream.Position = 0; copiedMedia.Properties[UmbracoFileAlias].Value = null; copiedMedia.SetValue(UmbracoFileAlias, Path.GetFileName(path), inStream); } } catch { return; } }
private void MediaService_Saving(IMediaService sender, SaveEventArgs <IMedia> e) { VisionServiceClient visionServiceClient = new VisionServiceClient(_visionApiKey, _visionApiUrl); foreach (IMedia media in e.SavedEntities) { string relativeImagePath = ImagePathHelper.GetImageFilePath(media); string fullPath = _fs.GetFullPath(relativeImagePath); // Computer Vision API using (Stream imageFileStream = File.OpenRead(fullPath)) { // Call the Computer Vision API Task <AnalysisResult> analysisResultTask = visionServiceClient .AnalyzeImageAsync( imageFileStream, new [] { VisualFeature.Description, VisualFeature.Adult, VisualFeature.Tags, VisualFeature.Categories }, new[] { "celebrities", "landmarks" } ); analysisResultTask.Wait(); var computervisionResult = analysisResultTask.Result; // Get the result and set the values of the ContentItem var celebrityTags = new List <string>(); var landmarksTags = new List <string>(); foreach (Category category in computervisionResult.Categories.Where(a => a.Detail != null)) { var detailResult = JsonConvert.DeserializeObject <DetailsModel>(category.Detail.ToString()); celebrityTags.AddRange(detailResult.Celebrities.Select(a => a.Name)); landmarksTags.AddRange(detailResult.Landmarks.Select(a => a.Name)); } IEnumerable <string> tags = computervisionResult.Tags.Select(a => a.Name); string caption = computervisionResult.Description.Captions.First().Text; bool isAdult = computervisionResult.Adult.IsAdultContent; bool isRacy = computervisionResult.Adult.IsRacyContent; media.SetTags("tags", tags, true); media.SetTags("celebrities", celebrityTags, true); media.SetTags("landmarks", landmarksTags, true); media.SetValue("description", caption); media.SetValue("isAdult", isAdult); media.SetValue("isRacy", isRacy); } } }
void MediaService_Saving(IMediaService mediaService, SaveEventArgs <IMedia> args) { // Don't allow upload in root if (args.SavedEntities.Any() && args.SavedEntities.First().ParentId == -1 && args.SavedEntities.First().HasProperty("umbracoFile")) { LogHelper.Warn(this.GetType(), "Files are not allowed to be uploaded in the root folder"); args.Cancel = true; return; } MediaFileSystem mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>(); IContentSection contentSection = UmbracoConfig.For.UmbracoSettings().Content; IEnumerable <string> supportedTypes = contentSection.ImageFileTypes.ToList(); foreach (IMedia media in args.SavedEntities) { if (media.HasProperty("umbracoFile")) { // Make sure it's an image. string path = media.GetValue <string>("umbracoFile"); var fullExtension = Path.GetExtension(path); if (fullExtension != null) { string extension = fullExtension.Substring(1); if (supportedTypes.InvariantContains(extension)) { // Get maxwidth from parent folder var maxWidth = GetMaxWidthFromParent(DefaultMaxWidth, media); if (maxWidth < media.GetValue <int>("umbracoWidth")) { // Resize the image to maxWidth:px wide, height is driven by the aspect ratio of the image. string fullPath = mediaFileSystem.GetFullPath(path); using (ImageFactory imageFactory = new ImageFactory(true)) { ResizeLayer layer = new ResizeLayer(new Size(maxWidth, 0), resizeMode: ResizeMode.Max, anchorPosition: AnchorPosition.Center, upscale: false); imageFactory.Load(fullPath).Resize(layer).Save(fullPath); } } } } } } }
/* ==> Stap 7 -> Detect faces and match them to Umbraco Members */ private void MediaService_Saving(IMediaService sender, SaveEventArgs <IMedia> e) { FaceServiceClient faceServiceClient = new FaceServiceClient(_faceApiKey, _faceApiUrl); IMemberService memberService = ApplicationContext.Current.Services.MemberService; foreach (IMedia media in e.SavedEntities) { string relativeImagePath = ImagePathHelper.GetImageFilePath(media); string fullPath = _fs.GetFullPath(relativeImagePath); using (Stream imageFileStream = File.OpenRead(fullPath)) { var faces = AsyncHelpers.RunSync(() => faceServiceClient.DetectAsync(imageFileStream)); if (faces.Any()) { Guid[] faceIds = faces.Select(a => a.FaceId).ToArray(); IdentifyResult[] results = AsyncHelpers.RunSync(() => faceServiceClient.IdentifyAsync(_faceApiGroup, faceIds, 5)); var matchedPersons = new List <IMember>(); foreach (IdentifyResult identifyResult in results) { foreach (var candidate in identifyResult.Candidates) { IEnumerable <IMember> searchResult = memberService.GetMembersByPropertyValue("personId", candidate.PersonId.ToString()); matchedPersons.AddRange(searchResult); } } if (matchedPersons.Any()) { media.SetValue("persons", string.Join(",", matchedPersons.Select(a => a.GetUdi().ToString()))); } } } } }
private MediaResizeItem ResizeImage(IMedia image, string path, MediaFileSystem mediaFileSystem, int count, int imagesCount, out long size) { using (ImageFactory imageFactory = new ImageFactory(true)) { var fullPath = mediaFileSystem.GetFullPath(path); ResizeLayer layer = new ResizeLayer(new Size(330, 0), ResizeMode.Max) { Upscale = false }; var process = imageFactory.Load(fullPath) .Resize(layer) .Save(fullPath); var msg = "\r\n" + image.Name + " (Id: " + image.Id + ") - Succesfully resized \r\n" + "Original size: " + image.GetValue <string>("umbracoWidth") + "px x " + image.GetValue <string>("umbracoHeight") + "px \r\n" + "New size: " + process.Image.Width + "px x " + process.Image.Height + "px \r\n" + "Count: " + count + " of " + imagesCount + "\r\n"; LogHelper.Info <ResizeMediaController>(msg); ApplicationContext.Services.MediaService.Save(image); size = ApplicationContext.Services.MediaService.GetById(image.Id).GetValue <long>("umbracoBytes"); return(new MediaResizeItem { Id = image.Id, Name = image.Name, NewWidth = process.Image.Width + "px", NewHeight = process.Image.Height + "px", OldHeight = image.GetValue <string>("umbracoHeight") + "px", OldWidth = image.GetValue <string>("umbracoWidth") + "px" }); } }
private void MemberService_Saving(IMemberService sender, SaveEventArgs <IMember> e) { var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var faceServiceClient = new FaceServiceClient(_faceApiKey, _faceApiUrl); foreach (IMember member in e.SavedEntities) { var profileImage = member.GetValue <string>("profilePicture"); if (!string.IsNullOrWhiteSpace(profileImage)) { var profileImageUdi = Udi.Parse(profileImage); var profileImageMedia = umbracoHelper.TypedMedia(profileImageUdi); string fullPath = _fs.GetFullPath(profileImageMedia.Url); /* Stap 2 -> Face API: Delete the person if exists */ if (!string.IsNullOrWhiteSpace(member.GetValue <string>("personId"))) { try { var personId = Guid.Parse(member.GetValue <string>("personId")); AsyncHelpers.RunSync(() => faceServiceClient.DeletePersonAsync(_faceApiGroup, personId)); } catch { // ignored } } /* Stap 3 -> Face API: Detect face and attributes */ using (Stream imageFileStream = _fs.OpenFile(fullPath)) { Face[] detectface = AsyncHelpers.RunSync( () => faceServiceClient.DetectAsync(imageFileStream, false, false, new[] { FaceAttributeType.Age, FaceAttributeType.Gender, FaceAttributeType.Glasses, FaceAttributeType.Makeup, FaceAttributeType.Hair, })); // Getting values and setting the properties on the member string age = detectface.First().FaceAttributes.Age.ToString(); string gender = detectface.First().FaceAttributes.Gender; string glasses = detectface.First().FaceAttributes.Glasses.ToString(); bool eyeMakeup = detectface.First().FaceAttributes.Makeup.EyeMakeup; bool lipMakeup = detectface.First().FaceAttributes.Makeup.LipMakeup; member.SetValue("Age", age); member.SetValue("Gender", gender); member.SetValue("glasses", glasses); member.SetValue("eyeMakeup", eyeMakeup); member.SetValue("lipMakeup", lipMakeup); } // ==> Stap 4 -> Create a person in the persongroup CreatePersonResult person = AsyncHelpers.RunSync(() => faceServiceClient.CreatePersonAsync(_faceApiGroup, member.Name)); member.SetValue("personId", person.PersonId.ToString()); // ==> Stap 5 -> Add face to person and make persistent using (Stream imageFileStream = _fs.OpenFile(fullPath)) { AddPersistedFaceResult result = AsyncHelpers.RunSync(() => faceServiceClient.AddPersonFaceAsync(_faceApiGroup, person.PersonId, imageFileStream)); member.SetValue("faceId", result.PersistedFaceId.ToString()); } } } // ==> Stap 6 -> Train the facegroup AsyncHelpers.RunSync(() => faceServiceClient.TrainPersonGroupAsync(_faceApiGroup)); }