I am attempting to create a new media item (an image) using the MediaHelper and organise it in a specific folder (creating this folder if it doesn't already exist).
I have this working to a degree using MediaHelper.GetById([GUID]) to find the existing folder, but I do not like this approach as the GUID for my /media/Avatars folder will change across environments (Dev, UAT, Prod etc) and either hard coding this or having to configure it in web.config seems silly.
What would we much more environment agnostic, would be to use the MediaHelper.GetMediaByPath("/media/Avatars") method (as my folder name and thus path, will be consistent across all environments)
However, it doesn't seem possible to use GetMediaByPath() to lookup folders, only files? It seems to look in the [dbo].[umbracoMediaVersion] tables [Path] column, but this table only holds details of files, not folders
Is this understanding correct and, if so, are there any alternative ways of looking up an existing folder under /media in this dynamic way (avoiding hard coding IDs)?
Some example code:
IMedia folder;
// This works, but means hard coding GUID's which will differ across environments
folder = Services.MediaService.GetById(new Guid("d3d63850-a787-424f-bd26-d0975e43f8ee"));
// This is what I want to achieve, but doesn't seem to work for folders, despite both Folders and Files being IMedia instances?
//folder = Services.MediaService.GetMediaByPath("/media/Avatars");
if (folder == null)
{
// Create 'Avatars' folder under root if doens't exist
folder = Services.MediaService.CreateMediaWithIdentity("Avatars", Constants.System.Root, Constants.Conventions.MediaTypes.Folder);
}
// Initialize a new image withing the Avatars folder and assign ownership to the current member
IMedia avatar = Services.MediaService.CreateMedia(firstName + " " + lastName + " Avatar", folder, Constants.Conventions.MediaTypes.Image, member.Id);
MediaService.GetMediaByPath() to Get Folders
Umbraco Version: 8.11.1
I am attempting to create a new media item (an image) using the MediaHelper and organise it in a specific folder (creating this folder if it doesn't already exist).
I have this working to a degree using MediaHelper.GetById([GUID]) to find the existing folder, but I do not like this approach as the GUID for my /media/Avatars folder will change across environments (Dev, UAT, Prod etc) and either hard coding this or having to configure it in web.config seems silly.
What would we much more environment agnostic, would be to use the MediaHelper.GetMediaByPath("/media/Avatars") method (as my folder name and thus path, will be consistent across all environments)
However, it doesn't seem possible to use GetMediaByPath() to lookup folders, only files? It seems to look in the [dbo].[umbracoMediaVersion] tables [Path] column, but this table only holds details of files, not folders
Is this understanding correct and, if so, are there any alternative ways of looking up an existing folder under /media in this dynamic way (avoiding hard coding IDs)?
Some example code:
Thanks in advance,
HARVS1789UK
is working on a reply...