示例#1
0
        public void CopyAvatars(string sourceFolder, AvatarType backgroundType, AvatarSizeType copyToNewSize)
        {
            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(),
                                              CreateAvatarPath(backgroundType, copyToNewSize));

            Size size;

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

            case AvatarSizeType.Small:
                size = new SmallAvatarSizeFolder(null).Size;
                break;

            case AvatarSizeType.Large:
                size = new LargeAvatarSizeFolder(null).Size;
                break;

            default: throw new Exception("Such AvatarSizeType 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);
                    }
                }
            }
        }
示例#2
0
 protected AvatarSizeFolders(string folderName, BaseFolder rootFolder) : base(folderName, rootFolder)
 {
     Smalls    = new SmallAvatarSizeFolder(this);
     Larges    = new LargeAvatarSizeFolder(this);
     Originals = new OriginalSizeFolder(this);
 }