protected virtual void Shrink(object[] parameters) { Item item = parameters[0] as Item; if (item == null) { throw new Exception("Parameter 0 was not a item"); } else { mi = new MediaItem(item); crunchOptions = new CrunchOptions(); objFillSetting = new FillSetting(); objTennantSetting = objFillSetting.getSetting(mi.MediaPath, mi.Database.Name, mi.InnerItem.Language.ToString()); if (mi.Size > objTennantSetting.MinimumKBSize && mi.Size < objTennantSetting.MaxImageSize) { crunchOptions.APIKey = objTennantSetting.ApiKey; crunchOptions.APISecret = objTennantSetting.ApiSecret; crunchOptions.fullname = mi.Name + "." + mi.Extension; crunchOptions.IsEnabled = objTennantSetting.IsEnabled; crunchOptions.wait = true; crunchOptions.lossy = objTennantSetting.Lossy; crunchOptions.dev = objTennantSetting.IsDev; crunchOptions.enhance = objTennantSetting.Enhance; CrunchImage.ProcessMediaItem(mi, crunchOptions); } else { Log.Info(string.Format("Image Size is {0} {1}", mi.Size, ",KB which is not fit in minimum & maximum size defined in setting"), this); } } }
protected override void Shrink(object[] parameters) { Item item = parameters[0] as Item; Job job = Context.Job; if (crunchedStats == null) { crunchedStats = new CrunchedStats(); crunchedStats.JobID = job.Name; crunchedStats.typeofExecution = CrunchedStats.TypeofExecution.CrunchbyTreeRibbonCommand; } if (item == null) { return; } else if (InSitecoreSitecore.Common.Functions.IsMediaItem(item)) { mi = new MediaItem(item); //This make sures that no need to get setting item recursively for each child. if (objFillSetting == null) { crunchOptions = new CrunchOptions(); objFillSetting = new FillSetting(); objTennantSetting = objFillSetting.getSetting(mi.MediaPath, mi.Database.Name, mi.InnerItem.Language.ToString()); crunchOptions.APIKey = objTennantSetting.ApiKey; crunchOptions.APISecret = objTennantSetting.ApiSecret; crunchOptions.wait = true; crunchOptions.IsEnabled = objTennantSetting.IsEnabled; crunchOptions.enhance = objTennantSetting.Enhance; crunchOptions.lossy = objTennantSetting.Lossy; crunchOptions.dev = objTennantSetting.IsDev; crunchOptions.enhance = objTennantSetting.Enhance; crunchedStats.Database = mi.Database.Name; crunchedStats.InitiatedBy = Sitecore.Context.GetUserName(); Log.Info(string.Format("Job Started {0} by {1} on Database {2}", crunchedStats.JobID, crunchedStats.InitiatedBy, crunchedStats.Database), this); } if (objFillSetting.isInitialised) { crunchOptions.fullname = mi.Name + "." + mi.Extension; if (mi.Size > objTennantSetting.MinimumKBSize && mi.Size < objTennantSetting.MaxImageSize) { try { crunchedStats.BeforeCrunchSize = mi.Size; if (sw == null) { sw = new Stopwatch(); } sw.Start(); crunchedStats.AfterCrunchSize = CrunchImage.ProcessMediaItem(mi, crunchOptions); sw.Stop(); crunchedStats.TimeTaken = sw.ElapsedMilliseconds; if (job != null) { job.Status.LogInfo(string.Format("Done: {0}", item.Paths.FullPath)); Log.Info(string.Format("Done: {0}", item.Paths.FullPath), this); job.Status.LogInfo(string.Format("Stats - Before Crunch Size: {0}", crunchedStats.BeforeCrunchSize)); Log.Info(string.Format("Stats - Before Crunch Size: {0}", crunchedStats.BeforeCrunchSize), this); job.Status.LogInfo(string.Format("Stats - After Crunch Size: {0}", crunchedStats.AfterCrunchSize)); Log.Info(string.Format("Stats - After Crunch Size: {0}", crunchedStats.AfterCrunchSize), this); } } catch (Exception catche) { if (job != null) { job.Status.LogInfo(string.Format("{0}: {1}", catche.Message, item.Paths.FullPath)); } } } else { Log.Info(string.Format("Image Size is {0} {1}", mi.Size, ",KB which is not fit in minimum & maximum size defined in setting"), this); } } } foreach (var child in item.Children) { this.Shrink(new[] { child }); } }
private JsonResult DoUpload(string database, string destinationUrl) { if (string.IsNullOrEmpty(destinationUrl)) { destinationUrl = "/sitecore/media library"; } List <UploadedFileItem> uploadedFileItemList = new List <UploadedFileItem>(); SitecoreViewModelResult result = new SitecoreViewModelResult(); if (!ImageCrunchController.ValidateDestination(database, destinationUrl, result)) { return((JsonResult)result); } objFillSetting = new FillSetting(); crunchOptions = new CrunchOptions(); foreach (string file1 in (NameObjectCollectionBase)this.Request.Files) { HttpPostedFileBase file2 = this.Request.Files[file1]; if (file2 != null) { string withoutExtension = Path.GetFileNameWithoutExtension(file2.FileName); if (!string.IsNullOrEmpty(this.Request.Form["name"])) { withoutExtension = this.Request.Form["name"]; } string str = ItemUtil.ProposeValidItemName(withoutExtension, "default"); string empty = string.Empty; if (!string.IsNullOrEmpty(this.Request.Form["alternate"])) { empty = this.Request.Form["alternate"]; } Database database1 = Context.ContentDatabase; if (!string.IsNullOrEmpty(database)) { database1 = Factory.GetDatabase(database); } if (database1 == null) { database1 = Context.ContentDatabase; } MediaCreatorOptions options1 = new MediaCreatorOptions() { AlternateText = empty, Database = database1, FileBased = Settings.Media.UploadAsFiles, IncludeExtensionInItemName = Settings.Media.IncludeExtensionsInItemNames, OverwriteExisting = false, Language = LanguageManager.DefaultLanguage, Versioned = Settings.Media.UploadAsVersionableByDefault, Destination = this.ParseDestinationUrl(destinationUrl) + str }; if (!ImageCrunchController.ValidateFile(file2, result)) { return((JsonResult)result); } Item fromStream = MediaManager.Creator.CreateFromStream(file2.InputStream, "/upload/" + file2.FileName, options1); if (!string.IsNullOrEmpty(this.Request.Form["description"])) { fromStream.Editing.BeginEdit(); fromStream["Description"] = this.Request.Form["description"]; fromStream.Editing.EndEdit(); } MediaItem mediaItem = new MediaItem(fromStream); ///Code to Shrunk the Media Item begin objTennantSetting = objFillSetting.getSetting(mediaItem.MediaPath, mediaItem.Database.Name, mediaItem.InnerItem.Language.ToString()); crunchOptions.APIKey = objTennantSetting.ApiKey; crunchOptions.APISecret = objTennantSetting.ApiSecret; crunchOptions.IsEnabled = objTennantSetting.IsEnabled; crunchOptions.wait = true; crunchOptions.lossy = objTennantSetting.Lossy; crunchOptions.dev = objTennantSetting.IsDev; crunchOptions.enhance = objTennantSetting.Enhance; try { //checking maximum & minimum size condition defined for the tenant if (mediaItem != null && mediaItem.Size > objTennantSetting.MinimumKBSize && mediaItem.Size < objTennantSetting.MaxImageSize) { crunchOptions.fullname = mediaItem.Name + "." + mediaItem.Extension; //As of now API is based out of post approach CrunchImage.ProcessMediaItem(mediaItem, crunchOptions); } else { Log.Info(string.Format("Image Size is {0} {1}", mediaItem.Size, ",KB which is not fit in minimum & maximum size defined in setting"), this); } } catch (System.Exception exception) { Log.Error(string.Format("Could not shrink item {0}", mediaItem.InnerItem.Paths.FullPath), exception, this); } ///Code to shrunk media item end MediaUrlOptions options2 = new MediaUrlOptions(130, 130, false) { Thumbnail = true, BackgroundColor = Color.Transparent, Database = mediaItem.Database }; string mediaUrl = MediaManager.GetMediaUrl(mediaItem, options2); uploadedFileItemList.Add(new UploadedFileItem(fromStream.Name, fromStream.ID.ToString(), fromStream.ID.ToShortID().ToString(), mediaUrl)); } } ((dynamic)result.Result).uploadedFileItems = uploadedFileItemList; return((JsonResult)result); }