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"); }
protected BackgroundSizeFolders(string folderName, BaseFolder rootFolder) : base(folderName, rootFolder) { Originals = new OriginalSizeFolder(this); Mobiles = new MobileBackgroundSizeFolder(this); }