How to sync existing files to file system library?
I have thousands of physical files and folders within my site under ~/Docs. I am trying to use the new file system library in 4.2. So I adjusted the file system provider path in settings and created a new library called "Docs" using the file system provider in the advanced options. I was hoping to see my existing files and folders in albums and files within the Sitefinity admin in real time, but it is empty. So how do I make them show up without having to upload thousands of files within the admin?
Hello Basem,
There is no sync option out of the box that comes with 4.2 and we will consider its implementation for 4.3. Currently you can do this programmatically. Below is a sample code
public
static
void
CreateAlbumAndUploadImages(
string
rootFolderPath,
string
blobStorageProvider =
null
)
DirectoryInfo rootFolder =
new
DirectoryInfo(rootFolderPath);
if
(!rootFolder.Exists)
throw
new
ApplicationException(
string
.Format(
"A folder with path '0' does not exist"
, rootFolder.FullName));
var manager = LibrariesManager.GetManager();
var albumTitle = rootFolder.Name;
var albumName = albumTitle.ToLower().Replace(
' '
,
'_'
);
if
(manager.GetAlbums().Where(i => i.UrlName == albumName).Any())
throw
new
ApplicationException(
string
.Format(
"An album with UrlName '0' already exists"
, albumName));
if
(blobStorageProvider ==
null
)
blobStorageProvider = Config.Get<LibrariesConfig>().BlobStorage.DefaultProvider;
var album = manager.CreateAlbum();
album.Title = albumTitle;
album.UrlName = albumName;
album.BlobStorageProvider = blobStorageProvider;
manager.RecompileItemUrls<Album>(album);
foreach
(var file
in
rootFolder.GetFiles())
var image = manager.CreateImage();
var extension = file.Extension;
var title = file.Name;
if
(extension.Length > 0)
title = title.Substring(0, title.Length - extension.Length);
image.Parent = album;
image.Title = title;
image.UrlName = title.ToLower().Replace(
' '
,
'-'
);
manager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(image);
using
(var fileStream = file.OpenRead())
manager.Upload(image, fileStream, file.Extension);
manager.Publish(image);
manager.SaveChanges();
rootFolderPath
- path where you store your imagesblobStorageProvider
- FileSystem