コード例 #1
0
        public void CopyBackgrounds(string sourceFolder, BackgroundType backgroundType, BackgroundSizeType copyToNewSize)
        {
            _logger.LogInformation($"{nameof(MediaService)}.{nameof(CopyBackgrounds)}.Start");
            if (string.IsNullOrEmpty(sourceFolder))
            {
                throw new Exception("SourceUrl is null or empty");
            }
            var fullSourcePath = Path.Combine(_fileManager.GetAbsoluteMediaRootPath(), sourceFolder);
            var fullNewPath    = Path.Combine(_fileManager.GetAbsoluteMediaRootPath(),
                                              CreateBackgroundPath(backgroundType, copyToNewSize));

            Size size;

            switch (copyToNewSize)
            {
            case BackgroundSizeType.Original:
                size = null;
                break;

            case BackgroundSizeType.Mobile:
                size = new MobileBackgroundSizeFolder(null).Size;
                break;

            default: throw new Exception("Such BackgrodunSizeType does not exist.");
            }

            var files = Directory.GetFiles(fullSourcePath);

            foreach (var file in files)
            {
                var fileName = Path.GetFileNameWithoutExtension(file);
                var ext      = Path.GetExtension(file);

                if (fileName.Contains("original"))
                {
                    fileName = fileName.Replace("original", copyToNewSize.ToString().ToLower());
                }
                else
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 10) + "_" +
                               copyToNewSize.ToString().ToLower();
                }

                var fullNewFilePath = Path.Combine(fullNewPath, fileName + ext);

                if (!File.Exists(fullNewFilePath))
                {
                    if (size == null)
                    {
                        CopyFile(file, fullNewFilePath);
                    }
                    else
                    {
                        ResizeImage(file, fullNewFilePath, size.Height, size.Width);
                    }
                }
            }
            _logger.LogInformation($"{nameof(MediaService)}.{nameof(CopyBackgrounds)}.End");
        }
コード例 #2
0
        public string CreateRelativeBackgroundPath(string imageName, BackgroundType backgroundType, BackgroundSizeType size)
        {
            BackgroundSizeFolders backgroundSizeFolders;

            switch (backgroundType)
            {
            case BackgroundType.Standard:
                backgroundSizeFolders = _folderConfiguration.Backgrounds.Standards;
                break;

            case BackgroundType.Custom:
            default:
                backgroundSizeFolders = _folderConfiguration.Backgrounds.Customs;
                break;
            }

            var name           = Path.GetFileNameWithoutExtension(imageName);
            var ext            = Path.GetExtension(imageName);
            var relativeFolder = "";

            switch (size)
            {
            case BackgroundSizeType.Original:
                relativeFolder = backgroundSizeFolders.Originals.GetFullHashPath();
                break;

            case BackgroundSizeType.Mobile:
                relativeFolder = backgroundSizeFolders.Mobiles.GetFullHashPath();
                break;
            }

            return(Path.Combine(relativeFolder, name + $"_{size.ToString().ToLower()}" + ext));
        }
コード例 #3
0
        public string ToFullBackgroundurlUrl(string shortOriginalUrl, BackgroundSizeType backgroundSize)
        {
            if (string.IsNullOrEmpty(shortOriginalUrl))
            {
                return(null);
            }
            if (shortOriginalUrl.Contains(MediaUrl))
            {
                shortOriginalUrl = shortOriginalUrl.Replace(MediaUrl, "");
            }
            var name = Path.GetFileName(shortOriginalUrl);

            shortOriginalUrl = Path.GetDirectoryName(string.Concat(shortOriginalUrl.SkipWhile(x => x == '/')));

            if (backgroundSize == BackgroundSizeType.Original)
            {
                return(MediaUrl + shortOriginalUrl.Replace("\\", "/") + '/' + name);
            }

            name = name.Replace("original", backgroundSize.ToString().ToLower());

            switch (backgroundSize)
            {
            case BackgroundSizeType.Mobile:
                return(MediaUrl + shortOriginalUrl.Replace("\\", "/").Replace(new OriginalSizeFolder(null).HashName,
                                                                              new MobileBackgroundSizeFolder(null).HashName) + '/' + name);

            default: throw new Exception($"There is no such background size {backgroundSize}");
            }
        }
コード例 #4
0
        public string CreateRelativeBackgroundPath(string imageName, BackgroundType backgroundType, BackgroundSizeType size)
        {
            _logger.LogInformation($"{nameof(MediaService)}.{nameof(CreateRelativeBackgroundPath)}.Start");
            BackgroundSizeFolders backgroundSizeFolders;

            switch (backgroundType)
            {
            case BackgroundType.Standard:
                backgroundSizeFolders = _folderConfiguration.Backgrounds.Standards;
                break;

            case BackgroundType.Custom:
            default:
                backgroundSizeFolders = _folderConfiguration.Backgrounds.Customs;
                break;
            }

            var name           = Path.GetFileNameWithoutExtension(imageName);
            var ext            = Path.GetExtension(imageName);
            var relativeFolder = "";

            switch (size)
            {
            case BackgroundSizeType.Original:
                relativeFolder = backgroundSizeFolders.Originals.GetFullHashPath();
                break;

            case BackgroundSizeType.Mobile:
                relativeFolder = backgroundSizeFolders.Mobiles.GetFullHashPath();
                break;
            }

            var result = Path.Combine(relativeFolder, name + $"_{size.ToString().ToLower()}" + ext);

            _logger.LogInformation($"{nameof(MediaService)}.{nameof(CreateRelativeBackgroundPath)}.End");
            return(result);
        }
コード例 #5
0
 private string CreateBackgroundPath(BackgroundType backgroundType, BackgroundSizeType copyToNewSize)
 {
     return(Path.Combine(GetHashFolderName("Background"), GetHashFolderName(backgroundType.ToString()),
                         GetHashFolderName(copyToNewSize.ToString())));
 }