Ejemplo n.º 1
0
        public virtual void ImageStreamFromFileID(HttpContext context)
        {
            YZRequest   request = new YZRequest(context);
            string      fileid  = request.GetString("fileid", null);
            ScaleMode   scale   = request.GetEnum <ScaleMode>("scale", ScaleMode.None);
            ImageFormat format  = YZImageHelper.ParseImageFormat(request.GetString("format", "png"));

            int width  = 0;
            int height = 0;

            if (scale != ScaleMode.None)
            {
                width  = request.GetInt32("width");
                height = request.GetInt32("height");
            }

            AttachmentInfo attachment;
            string         filePath = this.GetImageFileFromFileID(fileid, scale, width, height, format, out attachment);

            if (String.IsNullOrEmpty(filePath))
            {
                if (attachment == null)
                {
                    filePath = context.Server.MapPath("~/YZSoft/Attachment/img/EmptyImageAttachment.png");
                }
                else
                {
                    if (YZMimeMapping.isVedioFile(attachment.Ext))
                    {
                        filePath = context.Server.MapPath("~/YZSoft/Attachment/img/EmptyImageAttachment.png");
                    }
                    else
                    {
                        filePath = context.Server.MapPath("~/YZSoft/Attachment/img/EmptyImageAttachment.png");
                    }
                }
            }

            string fileName = Path.GetFileNameWithoutExtension(attachment == null ? "empty.png" : attachment.Name);

            if (String.IsNullOrEmpty(Path.GetExtension(filePath))) //原图
            {
                fileName += attachment.Ext;
            }
            else
            {
                fileName += Path.GetExtension(filePath);
            }

            this.ProcessResponseHeader(context, fileName, true);
            context.Response.TransmitFile(filePath);
        }
Ejemplo n.º 2
0
        public JObject Base64ImageFromFileID(HttpContext context)
        {
            YZRequest   request = new YZRequest(context);
            string      fileid  = request.GetString("fileid", null);
            ScaleMode   scale   = request.GetEnum <ScaleMode>("scale", ScaleMode.None);
            ImageFormat format  = YZImageHelper.ParseImageFormat(request.GetString("format", "png"));

            int width  = 0;
            int height = 0;

            if (scale != ScaleMode.None)
            {
                width  = request.GetInt32("width");
                height = request.GetInt32("height");
            }

            AttachmentInfo attachment;
            string         filePath = this.GetImageFileFromFileID(fileid, scale, width, height, format, out attachment);

            if (String.IsNullOrEmpty(filePath))
            {
                if (YZMimeMapping.isVedioFile(attachment.Ext))
                {
                    filePath = context.Server.MapPath("~/YZSoft/Attachment/img/EmptyImageAttachment.png");
                }
                else
                {
                    filePath = context.Server.MapPath("~/YZSoft/Attachment/img/EmptyImageAttachment.png");
                }
            }

            string fileName = Path.GetFileNameWithoutExtension(attachment.Name) + Path.GetExtension(filePath);

            Bitmap       bmp = new Bitmap(filePath);
            MemoryStream ms  = new MemoryStream();

            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();

            JObject rv = new JObject();

            rv["base64"] = Convert.ToBase64String(arr);
            return(rv);
        }
Ejemplo n.º 3
0
        public AttachmentInfo GetAttachmentInfo(HttpContext context)
        {
            YZRequest      request = new YZRequest(context);
            string         fileid  = request.GetString("fileid");
            AttachmentInfo rv;

            using (IYZDbProvider provider = YZDbProviderManager.DefaultProvider)
            {
                using (IDbConnection cn = provider.OpenConnection())
                {
                    rv = AttachmentManager.GetAttachmentInfo(provider, cn, fileid);
                }
            }

            rv.MimeType = YZMimeMapping.GetMimeType(rv.Ext);
            return(rv);
        }
Ejemplo n.º 4
0
        protected virtual string GetImageFile(AttachmentInfo attachment, ScaleMode scale, int width, int height, ImageFormat format)
        {
            if (attachment == null)
            {
                return(null);
            }

            string filePath;

            if (YZMimeMapping.isVedioFile(attachment.Ext))
            {
                filePath = this.GetVideoOutputFile(attachment, scale, width, height, format, null);
            }
            else
            {
                filePath = this.GetImageOutputFile(attachment, scale, width, height, format, null);
            }

            return(filePath);
        }
Ejemplo n.º 5
0
        protected virtual void ProcessResponseHeader(HttpContext context, string fileName, bool attachment)
        {
            string fileExt     = Path.GetExtension(fileName);
            string contentType = YZMimeMapping.GetMimeType(fileExt);

            fileName = context.Server.UrlEncode(fileName);
            fileName = fileName.Replace("%5b", "[");
            fileName = fileName.Replace("%5d", "]");

            context.Response.AppendHeader("Content-Type", contentType);
            if (attachment)
            {
                context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            }
            else
            {
                context.Response.AppendHeader("Content-Disposition", "filename=" + fileName);
            }

            context.Response.AppendHeader("Accept-Ranges", "bytes");
        }
Ejemplo n.º 6
0
        public virtual void Download(HttpContext context)
        {
            YZRequest request = new YZRequest(context);
            bool      osfile  = request.GetBool("osfile", false);

            //if (context.Request.Headers["If-None-Match"] != null || context.Request.Headers["If-Modified-Since"] != null)
            //{
            //    context.Response.Status = "304 Not Modified";
            //    context.Response.Cache.AppendCacheExtension("max-age=" + 365 * 24 * 60 * 60);
            //    context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
            //    context.Response.AppendHeader("ETag", "Never_Modify");
            //    context.Response.Cache.SetETag("Never_Modify");
            //    context.Response.Cache.SetLastModified(DateTime.Now.AddMinutes(-1));
            //    context.Response.End();
            //    return;
            //}

            string filePath;
            string fileName;
            long   fileSize;
            string fileExt;

            if (osfile)
            {
                string root = request.GetString("root");
                string path = request.GetString("path");
                fileName = request.GetString("name");
                string rootPath = context.Server.MapPath(YZSoft.FileSystem.OSDirectoryManager.GetRootPath(root));
                filePath = Path.Combine(rootPath, path, fileName);

                if (!File.Exists(filePath))
                {
                    throw new Exception(String.Format(Resources.YZStrings.Aspx_Upload_FileIDNotFount, fileName));
                }

                FileInfo fileInfo = new FileInfo(filePath);
                fileSize = fileInfo.Length;
                fileExt  = fileInfo.Extension;
            }
            else
            {
                string fileId = request.GetString("fileid");

                AttachmentInfo attachment;
                using (IYZDbProvider provider = YZDbProviderManager.DefaultProvider)
                {
                    using (IDbConnection cn = provider.OpenConnection())
                    {
                        attachment = AttachmentManager.GetAttachmentInfo(provider, cn, fileId);
                    }
                }

                fileName = attachment.Name;
                fileExt  = attachment.Ext;
                fileSize = attachment.Size;
                filePath = AttachmentInfo.FileIDToPath(fileId, AttachmentManager.AttachmentRootPath);

                if (!File.Exists(filePath))
                {
                    throw new Exception(String.Format(Resources.YZStrings.Aspx_Upload_FileIDNotFount, fileId));
                }
            }

            string fileExtNoDot = fileExt == null ? "" : fileExt.TrimStart('.');

            bool   contentDisposition = true;
            string range       = context.Request.Headers["Range"];
            string contentType = YZMimeMapping.GetMimeType(fileExt);

            context.Response.AppendHeader("Content-Type", contentType);

            if (contentDisposition)
            {
                context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode(fileName));
            }

            context.Response.AppendHeader("Accept-Ranges", "bytes");

            if (range == null)
            {
                FileInfo fileinfo = new FileInfo(filePath);

                //全新下载
                context.Response.AppendHeader("Content-Length", fileinfo.Length.ToString());
                //context.Response.CacheControl = HttpCacheability.Public.ToString();
                //context.Response.Cache.AppendCacheExtension("max-age=" + 365 * 24 * 60 * 60);
                //context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
                //context.Response.AppendHeader("ETag", "Never_Modify");
                //context.Response.Cache.SetETag("Never_Modify");
                //context.Response.Cache.SetLastModified(DateTime.Now.AddMinutes(-1));

                context.Response.TransmitFile(filePath);
            }
            else
            {
                //断点续传以及多线程下载支持
                string[] file_range = range.Substring(6).Split(new char[1] {
                    '-'
                });
                if (string.IsNullOrEmpty(file_range[0]))
                {
                    file_range[0] = "0";
                }
                if (string.IsNullOrEmpty(file_range[1]))
                {
                    file_range[1] = fileSize.ToString();
                }
                context.Response.Status = "206 Partial Content";
                context.Response.AppendHeader("Content-Range", "bytes " + file_range[0] + "-" + file_range[1] + "/" + fileSize.ToString());
                context.Response.AppendHeader("Content-Length", (Int32.Parse(file_range[1]) - Int32.Parse(file_range[0])).ToString());
                context.Response.TransmitFile(filePath, long.Parse(file_range[0]), (long)(Int32.Parse(file_range[1]) - Int32.Parse(file_range[0])));
            }
        }
Ejemplo n.º 7
0
        public virtual void Preview(HttpContext context)
        {
            YZRequest request = new YZRequest(context);
            bool      osfile  = request.GetBool("osfile", false);
            BPMObjectNameCollection supports = BPMObjectNameCollection.FromStringList(request.GetString("supports", null), ',');

            string filePath;
            string fileName;
            long   fileSize;
            string fileExt;

            if (osfile)
            {
                string root = request.GetString("root");
                string path = request.GetString("path");
                fileName = request.GetString("name");
                string rootPath = context.Server.MapPath(YZSoft.FileSystem.OSDirectoryManager.GetRootPath(root));
                filePath = Path.Combine(rootPath, path, fileName);

                if (!File.Exists(filePath))
                {
                    throw new Exception(String.Format(Resources.YZStrings.Aspx_Upload_FileIDNotFount, fileName));
                }

                FileInfo fileInfo = new FileInfo(filePath);
                fileSize = fileInfo.Length;
                fileExt  = fileInfo.Extension;
            }
            else
            {
                string fileId = request.GetString("fileid");

                AttachmentInfo attachment;
                using (IYZDbProvider provider = YZDbProviderManager.DefaultProvider)
                {
                    using (IDbConnection cn = provider.OpenConnection())
                    {
                        attachment = AttachmentManager.GetAttachmentInfo(provider, cn, fileId);
                    }
                }

                fileName = attachment.Name;
                fileExt  = attachment.Ext;
                fileSize = attachment.Size;
                filePath = AttachmentInfo.FileIDToPath(fileId, AttachmentManager.AttachmentRootPath);

                if (!File.Exists(filePath))
                {
                    throw new Exception(String.Format(Resources.YZStrings.Aspx_Upload_FileIDNotFount, fileId));
                }
            }

            string fileExtNoDot = fileExt == null ? "" : fileExt.TrimStart('.');

            //有请求格式并且请求格式非元文件格式
            if (supports.Count != 0 && !supports.Contains(fileExtNoDot))
            {
                //发现已有转换文件
                string existFile = null;
                foreach (string format in supports)
                {
                    string outputFile = Path.Combine(Path.GetDirectoryName(filePath), String.Format("{0}.{1}", Path.GetFileNameWithoutExtension(filePath), format));
                    if (File.Exists(outputFile))
                    {
                        existFile = outputFile;
                        break;
                    }
                }

                if (!String.IsNullOrEmpty(existFile))
                {
                    filePath = existFile;
                }
                else
                {
                    //转换文件
                    string         targetExt    = supports[0];
                    DocumentFormat targetFormat = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), targetExt, true);
                    string         outputFile   = Path.Combine(Path.GetDirectoryName(filePath), String.Format("{0}.{1}", Path.GetFileNameWithoutExtension(filePath), targetExt));
                    DocumentFormat srcFormat    = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), fileExtNoDot, true);

                    if (srcFormat == DocumentFormat.Pdf && targetFormat == DocumentFormat.Html)
                    {
                        YZSoft.Web.File.FileConvert.Pdf2Html(filePath);
                        filePath = outputFile;
                    }
                    else
                    {
                        DocumentConverterResult result = DocumentConverter.Convert(
                            new GleamTech.IO.BackSlashPath(filePath),
                            new InputOptions(srcFormat),
                            new GleamTech.IO.BackSlashPath(outputFile),
                            targetFormat
                            );
                        filePath = result.OutputFiles[0];
                    }
                }

                fileExt = Path.GetExtension(filePath);
            }

            string range       = context.Request.Headers["Range"];
            string contentType = YZMimeMapping.GetMimeType(fileExt);

            context.Response.AppendHeader("Content-Type", contentType);
            context.Response.AppendHeader("Accept-Ranges", "bytes");

            if (range == null)
            {
                FileInfo fileinfo = new FileInfo(filePath);

                //全新下载
                context.Response.AppendHeader("Content-Length", fileinfo.Length.ToString());
                //context.Response.CacheControl = HttpCacheability.Public.ToString();
                //context.Response.Cache.AppendCacheExtension("max-age=" + 365 * 24 * 60 * 60);
                //context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
                //context.Response.AppendHeader("ETag", "Never_Modify");
                //context.Response.Cache.SetETag("Never_Modify");
                //context.Response.Cache.SetLastModified(DateTime.Now.AddMinutes(-1));

                context.Response.TransmitFile(filePath);
            }
            else
            {
                //断点续传以及多线程下载支持
                string[] file_range = range.Substring(6).Split(new char[1] {
                    '-'
                });
                context.Response.Status = "206 Partial Content";
                context.Response.AppendHeader("Content-Range", "bytes " + file_range[0] + "-" + file_range[1] + "/" + fileSize.ToString());
                context.Response.AppendHeader("Content-Length", (Int32.Parse(file_range[1]) - Int32.Parse(file_range[0]) + 1).ToString());
                context.Response.TransmitFile(filePath, long.Parse(file_range[0]), (long)(Int32.Parse(file_range[1]) - Int32.Parse(file_range[0]) + 1));
            }
        }
Ejemplo n.º 8
0
        public object GetImageSize(HttpContext context)
        {
            YZRequest request = new YZRequest(context);
            string    fileid  = request.GetString("fileid");
            Size      size;

            AttachmentInfo attachment;

            using (IYZDbProvider provider = YZDbProviderManager.DefaultProvider)
            {
                using (IDbConnection cn = provider.OpenConnection())
                {
                    attachment = AttachmentManager.TryGetAttachmentInfo(provider, cn, fileid);
                }
            }

            if (attachment == null)
            {
                size = new Size(55, 47);
            }
            else
            {
                if (attachment.LParam1 != -1)
                {
                    size = new Size(attachment.LParam1 / 10000, attachment.LParam1 % 10000);
                }
                else
                {
                    string filePath = this.GetImageFile(attachment, ScaleMode.None, 0, 0, null);
                    if (!String.IsNullOrEmpty(filePath))
                    {
                        size = this.GetImageSize(filePath);
                        attachment.LParam1 = size.Width * 10000 + size.Height;

                        using (IYZDbProvider provider = YZDbProviderManager.DefaultProvider)
                        {
                            using (IDbConnection cn = provider.OpenConnection())
                            {
                                AttachmentManager.Update(provider, cn, attachment);
                            }
                        }
                    }
                    else
                    {
                        if (YZMimeMapping.isVedioFile(attachment.Ext))
                        {
                            size = new Size(55, 47);
                        }
                        else
                        {
                            size = new Size(55, 47);
                        }
                    }
                }
            }

            return(new {
                width = size.Width,
                height = size.Height
            });
        }