/// <summary> /// 是否允许购买(包含已经下载过、或者不需要购买) /// </summary> /// <param name="attachment"></param> /// <returns></returns> public bool Attachment_Buy(Attachment attachment) { if (Attachment_Download(attachment)) return true; if (UserContext.CurrentUser != null && attachment.Price <= UserContext.CurrentUser.TradePoints) return true; return false; }
/// <summary> /// 是否具有删除附件的权限 /// </summary> /// <returns></returns> public bool Attachment_Delete(Attachment attachment) { IUser currentUser = UserContext.CurrentUser; if (currentUser == null) return false; TenantType tenantType = tenantTypeService.Get(attachment.TenantTypeId); if (tenantType == null) return false; if (IsAdministrator(tenantType.ApplicationId)) return true; if (AuthorizationService.IsOwner(currentUser, attachment.UserId)) return true; if (AuthorizationService.IsTenantManager(currentUser, attachment.TenantTypeId, attachment.OwnerId)) return true; return false; }
public static bool BarAttachement_Download(this IUser currentUser, Attachment attachment) { if (DIContainer.Resolve<Authorizer>().Attachment_Edit(attachment)) return true; //处理仅允许注册用户下载 //if (thread.OnlyAllowRegisteredUserDownload && currentUser == null) // return false; //处理售价 if (attachment.Price <= 0) return true; AttachmentDownloadService attachementDownloadService = new AttachmentDownloadService(); if (currentUser != null && attachementDownloadService.IsDownloaded(currentUser.UserId, attachment.AttachmentId)) return true; if (currentUser != null && attachment.Price < currentUser.TradePoints) return true; return false; }
/// <summary> /// 下载远程图片 /// </summary> /// <param name="imageUrl">将要下载的图片地址</param> /// <param name="microblogId">微博Id</param> private void DownloadRemoteImage(string imageUrl, long microblogId) { if (UserContext.CurrentUser == null || microblogId <= 0 || string.IsNullOrEmpty(imageUrl)) return; try { WebRequest webRequest = WebRequest.Create(SiteUrls.FullUrl(imageUrl)); HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); Stream stream = httpWebResponse.GetResponseStream(); MemoryStream ms = new MemoryStream(); stream.CopyTo(ms); string friendlyFileName = imageUrl.Substring(imageUrl.LastIndexOf("/") + 1); string contentType = MimeTypeConfiguration.GetMimeType(friendlyFileName); bool isImage = contentType.StartsWith("image"); if (!isImage || stream == null || !stream.CanRead) return; Attachment attachment = new Attachment(ms, contentType, friendlyFileName); attachment.FileLength = httpWebResponse.ContentLength; attachment.AssociateId = microblogId; attachment.UserId = UserContext.CurrentUser.UserId; attachment.UserDisplayName = UserContext.CurrentUser.DisplayName; attachment.TenantTypeId = TenantTypeIds.Instance().Microblog(); var attachmentService = new AttachmentService(TenantTypeIds.Instance().Microblog()); attachmentService.Create(attachment, ms); ms.Dispose(); ms.Close(); } catch { } }
/// <summary> /// 添加BBTag实体 /// </summary> /// <param name="htmlTemplate">html模板</param> /// <param name="attachment">带替换附件</param> /// <returns></returns> private BBTag AddBBTag(string htmlTemplate, Attachment attachment) { BBAttribute bbAttribute = new BBAttribute("attachTemplate", "", n => { return string.Format(htmlTemplate, attachment.FriendlyFileName, attachment.FriendlyFileLength, attachment.DownloadCount, SiteUrls.Instance().AttachmentUrl(attachment.AttachmentId,TenantTypeIds.Instance().ContentItem())); }, HtmlEncodingMode.UnsafeDontEncode); return new BBTag("attach:" + attachment.AttachmentId, "${attachTemplate}", "", false, BBTagClosingStyle.LeafElementWithoutContent, null, bbAttribute); }
/// <summary> /// 添加BBTag实体 /// </summary> /// <param name="htmlTemplate">html模板</param> /// <param name="attachment">带替换附件</param> /// <returns>BBTag实体</returns> private BBTag AddBBTag(string htmlTemplate, Attachment attachment) { PointCategory pointCategory = new PointService().GetPointCategory(PointCategoryKeys.Instance().TradePoints()); string categoryName = pointCategory != null ? pointCategory.CategoryName : "金币"; BBAttribute bbAttribute = new BBAttribute("attachTemplate", "", n => { return string.Format(htmlTemplate, attachment.FriendlyFileName, attachment.FriendlyFileLength, attachment.Price > 0 ? ",<em>需要" + attachment.Price + categoryName + "</em>" : "", attachment.DownloadCount, attachment.AttachmentId); }, HtmlEncodingMode.UnsafeDontEncode); return new BBTag("attach:" + attachment.AttachmentId, "${attachTemplate}", "", false, BBTagClosingStyle.LeafElementWithoutContent, null, bbAttribute); }
public ActionResult UploadFile(string tenantTypeId, string requestName, long associateId, bool resize = false) { IUser user = UserContext.CurrentUser; if (user == null) { return new EmptyResult(); } AttachmentService<Attachment> attachementService = new AttachmentService<Attachment>(tenantTypeId); long userId = user.UserId, ownerId = user.UserId; string userDisplayName = user.DisplayName; long attachmentId = 0; if (Request.Files.Count > 0 && !string.IsNullOrEmpty(Request.Files[requestName].FileName)) { HttpPostedFileBase postFile = Request.Files[requestName]; string fileName = postFile.FileName; TenantAttachmentSettings tenantAttachmentSettings = TenantAttachmentSettings.GetRegisteredSettings(tenantTypeId); //图片类型支持:gif,jpg,jpeg,png string[] picTypes = { ".gif", ".jpg", ".jpeg", ".png" }; if (!tenantAttachmentSettings.ValidateFileExtensions(fileName) && !picTypes.Contains(fileName.Substring(fileName.LastIndexOf('.')))) { throw new ExceptionFacade(string.Format("只允许上传后缀名为{0}的文件", tenantAttachmentSettings.AllowedFileExtensions)); } if (!tenantAttachmentSettings.ValidateFileLength(postFile.ContentLength)) { throw new ExceptionFacade(string.Format("文件大小不允许超过{0}", tenantAttachmentSettings.MaxAttachmentLength)); } string contentType = MimeTypeConfiguration.GetMimeType(postFile.FileName); Attachment attachment = new Attachment(postFile, contentType); attachment.UserId = userId; attachment.AssociateId = associateId; attachment.TenantTypeId = tenantTypeId; attachment.OwnerId = ownerId; attachment.UserDisplayName = userDisplayName; using (Stream stream = postFile.InputStream) { attachementService.Create(attachment, stream); } attachmentId = attachment.AttachmentId; } return Json(new { AttachmentId = attachmentId }); }
/// <summary> /// 是否拥有下载的权限 /// </summary> /// <param name="attachment"></param> /// <returns></returns> public bool Attachment_Download(Attachment attachment) { //处理仅允许注册用户下载 //if (thread.OnlyAllowRegisteredUserDownload && currentUser == null) // return false; //处理售价 if (attachment.Price <= 0) return true; //if (DIContainer.Resolve<Authorizer>().Attachment_Edit(attachment)) // return true; IUser currentUser = UserContext.CurrentUser; if (currentUser == null) return false; if (AuthorizationService.IsOwner(currentUser, attachment.UserId)) return true; AttachmentDownloadService attachementDownloadService = new AttachmentDownloadService(); if (UserContext.CurrentUser != null && attachementDownloadService.IsDownloaded(UserContext.CurrentUser.UserId, attachment.AttachmentId)) return true; return false; }
/// <summary> /// 附件下载的直连地址 /// </summary> /// <param name="attachment">附件实体</param> /// <param name="enableClientCaching">是否缓存</param> /// <returns></returns> public string AttachmentDirectUrl(Attachment attachment, bool enableClientCaching = true) { if (attachment == null) { return string.Empty; } string attachmentPath = attachment.GetRelativePath() + "/" + attachment.FileName; if (enableClientCaching) { return storeProvider.GetDirectlyUrl(attachmentPath); } else { return storeProvider.GetDirectlyUrl(attachmentPath, DateTime.Now); } }
/// <summary> /// 获取图片的URL /// </summary> /// <param name="attachment">附件</param> /// <param name="tenantTypeId">租户类型Id</param> /// <param name="imageSizeTypeKey">尺寸类型(通过ImageSizeTypeKeys类获取)</param> /// <param name="enableClientCaching">是否使用客户端缓存</param> /// <returns></returns> public string ImageUrl(Attachment attachment, string tenantTypeId, string imageSizeTypeKey, bool enableClientCaching = true) { if (attachment == null) { return WebUtility.ResolveUrl("~/Themes/Shared/Styles/Images/default_img.png"); } return ImageUrl(attachment.GetRelativePath() + "/" + attachment.FileName, tenantTypeId, imageSizeTypeKey, enableClientCaching); }
/// <summary> /// 实例化下载记录对象 /// </summary> /// <param name="attachment">附件实体(用来为下载记录提供一些信息)</param> /// <returns></returns> public static AttachmentDownloadRecord New(Attachment attachment) { return new AttachmentDownloadRecord() { OwnerId = attachment.OwnerId, //UserId = attachment.UserId, //UserDisplayName = attachment.UserDisplayName, AttachmentId = attachment.AttachmentId, TenantTypeId = attachment.TenantTypeId, AssociateId = attachment.AssociateId, FromUrl = HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.UrlReferrer!=null ? HttpContext.Current.Request.UrlReferrer.ToString() : string.Empty, IP = WebUtility.GetIP(), Price = attachment.Price, DownloadDate = DateTime.UtcNow, LastDownloadDate = DateTime.UtcNow }; }