Пример #1
0
        public async Task <IActionResult> Index(string uuid, NewProjectViewModel newPorject)
        {
            if (uuid == "New")
            {
                if (newPorject == null)
                {
                    return(BadRequest());
                }
                if (!ModelState.IsValid)
                {
                    return(View("New", newPorject));
                }

                var version = _context.MinecraftVersions
                              .Include(v => v.ForgeVersions)
                              .Where(v => v.Id == newPorject.MinecraftVersionId)
                              .SelectMany(v => v.ForgeVersions, (mv, fv) => CreateVersionTuple(mv, fv))
                              .FirstOrDefault(v => v.forgeVersion.Id == newPorject.ForgeVersionId);
                if (version.minecraftVersion == null || version.forgeVersion == null)
                {
                    return(BadRequest());
                }
                // Check valid zip is exists
                var forgeZipFile = _storageSetting.GetForgeStorageSetting().GetForgeZipFile(version.minecraftVersion, version.forgeVersion);
                if (!forgeZipFile.Exists)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Specified template of Minecraft and Forge version is not found."));
                }
                try
                {
                    var safeUser = await _context.SafeUsers
                                   .SingleOrDefaultAsync(m => m.Id == _userManager.GetUserId(HttpContext.User));

                    var now         = DateTime.UtcNow;
                    var accessRight = await _context.AccessRights.AddAsync(new AccessRight
                    {
                        User  = safeUser,
                        Level = AccessRightLevel.Owner.Level
                    });

                    var createdProject = await _context.Projects.AddAsync(new Project
                    {
                        Name             = newPorject.Name,
                        Description      = newPorject.Description,
                        MinecraftVersion = version.minecraftVersion,
                        ForgeVersion     = version.forgeVersion,
                        CreatedAt        = now,
                        UpdatedAt        = now,
                        AccessRights     = new List <AccessRight> {
                            accessRight.Entity
                        }
                    });

                    await _context.SaveChangesAsync();

                    // Expand zip file
                    var projectId = createdProject.Entity.Id;
                    var targetDir = _storageSetting.GetProjectStorageSetting().GetRootDirectory().ResolveDir(projectId);
                    ZipFile.ExtractToDirectory(forgeZipFile.FullName, targetDir.FullName);
                    return(RedirectToAction(nameof(Index), "Projects", new RouteValueDictionary
                    {
                        { "uuid", projectId }
                    }));
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "An error occurred in creating new project.");
                    return(StatusCode(StatusCodes.Status500InternalServerError, "An internal error occurred."));
                }
            }
            return(NotFound());
        }
Пример #2
0
        private async Task <IActionResult> ForgeVersion(string minecraftVersionId, string forgeVersionId, string action)
        {
            var minecraftVersion = await _context.MinecraftVersions.FindAsync(minecraftVersionId);

            if (minecraftVersion == null)
            {
                return(NotFound());
            }
            if (!Util.IsCorrectUuid(forgeVersionId))
            {
                return(NotFound());
            }
            switch (action)
            {
            case "Up":
            {
                var forgeVersion = await _context.ForgeVersions.FindAsync(forgeVersionId);

                forgeVersion.Rank--;
                forgeVersion.UpdatedAt = DateTime.UtcNow;
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Versions), "Admin", new RouteValueDictionary
                    {
                        { "id", minecraftVersionId },
                        { "subaction", "" },
                        { "subsubaction", "" }
                    }));
            }

            case "Down":
            {
                var forgeVersion = await _context.ForgeVersions.FindAsync(forgeVersionId);

                forgeVersion.Rank++;
                forgeVersion.UpdatedAt = DateTime.UtcNow;
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Versions), "Admin", new RouteValueDictionary
                    {
                        { "id", minecraftVersionId },
                        { "subaction", "" },
                        { "subsubaction", "" }
                    }));
            }

            case "Download":
            {
                var forgeZipFile = _storageSetting.GetForgeStorageSetting()
                                   .GetForgeZipFile(await _context.MinecraftVersions.FindAsync(minecraftVersionId), await _context.ForgeVersions.FindAsync(forgeVersionId));
                return(PhysicalFile(forgeZipFile.FullName, MediaTypeNames.Application.Zip, forgeZipFile.Name));
            }

            case "Edit":
            {
                var forgeVersion = await _context.ForgeVersions.FindAsync(forgeVersionId);

                return(View("VersionDetailForgeEdit", new VersionsPostViewModel
                    {
                        ForgeEditViewModel = new VersionDetailForgeEditViewModel
                        {
                            MinecraftVersion = minecraftVersion,
                            Id = forgeVersionId,
                            Version = forgeVersion.Version,
                            IsRecommend = forgeVersion.IsRecommend
                        }
                    }));
            }
            }
            return(NotFound());
        }