Пример #1
0
        private static int?GetDesiredLogoScale(IList <string> paths, FindLogoScaleStrategy findLogoScaleStrategy, string token, int desiredScaleValue)
        {
            var scales = new List <int>();

            foreach (var path in paths)
            {
                int    pos      = path.ToLower().IndexOf(token) + token.Length;
                string sizeText = path.Substring(pos);
                int    size;

                if (int.TryParse(sizeText, out size))
                {
                    scales.Add(size);
                }
            }

            if (scales.Count > 0)
            {
                int closestScale = desiredScaleValue;

                if (findLogoScaleStrategy == FindLogoScaleStrategy.Highest)
                {
                    closestScale = scales.Max();
                }
                else if (findLogoScaleStrategy == FindLogoScaleStrategy.NeareastToCustomScale)
                {
                    closestScale =
                        scales.Aggregate((x, y) => Math.Abs(x - desiredScaleValue) < Math.Abs(y - desiredScaleValue) ? x : y);
                }

                return(closestScale);
            }

            return(null);
        }
Пример #2
0
        public static string FindLogoImagePath(string path, string resourceName, FindLogoScaleStrategy findLogoScaleStrategy, int scaleValue = 100)
        {
            var isValidFindStrategy = Enum.IsDefined(typeof(FindLogoScaleStrategy), findLogoScaleStrategy);

            if (!isValidFindStrategy)
            {
                throw new ArgumentException("Invalid find logo strategy." + findLogoScaleStrategy);
            }

            if (string.IsNullOrWhiteSpace(resourceName))
            {
                return(null);
            }

            const string fileNameScaleToken = ".scale-";

            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(resourceName);
            string fileExtension            = Path.GetExtension(resourceName);

            var folderPath = Path.Combine(path, Path.GetDirectoryName(resourceName));

            if (!Directory.Exists(folderPath))
            {
                return(null);
            }

            // Try to find logos by file names in folder first
            var files = Directory.EnumerateFiles(Path.Combine(path, folderPath), fileNameWithoutExtension + fileNameScaleToken + "*" + fileExtension).ToList();

            files = files.Select(Path.GetFileNameWithoutExtension).ToList();

            if (files.Any())
            {
                var foundFilesScale = GetDesiredLogoScale(files, findLogoScaleStrategy, fileNameScaleToken, scaleValue);

                var sizedFilePath = Path.Combine(path, Path.GetDirectoryName(resourceName), fileNameWithoutExtension + fileNameScaleToken + foundFilesScale + fileExtension);

                if (File.Exists(sizedFilePath))
                {
                    return(sizedFilePath);
                }
            }

            // If no files found try to mach again sub folder names
            const string folderNameScaleToken = "scale-";

            var folders = Directory.EnumerateDirectories(folderPath).Where(p => p.Contains("scale-")).ToList();

            if (folders.Any())
            {
                var foundScale = GetDesiredLogoScale(folders, findLogoScaleStrategy, folderNameScaleToken, scaleValue);

                if (foundScale != null)
                {
                    var sizedFolderPath = Path.Combine(
                        folderPath,
                        folderNameScaleToken + foundScale,
                        fileNameWithoutExtension + fileExtension);

                    if (File.Exists(sizedFolderPath))
                    {
                        return(sizedFolderPath);
                    }
                }
            }

            // Finally just do an exact match with no
            // scale specifier
            var finalPath = Path.Combine(path, resourceName);

            if (File.Exists(finalPath))
            {
                return(finalPath);
            }

            return(null);
        }