Ejemplo n.º 1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userProfileSettings">用户资料设置</param>
        /// <param name="userSettings">用户设置</param>
        /// <param name="inviteFriendSettings">邀请朋友设置</param>
        public AttachmentSettingsEditModel(AttachmentSettings attachmentSettings)
        {
            if (attachmentSettings != null)
            {
                AllowedFileExtensions         = attachmentSettings.AllowedFileExtensions;
                BatchUploadLimit              = attachmentSettings.BatchUploadLimit;
                InlinedImageHeight            = attachmentSettings.InlinedImageHeight;
                InlinedImageWidth             = attachmentSettings.InlinedImageWidth;
                MaxAttachmentLength           = attachmentSettings.MaxAttachmentLength;
                MaxImageHeight                = attachmentSettings.MaxImageHeight;
                MaxImageWidth                 = attachmentSettings.MaxImageWidth;
                TemporaryAttachmentStorageDay = attachmentSettings.TemporaryAttachmentStorageDay;

                WatermarkSettings watermarkSettings = attachmentSettings.WatermarkSettings;
                if (watermarkSettings != null)
                {
                    WatermarkImageName = watermarkSettings.WatermarkImageName;
                    WatermarkLocation  = watermarkSettings.WatermarkLocation;
                    WatermarkMinHeight = watermarkSettings.WatermarkMinHeight;
                    WatermarkMinWidth  = watermarkSettings.WatermarkMinWidth;
                    WatermarkOpacity   = watermarkSettings.WatermarkOpacity;
                    WatermarkText      = watermarkSettings.WatermarkText;
                    WatermarkType      = watermarkSettings.WatermarkType;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 删除垃圾临时附件
        /// </summary>
        public void DeleteTrashTemporaryAttachments()
        {
            IAttachmentSettingsManager attachmentSettingsManager = DIContainer.Resolve <IAttachmentSettingsManager>();

            if (attachmentSettingsManager == null)
            {
                return;
            }
            AttachmentSettings attachmentSettings = attachmentSettingsManager.Get();
            int temporaryAttachmentStorageDay     = attachmentSettings.TemporaryAttachmentStorageDay;

            if (temporaryAttachmentStorageDay < 1)
            {
                temporaryAttachmentStorageDay = 1;
            }
            IEnumerable <T> attachments = attachmentRepository.GetTrashTemporaryAttachments(temporaryAttachmentStorageDay);

            if (attachments.Count() == 0)
            {
                return;
            }
            foreach (var attachment in attachments)
            {
                DeleteStoredFile(attachment);
            }
            attachmentRepository.DeleteTrashTemporaryAttachments(temporaryAttachmentStorageDay);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 转换为userProfileSettings用于数据库存储
        /// </summary>
        public AttachmentSettings AsAttachmentSettings()
        {
            AttachmentSettings attachmentSettings = DIContainer.Resolve <IAttachmentSettingsManager>().Get();

            attachmentSettings.AllowedFileExtensions         = AllowedFileExtensions;
            attachmentSettings.BatchUploadLimit              = BatchUploadLimit;
            attachmentSettings.InlinedImageHeight            = InlinedImageHeight;
            attachmentSettings.InlinedImageWidth             = InlinedImageWidth;
            attachmentSettings.MaxAttachmentLength           = MaxAttachmentLength;
            attachmentSettings.MaxImageHeight                = MaxImageHeight;
            attachmentSettings.MaxImageWidth                 = MaxImageWidth;
            attachmentSettings.TemporaryAttachmentStorageDay = TemporaryAttachmentStorageDay;

            WatermarkSettings watermarkSettings = attachmentSettings.WatermarkSettings ?? new WatermarkSettings();

            watermarkSettings.WatermarkType = WatermarkType;
            if (this.WatermarkType != WatermarkType.None)
            {
                watermarkSettings.WatermarkImageName = WatermarkImageName;
                watermarkSettings.WatermarkLocation  = WatermarkLocation;
                watermarkSettings.WatermarkMinHeight = WatermarkMinHeight;
                watermarkSettings.WatermarkMinWidth  = WatermarkMinWidth;
                watermarkSettings.WatermarkOpacity   = WatermarkOpacity;
                watermarkSettings.WatermarkText      = WatermarkText;
            }
            attachmentSettings.WatermarkSettings = watermarkSettings;
            return(attachmentSettings);
        }
Ejemplo n.º 4
0
 public Attachment(AttachmentSettings settings, IOffensiveStats stats, Transform tr, int teamId, CombatActionEffect.Factory combatActionEffectFactory)
 {
     _settings                  = settings;
     _stats                     = stats;
     _ownerTr                   = tr;
     _teamId                    = teamId;
     _cooldown.Total            = settings.Cooldown;
     _cooldown.Remaining        = settings.Cooldown;
     _combatActionEffectFactory = combatActionEffectFactory;
 }
Ejemplo n.º 5
0
        public static string[] GetAllowedExtensions(this AttachmentSettings settings, User user)
        {
            // We need to be authenticated to upload attachments
            if (user == null)
            {
                return(null);
            }

            // We need to be authenticated to upload attachments
            if (user.UserRoles == null)
            {
                return(null);
            }

            // No settings use defaults
            if (settings == null)
            {
                return(DefaultAttachmentSettings.AllowedExtensions);
            }

            // No settings use defaults
            if (settings.Settings == null)
            {
                return(DefaultAttachmentSettings.AllowedExtensions);
            }

            // Accumulate all unique extensions for given roles
            List <string> output = null;
            var           roles  = user.UserRoles;

            foreach (var role in roles)
            {
                foreach (var setting in settings.Settings)
                {
                    if (setting.RoleId == role.Id)
                    {
                        foreach (var extension in setting.AllowedExtensions)
                        {
                            if (output == null)
                            {
                                output = new List <string>();
                            }
                            if (!output.Contains(extension))
                            {
                                output.Add(extension);
                            }
                        }
                    }
                }
            }

            return(output?.ToArray());
        }
Ejemplo n.º 6
0
 static Cleaner CreateCleaner(AttachmentSettings settings, IPersister persister, IBuilder builder)
 {
     return(new Cleaner(async token =>
     {
         using (var connection = await settings.ConnectionFactory().ConfigureAwait(false))
         {
             await persister.CleanupItemsOlderThan(connection, null, DateTime.UtcNow, token).ConfigureAwait(false);
         }
     },
                        criticalError: builder.Build <CriticalError>().Raise,
                        frequencyToRunCleanup: TimeSpan.FromHours(1),
                        timer: new AsyncTimer()));
 }
Ejemplo n.º 7
0
 public IActionResult Attachment(AttachmentSettings model)
 {
     if (model != null)
     {
         bool result = settingService.SaveAttachment(model);
         messages.Msg     = result ? "保存成功" : "保存失败";
         messages.Success = result;
     }
     else
     {
         messages.Msg = "请填写完整";
     }
     return(Json(messages));
 }
Ejemplo n.º 8
0
        public IActionResult Attachment()
        {
            LoadFont();
            AttachmentSettings model = new AttachmentSettings();

            model.WatermarkTextSize = 12;
            model.WatermarkType     = 0;
            model.WatermarkPosition = 9;
            Dictionary <string, string> dic = settingService.GetConfigByGroupId((int)SettingEnum.Attachment);

            if (dic != null)
            {
                model = new AttachmentSettings(dic);
            }
            return(View(model));
        }
Ejemplo n.º 9
0
        public static long GetAvailableSpace(this AttachmentSettings settings, User user)
        {
            // We need to be authenticated to upload attachments
            if (user == null)
            {
                return(0);
            }

            // We need to be authenticated to upload attachments
            if (user.UserRoles == null)
            {
                return(0);
            }

            // No settings use defaults
            if (settings == null)
            {
                return(DefaultAttachmentSettings.AvailableSpace);
            }

            // No settings use defaults
            if (settings.Settings == null)
            {
                return(DefaultAttachmentSettings.AvailableSpace);
            }

            // Calculate the highest available space for given roles
            long output = 0;
            var  roles  = user.UserRoles;

            foreach (var role in roles)
            {
                foreach (var setting in settings.Settings)
                {
                    if (setting.RoleId == role.Id)
                    {
                        if (setting.AvailableSpace > output)
                        {
                            output = setting.AvailableSpace;
                        }
                    }
                }
            }

            return(output);
        }
Ejemplo n.º 10
0
        public static bool Contains(this AttachmentSettings settings, AttachmentSetting comparer)
        {
            if (settings == null)
            {
                return(false);
            }

            foreach (var setting in settings.Settings)
            {
                if (setting.RoleId == comparer.RoleId)
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 添加附件配置数据
        /// </summary>
        /// <param name="model">附件配置Model</param>
        public bool SaveAttachment(AttachmentSettings model)
        {
            int flag = (int)SettingEnum.Attachment;

            using (DataTable dt = new DataTable())
            {
                dt.Columns.Add("S_GROUPID", typeof(string));
                dt.Columns.Add("S_NAME", typeof(string));
                dt.Columns.Add("S_VALUE", typeof(string));
                PropertyInfo[] properties = model.GetType().GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = flag;
                    dr[1] = property.Name;
                    dr[2] = property.GetValue(model, null);
                    dt.Rows.Add(dr);
                }
                return(this.Save(dt, flag) > 0);
            }
        }
Ejemplo n.º 12
0
 public NeedToInstallSomething(ReadOnlySettings settings)
 {
     this.settings = settings.GetOrDefault <AttachmentSettings>();
 }
Ejemplo n.º 13
0
        /// <summary>
        /// 创建附件
        /// </summary>
        /// <param name="attachment">附件</param>
        /// <param name="contentStream">文件流</param>
        public void Create(T attachment, Stream contentStream)
        {
            if (contentStream == null)
            {
                return;
            }

            if (attachment.MediaType == MediaType.Image)
            {
                //检查是否需要缩放原图
                if (TenantAttachmentSettings.MaxImageWidth > 0 || TenantAttachmentSettings.MaxImageHeight > 0)
                {
                    int maxWidth  = TenantAttachmentSettings.MaxImageWidth > 0 ? TenantAttachmentSettings.MaxImageWidth : attachment.Width;
                    int maxHeight = TenantAttachmentSettings.MaxImageHeight > 0 ? TenantAttachmentSettings.MaxImageHeight : attachment.Height;

                    if (attachment.Width > maxWidth || attachment.Height > maxHeight)
                    {
                        Stream resizedStream = ImageProcessor.Resize(contentStream, maxWidth, maxHeight, ResizeMethod.KeepAspectRatio);
                        if (resizedStream != contentStream)
                        {
                            contentStream.Dispose();
                        }
                        contentStream = resizedStream;
                    }
                }

                IAttachmentSettingsManager attachmentSettingsManager = DIContainer.Resolve <IAttachmentSettingsManager>();
                AttachmentSettings         attachmentSettings        = attachmentSettingsManager.Get();

                Image image = Image.FromStream(contentStream);
                bool  isGif = ImageProcessor.IsGIFAnimation(image);

                //检查是否需要打水印
                if (!isGif && TenantAttachmentSettings.EnableWatermark && attachmentSettings.WatermarkSettings.WatermarkType != WatermarkType.None && image.Width >= attachmentSettings.WatermarkSettings.WatermarkMinWidth && image.Height >= attachmentSettings.WatermarkSettings.WatermarkMinHeight)
                {
                    ImageProcessor imageProcessor = new ImageProcessor();

                    if (attachmentSettings.WatermarkSettings.WatermarkType == WatermarkType.Text)
                    {
                        TextWatermarkFilter watermarkFilter = new TextWatermarkFilter(attachmentSettings.WatermarkSettings.WatermarkText, attachmentSettings.WatermarkSettings.WatermarkLocation, attachmentSettings.WatermarkSettings.WatermarkOpacity);
                        imageProcessor.Filters.Add(watermarkFilter);
                    }
                    else if (attachmentSettings.WatermarkSettings.WatermarkType == WatermarkType.Image)
                    {
                        ImageWatermarkFilter watermarkFilter = new ImageWatermarkFilter(attachmentSettings.WatermarkSettings.WatermarkImagePhysicalPath, attachmentSettings.WatermarkSettings.WatermarkLocation, attachmentSettings.WatermarkSettings.WatermarkOpacity);
                        imageProcessor.Filters.Add(watermarkFilter);
                    }

                    //如果需要添加水印,则除水印图片以外,还需要保留原图
                    StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.GetOriginalFileName(), contentStream);

                    //给原始尺寸的图添加水印
                    using (Stream watermarkImageStream = imageProcessor.Process(contentStream))
                    {
                        StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, watermarkImageStream);
                    }

                    //根据设置生成不同尺寸的图片,并添加水印
                    if (TenantAttachmentSettings.ImageSizeTypes != null && TenantAttachmentSettings.ImageSizeTypes.Count > 0)
                    {
                        foreach (var imageSizeType in TenantAttachmentSettings.ImageSizeTypes)
                        {
                            Stream resizedStream = ImageProcessor.Resize(contentStream, imageSizeType.Size.Width, imageSizeType.Size.Height, imageSizeType.ResizeMethod);
                            image = Image.FromStream(resizedStream);
                            if (image.Width >= attachmentSettings.WatermarkSettings.WatermarkMinWidth && image.Height >= attachmentSettings.WatermarkSettings.WatermarkMinHeight)
                            {
                                using (Stream watermarkImageStream = imageProcessor.Process(resizedStream))
                                {
                                    StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), watermarkImageStream);
                                }
                            }
                            else
                            {
                                StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), resizedStream);
                            }

                            if (resizedStream != contentStream)
                            {
                                resizedStream.Dispose();
                            }
                        }
                    }
                }
                else
                {
                    StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, contentStream);

                    if (!isGif)
                    {
                        //根据设置生成不同尺寸的图片
                        if (TenantAttachmentSettings.ImageSizeTypes != null && TenantAttachmentSettings.ImageSizeTypes.Count > 0)
                        {
                            foreach (var imageSizeType in TenantAttachmentSettings.ImageSizeTypes)
                            {
                                Stream resizedStream = ImageProcessor.Resize(contentStream, imageSizeType.Size.Width, imageSizeType.Size.Height, imageSizeType.ResizeMethod);
                                StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), StoreProvider.GetSizeImageName(attachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod), resizedStream);
                                if (resizedStream != contentStream)
                                {
                                    resizedStream.Dispose();
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                StoreProvider.AddOrUpdateFile(attachment.GetRelativePath(), attachment.FileName, contentStream);
            }

            if (contentStream != null)
            {
                contentStream.Dispose();
            }

            EventBus <T> .Instance().OnBefore(attachment, new CommonEventArgs(EventOperationType.Instance().Create()));

            attachmentRepository.Insert(attachment);
            EventBus <T> .Instance().OnAfter(attachment, new CommonEventArgs(EventOperationType.Instance().Create()));
        }
Ejemplo n.º 14
0
 public void Save(AttachmentSettings attachmentSettings)
 {
     repository.Save(attachmentSettings);
 }