Subversion Repository Public Repository

ChrisCompleteCodeTrunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace PremierMigration
{
    public class ConversionBuilder
    {
        public static string ConversionCommandMask = "magick.exe convert {0} -compress Fax output {1}";
        public List<DocuwareIdPath> DocuwareIdPaths { get; set; }
        public String Cabinet { get; set; }
        public String ExportPath { get; set; }
        public Guid ExportBatchId { get; set; }

        public ConversionBuilder(List<DocuwareIdPath> docuwareIdPaths, String cabinet, String exportPath, Guid exportBatchId)
        {
            DocuwareIdPaths = docuwareIdPaths;
            Cabinet = cabinet;
            ExportPath = exportPath;
            ExportBatchId = exportBatchId;
        }

        public void Run()
        {
            foreach (DocuwareIdPath docuwareIdPath in DocuwareIdPaths)
            {
                string files = String.Empty;

                foreach (FileInfo fileInfo in docuwareIdPath.GetCalulatedPages())
                {
                    if (IsGraphicsFile(fileInfo.FullName))
                    {
                        files += "\"" + fileInfo.FullName + "\" ";
                    }
                    else
                    {
                        OleDbConnection oleDbConnection = Program.GetNewOleDbConnection();
                        OleDbCommand oleDbCommand = new OleDbCommand("INSERT INTO [ExportLog].[dbo].[ExportError] ([ExportBatchId],[Docuware46Id],[ErrorReason],[DateCompleted],[Page]) VALUES (?, ?, 'Not a valid graphics file.', GETDATE(), ?);", oleDbConnection);
                        oleDbCommand.CommandType = CommandType.Text;
                        oleDbCommand.CommandText = "INSERT INTO [ExportLog].[dbo].[ExportError] ([ExportBatchId],[Docuware46Id],[ErrorReason],[DateCompleted],[Page]) VALUES (?, ?, 'Not a valid graphics file.', GETDATE(), ?);";
                        oleDbCommand.Parameters.AddWithValue("@ExportBatchId", ExportBatchId);
                        oleDbCommand.Parameters.AddWithValue("@DWDOCID", docuwareIdPath.DWDOCID);
                        oleDbCommand.Parameters.AddWithValue("@Page", fileInfo.FullName);
                        oleDbCommand.ExecuteNonQuery();
                        oleDbCommand.Dispose();
                        oleDbConnection.Close();
                        oleDbConnection.Dispose();
                    }
                }
                files = files.TrimEnd(new char[] { ' ' });
                docuwareIdPath.ConversionCommand = String.Format(
                    ConversionCommandMask,
                    new object[]
                    {
                        files.TrimEnd(new char[] { ' ' }),
                        /*
                        ConfigurationManager.AppSettings["IM_DENSITY"].ToString(),
                        ConfigurationManager.AppSettings["IM_RESAMPLE"].ToString(),
                        ConfigurationManager.AppSettings["IM_QUALITY"].ToString(),
                        ConfigurationManager.AppSettings["IM_RESIZE"].ToString(),
                        */
                        "\"" + Path.Combine(
                            ExportPath,
                            Cabinet + "_"  + docuwareIdPath.PageCount.ToString() + "_" + docuwareIdPath.DWDOCID.ToString() + ".pdf"
                        ).Trim() + "\""
                    }
                );
                docuwareIdPath.ExportPdfFilePath = Path.Combine(ExportPath, Cabinet + "_" + docuwareIdPath.PageCount.ToString() + "_" + docuwareIdPath.DWDOCID.ToString() + ".pdf").Trim();
            }
        }

        public bool IsGraphicsFile(string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (!fileInfo.Exists)
                return false;
            Image image = null;
            try
            {
                image = Image.FromFile(filePath);
            }
            catch
            {
                if (IsPdf(filePath))
                    return true;
                return false;
            }
            if (image != null)
                image.Dispose();
            return true;
        }

        bool IsPdf(string path)
        {
            var pdfString = "%PDF-";
            var pdfBytes = Encoding.ASCII.GetBytes(pdfString);
            var len = pdfBytes.Length;
            var buf = new byte[len];
            var remaining = len;
            var pos = 0;
            using (var f = File.OpenRead(path))
            {
                while (remaining > 0)
                {
                    var amtRead = f.Read(buf, pos, remaining);
                    if (amtRead == 0) return false;
                    remaining -= amtRead;
                    pos += amtRead;
                }
            }
            return pdfBytes.SequenceEqual(buf);
        }

    }
}

Commits for ChrisCompleteCodeTrunk/PremierMigration/PremierMigration/ConversionBuilder.cs

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000