예제 #1
0
        protected long CopyCoverToSubBuffer(string coverFile, string subBuffer, int fileIndex, int archiveNb)
        {
            long coverSize = 0;

            if (string.IsNullOrWhiteSpace(coverFile))
            {
                return(coverSize);
            }
            FileInfo coverInfo = new FileInfo(coverFile);
            string   destFile  = Path.Combine(subBuffer, coverInfo.Name);

            using (Bitmap bitmap = (Bitmap)Image.FromFile(coverFile))
            {
                if (Settings.Instance.AddFileIndexToCovers)
                {
                    string issueText = $"{fileIndex.ToString().PadLeft(archiveNb.ToString().Length, '0')}/{archiveNb}";

                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        using (Font arialFont = new Font("Arial", 220f, FontStyle.Regular, GraphicsUnit.Point))
                        {
                            SizeF size = graphics.MeasureString(issueText, arialFont);
                            using (SolidBrush whiteBrush = new SolidBrush(Color.FromArgb(200, 255, 255, 255)))
                            {
                                graphics.FillRectangle(whiteBrush, 5f, 5f, size.Width + 10f, size.Height + 10f);
                            }


                            graphics.DrawString(issueText, arialFont, Brushes.Black, 10f, 10f);
                        }
                    }
                }

                JpgConverter jpgConverter = new JpgConverter(_logger, 80);
                jpgConverter.SaveJpeg(bitmap, destFile);
            }

            coverInfo = new FileInfo(destFile);
            coverSize = coverInfo.Length;
            return(coverSize);
        }
예제 #2
0
        protected bool MovePicturesToSubBuffer(string destFolder, List <FileInfo> files, string archiveName, bool isFirstArchive, long imageCompression)
        {
            bool result        = true;
            int  increaseIndex = 1;

            if (Settings.Instance.IncludeCover)
            {
                increaseIndex = isFirstArchive ? 1 : 2;
            }
            _logger.Log($"Copy the selected files in {destFolder}");
            try
            {
                Directory.CreateDirectory(destFolder);
                int          padSize      = Math.Max(2, files.Count.ToString().Length);
                JpgConverter jpgConverter = new JpgConverter(_logger, imageCompression);
                for (int i = 0; i < files.Count; ++i)
                {
                    string destFile = Path.Combine(destFolder, $"{archiveName}_{(i + increaseIndex).ToString().PadLeft(padSize, '0')}{files[i].Extension}".Replace(' ', '_'));
                    if (imageCompression == 100)
                    {
                        // rename the files in the directories
                        File.Move(files[i].FullName, destFile);
                    }
                    else
                    {
                        jpgConverter.SaveJpeg(files[i].FullName, destFile);
                        File.Delete(files[i].FullName);
                    }
                }
            }
            catch (Exception e)
            {
                result = false;
                _logger.Log($"ERROR: Cannot split archive {e.Message}");
            }
            return(result);
        }
예제 #3
0
        public void Merge(string outputFile, IList <string> files, long imageQuality)
        {
            _eventAggregator.GetEvent <BusinessEvent>().Publish(true);
            _logger.Log($"Check input validity vs settings");
            FileInfo fi                = new FileInfo(outputFile);
            string   fileName          = fi.Name;
            string   settingsExtension = $".{Settings.Instance.OutputFormat.ToString().ToLower()}";

            if (string.IsNullOrEmpty(fi.Extension))
            {
                _logger.Log($"Add Extension to filename {settingsExtension}");
                outputFile += settingsExtension;
            }
            else
            {
                if (fi.Extension != settingsExtension)
                {
                    _logger.Log($"Incorrect extension found in filename {fi.Extension} replaced with {settingsExtension}");
                    outputFile = outputFile.Substring(0, outputFile.Length - fi.Extension.Length) + settingsExtension;
                }
            }
            _logger.Log($"Start merging input archives in {outputFile}");
            string nameTemplate = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length);
            string bufferPath   = Settings.Instance.GetBufferDirectory(files[0], nameTemplate);
            string outputBuffer = Path.Combine(bufferPath, "outputBuffer");

            Directory.CreateDirectory(outputBuffer);
            List <FileInfo> metadataFiles = new List <FileInfo>();
            List <FileInfo> pages         = new List <FileInfo>();
            int             bufferPadSize = files.Count.ToString().Length;

            for (int i = 0; i < files.Count; ++i)
            {
                CompressionHelper ch1 = new CompressionHelper(_logger);
                string            decompressionBuffer = Path.Combine(bufferPath, $"archive_{i.ToString().PadLeft(bufferPadSize, '0')}");
                _logger.Log($"Uncompress {files[i]} in {decompressionBuffer}");
                ch1.DecompressToDirectory(files[i], decompressionBuffer);
                _logger.Log($"Extraction done.");
                ParseArchiveFiles(decompressionBuffer, ref metadataFiles, ref pages);
            }
            if (Settings.Instance.IncludeMetadata)
            {
                if (metadataFiles.Count > 0)
                {
                    string destFile = Path.Combine(outputBuffer, metadataFiles[0].Name);
                    _logger.Log($"copy metadata {metadataFiles[0].FullName}  in {destFile}");
                    File.Copy(metadataFiles[0].FullName, destFile, true);
                }
            }
            int          pagePadSize  = pages.Count.ToString().Length;
            int          pageAdded    = 1;
            JpgConverter jpgConverter = new JpgConverter(_logger, imageQuality);

            for (int i = 0; i < pages.Count; ++i)
            {
                if (pages[i].Extension != ".xml")
                {
                    string destFile = Path.Combine(outputBuffer, $"{nameTemplate}_{pageAdded.ToString().PadLeft(pagePadSize, '0')}{pages[i].Extension}".Replace(' ', '_'));
                    if (imageQuality == 100)
                    {
                        // rename the files in the directories
                        File.Move(pages[i].FullName, destFile);
                    }
                    else
                    {
                        jpgConverter.SaveJpeg(pages[i].FullName, destFile);
                        File.Delete(pages[i].FullName);
                    }
                    ++pageAdded;
                }
            }
            CompressionHelper ch2 = new CompressionHelper(_logger);

            _logger.Log($"Start compression of {outputBuffer} into {outputFile} ...");
            ch2.CompressDirectoryContent(outputBuffer, outputFile);
            _logger.Log($"Compression done.");
            _logger.Log($"Clean Buffer {bufferPath}");
            SystemTools.CleanDirectory(bufferPath, _logger);
            _logger.Log("Done.");
            _eventAggregator.GetEvent <BusinessEvent>().Publish(false);
        }