コード例 #1
0
        public async Task <IActionResult> Get([FromQuery] string projectName, [FromQuery] Guid build1Id, [FromQuery] string locale1, [FromQuery] Guid build2Id, [FromQuery] string locale2, [FromQuery] string threshold, bool sliderIsDisabled)
        {
            ComparisonHelper   comparisonHelper = new ComparisonHelper();
            IList <Comparison> comparisons      = await comparisonHelper.GetComparisons(_unitOfWork, projectName, build1Id, locale1, build2Id, locale2);

            // Threshold is read as a percentage
            double dThreshold = Convert.ToDouble(threshold) / 100;

            List <ScreenInBuild> leftScreens  = new List <ScreenInBuild>();
            List <ScreenInBuild> rightScreens = new List <ScreenInBuild>();
            //List<string> diffScreens = new List<string>();
            Dictionary <string, string> diffScreens = new Dictionary <string, string>();

            foreach (var screen in comparisons)
            {
                if (screen.SourceScreenInBuild != null || screen.TargetScreenInBuild != null)
                {
                    // If the slider is enabled, only add differences
                    if (!sliderIsDisabled)
                    {
                        if (dThreshold == 0)
                        {
                            if (screen.Difference > 0)
                            {
                                leftScreens.Add(screen.SourceScreenInBuild);
                                rightScreens.Add(screen.TargetScreenInBuild);
                                leftBuild  = screen.SourceScreenInBuild.Build.BuildName;
                                rightBuild = screen.TargetScreenInBuild.Build.BuildName;

                                diffScreens.Add(StorageHelper.GetDiffImagePath(screen.SourceScreenInBuildId, screen.TargetScreenInBuildId), screen.SourceScreenInBuild.ScreenName);
                            }
                        }
                        else // Threshold is not 0
                        {
                            if (screen.Difference >= dThreshold)
                            {
                                leftScreens.Add(screen.SourceScreenInBuild);
                                rightScreens.Add(screen.TargetScreenInBuild);
                                leftBuild  = screen.SourceScreenInBuild.Build.BuildName;
                                rightBuild = screen.TargetScreenInBuild.Build.BuildName;

                                diffScreens.Add(StorageHelper.GetDiffImagePath(screen.SourceScreenInBuildId, screen.TargetScreenInBuildId), screen.SourceScreenInBuild.ScreenName);
                            }
                        }
                    }
                    else // slider is disabled, so add everything
                    {
                        leftScreens.Add(screen.SourceScreenInBuild);
                        rightScreens.Add(screen.TargetScreenInBuild);
                        leftBuild  = screen.SourceScreenInBuild.Build.BuildName;
                        rightBuild = screen.TargetScreenInBuild.Build.BuildName;

                        diffScreens.Add(StorageHelper.GetDiffImagePath(screen.SourceScreenInBuildId, screen.TargetScreenInBuildId), screen.SourceScreenInBuild.ScreenName);
                    }
                }
                else // There is no comparison, so screen.SourceScreenInBuild is null
                {
                    leftScreens  = getAllScreens(projectName, locale1, build1Id);
                    rightScreens = getAllScreens(projectName, locale2, build2Id);
                    break;
                }
            }

            string tempFolder = createTempFolder();

            List <string> leftScreenPaths  = copyScreens(leftScreens, tempFolder, false);
            List <string> rightScreenPaths = copyScreens(rightScreens, tempFolder, true);

            copyDiffImages(diffScreens, tempFolder, projectName);

            HtmlReportHelper.CreateHtmlFile(leftScreenPaths, projectName, leftBuild, rightBuild, locale1, locale2, tempFolder);

            string randomFileName = Path.GetRandomFileName();
            string zipFile        = Path.Combine(Path.GetTempPath(), randomFileName + ".zip");

            if (!System.IO.File.Exists(zipFile))
            {
                ZipFile.CreateFromDirectory(tempFolder, zipFile, CompressionLevel.Optimal, false);
            }

            byte[] fileBytes = System.IO.File.ReadAllBytes(zipFile);
            string fileName  = "screenshots.zip";

            return(File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName));
        }