Beispiel #1
0
        private void importSelectionsAndParameters(ArchiveTemplate archTemp)
        {
            List <Entity> sels = new List <Entity>();

            foreach (ArchiveSelection archSel in archTemp.Selections)
            {
                Selection sel = new Selection(archSel);
                sel.TaskTemplateID = archTemp.ID;
                sels.Add(sel);
            }
            DatabaseManager.SharedManager.insertMultipleEntities(sels);
            foreach (ArchiveSelection archSel in archTemp.Selections)
            {
                archSel.ID = sels[archTemp.Selections.IndexOf(archSel)].ID;
            }
            List <Entity> pars = new List <Entity>();

            foreach (ArchiveParameter archPar in archTemp.Parameters)
            {
                Parameter par = new Parameter(archPar);
                par.TaskTemplateID = archTemp.ID;
                pars.Add(par);
            }
            DatabaseManager.SharedManager.insertMultipleEntities(pars);
            foreach (ArchiveParameter archPar in archTemp.Parameters)
            {
                archPar.ID = pars[archTemp.Parameters.IndexOf(archPar)].ID;
            }
        }
Beispiel #2
0
        private void importTemplate(ArchiveTemplate archTemp, int taskId)
        {
            TaskTemplate template = new TaskTemplate(archTemp);

            template.TaskID = taskId;
            template.save();
            archTemp.ID = template.ID;
            importSelectionsAndParameters(archTemp);
        }
Beispiel #3
0
        protected string ExtractArchive(string filePath, ArchiveTemplate archiveTemplate)
        {
            string            pathToBuffer = Settings.Instance.GetBufferDirectory(filePath, archiveTemplate.ComicName);
            CompressionHelper ch           = new CompressionHelper(_logger);

            _logger.Log($"Start extraction of {filePath} into {pathToBuffer} ...");
            ch.DecompressToDirectory(filePath, pathToBuffer);
            _logger.Log($"Extraction done.");
            return(pathToBuffer);
        }
Beispiel #4
0
        protected void CompressArchiveContent(string directory, ArchiveTemplate archiveTemplate)
        {
            string archiveExtension = $".{Settings.Instance.OutputFormat.ToString().ToLower()}";

            DirectoryInfo     di         = new DirectoryInfo(directory);
            string            outputFile = Path.Combine(archiveTemplate.OutputDir, $"{di.Name}{archiveExtension}");
            CompressionHelper ch         = new CompressionHelper(_logger);

            _logger.Log($"Start compression of {directory} into {outputFile} ...");
            ch.CompressDirectoryContent(directory, outputFile);
            _logger.Log($"Compression done.");
        }
Beispiel #5
0
        private string BuildSplittedArchive(ArchiveTemplate template, int fileIndex, ref int sourcePageIndex)
        {
            // Create the subBuffer
            string subBufferPath = Path.Combine(template.PathToBuffer, $"{template.ComicName}_{(fileIndex + 1).ToString().PadLeft(template.IndexSize, '0')}");

            _logger.Log($"Create the subFolder {subBufferPath}");
            int             pagesAdded = 0;
            List <FileInfo> pagesToAdd = new List <FileInfo>();

            for (int currentPageIndex = sourcePageIndex; (pagesAdded < template.PagesPerFile) && (currentPageIndex < template.Pages.Count); ++currentPageIndex)
            {
                if (template.Pages[currentPageIndex].Extension != ".xml")
                {
                    pagesToAdd.Add(template.Pages[currentPageIndex]);
                    ++pagesAdded;
                }
                sourcePageIndex = currentPageIndex + 1;
            }
            if (fileIndex == template.NumberOfSplittedFiles - 1)
            {
                for (int i = sourcePageIndex; i < template.Pages.Count; ++i)
                {
                    if (template.Pages[i].Extension != ".xml")
                    {
                        pagesToAdd.Add(template.Pages[i]);
                    }
                }
            }
            bool ok = MovePicturesToSubBuffer(subBufferPath, pagesToAdd, template.ComicName, fileIndex == 0, template.ImageCompression);

            if (!ok)
            {
                SystemTools.CleanDirectory(subBufferPath, _logger);
                return("");
            }
            if (!string.IsNullOrWhiteSpace(template.CoverPath))
            {
                if (fileIndex != 0)
                {
                    CopyCoverToSubBuffer(template.CoverPath, subBufferPath, fileIndex + 1, (int)template.NumberOfSplittedFiles);
                }
            }
            if (Settings.Instance.IncludeMetadata)
            {
                CopyMetaDataToSubBuffer(template.MetadataFiles, subBufferPath);
            }
            return(subBufferPath);
        }
        private void DoSplit()
        {
            ArchiveTemplate arctemp = new ArchiveTemplate()
            {
                ComicName               = NameTemplate,
                OutputDir               = OutputDir,
                NumberOfSplittedFiles   = FileNb,
                MaxPagesPerSplittedFile = MaxFilePerArchive,
                MaxSizePerSplittedFile  = MaxFileSize,
                PagesIndexToSplit       = _pagesToSplitIndex,
                ImageCompression        = ImageQuality
            };
            var splitter = _container.Resolve <ISplitter>(SelectedStyle);

            Task.Run(() => splitter.Split(FileToSplit, arctemp));
        }
Beispiel #7
0
        private int ComputeNumberOfSplittedFiles(int totalPagesCount, ArchiveTemplate template)
        {
            int numberOfSplittedFiles = 0;
            int remainder             = 0;

            if (Settings.Instance.IncludeCover)
            {
                numberOfSplittedFiles = Math.DivRem(totalPagesCount - 1, (int)template.MaxPagesPerSplittedFile - 1, out remainder);
            }
            else
            {
                numberOfSplittedFiles = Math.DivRem(totalPagesCount, (int)template.MaxPagesPerSplittedFile, out remainder);
            }
            if (remainder > 0)
            {
                ++numberOfSplittedFiles;
            }
            return(numberOfSplittedFiles);
        }
Beispiel #8
0
 protected string GetSubBufferPath(ArchiveTemplate template, int fileIndex)
 {
     return(Path.Combine(template.PathToBuffer, $"{template.ComicName}_{(fileIndex + 1).ToString().PadLeft(template.IndexSize, '0')}"));
 }
Beispiel #9
0
        public void Split(string filePath, ArchiveTemplate archiveTemplate)
        {
            _eventAggregator.GetEvent <BusinessEvent>().Publish(true);
            if (archiveTemplate.NumberOfSplittedFiles < 2)
            {
                _logger.Log($"Cannot split archive in {archiveTemplate.NumberOfSplittedFiles} files");
                _eventAggregator.GetEvent <BusinessEvent>().Publish(false);
                return;
            }
            //Extract file in buffer
            string pathToBuffer = ExtractArchive(filePath, archiveTemplate);

            //Count files in directory except metadata
            ParseArchiveFiles(pathToBuffer, out List <FileInfo> metadataFiles, out List <FileInfo> pages);
            int totalPagesCount = pages.Count - metadataFiles.Count;

            _logger.Log($"Total number of pages is {totalPagesCount}");
            // Check that the resulting split files number is consistent with the number of pages
            if (archiveTemplate.NumberOfSplittedFiles > totalPagesCount)
            {
                _logger.Log($"Not enough pages to split into {archiveTemplate.NumberOfSplittedFiles} files.");
                SystemTools.CleanDirectory(pathToBuffer, _logger);
                _eventAggregator.GetEvent <BusinessEvent>().Publish(false);
                return;
            }
            //Create one folder per resulting file and copy pictures in it
            int pagesPerFile = Math.DivRem(totalPagesCount, (int)archiveTemplate.NumberOfSplittedFiles, out int extraPages);

            _logger.Log($"Pages per resulting file : {pagesPerFile}");

            int    indexSize = Math.Max(archiveTemplate.NumberOfSplittedFiles.ToString().Length, 2);
            string coverPath = "";

            if (Settings.Instance.IncludeCover)
            {
                coverPath = SaveCoverInBuffer(pathToBuffer, archiveTemplate.ComicName, indexSize, pages);
            }
            int sourcePageIndex = 0;

            for (int fileIndex = 0; fileIndex < archiveTemplate.NumberOfSplittedFiles; ++fileIndex)
            {
                archiveTemplate.PathToBuffer  = pathToBuffer;
                archiveTemplate.IndexSize     = indexSize;
                archiveTemplate.PagesPerFile  = pagesPerFile;
                archiveTemplate.Pages         = pages;
                archiveTemplate.MetadataFiles = metadataFiles;
                archiveTemplate.CoverPath     = coverPath;

                string splittedContentPath = BuildSplittedArchive(archiveTemplate, fileIndex, ref sourcePageIndex);
                if (string.IsNullOrWhiteSpace(splittedContentPath))
                {
                    _logger.Log("ERROR: Failure to split the file");
                    break;
                }
                _logger.Log($"Compress {splittedContentPath}");
                CompressArchiveContent(splittedContentPath, archiveTemplate);
                _logger.Log($"Clean Buffer {splittedContentPath}");
                SystemTools.CleanDirectory(splittedContentPath, _logger);
            }
            _logger.Log($"Clean Buffer {pathToBuffer}");
            SystemTools.CleanDirectory(pathToBuffer, _logger);
            // compress the resulting file
            // clean the temp directories
            _logger.Log("Done.");
            _eventAggregator.GetEvent <BusinessEvent>().Publish(false);
        }
Beispiel #10
0
        public void Split(string filePath, ArchiveTemplate archiveTemplate)
        {
            _eventAggregator.GetEvent <BusinessEvent>().Publish(true);
            if (archiveTemplate.MaxPagesPerSplittedFile < 2)
            {
                _logger.Log($"Cannot split archive with {archiveTemplate.MaxPagesPerSplittedFile} page per file");
                _eventAggregator.GetEvent <BusinessEvent>().Publish(false);
                return;
            }
            //Extract file in buffer
            archiveTemplate.PathToBuffer = ExtractArchive(filePath, archiveTemplate);
            //Count files in directory except metadata
            ParseArchiveFiles(archiveTemplate.PathToBuffer, out List <FileInfo> metadataFiles, out List <FileInfo> pages);
            archiveTemplate.Pages         = pages;
            archiveTemplate.MetadataFiles = metadataFiles;
            int totalPagesCount = archiveTemplate.Pages.Count - archiveTemplate.MetadataFiles.Count;

            _logger.Log($"Total number of pages is {totalPagesCount}");
            int numberOfSplittedFiles = ComputeNumberOfSplittedFiles(totalPagesCount, archiveTemplate);

            _logger.Log($"Creating {numberOfSplittedFiles} splitted files");
            archiveTemplate.IndexSize = Math.Max(archiveTemplate.NumberOfSplittedFiles.ToString().Length, 2);
            archiveTemplate.CoverPath = "";
            if (Settings.Instance.IncludeCover)
            {
                archiveTemplate.CoverPath = SaveCoverInBuffer(archiveTemplate.PathToBuffer, archiveTemplate.ComicName, archiveTemplate.IndexSize, archiveTemplate.Pages);
            }

            int             pagesAdded    = 0;
            int             fileIndex     = 0;
            string          subBufferPath = "";
            List <FileInfo> pagesToAdd    = new List <FileInfo>();

            for (int i = 0; i < totalPagesCount; ++i)
            {
                if (pagesAdded == 0)
                {
                    pagesToAdd    = new List <FileInfo>();
                    subBufferPath = GetSubBufferPath(archiveTemplate, fileIndex);
                    Directory.CreateDirectory(subBufferPath);
                    if (Settings.Instance.IncludeMetadata)
                    {
                        CopyMetaDataToSubBuffer(archiveTemplate.MetadataFiles, subBufferPath);
                    }
                    if (fileIndex != 0)
                    {
                        if (Settings.Instance.IncludeCover)
                        {
                            CopyCoverToSubBuffer(archiveTemplate.CoverPath, subBufferPath, fileIndex + 1, (int)archiveTemplate.NumberOfSplittedFiles);
                            ++pagesAdded;
                        }
                    }
                }
                while (pagesAdded < archiveTemplate.MaxPagesPerSplittedFile && i < totalPagesCount)
                {
                    if (archiveTemplate.Pages[i].Extension != ".xml")
                    {
                        pagesToAdd.Add(archiveTemplate.Pages[i]);
                        ++pagesAdded;
                    }
                    ++i;
                }
                bool ok = MovePicturesToSubBuffer(subBufferPath, pagesToAdd, archiveTemplate.ComicName, fileIndex == 0, archiveTemplate.ImageCompression);
                if (!ok)
                {
                    SystemTools.CleanDirectory(subBufferPath, _logger);
                    break;
                }
                _logger.Log($"Compress {subBufferPath}");
                CompressArchiveContent(subBufferPath, archiveTemplate);
                _logger.Log($"Clean Buffer {subBufferPath}");
                SystemTools.CleanDirectory(subBufferPath, _logger);
                pagesAdded = 0;
                ++fileIndex;
            }
            _logger.Log($"Clean Buffer {archiveTemplate.PathToBuffer}");
            SystemTools.CleanDirectory(archiveTemplate.PathToBuffer, _logger);
            _logger.Log("Done.");
            _eventAggregator.GetEvent <BusinessEvent>().Publish(false);
        }
        public Archive generateArchiveSystem()
        {
            scenarioRelations.Clear();
            selectionRelations.Clear();
            taskSolverRelations.Clear();
            Archive       archive = new Archive();
            List <Entity> tasks   = models.Task.all(typeof(models.Task));

            foreach (models.Task task in tasks)
            {
                ArchiveTask         archTask  = new ArchiveTask(task);
                List <TaskTemplate> templates = TaskTemplate.templatesOfTaskId(task.ID);
                foreach (TaskTemplate template in templates)
                {
                    ArchiveTemplate  archTemp   = new ArchiveTemplate(template);
                    List <Selection> selections = Selection.selectionsOfTaskTemplateId(template.ID);
                    foreach (Selection sel in selections)
                    {
                        ArchiveSelection archSel = new ArchiveSelection(sel);
                        selectionRelations.Add(new RelationExport(sel.ID, archSel));
                        archTemp.Selections.Add(archSel);
                    }
                    List <Parameter> parameters = Parameter.parametersOfTaskTemplateId(template.ID);
                    foreach (Parameter par in parameters)
                    {
                        ArchiveParameter archPar = new ArchiveParameter(par);
                        archTemp.Parameters.Add(archPar);
                    }
                    archTask.Templates.Add(archTemp);
                }
                List <TaskSolver> solvers = TaskSolver.solversOfTaskId(task.ID);
                foreach (TaskSolver solver in solvers)
                {
                    ArchiveTaskSolver archSol = new ArchiveTaskSolver(solver);
                    taskSolverRelations.Add(new RelationExport(solver.ID, archSol));
                    archTask.Solvers.Add(archSol);
                }
                archive.Tasks.Add(archTask);
            }
            List <Entity> scenarios = LearningScenario.all(typeof(LearningScenario));

            foreach (LearningScenario scenario in scenarios)
            {
                ArchiveScenario archScenario = new ArchiveScenario(scenario);
                scenarioRelations.Add(new RelationExport(scenario.ID, archScenario));
                archive.Scenarios.Add(archScenario);
            }
            List <Entity> lSolvers = LearnedSolver.all(typeof(LearnedSolver));

            foreach (LearnedSolver solver in lSolvers)
            {
                ArchiveLearnedSolver   archSol   = new ArchiveLearnedSolver(solver);
                List <LearningQuality> quailties = LearningQuality.qualitiesOfSolverId(solver.ID);
                foreach (LearningQuality quality in quailties)
                {
                    ArchiveLearningQuality archQ = new ArchiveLearningQuality(quality);
                    archSol.Qualities.Add(archQ);
                }
                var find = taskSolverRelations.Find(x => x.ID == solver.TaskSolverID);
                archSol.TaskSolver = (find != null) ? (ArchiveTaskSolver)find.model : null;
                find = selectionRelations.Find(x => x.ID == solver.SelectionID);
                archSol.Selection = (find != null) ? (ArchiveSelection)find.model : null;
                find             = scenarioRelations.Find(x => x.ID == solver.LearningScenarioID);
                archSol.Scenario = (find != null) ? (ArchiveScenario)find.model : null;
                archive.LearnedSolvers.Add(archSol);
            }
            return(archive);
        }