Ejemplo n.º 1
0
        /// <summary>
        /// 为指定用户生成指定附件的拷贝
        /// </summary>
        /// <param name="attachment">指定附件实体</param>
        /// <param name="userId">指定用户的id</param>
        /// <returns>新附件实体</returns>
        public T CloneForUser(T attachment, long userId)
        {
            //复制数据库记录
            T newAttachment = (T) new Attachment();

            newAttachment.ContentType      = attachment.ContentType;
            newAttachment.FileLength       = attachment.FileLength;
            newAttachment.FriendlyFileName = attachment.FriendlyFileName;
            newAttachment.Height           = attachment.Height;
            newAttachment.MediaType        = attachment.MediaType;
            newAttachment.OwnerId          = userId;
            newAttachment.TenantTypeId     = attachment.TenantTypeId;
            newAttachment.UserDisplayName  = userService.GetUser(userId).DisplayName;
            newAttachment.UserId           = userId;
            newAttachment.Width            = attachment.Width;
            newAttachment.FileName         = newAttachment.GenerateFileName();

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

            attachmentRepository.Insert(newAttachment);
            EventBus <T> .Instance().OnAfter(newAttachment, new CommonEventArgs(EventOperationType.Instance().Create()));

            //复制文件
            Stream stream = null;

            try
            {
                IStoreFile file = StoreProvider.GetFile(attachment.GetRelativePath(), attachment.FileName);
                stream = file.OpenReadStream();
                StoreProvider.AddOrUpdateFile(newAttachment.GetRelativePath(), newAttachment.FileName, stream);
            }
            catch
            {
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }

            //根据不同租户类型的设置生成不同尺寸的图片,用于图片直连访问
            if (newAttachment.MediaType == MediaType.Image)
            {
                TenantAttachmentSettings tenantAttachmentSettings = TenantAttachmentSettings.GetRegisteredSettings(newAttachment.TenantTypeId);
                if (tenantAttachmentSettings != null && tenantAttachmentSettings.ImageSizeTypes != null && tenantAttachmentSettings.ImageSizeTypes.Count > 0)
                {
                    foreach (var imageSizeType in tenantAttachmentSettings.ImageSizeTypes)
                    {
                        IStoreFile file = StoreProvider.GetResizedImage(newAttachment.GetRelativePath(), newAttachment.FileName, imageSizeType.Size, imageSizeType.ResizeMethod);
                    }
                }
            }

            return(newAttachment);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 上传Logo
        /// </summary>
        /// <param name="associateId"></param>
        /// <param name="stream"></param>
        /// <returns>上传文件的相对路径(包含文件名)</returns>
        public string UploadLogo(object associateId, Stream stream)
        {
            string relativeFileName = string.Empty;

            if (stream != null)
            {
                ILogoSettingsManager logoSettingsManager = DIContainer.Resolve <ILogoSettingsManager>();
                LogoSettings         logoSettings        = logoSettingsManager.Get();

                //检查是否需要缩放原图
                Image image = Image.FromStream(stream);
                if (image.Height > this.TenantLogoSettings.MaxHeight || image.Width > this.TenantLogoSettings.MaxWidth)
                {
                    stream = ImageProcessor.Resize(stream, this.TenantLogoSettings.MaxWidth, this.TenantLogoSettings.MaxHeight, logoSettings.ResizeMethod);
                }

                string relativePath = GetLogoRelativePath(associateId);
                string fileName     = GetLogoFileName(associateId);
                relativeFileName = relativePath + "\\" + fileName;

                StoreProvider.AddOrUpdateFile(relativePath, fileName, stream);
                stream.Dispose();

                //根据不同租户类型的设置生成不同尺寸的图片,用于图片直连访问
                if (this.TenantLogoSettings.ImageSizeTypes != null && this.TenantLogoSettings.ImageSizeTypes.Count > 0)
                {
                    foreach (var imageSizeType in this.TenantLogoSettings.ImageSizeTypes.Values)
                    {
                        string sizedFileName = StoreProvider.GetSizeImageName(fileName, imageSizeType.Key, imageSizeType.Value);
                        StoreProvider.DeleteFile(relativePath, sizedFileName);
                        IStoreFile file = StoreProvider.GetResizedImage(relativePath, fileName, imageSizeType.Key, imageSizeType.Value);
                    }
                }
            }

            return(relativeFileName);
        }
Ejemplo n.º 3
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()));
        }