Как вы можете видеть на скриншоте ниже, Kentico CMS создает дополнительный файл при попытке перезаписать существующий медиафайл с помощью Kentico API 11.0.26. Моя цель - просто иметь один файл.
Вот мой код API
// Gets the media library
MediaLibraryInfo library = MediaLibraryInfoProvider.GetMediaLibraryInfo(libraryName, siteName);
if (library != null)
{
// Gets all .png files from the "NewFolder" folder of the specified media library
var mediaFiles = MediaFileInfoProvider.GetMediaFiles()
.WhereEquals("FileLibraryID", library.LibraryID)
.WhereEquals("FileName", fileName)
.WhereStartsWith("FilePath", folderName);
if (mediaFiles.Count == 0)//no existing file by that name, create new one
{
// Creates a new media library file object
MediaFileInfo mediaFile = new MediaFileInfo();
// Sets the media library file properties
mediaFile.FileSiteID = siteId;
mediaFile.FileLibraryID = library.LibraryID;
mediaFile.FileName = fileName;
mediaFile.FileTitle = headerSubtitle;
mediaFile.FileDescription = "some report";
mediaFile.FilePath = string.Format("{0}/", folderName); // Sets the path within the media library's folder structure
mediaFile.FileExtension = ".pdf";
mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(".pdf");
using (StreamReader streamReader = new StreamReader(outputFilename))
{
mediaFile.FileBinaryStream = streamReader.BaseStream;
mediaFile.FileSize = streamReader.BaseStream.Length;
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
}
}
else//file already existing by that name, update it's contents
{
// Gets the media file
MediaFileInfo updateFile = mediaFiles.FirstObject;
using (StreamReader streamReader = new StreamReader(outputFilename))
{
updateFile.FileBinaryStream = streamReader.BaseStream;
updateFile.FileSize = streamReader.BaseStream.Length;
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(updateFile);
}
}
}