Importing media files
To import the media files, you can use the Litium backoffice or automate the process.
To automate media import use the Integration Kit add-on and the Product Media Mapper add-on.
Importing media files
Following is a sample code from integration kit addon, which shows how a media file is imported into Litium.
The field template service is used to get the field template for the media files. This is achieved by searching for field templates associated with the file extensions of the files to be imported.
A search is performed in the import folder through the Exists method, to see whether the file already exists in the target folder. The Exists method creates a data query that is executed against the database to check if there is a file with the same name in the current folder.
private bool Exists(string fileName, Guid folderId, out File existingFile)
{
using (var query = _dataService.CreateQuery<File>())
{
var items = query.Filter(x => x
.Bool(b => b
.Must(m => m.Field(SystemFieldDefinitionConstants.NameInvariantCulture, "eq", fileName)
.FolderSystemId(folderId))));
existingFile = items.FirstOrDefault();
return existingFile != null;
}
}
A blob container is created by the blob service and the file to import is streamed into the blob container.
protected override void ProcessItem(Item item)
{
var fieldTemplate = _fieldTemplateService.FindFileTemplate(item.FileInfo.Extension);
if(fieldTemplate == null)
{
throw new Exception($"{Messages.MediaFieldTemplateNotFoundForExtension} {item.FileInfo.Extension}");
}
long fileSize = 0;
var blobContainer = _blobService.Create(File.BlobAuthority);
using (var fileStream = item.FileInfo.OpenRead())
{
using (var stream = blobContainer.GetDefault().OpenWrite())
{
fileStream.CopyTo(stream);
fileSize = fileStream.Length;
}
}
if(Exists(item.FileInfo.Name, _importFolder.SystemId, out File existingFile))
{
existingFile = existingFile.MakeWritableClone();
existingFile.BlobUri = blobContainer.Uri;
existingFile.FileSize = fileSize;
_fileService.Update(existingFile);
}
else
{
var file = new File(fieldTemplate.SystemId, _importFolder.SystemId, blobContainer.Uri, item.FileInfo.Name)
{
FileSize = fileSize,
Id = item.FileInfo.Name
};
_fileService.Create(file);
}
}