1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Image Sync

Image Sync

3 posts, 1 answered
  1. Tony
    Tony avatar
    20 posts
    Registered:
    12 Oct 2012
    17 Aug 2011
    Link to this post
    I have a ton (like 3k+) of images I'd like to bring over from Gallery Server Pro.  Now that 4.2 includes a filesystem provider for images and videos I was wondering if there was a way to synchronize a folder in the website with a designated album?  I mean if there isn't one I can always do it programmatically, just thought I'd check first :-)
  2. R. Atanasov
    R. Atanasov avatar
    19 posts
    Registered:
    03 Nov 2014
    22 Aug 2011
    Link to this post
    Hi Tony,

    Unfortunately there is no out-of-the-box way to do this yet, but here's a sample code you can drop in a page in your site, that might be useful to start from:

    using System.IO;
    using System.Linq;
    using Telerik.Sitefinity.Configuration;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Libraries.Model;
    using Telerik.Sitefinity.Modules.Libraries;
    using Telerik.Sitefinity.Modules.Libraries.Configuration;
     
    ...
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        var physicalPath = HttpContext.Current.Server.MapPath("~/App_Data/Images");           
        Sitefinity.Samples.Libraries.CreateAlbumAndUploadImages(physicalPath, "Imported Images", "FileSystem");
    }
     
    private static void CreateAlbumAndUploadImages(string rootFolderPath, string albumTitle = null, 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));
     
        albumTitle = albumTitle ?? rootFolder.Name;
        blobStorageProvider = blobStorageProvider ?? Config.Get<LibrariesConfig>().BlobStorage.DefaultProvider;
     
        var manager = LibrariesManager.GetManager();
     
        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));
     
        var album = manager.CreateAlbum();
        album.Title = albumTitle;
        album.UrlName = albumName;
        // Set blob storage provider for  this album
        // Currently available blob storage providers can be found in Config.Get<LibrariesConfig>().BlobStorage.Providers
        // or BlobStorageManager.ProvidersCollection
        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();
    }


    All the best,
    Radoslav Atanasov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

    Answered
  3. R. Atanasov
    R. Atanasov avatar
    19 posts
    Registered:
    03 Nov 2014
    22 Aug 2011
    Link to this post
    Hi Tony,

    I spotted a copy/paste mistake in the code. In the Button1_Click handler, the call to the CreateAlbumAndUploadImages method shouldn't specify a namespace:

    protected void Button1_Click(object sender, EventArgs e)
    {
        var physicalPath = HttpContext.Current.Server.MapPath("~/App_Data/Images");           
        Sitefinity.Samples.Libraries.CreateAlbumAndUploadImages(physicalPath, "Imported Images", "FileSystem");
    }


    Kind regards,
    Radoslav Atanasov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

3 posts, 1 answered