private Property CopyUmbracoFileProperty(Property umbFileProp) { if (umbFileProp.Value == null) { return(umbFileProp); } string imageSource; dynamic umbFile = new { }; bool isPropertyDynamic = false; try { umbFile = JsonConvert.DeserializeObject <dynamic>(umbFileProp.Value.ToString()); imageSource = umbFile.src; isPropertyDynamic = true; } catch (Exception e) { imageSource = umbFileProp.Value.ToString(); } if (String.IsNullOrEmpty(imageSource)) { return(umbFileProp); } string currentFolderName = GetCurrentFolderName(imageSource); string src = imageSource.Replace("/media", ""); if (!VirtualPathUtility.IsAbsolute(src)) { Uri uriImageSource = new Uri(src); src = uriImageSource.PathAndQuery; } try { //Get the media folder number. long newFolderName = GetMediaFolderCount(); //convert the old source into the new image source. string newImageSrc = "/media" + src.Replace(currentFolderName, newFolderName.ToString()); //Get the index of the last /, which should be after the media folder number int lastIndexOfSlash = newImageSrc.LastIndexOf("/"); //Remove the image name from the path. string newImageFolder = newImageSrc.Substring(0, lastIndexOfSlash); //Map the new media folder path to the server. string mappedServerPath = HttpContext.Current.Server.MapPath(newImageFolder); string mappedNewImageSrc = HttpContext.Current.Server.MapPath(newImageSrc); //Create the directory for the new media folder DirectoryInfo di = Directory.CreateDirectory(mappedServerPath); //Get umbracos MediaFileSystem MediaFileSystem fs = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>(); //Copy the old image to the new directory. try { //Most likely reason to fail would be that the src file doesn't exist on the filesystem fs.CopyFile(src, mappedNewImageSrc); } catch (Exception e) { //If this failed just take the imageSource and use it as the property value, no use making everything crap out. return(umbFileProp); //Return this because no changes/files were made/created. } //Now set the umbfilesource to the new source. string fullNewImageSource = imageSource.Replace(currentFolderName, newFolderName.ToString()); if (!VirtualPathUtility.IsAbsolute(imageSource)) { //So we are dealing with a full url "https://something.com/bla/blah/blah.png Uri fullUri = new Uri(imageSource); fullNewImageSource = fullUri.GetLeftPart(UriPartial.Authority) + fullNewImageSource; } if (isPropertyDynamic) { umbFile.src = fullNewImageSource; umbFileProp.Value = JsonConvert.SerializeObject(umbFile); } else { //its just a string so we can just set the value umbFileProp.Value = fullNewImageSource; } return(umbFileProp); } catch (Exception e) { return(null); } }