public static void ConvertHandler(HttpContext context)
        {
            DocumentConverterResult result;

            try
            {
                var inputDocument  = new BackSlashPath(ExamplesConfiguration.UnprotectString(context.Request["inputDocument"]));
                var outputFormat   = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), context.Request["outputFormat"]);
                var fileName       = inputDocument.FileNameWithoutExtension + "." + DocumentFormatInfo.Get(outputFormat).DefaultExtension;
                var outputPath     = ConvertedPath.Append(context.Session.SessionID).Append(fileName);
                var outputDocument = outputPath.Append(fileName);

                if (Directory.Exists(outputPath))
                {
                    Directory.Delete(outputPath, true);
                }
                Directory.CreateDirectory(outputPath);
                result = DocumentConverter.Convert(inputDocument, outputDocument, outputFormat);
            }
            catch (Exception exception)
            {
                context.Response.Write("<span style=\"color: red; font-weight: bold\">Conversion failed</span><br/>");
                context.Response.Write(exception.Message);
                return;
            }

            context.Response.Write("<span style=\"color: green; font-weight: bold\">Conversion successful</span>");
            context.Response.Write("<br/>Conversion time: " + result.ElapsedTime);
            context.Response.Write("<br/>Output files:");

            if (result.OutputFiles.Length > 1)
            {
                context.Response.Write(" - " + GetZipDownloadLink(new FileInfo(result.OutputFiles[0]).Directory));
            }

            context.Response.Write("<br/><ol>");
            foreach (var outputFile in result.OutputFiles)
            {
                if (outputFile.EndsWith("\\"))
                {
                    var directoryInfo = new DirectoryInfo(outputFile);
                    context.Response.Write(string.Format(
                                               "<br/><li><b>{0}\\</b> - {1}</li>",
                                               directoryInfo.Name,
                                               GetZipDownloadLink(directoryInfo))
                                           );
                }
                else
                {
                    var fileInfo = new FileInfo(outputFile);
                    context.Response.Write(string.Format(
                                               "<br/><li><b>{0}</b> ({1} bytes) - {2}</li>",
                                               fileInfo.Name,
                                               fileInfo.Length,
                                               GetDownloadLink(fileInfo))
                                           );
                }
            }
            context.Response.Write("<br/></ol>");
        }
Exemplo n.º 2
0
        private Document GetCurrentDocument()
        {
            var dao       = new DocumentDao();
            var converter = new DocumentConverter();
            var document  = converter.Convert(dao.GetByDraftee(DrafteeId));

            if (document == null)
            {
                document           = new Document();
                document.DrafteeId = DrafteeId;
            }
            return(document);
        }
Exemplo n.º 3
0
        public void UsesDocumentWriterFactory()
        {
            // Background: Proves that the converter uses the factory
            // Arrange
            var factory   = Substitute.For <IDocumentWriterFactory>();
            var converter = new DocumentConverter(factory);
            var input     = new MemoryStream();
            var output    = new MemoryStream();

            // Act
            converter.Convert(input, output);

            // Assert
            factory.Received(1).Create(output);
        }
        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            FillData(GetCurrentDraftee());
            var dao       = new DocumentDao();
            var converter = new DocumentConverter();
            var document  = converter.Convert(dao.GetByDraftee(DrafteeId));

            if (document != null)
            {
                Document = document;
            }
            else
            {
                Document           = new Document();
                Document.DrafteeId = DrafteeId;
            }
        }
Exemplo n.º 5
0
        private void Act(string input, Action <IDocumentWriter> assert)
        {
            var factory      = Substitute.For <IDocumentWriterFactory>();
            var writer       = Substitute.For <IDocumentWriter>();
            var converter    = new DocumentConverter(factory);
            var inputStream  = new MemoryStream();
            var outputStream = new MemoryStream();

            factory.Create(outputStream).Returns(writer);

            using (var streamWriter = new StreamWriter(inputStream, leaveOpen: true))
            {
                streamWriter.Write(input);
            }

            inputStream.Position = 0;

            converter.Convert(inputStream, outputStream);

            assert(writer);
        }
Exemplo n.º 6
0
        public static string Excel2Html(Stream stream, string ext)
        {
            string         fileExtNoDot = ext == null ? "" : ext.TrimStart('.');
            string         folder       = Path.Combine(AttachmentManager.AttachmentRootPath, "temp");
            string         fileName     = "Excel_" + Guid.NewGuid().ToString();
            DocumentFormat excelFormat  = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), fileExtNoDot, true);

            string excelFile = Path.Combine(folder, String.Format("{0}.{1}", fileName, fileExtNoDot));

            Directory.CreateDirectory(folder);

            try
            {
                using (FileStream filestream = System.IO.File.Create(excelFile))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.CopyTo(filestream);
                }

                DocumentConverterResult result = DocumentConverter.Convert(
                    new GleamTech.IO.BackSlashPath(excelFile),
                    new InputOptions(excelFormat),
                    DocumentFormat.Html
                    );

                string htmlFile = result.OutputFiles[0];

                for (int i = 1; i < result.OutputFiles.Length; i++)
                {
                    System.IO.File.Delete(result.OutputFiles[i]);
                }

                return(htmlFile);
            }
            finally
            {
                System.IO.File.Delete(excelFile);
            }
        }
Exemplo 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));
            }
        }