Пример #1
0
        public string Process(string body, string tenantTypeId, long associateId, long userId)
        {
            if (string.IsNullOrEmpty(body) || !body.Contains("[attach:"))
            {
                return(body);
            }

            List <long> attachmentIds = new List <long>();

            Regex           rg      = new Regex(@"\[attach:(?<id>[\d]+)\]", RegexOptions.Multiline | RegexOptions.Singleline);
            MatchCollection matches = rg.Matches(body);

            if (matches != null)
            {
                foreach (Match m in matches)
                {
                    if (m.Groups["id"] == null || string.IsNullOrEmpty(m.Groups["id"].Value))
                    {
                        continue;
                    }
                    long attachmentId = 0;
                    long.TryParse(m.Groups["id"].Value, out attachmentId);
                    if (attachmentId > 0 && !attachmentIds.Contains(attachmentId))
                    {
                        attachmentIds.Add(attachmentId);
                    }
                }
            }

            IEnumerable <ContentAttachment> attachments = new ContentAttachmentService().Gets(attachmentIds);

            if (attachments != null && attachments.Count() > 0)
            {
                IList <BBTag> bbTags       = new List <BBTag>();
                string        htmlTemplate = "<div class=\"tn-annexinlaid\"><a href=\"{3}\" rel=\"nofollow\">{0}</a>(<em>{1}</em>,<em>下载次数:{2}</em>)<a href=\"{3}\" rel=\"nofollow\">下载</a> </div>";

                //解析文本中附件
                foreach (var attachment in attachments)
                {
                    bbTags.Add(AddBBTag(htmlTemplate, attachment));
                }

                body = HtmlUtility.BBCodeToHtml(body, bbTags);
            }
            return(body);
        }
        public override void ProcessRequest(HttpContext context)
        {
            long attachmentId = context.Request.QueryString.Get <long>("attachmentId", 0);

            if (attachmentId <= 0)
            {
                WebUtility.Return404(context);
                return;
            }

            ContentAttachmentService contentAttachmentService = new ContentAttachmentService();
            ContentAttachment        attachment = contentAttachmentService.Get(attachmentId);

            if (attachment == null)
            {
                WebUtility.Return404(context);
                return;
            }

            IUser currentUser = UserContext.CurrentUser;

            //下载计数
            CountService countService = new CountService(TenantTypeIds.Instance().ContentAttachment());

            countService.ChangeCount(CountTypes.Instance().DownloadCount(), attachment.AttachmentId, attachment.UserId, 1, false);

            bool enableCaching = context.Request.QueryString.GetBool("enableCaching", true);

            context.Response.Status     = "302 Object Moved";
            context.Response.StatusCode = 302;

            LinktimelinessSettings linktimelinessSettings = DIContainer.Resolve <ISettingsManager <LinktimelinessSettings> >().Get();
            string token = Utility.EncryptTokenForAttachmentDownload(linktimelinessSettings.Highlinktimeliness, attachmentId);

            context.Response.Redirect(SiteUrls.Instance().ContentAttachmentTempUrl(attachment.AttachmentId, token, enableCaching), true);
            context.Response.Flush();
            context.Response.End();
        }
        public override void ProcessRequest(HttpContext context)
        {
            long attachmentId = context.Request.QueryString.Get <long>("attachmentId", 0);

            if (attachmentId <= 0)
            {
                WebUtility.Return404(context);
                return;
            }

            //检查链接是否过期
            string token               = context.Request.QueryString.GetString("token", string.Empty);
            bool   isTimeout           = true;
            long   attachmentIdInToken = Utility.DecryptTokenForAttachmentDownload(token, out isTimeout);

            if (isTimeout || attachmentIdInToken != attachmentId)
            {
                WebUtility.Return403(context);
                return;
            }

            ContentAttachmentService contentAttachmentService = new ContentAttachmentService();
            ContentAttachment        attachment = contentAttachmentService.Get(attachmentId);

            if (attachment == null)
            {
                WebUtility.Return404(context);
                return;
            }


            bool     enableCaching = context.Request.QueryString.GetBool("enableCaching", true);
            DateTime lastModified  = attachment.DateCreated.ToUniversalTime();

            if (enableCaching && IsCacheOK(context, lastModified))
            {
                WebUtility.Return304(context);
                return;
            }

            //输出文件流
            IStoreFile storeFile = storeProvider.GetFile(attachment.GetRelativePath(), attachment.FileName);

            if (storeFile == null)
            {
                WebUtility.Return404(context);
                return;
            }

            context.Response.Clear();
            //context.Response.ClearHeaders();
            //context.Response.Cache.VaryByParams["attachmentId"] = true;
            string fileExtension    = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.') + 1);
            string friendlyFileName = attachment.FriendlyFileName + (attachment.FriendlyFileName.EndsWith(fileExtension) ? "" : "." + fileExtension);

            SetResponsesDetails(context, attachment.ContentType, friendlyFileName, lastModified);

            DefaultStoreFile fileSystemFile = storeFile as DefaultStoreFile;

            if (!fileSystemFile.FullLocalPath.StartsWith(@"\"))
            {
                //本地文件下载
                context.Response.TransmitFile(fileSystemFile.FullLocalPath);
                context.Response.End();
            }
            else
            {
                context.Response.AddHeader("Content-Length", storeFile.Size.ToString("0"));
                context.Response.Buffer       = false;
                context.Response.BufferOutput = false;

                using (Stream stream = fileSystemFile.OpenReadStream())
                {
                    if (stream == null)
                    {
                        WebUtility.Return404(context);
                        return;
                    }
                    long   bufferLength = fileSystemFile.Size <= DownloadFileHandlerBase.BufferLength ? fileSystemFile.Size : DownloadFileHandlerBase.BufferLength;
                    byte[] buffer       = new byte[bufferLength];

                    int readedSize;
                    while ((readedSize = stream.Read(buffer, 0, (int)bufferLength)) > 0 && context.Response.IsClientConnected)
                    {
                        context.Response.OutputStream.Write(buffer, 0, readedSize);
                        context.Response.Flush();
                    }

                    stream.Close();
                }
                context.Response.End();
            }
        }