private void ProcessCallback(FormDataCollection kCallbackFormData) { string krakenCallbackId = kCallbackFormData.Get("id"); string fileName = kCallbackFormData.Get("file_name"); string krakedUrl = kCallbackFormData.Get("kraked_url"); bool success = Boolean.Parse(kCallbackFormData.Get("success")); string error = kCallbackFormData.Get("error"); Guid albumId = Global.KrakenCallbackIds[krakenCallbackId]; Global.KrakenCallbackIds.Remove(krakenCallbackId); if (success == false || error != null) { return; } LibrariesManager _librariesManager = LibrariesManager.GetManager(); _librariesManager.Provider.SuppressSecurityChecks = true; Album album = _librariesManager.GetAlbum(albumId); var albumProvider = (LibrariesDataProvider)album.Provider; Image image = _librariesManager.GetImage(Guid.Parse(Path.GetFileNameWithoutExtension(krakedUrl))); if (!Regex.IsMatch(krakedUrl, @"https?://(?:api-worker-\d|dl).kraken.io/" + krakenCallbackId + "/" + image.Id.ToString() + @"\.(?:jpg|jpeg|png|gif|svg)")) { return; } using (var webClient = new WebClient()) using (var stream = webClient.OpenRead(krakedUrl)) { //Check out the master to get a temp version. Image temp = _librariesManager.Lifecycle.CheckOut(image) as Image; //Make the modifications to the temp version. _librariesManager.Upload(temp, stream, Path.GetExtension(fileName)); //Checkin the temp and get the updated master version. //After the check in the temp version is deleted. _librariesManager.Lifecycle.CheckIn(temp); _librariesManager.SaveChanges(); // Check to see if this image is already published. // If it is, we need to publish the "Master" to update "Live" if (image.GetWorkflowState() == "Published") { var bag = new Dictionary <string, string>(); bag.Add("ContentType", typeof(Image).FullName); WorkflowManager.MessageWorkflow(image.Id, typeof(Image), albumProvider.Name, "Publish", false, bag); } } _librariesManager.Provider.SuppressSecurityChecks = false; }
public static IQueryable <Image> GetImagesByAlbumNativeAPI(Guid albumId) { LibrariesManager librariesManager = LibrariesManager.GetManager(); Album album = librariesManager.GetAlbum(albumId); IQueryable <Image> images = album.Images().Where(i => i.Status == ContentLifecycleStatus.Live); return(images); }
public override void ExecuteTask() { LibrariesManager _librariesManager = LibrariesManager.GetManager(); Album album = _librariesManager.GetAlbum(this.AlbumId); var albumProvider = (LibrariesDataProvider)album.Provider; var images = album.Images().Where(i => i.Status == ContentLifecycleStatus.Master && !i.GetValue <bool>("Optimized")); _itemsCount = images.Count(); AlbumOptimizationConfig _albumOptimaztionConfig = Config.Get <AlbumOptimizationConfig>(); int _imageQuality = 100; if (_albumOptimaztionConfig.ImageQuality != null) { _imageQuality = _albumOptimaztionConfig.ImageQuality; } foreach (Telerik.Sitefinity.Libraries.Model.Image image in images) { // Pull the Stream of the image from the provider. // This saves us from having to care about BlobStorage Stream imageData = albumProvider.Download(image); using (MemoryStream compressed = new MemoryStream()) { MagickReadSettings magickSettings = new MagickReadSettings(); switch (image.Extension) { case ".png": magickSettings.Format = MagickFormat.Png; break; case ".jpg": magickSettings.Format = MagickFormat.Jpg; break; case ".jpeg": magickSettings.Format = MagickFormat.Jpeg; break; case ".bmp": magickSettings.Format = MagickFormat.Bmp; break; default: magickSettings.Format = MagickFormat.Jpg; break; } using (MagickImage img = new MagickImage(imageData, magickSettings)) { img.Quality = _imageQuality; img.Write(compressed); if (compressed == null) { UpdateProgress(); continue; } //Check out the master to get a temp version. Image temp = _librariesManager.Lifecycle.CheckOut(image) as Telerik.Sitefinity.Libraries.Model.Image; //Make the modifications to the temp version. _librariesManager.Upload(temp, compressed, image.Extension); temp.SetValue("Optimized", true); //Checkin the temp and get the updated master version. //After the check in the temp version is deleted. _librariesManager.Lifecycle.CheckIn(temp); _librariesManager.SaveChanges(); // Check to see if this image is already published. // If it is, we need to publish the "Master" to update "Live" if (image.GetWorkflowState() == "Published") { var bag = new Dictionary <string, string>(); bag.Add("ContentType", typeof(Telerik.Sitefinity.Libraries.Model.Image).FullName); WorkflowManager.MessageWorkflow(image.Id, typeof(Telerik.Sitefinity.Libraries.Model.Image), albumProvider.Name, "Publish", false, bag); } } } UpdateProgress(); } }
public override void ExecuteTask() { KrakenConfig _kConfig = Config.Get <KrakenConfig>(); LibrariesManager _librariesManager = LibrariesManager.GetManager(); string key = _kConfig.APIKey; string secret = _kConfig.APISecret; bool useCallbacks = _kConfig.UseCallbacks; bool useLossy = _kConfig.LossyOptimization; Kraken k = new Kraken(key, secret); Album album = _librariesManager.GetAlbum(this.AlbumId); var albumProvider = (LibrariesDataProvider)album.Provider; var images = album.Images().Where(i => i.Status == ContentLifecycleStatus.Master); _itemsCount = images.Count(); foreach (Image image in images) { KrakenRequest kr = new KrakenRequest(); kr.Lossy = useLossy; if (_kConfig.UseCallbacks) { kr.CallbackUrl = _kConfig.CallbackURL; } else { kr.Wait = true; } // Pull the Stream of the image from the provider. // This saves us from having to care about BlobStorage Stream imageData = albumProvider.Download(image); // Can't trust the length of Stream. Converting to a MemoryStream using (MemoryStream ms = new MemoryStream()) { imageData.CopyTo(ms); kr.File = ms.ToArray(); } var response = k.Upload(kr, image.Id.ToString(), image.Extension); if (_kConfig.UseCallbacks) { Global.KrakenCallbackIds.Add(response.Id, album.Id); } else { if (response.Success == false || response.Error != null) { UpdateProgress(); continue; } using (var webClient = new WebClient()) using (var stream = webClient.OpenRead(response.KrakedUrl)) { //Check out the master to get a temp version. Image temp = _librariesManager.Lifecycle.CheckOut(image) as Image; //Make the modifications to the temp version. _librariesManager.Upload(temp, stream, Path.GetExtension(response.KrakedUrl)); //Checkin the temp and get the updated master version. //After the check in the temp version is deleted. _librariesManager.Lifecycle.CheckIn(temp); _librariesManager.SaveChanges(); // Check to see if this image is already published. // If it is, we need to publish the "Master" to update "Live" if (image.GetWorkflowState() == "Published") { var bag = new Dictionary <string, string>(); bag.Add("ContentType", typeof(Image).FullName); WorkflowManager.MessageWorkflow(image.Id, typeof(Image), albumProvider.Name, "Publish", false, bag); } } } UpdateProgress(); } }
/// <summary> /// Returns an Album object from a Guid /// ** Sitefinitysteve.com Extension ** /// </summary> public static Album GetAlbum(this Guid albumID) { LibrariesManager manager = LibrariesManager.GetManager(); return((albumID != Guid.Empty) ? manager.GetAlbum(albumID) : null); }
private Guid CreateImageNativeAPI(string imageTitle, Stream imageStream, string imageExtension, LibrariesManager librariesManager) { Album album = librariesManager.GetAlbums().Where(al => al.Title == "Facebook Users").FirstOrDefault(); if (album == null) { Guid albumId = Guid.NewGuid(); album = librariesManager.GetAlbums().Where(a => a.Id == albumId).FirstOrDefault(); if (album == null) { //Create the album. album = librariesManager.CreateAlbum(albumId); //Set the properties of the album. album.Title = "Facebook Users"; album.DateCreated = DateTime.UtcNow; album.LastModified = DateTime.UtcNow; album.UrlName = Regex.Replace("Facebook Users".ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"); //Save the changes. librariesManager.SaveChanges(); } album = librariesManager.GetAlbum(albumId); } //The album post is created as master. The masterImageId is assigned to the master version. Image image = librariesManager.CreateImage(); librariesManager.SaveChanges(); librariesManager.Provider.SuppressSecurityChecks = true; //Set the parent album. //Set the properties of the profile image image.Title = imageTitle; image.DateCreated = DateTime.UtcNow; image.PublicationDate = DateTime.UtcNow; image.LastModified = DateTime.UtcNow; image.UrlName = Regex.Replace(imageTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"); librariesManager.Provider.SuppressSecurityChecks = true; image.Parent = album; //Upload the image file. librariesManager.Upload(image, imageStream, imageExtension); //Save the changes. librariesManager.SaveChanges(); //Publish the Albums item. The live version acquires new ID. var bag = new Dictionary<string, string>(); bag.Add("ContentType", typeof(Image).FullName); WorkflowManager.MessageWorkflow(image.Id, typeof(Image), null, "Publish", false, bag); return image.Id; }
private Guid CreateImageNativeAPI(string imageTitle, Stream imageStream, string imageExtension, LibrariesManager librariesManager) { Album album = librariesManager.GetAlbums().Where(al => al.Title == "Facebook Users").FirstOrDefault(); if (album == null) { Guid albumId = Guid.NewGuid(); album = librariesManager.GetAlbums().Where(a => a.Id == albumId).FirstOrDefault(); if (album == null) { //Create the album. album = librariesManager.CreateAlbum(albumId); //Set the properties of the album. album.Title = "Facebook Users"; album.DateCreated = DateTime.UtcNow; album.LastModified = DateTime.UtcNow; album.UrlName = Regex.Replace("Facebook Users".ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"); //Save the changes. librariesManager.SaveChanges(); } album = librariesManager.GetAlbum(albumId); } //The album post is created as master. The masterImageId is assigned to the master version. Image image = librariesManager.CreateImage(); librariesManager.SaveChanges(); librariesManager.Provider.SuppressSecurityChecks = true; //Set the parent album. //Set the properties of the profile image image.Title = imageTitle; image.DateCreated = DateTime.UtcNow; image.PublicationDate = DateTime.UtcNow; image.LastModified = DateTime.UtcNow; image.UrlName = Regex.Replace(imageTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"); librariesManager.Provider.SuppressSecurityChecks = true; image.Parent = album; //Upload the image file. librariesManager.Upload(image, imageStream, imageExtension); //Save the changes. librariesManager.SaveChanges(); //Publish the Albums item. The live version acquires new ID. var bag = new Dictionary <string, string>(); bag.Add("ContentType", typeof(Image).FullName); WorkflowManager.MessageWorkflow(image.Id, typeof(Image), null, "Publish", false, bag); return(image.Id); }