Best way to check whether a media file already exists?
Hey Guys,
What would you say is the best way to check whether a media file already exists within a folder in the media section?
ie given the node ID of a media folder, and a file name, I want to know if a media item exists with an umbracoFile property whith the same name as the given file name.
My gut feeling is to just query the database direct to save itterating over all the media items, but anybody else got any other suggestions?
The following code is not tested, but maybe something like this could do the trick? If you have the node id of the media folder and the filename of the file:
Media rootFolder = new Media(1020);
var media = from m in rootFolder.Children
where m.getProperty("umbracoFile").Value.ToString().Contains("nameOfFile")
select m;
if (media != null)
{
// do work
}
else
{
// do work
}
That is pretty much how I ended up doing it. I was just going to hit the DB to save itterating the child media items, but had to itterate them anyway as I'd need the property ID for the query (and some other checks I'm doing).
Best way to check whether a media file already exists?
Hey Guys,
What would you say is the best way to check whether a media file already exists within a folder in the media section?
ie given the node ID of a media folder, and a file name, I want to know if a media item exists with an umbracoFile property whith the same name as the given file name.
My gut feeling is to just query the database direct to save itterating over all the media items, but anybody else got any other suggestions?
Matt
Hi Matt,
The following code is not tested, but maybe something like this could do the trick? If you have the node id of the media folder and the filename of the file:
Media rootFolder = new Media(1020); var media = from m in rootFolder.Children where m.getProperty("umbracoFile").Value.ToString().Contains("nameOfFile") select m; if (media != null) { // do work } else { // do work }
.. just a shot in the dark really ;-)
All the best,
Bo
Hey Bo,
That is pretty much how I ended up doing it. I was just going to hit the DB to save itterating the child media items, but had to itterate them anyway as I'd need the property ID for the query (and some other checks I'm doing).
Matt
is working on a reply...