Media folder for members

I created a “Members” folder in the Media Section, where all the member images will be saved.

The code starts with checking that a Members folder exists. 

Then it checks if there is a folder with the members name. 

If not, it creates it. 

Then it saves the file to the members folder. 

That way it is easier to maintain the folders, and all images are not just thrown together, thereby making it harder to maintain, and clean up.

private GuidUdi SaveImageToMember(Models.ModelsBuilder.Member member, HttpPostedFileBase file)
        {
            if (!file.IsImage())
            {
                return null;
            }
            try
            {
                // save the media, 
                // save the reference to the member
                // maybe has to move the media to the proper folder.
                Guid memberRootFolderKey;
                var membersRoot = mediaService.GetRootMedia().FirstOrDefault(x => x.Name == "Members");
                if (membersRoot != null)
                {

                    var membersRootMedia = umbracoHelper.Media(membersRoot.Id);
                    var memberRootMediaFolder = membersRootMedia.Children.FirstOrDefault(x => x.Name == member.Name);
                    if (memberRootMediaFolder == null)
                    {
                        logger.Info<MemberUpdateHelper>($"No media folder found on member {member.Name}, creating a new one.");

                        // no member folder, so we create it, in their name.
                        var memberRootFolder = mediaService.CreateMedia(member.Name, membersRootMedia.Id, "Folder");
                        mediaService.Save(memberRootFolder, -1);
                        memberRootFolderKey = memberRootFolder.Key;


                    }
                    else
                    {
                        logger.Info<MemberUpdateHelper>($"Member {member.Name} has a folder, proceeding.");

                        // there is a member folder in their name.
                        memberRootFolderKey = memberRootMediaFolder.Key;
                    }

                    // save in memberrootfolderkey location.
                    // Stream the file into a new umbraco media item
                    logger.Info<MemberUpdateHelper>($"Creating new file.");

                    var imageMedia = mediaService.CreateMedia(file.FileName, memberRootFolderKey, "Image");
                    imageMedia.SetValue(contentTypeBaseServiceProvider, "umbracoFile", file.FileName, file.InputStream);
                    mediaService.Save(imageMedia, -1);
                    var guid = new GuidUdi("media", imageMedia.Key);
                    logger.Info<MemberUpdateHelper>($"New media created for member {member.Name} with guidudi {guid}");
                    return guid;

                }
                logger.Error<MemberUpdateHelper>($"Was unable to find a members folder");
                return null;
            }
            catch (Exception ex)
            {
                logger.Error<MemberUpdateHelper>($"Was unable to save Image on Member {member.Name} on SaveImageToMember Error {ex}");
                return null;
            }
        }

Leave a Reply

Your email address will not be published. Required fields are marked *