To calculate the next number (although think this is probably changed in V8)
Umbraco looks on the file system loops through all folders in the media folder, and finds the largest numbered folder and then adds one to it.
private void EnsureFolderCounterIsInitialized()
{
lock (_folderCounterLock)
{
if (_folderCounterInitialized) return;
_folderCounter = 1000; // seed
var directories = GetDirectories("");
foreach (var directory in directories)
{
long folderNumber;
if (long.TryParse(directory, out folderNumber) && folderNumber > _folderCounter)
_folderCounter = folderNumber;
}
// note: not multi-domains ie LB safe as another domain could create directories
// while we read and parse them - don't fix, move to new scheme eventually
_folderCounterInitialized = true;
}
}
So in the context of where you were running your import code (console app, or locally, if you did not have the media folders on disk, then it wouldn't know about them, and would start again at 1000.... the 'next' folder number isn't stored in the database, so depends upon the folders on disk.
You can work around this by creating an empty folder in the media section with a number arbitrarily higher than the highest number in your Umbraco site, to prevent clashes.
I assume you found this code somewhere inside Umbraco? Good job.
But I actually made the same guess, so I added an empty folder with a number I was sure was higher than the highest that had been used before. But it did not work. If the numbering had restarted on 1000 I would have understood it but it restarted on 1011 which to me seemed to be very odd since I hade folders way past that in folder numbering.
The result was that the new files were added as duplet files beside the existing ones inside the already created folders.
Followup 1:
I tried your theory by adding a folder name and added a new media file from Backend UI. It works exactly as you described it! It must be something odd going on when bypassing UI environment ...
Followup 2:
I found out something interesting. I remove a media file from Backend UI and empty Recycle Bin to ensure it is completely gone, I then add a new media file. It is not generated in the next numbering according to folder on disk, but rather censecutive after the one that was just removed!
But if I restart Umbraco, then the next created file is generated in next folder number as expected. This gives me the impression that Umbraco shadows the last used folder number somewhere.
when Umbraco starts up, it checks to see if the folder counter number has been initialised previously before looking for the highest folder number, to set the seed.
MediaService CreateMedia creates wrong folders
I needed to import a lot of media files into my site and created a unit test to do that.
Everything went well except that the media files are copied into folders that are already occupied by other files.
I got the impression that each file is supposed to get its own folder. When adding files through the backend UI that is what happens.
Is there a hidden property for last used media folder id that is bypassed when calling media service from unit test?
Hi Jakob
To calculate the next number (although think this is probably changed in V8)
Umbraco looks on the file system loops through all folders in the media folder, and finds the largest numbered folder and then adds one to it.
So in the context of where you were running your import code (console app, or locally, if you did not have the media folders on disk, then it wouldn't know about them, and would start again at 1000.... the 'next' folder number isn't stored in the database, so depends upon the folders on disk.
You can work around this by creating an empty folder in the media section with a number arbitrarily higher than the highest number in your Umbraco site, to prevent clashes.
regards
marc
Thanks for your answer!
I assume you found this code somewhere inside Umbraco? Good job.
But I actually made the same guess, so I added an empty folder with a number I was sure was higher than the highest that had been used before. But it did not work. If the numbering had restarted on 1000 I would have understood it but it restarted on 1011 which to me seemed to be very odd since I hade folders way past that in folder numbering.
The result was that the new files were added as duplet files beside the existing ones inside the already created folders.
Followup 1:
I tried your theory by adding a folder name and added a new media file from Backend UI. It works exactly as you described it! It must be something odd going on when bypassing UI environment ...
Followup 2:
I found out something interesting. I remove a media file from Backend UI and empty Recycle Bin to ensure it is completely gone, I then add a new media file. It is not generated in the next numbering according to folder on disk, but rather censecutive after the one that was just removed! But if I restart Umbraco, then the next created file is generated in next folder number as expected. This gives me the impression that Umbraco shadows the last used folder number somewhere.
Hi Jakob
yes, sorry should have linked to the code:
https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Core/IO/MediaFileSystem.cs
when Umbraco starts up, it checks to see if the folder counter number has been initialised previously before looking for the highest folder number, to set the seed.
regards
Marc
Thanks, it makes sense.
But it still doesn't solve my problem when calling Umbraco from unit test.
I will make a loop and manually move my files and add them to folders with expected numbers.
Your answer helped me to trust I don't destroy anything for the ongoing Umbraco handling of files!
Hi Jakob
Have you had a look at Chauffeur?
https://our.umbraco.com/packages/collaboration/chauffeur/
might be a better tool or at least a better context, from which to write an import deliverable for your media import, than the unit test
regards
marc
I have never heard of Chauffeur.
My media files are from a non Umbraco storage with meta data in a database.
Can Chauffeur really be used in this context?
is working on a reply...