/// <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); }
/// <summary> /// 获取不同尺寸的Logo /// </summary> /// <param name="associateId"></param> /// <param name="imageSizeTypeKey"></param> /// <returns></returns> public IStoreFile GetResizedLogo(object associateId, string imageSizeTypeKey) { IStoreFile logoFile = null; if (TenantLogoSettings.ImageSizeTypes == null || !TenantLogoSettings.ImageSizeTypes.ContainsKey(imageSizeTypeKey)) { logoFile = GetLogo(associateId); } else { var pair = TenantLogoSettings.ImageSizeTypes[imageSizeTypeKey]; logoFile = StoreProvider.GetResizedImage(GetLogoRelativePath(associateId), GetLogoFileName(associateId), pair.Key, pair.Value); } return(logoFile); }
/// <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); }