private static string SizePhoto(Guid userID, byte[] data, long maxFileSize, Size size, bool now)
        {
            if (data == null || data.Length <= 0)
            {
                throw new UnknownImageFormatException();
            }
            if (maxFileSize != -1 && data.Length > maxFileSize)
            {
                throw new ImageWeightLimitException();
            }

            var resizeTask = new ResizeWorkerItem(userID, data, maxFileSize, size, GetDataStore(), UserPhotoThumbnailSettings.LoadForUser(userID));

            if (now)
            {
                //Resize synchronously
                ResizeImage(resizeTask);
                return(GetSizedPhotoAbsoluteWebPath(userID, size));
            }
            else
            {
                if (!ResizeQueue.GetItems().Contains(resizeTask))
                {
                    //Add
                    ResizeQueue.Add(resizeTask);
                    if (!ResizeQueue.IsStarted)
                    {
                        ResizeQueue.Start(ResizeImage);
                    }
                }
                return(GetDefaultPhotoAbsoluteWebPath(size));
                //NOTE: return default photo here. Since task will update cache
            }
        }
 public bool Equals(ResizeWorkerItem other)
 {
     if (other is null)
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(other.UserId.Equals(UserId) && other.MaxFileSize == MaxFileSize && other.Size.Equals(Size));
 }
        private static void ResizeImage(ResizeWorkerItem item)
        {
            try
            {
                CoreContext.TenantManager.SetCurrentTenant(item.TenantId);

                var data = item.Data;
                using (var stream = new MemoryStream(data))
                    using (var img = Image.FromStream(stream))
                    {
                        var imgFormat = img.RawFormat;
                        if (item.Size != img.Size)
                        {
                            using (var img2 = item.Settings.IsDefault ?
                                              CommonPhotoManager.DoThumbnail(img, item.Size, true, true, true) :
                                              UserPhotoThumbnailManager.GetBitmap(img, item.Size, item.Settings))
                            {
                                data = CommonPhotoManager.SaveToBytes(img2);
                            }
                        }
                        else
                        {
                            data = CommonPhotoManager.SaveToBytes(img);
                        }

                        var widening = CommonPhotoManager.GetImgFormatName(imgFormat);
                        var fileName = string.Format("{0}_size_{1}-{2}.{3}", item.UserId, item.Size.Width, item.Size.Height, widening);

                        using (var stream2 = new MemoryStream(data))
                        {
                            item.DataStore.Save(fileName, stream2).ToString();

                            AddToCache(item.UserId, item.Size, fileName);
                        }
                    }
            }
            catch (ArgumentException error)
            {
                throw new UnknownImageFormatException(error);
            }
        }
        private static void ResizeImage(ResizeWorkerItem item)
        {
            try
            {
                var data = item.Data;
                using (var stream = new MemoryStream(data))
                    using (var img = Image.FromStream(stream))
                    {
                        var imgFormat = img.RawFormat;
                        if (item.Size != img.Size)
                        {
                            using (var img2 = CommonPhotoManager.DoThumbnail(img, item.Size, true, true, true))
                            {
                                data = CommonPhotoManager.SaveToBytes(img2);
                            }
                        }
                        else
                        {
                            data = CommonPhotoManager.SaveToBytes(img);
                        }

                        var widening     = CommonPhotoManager.GetImgFormatName(imgFormat);
                        var trueFileName = string.Format("{0}{1}_size_{2}-{3}.{4}", (item.ModuleId == Guid.Empty ? "" : item.ModuleId.ToString()), item.UserId, item.Size.Width, item.Size.Height, widening);

                        using (var stream2 = new MemoryStream(data))
                        {
                            var photoUrl = item.DataStore.Save(trueFileName, stream2).ToString();
                            //NOTE: Update cache here
                            var fileName = Path.GetFileName(photoUrl);

                            AddToCache(item.UserId, item.Size, fileName, true);
                        }
                    }
            }
            catch (ArgumentException error)
            {
                throw new UnknownImageFormatException(error);
            }
        }
 public bool Equals(ResizeWorkerItem other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return other.ModuleId.Equals(ModuleId) && other.UserId.Equals(UserId) && other.MaxFileSize == MaxFileSize && other.Size.Equals(Size);
 }
        private static void ResizeImage(ResizeWorkerItem item)
        {
            try
            {
                var data = item.Data;
                using (var stream = new MemoryStream(data))
                using (var img = Image.FromStream(stream))
                {
                    var imgFormat = img.RawFormat;
                    if (item.Size != img.Size)
                    {
                        using (var img2 = DoThumbnail(img, item.Size, true))
                        {
                            data = SaveToBytes(img2);
                        }
                    }
                    else
                    {
                        data = SaveToBytes(img);
                    }

                    var widening = GetImgFormatName(imgFormat);
                    var trueFileName = string.Format("{0}{1}_size_{2}-{3}.{4}", (item.ModuleId == Guid.Empty ? "" : item.ModuleId.ToString()), item.UserId, item.Size.Width, item.Size.Height, widening);

                    using (var stream2 = new MemoryStream(data))
                    {
                        var photoUrl = item.DataStore.Save(trueFileName, stream2).ToString();
                        //NOTE: Update cache here
                        var fileName = Path.GetFileName(photoUrl);

                        AddToCache(item.UserId, item.Size, fileName, true);
                    }
                }
            }
            catch (ArgumentException error)
            {
                throw new UnknownImageFormatException(error);
            }
        }
        private static string SizePhoto(Guid moduleID, Guid userID, byte[] data, long maxFileSize, Size size, bool now)
        {
            if (data == null || data.Length <= 0) throw new UnknownImageFormatException();
            if (maxFileSize != -1 && data.Length > maxFileSize) throw new ImageWeightLimitException();

            var resizeTask = new ResizeWorkerItem(moduleID, userID, data, maxFileSize, size, GetDataStore());
            if (now)
            {
                //Resize synchronously
                ResizeImage(resizeTask);
                return GetSizedPhotoAbsoluteWebPath(moduleID, userID,size);
            }
            else
            {
                if (!ResizeQueue.GetItems().Contains(resizeTask))
                {
                    //Add
                    ResizeQueue.Add(resizeTask);
                    if (!ResizeQueue.IsStarted)
                    {
                        ResizeQueue.Start(ResizeImage);
                    }
                }
                return GetDefaultPhotoAbsoluteWebPath(size);
                    //NOTE: return default photo here. Since task will update cache
            }
        }