Example #1
0
        public async Task <PackageResponseModel> Package(PackageRequestModel package)
        {
            var app = await _appRepository.GetOneAsync(package.AppId);

            var collectLocales      = _localizationProvider.GetByAppId(app.Id);
            var collectStandards    = _standardServiceProvider.GetByAppId(app.Id);
            var collectCharts       = _chartServiceProvider.GetByAppId(app.Id);
            var collectDynamicLists = _dynamicListServiceProvider.GetByAppId(app.Id);
            var collectPages        = _pageServiceProvider.GetByAppId(app.Id);
            await Task.WhenAll(collectLocales, collectStandards, collectCharts, collectDynamicLists, collectPages);

            var appPackpageFlattern = new AppPackageFlatternModel
            {
                App               = app,
                Description       = package.Description,
                PackagedDate      = DateTime.UtcNow,
                Creator           = package.Creator,
                TotalCharts       = collectCharts.Result?.Count(),
                TotalDynamicLists = collectDynamicLists.Result?.Count(),
                TotalStandards    = collectStandards.Result?.Count(),
                TotalLocales      = collectLocales.Result?.Count(),
                TotalPages        = collectPages.Result?.Count(),
                ChainingFiles     = new List <string>()
            };

            var fileName     = DateTime.UtcNow.Ticks.ToString();
            var folderName   = app.Name + "_" + app.CurrentVersionNumber + "_" + fileName;
            var jsonFileName = folderName + ".json";
            var jsonFilePath = Path.Combine(Environment.CurrentDirectory, "Temp", "Packages", folderName);

            if (Directory.Exists(jsonFilePath))
            {
                Directory.Delete(jsonFilePath, true);
            }
            Directory.CreateDirectory(jsonFilePath);

            if (collectStandards.Result != null)
            {
                var jsonStandards = ConvertUtil.SerializeObject(collectStandards.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, STANDARD_FILE)))
                {
                    sw.Write(jsonStandards);
                }

                appPackpageFlattern.ChainingFiles.Add(STANDARD_FILE);
            }

            if (collectCharts.Result != null)
            {
                var jsonCharts = ConvertUtil.SerializeObject(collectCharts.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, CHART_FILE)))
                {
                    sw.Write(jsonCharts);
                }

                appPackpageFlattern.ChainingFiles.Add(CHART_FILE);
            }

            if (collectDynamicLists.Result != null)
            {
                var jsonDynamicLists = ConvertUtil.SerializeObject(collectDynamicLists.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, DYNAMICLIST_FILE)))
                {
                    sw.Write(jsonDynamicLists);
                }

                appPackpageFlattern.ChainingFiles.Add(DYNAMICLIST_FILE);
            }

            if (collectPages.Result != null)
            {
                var jsonPages = ConvertUtil.SerializeObject(collectPages.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, PAGE_FILE)))
                {
                    sw.Write(jsonPages);
                }

                appPackpageFlattern.ChainingFiles.Add(PAGE_FILE);
            }

            if (collectLocales.Result != null)
            {
                var jsonLocales = ConvertUtil.SerializeObject(collectLocales.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, LOCALE_FILE)))
                {
                    sw.Write(jsonLocales);
                }

                appPackpageFlattern.ChainingFiles.Add(LOCALE_FILE);
            }

            var jsonFlattern = ConvertUtil.SerializeObject(appPackpageFlattern, true);

            using (var sw = new StreamWriter(Path.Combine(jsonFilePath, jsonFileName)))
            {
                sw.Write(jsonFlattern);
            }

            ZipFile.CreateFromDirectory(jsonFilePath, Path.Combine(Environment.CurrentDirectory, "Temp", "Packages", folderName + ".zip"));

            // Store zip file into file server, allow to create zip file when downloading
            var uploadResponse = await _fileSeviceProvider
                                 .UploadFileAsync(
                Path.Combine(Environment.CurrentDirectory, "Temp", "Packages", folderName + ".zip"),
                package.Creator,
                true);

            return(new PackageResponseModel {
                DownloadableUrl = uploadResponse.DownloadableUrl
            });
        }