Пример #1
0
        public async Task <IActionResult> SetupDynamicAvatars()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            int siteId = GetCurrentSiteId();

            string assetPath = Path.Combine(
                Directory.GetParent(_hostingEnvironment.WebRootPath).FullName, "assets");

            if (!Directory.Exists(assetPath))
            {
                AlertDanger = $"Asset directory not found at: {assetPath}";
                return(View("Index"));
            }

            assetPath = Path.Combine(assetPath, "dynamicavatars");
            if (!Directory.Exists(assetPath))
            {
                AlertDanger = $"Asset directory not found at: {assetPath}";
                return(View("Index"));
            }

            IEnumerable <DynamicAvatarLayer> avatarList;
            var jsonPath = Path.Combine(assetPath, "default avatars.json");

            using (StreamReader file = System.IO.File.OpenText(jsonPath))
            {
                var jsonString = await file.ReadToEndAsync();

                avatarList = JsonConvert.DeserializeObject <IEnumerable <DynamicAvatarLayer> >(jsonString);
            }

            _logger.LogInformation($"Found {avatarList.Count()} DynamicAvatarLayer objects in avatar file");

            var time = _dateTimeProvider.Now;

            int totalFilesCopied = 0;

            foreach (var layer in avatarList)
            {
                int layerFilesCopied = 0;
                var userId           = GetId(ClaimType.UserId);
                if (layer.DynamicAvatarColors != null)
                {
                    foreach (var color in layer.DynamicAvatarColors)
                    {
                        color.CreatedAt = time;
                        color.CreatedBy = userId;
                    }
                }
                foreach (var item in layer.DynamicAvatarItems)
                {
                    item.CreatedAt = time;
                    item.CreatedBy = userId;
                }
                var addedLayer = await _dynamicAvatarService.AddLayerAsync(layer);

                var layerAssetPath  = Path.Combine(assetPath, addedLayer.Name);
                var destinationRoot = Path.Combine($"site{siteId}", "dynamicavatars", $"layer{addedLayer.Id}");
                var destinationPath = _pathResolver.ResolveContentFilePath(destinationRoot);
                if (!Directory.Exists(destinationPath))
                {
                    Directory.CreateDirectory(destinationPath);
                }

                List <DynamicAvatarElement> elementList = new List <DynamicAvatarElement>();
                _logger.LogInformation($"Processing {addedLayer.DynamicAvatarItems.Count()} items in {layer.Name}...");

                foreach (var item in addedLayer.DynamicAvatarItems)
                {
                    var itemRoot = Path.Combine(destinationRoot, $"item{item.Id}");
                    var itemPath = Path.Combine(destinationPath, $"item{item.Id}");
                    if (!Directory.Exists(itemPath))
                    {
                        Directory.CreateDirectory(itemPath);
                    }
                    if (addedLayer.DynamicAvatarColors.Count > 0)
                    {
                        foreach (var color in addedLayer.DynamicAvatarColors)
                        {
                            var element = new DynamicAvatarElement()
                            {
                                DynamicAvatarItemId  = item.Id,
                                DynamicAvatarColorId = color.Id,
                                Filename             = Path.Combine(itemRoot, $"{item.Id}_{color.Id}.png")
                            };
                            elementList.Add(element);
                            System.IO.File.Copy(
                                Path.Combine(layerAssetPath, $"{item.Name} {color.Color}.png"),
                                Path.Combine(itemPath, $"{item.Id}_{color.Id}.png"));
                            layerFilesCopied++;
                        }
                    }
                    else
                    {
                        var element = new DynamicAvatarElement()
                        {
                            DynamicAvatarItemId = item.Id,
                            Filename            = Path.Combine(itemRoot, $"{item.Id}.png")
                        };
                        elementList.Add(element);
                        System.IO.File.Copy(Path.Combine(layerAssetPath, $"{item.Name}.png"),
                                            Path.Combine(itemPath, $"{item.Id}.png"));
                        layerFilesCopied++;
                    }
                }
                await _dynamicAvatarService.AddElementListAsync(elementList);

                totalFilesCopied += layerFilesCopied;
                _logger.LogInformation($"Copied {layerFilesCopied} items for {layer.Name}");
            }
            _logger.LogInformation($"Copied {totalFilesCopied} items for all layers.");

            var bundleJsonPath = Path.Combine(assetPath, "default bundles.json");

            if (System.IO.File.Exists(bundleJsonPath))
            {
                IEnumerable <DynamicAvatarBundle> bundleList;
                using (StreamReader file = System.IO.File.OpenText(bundleJsonPath))
                {
                    var jsonString = await file.ReadToEndAsync();

                    bundleList = JsonConvert.DeserializeObject <IEnumerable <DynamicAvatarBundle> >(jsonString);
                }

                foreach (var bundle in bundleList)
                {
                    _logger.LogInformation($"Processing bundle {bundle.Name}...");
                    List <int> items = bundle.DynamicAvatarItems.Select(_ => _.Id).ToList();
                    bundle.DynamicAvatarItems = null;
                    var newBundle = await _dynamicAvatarService.AddBundleAsync(bundle, items);
                }
            }

            sw.Stop();
            string loaded = $"Default dynamic avatars added in {sw.Elapsed.TotalSeconds} seconds.";

            _logger.LogInformation(loaded);
            ShowAlertSuccess(loaded);
            return(View("Index"));
        }
Пример #2
0
        public async Task <IActionResult> SetupDynamicAvatars()
        {
            int siteId = GetCurrentSiteId();

            string assetPath = Path.Combine(
                Directory.GetParent(_hostingEnvironment.WebRootPath).FullName, "assets");

            if (!Directory.Exists(assetPath))
            {
                AlertDanger = $"Asset directory not found at: {assetPath}";
                return(View("Index"));
            }

            assetPath = Path.Combine(assetPath, "dynamicavatars");
            if (!Directory.Exists(assetPath))
            {
                AlertDanger = $"Asset directory not found at: {assetPath}";
                return(View("Index"));
            }

            IEnumerable <DynamicAvatarLayer> avatarList;
            var jsonPath = Path.Combine(assetPath, "default avatars.json");

            using (StreamReader file = System.IO.File.OpenText(jsonPath))
            {
                var jsonString = await file.ReadToEndAsync();

                avatarList = JsonConvert.DeserializeObject <IEnumerable <DynamicAvatarLayer> >(jsonString);
            }

            foreach (var layer in avatarList)
            {
                var time   = DateTime.Now;
                var userId = GetId(ClaimType.UserId);
                if (layer.DynamicAvatarColors != null)
                {
                    foreach (var color in layer.DynamicAvatarColors)
                    {
                        color.CreatedAt = time;
                        color.CreatedBy = userId;
                    }
                }
                foreach (var item in layer.DynamicAvatarItems)
                {
                    item.CreatedAt = time;
                    item.CreatedBy = userId;
                }
                var addedLayer = await _dynamicAvatarService.AddLayerAsync(layer);

                var layerAssetPath  = Path.Combine(assetPath, addedLayer.Name);
                var destinationRoot = Path.Combine($"site{siteId}", "dynamicavatars", $"layer{addedLayer.Id}");
                var destinationPath = _pathResolver.ResolveContentFilePath(destinationRoot);
                if (!Directory.Exists(destinationPath))
                {
                    Directory.CreateDirectory(destinationPath);
                }

                List <DynamicAvatarElement> elementList = new List <DynamicAvatarElement>();
                foreach (var item in addedLayer.DynamicAvatarItems)
                {
                    var itemRoot = Path.Combine(destinationRoot, $"item{item.Id}");
                    var itemPath = Path.Combine(destinationPath, $"item{item.Id}");
                    if (!Directory.Exists(itemPath))
                    {
                        Directory.CreateDirectory(itemPath);
                    }
                    if (addedLayer.DynamicAvatarColors.Count > 0)
                    {
                        foreach (var color in addedLayer.DynamicAvatarColors)
                        {
                            var element = new DynamicAvatarElement()
                            {
                                DynamicAvatarItemId  = item.Id,
                                DynamicAvatarColorId = color.Id,
                                Filename             = Path.Combine(itemRoot, $"{item.Id}_{color.Id}.png")
                            };
                            elementList.Add(element);
                            System.IO.File.Copy(
                                Path.Combine(layerAssetPath, $"{item.Name} {color.Color}.png"),
                                Path.Combine(itemPath, $"{item.Id}_{color.Id}.png"));
                        }
                    }
                    else
                    {
                        var element = new DynamicAvatarElement()
                        {
                            DynamicAvatarItemId = item.Id,
                            Filename            = Path.Combine(itemRoot, $"{item.Id}.png")
                        };
                        elementList.Add(element);
                        System.IO.File.Copy(Path.Combine(layerAssetPath, $"{item.Name}.png"),
                                            Path.Combine(itemPath, $"{item.Id}.png"));
                    }
                }
                await _dynamicAvatarService.AddElementListAsync(elementList);
            }

            IEnumerable <DynamicAvatarBundle> bundleList;
            var bundleJsonPath = Path.Combine(assetPath, "default bundles.json");

            using (StreamReader file = System.IO.File.OpenText(bundleJsonPath))
            {
                var jsonString = await file.ReadToEndAsync();

                bundleList = JsonConvert.DeserializeObject <IEnumerable <DynamicAvatarBundle> >(jsonString);
            }

            foreach (var bundle in bundleList)
            {
                List <int> items = bundle.DynamicAvatarItems.Select(_ => _.Id).ToList();
                bundle.DynamicAvatarItems = null;
                var newBundle = await _dynamicAvatarService.AddBundleAsync(bundle);

                foreach (var item in items)
                {
                    await _dynamicAvatarService.AddBundleItemAsync(newBundle.Id, item);
                }
            }
            ShowAlertSuccess("Default dynamic avatars have been successfully added.");
            return(View("Index"));
        }