Example #1
0
        public static void RemoveTestFiles()
        {
            HashSet <string> files = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            using (var connection = SqlConnections.New(DbSettings.Current.ServerConnectionString, DbSettings.Current.Provider))
                using (var reader = SqlHelper.ExecuteReader(connection, "SELECT physical_name FROM sys.master_files"))
                {
                    while (reader.Read())
                    {
                        if (!reader.IsDBNull(0))
                        {
                            files.Add(Path.GetFileName(reader.GetString(0)));
                        }
                    }
                }

            foreach (var file in Directory.GetFiles(DbSettings.Current.RootPath, "test*.*"))
            {
                if (!files.Contains(Path.GetFileName(file)))
                {
                    TemporaryFileHelper.TryDelete(file);
                }
            }
        }
Example #2
0
        public byte[] Execute()
        {
            var exePath = UtilityExePath ?? HostingEnvironment.MapPath("~/App_Data/Reporting/wkhtmltopdf.exe");

            if (!File.Exists(exePath))
            {
                throw new InvalidOperationException(String.Format("Can't find wkhtmltopdf.exe which is required for PDF generation.\n" +
                                                                  "Please download a stable version from http://wkhtmltopdf.org/downloads.html\n and place it under directory '{0}'.",
                                                                  Path.GetDirectoryName(exePath)));
            }

            if (String.IsNullOrEmpty(this.Url))
            {
                throw new ArgumentNullException("url");
            }

            var args = new List <string>();

            args.Add(!SmartShrinking ? "--disable-smart-shrinking" : "--enable-smart-shrinking");

            if (!string.IsNullOrEmpty(PageSize))
            {
                args.Add("--page-size");
                args.Add(PageSize);
            }

            if (!string.IsNullOrEmpty(PageWidth))
            {
                args.Add("--page-width");
                args.Add(PageWidth);
            }

            if (!string.IsNullOrEmpty(PageHeight))
            {
                args.Add("--page-height");
                args.Add(PageHeight);
            }

            if (MarginLeft != null)
            {
                args.Add("--margin-left");
                args.Add(MarginLeft);
            }

            if (MarginTop != null)
            {
                args.Add("--margin-top");
                args.Add(MarginTop);
            }

            if (MarginRight != null)
            {
                args.Add("--margin-right");
                args.Add(MarginRight);
            }

            if (MarginBottom != null)
            {
                args.Add("--margin-bottom");
                args.Add(MarginBottom);
            }

            if (Dpi != null)
            {
                args.Add("--dpi");
                args.Add(Dpi.Value.ToString(CultureInfo.InvariantCulture));
            }

            if (Zoom != null)
            {
                args.Add("--zoom");
                args.Add(Zoom);
            }

            if (UsePrintMediaType)
            {
                args.Add("--print-media-type");
            }
            else
            {
                args.Add("--no-print-media-type");
            }

            if (PrintBackground)
            {
                args.Add("--background");
            }
            else
            {
                args.Add("--no-background");
            }

            if (FooterHtmlUrl != null)
            {
                args.Add("--footer-html");
                args.Add(FooterHtmlUrl);
            }

            if (Landscape)
            {
                args.Add("--orientation");
                args.Add("Landscape");
            }

            foreach (var cookie in Cookies)
            {
                args.Add("--cookie");
                args.Add(cookie.Key);
                args.Add(cookie.Value);
            }

            foreach (var replace in FooterHeaderReplace)
            {
                args.Add("--replace");
                args.Add(replace.Key);
                args.Add(replace.Value);
            }

            args.Add(this.Url);
            foreach (var additional in AdditionalUrls)
            {
                args.Add(additional);
            }

            foreach (var arg in CustomArgs)
            {
                args.Add(arg);
            }

            var tempFile = Path.GetTempFileName();

            try
            {
                args.Add(tempFile);

                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(exePath, CommandLineTools.EscapeArguments(args.ToArray()))
                    {
                        UseShellExecute = false,
                        CreateNoWindow  = true
                    }
                };

                if (!process.Start())
                {
                    throw new InvalidOperationException("An error occured while starting PDF generator!");
                }

                if (!process.WaitForExit(TimeoutSeconds * 1000)) // max 300 seconds
                {
                    throw new InvalidOperationException("Timeout while PDF generation!");
                }

                if (process.ExitCode != 0 && process.ExitCode != 1)
                {
                    throw new InvalidOperationException(String.Format("PDF generator returned error code {0}!", process.ExitCode));
                }

                if (!File.Exists(tempFile))
                {
                    throw new InvalidOperationException("Can't find generatored PDF file!");
                }

                var bytes = File.ReadAllBytes(tempFile);
                if (bytes.Length == 0)
                {
                    throw new InvalidOperationException("Generated PDF file is empty!");
                }

                return(bytes);
            }
            finally
            {
                TemporaryFileHelper.TryDelete(tempFile);
            }
        }
Example #3
0
        public bool ProcessStream(Stream fileContent, string extension)
        {
            extension = extension.TrimToEmpty().ToLowerInvariant();
            if (IsDangerousExtension(extension))
            {
                ErrorMessage = "Unsupported file extension!";
                return(false);
            }

            CheckResult  = ImageCheckResult.InvalidImage;
            ErrorMessage = null;
            ImageWidth   = 0;
            ImageHeight  = 0;
            IsImage      = false;

            var success = false;

            var temporaryPath = UploadHelper.TemporaryPath;

            Directory.CreateDirectory(temporaryPath);
            TemporaryFileHelper.PurgeDirectoryDefault(temporaryPath);
            string baseFileName = System.IO.Path.Combine(temporaryPath, Guid.NewGuid().ToString("N"));

            try
            {
                try
                {
                    if (IsImageExtension(extension))
                    {
                        IsImage  = true;
                        success  = true;
                        FilePath = baseFileName + extension;
                        fileContent.Seek(0, SeekOrigin.Begin);
                        using (FileStream fs = new FileStream(FilePath, FileMode.Create))
                        {
                            fileContent.CopyTo(fs);
                            FileSize = fs.Length;
                        }
                        success = ProcessImageStream(fileContent, extension);
                    }
                    else
                    {
                        FilePath = baseFileName + extension;
                        fileContent.Seek(0, SeekOrigin.Begin);
                        using (FileStream fs = new FileStream(FilePath, FileMode.Create))
                        {
                            fileContent.CopyTo(fs);
                            FileSize = fs.Length;
                        }
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    ErrorMessage = ex.Message;
                    success      = false;
                    return(success);
                }
            }
            finally
            {
                if (!success)
                {
                    if (!ThumbFile.IsNullOrEmpty())
                    {
                        TemporaryFileHelper.TryDelete(ThumbFile);
                    }

                    if (!FilePath.IsNullOrEmpty())
                    {
                        TemporaryFileHelper.TryDelete(FilePath);
                    }
                }

                fileContent.Dispose();
            }

            return(success);
        }
Example #4
0
 public static void DeleteDb(string path)
 {
     TemporaryFileHelper.TryDelete(path);
     TemporaryFileHelper.TryDelete(Path.ChangeExtension(path, ".ldf"));
 }
Example #5
0
        public byte[] Execute()
        {
            var exePath = UtilityExePath ?? HostingEnvironment.MapPath("~/Reports/Utility/wkhtmltopdf.exe");

            if (!File.Exists(exePath))
            {
                throw new InvalidOperationException(String.Format("Can't find wkhtmltopdf.exe which is required for PDF generation at location: '{0}'!", exePath));
            }

            if (String.IsNullOrEmpty(this.Url))
            {
                throw new ArgumentNullException("url");
            }

            var args = new List <string>();

            if (UsePrintMediaType)
            {
                args.Add("--print-media-type");
            }
            else
            {
                args.Add("--no-print-media-type");
            }

            foreach (var cookie in Cookies)
            {
                args.Add("--cookie");
                args.Add(cookie.Key);
                args.Add(cookie.Value);
            }

            args.Add(this.Url);
            foreach (var additional in AdditionalUrls)
            {
                args.Add(additional);
            }

            var tempFile = Path.GetTempFileName();

            try
            {
                args.Add(tempFile);

                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(exePath, CommandLineTools.EscapeArguments(args.ToArray()))
                    {
                        UseShellExecute = false,
                        CreateNoWindow  = true
                    }
                };

                if (!process.Start())
                {
                    throw new InvalidOperationException("An error occured while starting PDF generator!");
                }

                if (!process.WaitForExit(TimeoutSeconds * 1000)) // max 300 seconds
                {
                    throw new InvalidOperationException("Timeout while PDF generation!");
                }

                if (process.ExitCode != 0 && process.ExitCode != 1)
                {
                    throw new InvalidOperationException(String.Format("PDF generator returned error code {0}!", process.ExitCode));
                }

                if (!File.Exists(tempFile))
                {
                    throw new InvalidOperationException("Can't find generatored PDF file!");
                }

                var bytes = File.ReadAllBytes(tempFile);
                if (bytes.Length == 0)
                {
                    throw new InvalidOperationException("Generated PDF file is empty!");
                }

                return(bytes);
            }
            finally
            {
                TemporaryFileHelper.TryDelete(tempFile);
            }
        }
        public byte[] Execute()
        {
            var exePath = UtilityExePath ?? HostingEnvironment.MapPath("~/Reports/Utility/wkhtmltopdf.exe");

            if (!File.Exists(exePath))
            {
                throw new InvalidOperationException(String.Format("PDF üretimi için gereken wkhtmltopdf.exe dosyası '{0}' konumunda bulunamadı'", exePath));
            }

            if (String.IsNullOrEmpty(this.Url))
            {
                throw new ArgumentNullException("url");
            }

            var args = new List <string>();

            if (UsePrintMediaType)
            {
                args.Add("--print-media-type");
            }
            else
            {
                args.Add("--no-print-media-type");
            }

            foreach (var cookie in Cookies)
            {
                args.Add("--cookie");
                args.Add(cookie.Key);
                args.Add(cookie.Value);
            }

            args.Add(this.Url);
            foreach (var additional in AdditionalUrls)
            {
                args.Add(additional);
            }

            var tempFile = Path.GetTempFileName();

            try
            {
                args.Add(tempFile);

                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(exePath, CommandLineTools.EscapeArguments(args.ToArray()))
                    {
                        UseShellExecute = false,
                        CreateNoWindow  = true
                    }
                };

                if (!process.Start())
                {
                    throw new InvalidOperationException("PDF üretim programı başlatılırken hata oluştu!");
                }

                if (!process.WaitForExit(TimeoutSeconds * 1000)) // max 300 sn de üretmeli
                {
                    throw new InvalidOperationException("PDF üretilirken zaman aşımı oluştu!");
                }

                if (process.ExitCode != 0 && process.ExitCode != 1)
                {
                    throw new InvalidOperationException(String.Format("PDF üretim programı {0} hata kodu döndürdü!", process.ExitCode));
                }

                if (!File.Exists(tempFile))
                {
                    throw new InvalidOperationException("PDF üretimi sonrasında geçici dosya bulunamadı!");
                }

                var bytes = File.ReadAllBytes(tempFile);
                if (bytes.Length == 0)
                {
                    throw new InvalidOperationException("Üretilen PDF dosyası boş!");
                }

                return(bytes);
            }
            finally
            {
                TemporaryFileHelper.TryDelete(tempFile);
            }
        }