示例#1
0
        public async Task <IActionResult> Edit(string id, ModVM mod)
        {
            var length = (int)mod.File.Length;
            var buffer = new byte[length];

            mod.File.OpenReadStream().Read(buffer, 0, length);

            var entry = new Mod();

            if (!entry.ExtractInfo(buffer))
            {
                return(this.UnprocessableEntity());
            }

            if (id != entry.Name)
            {
                return(this.NotFound());
            }

            if (this.ModelState.IsValid)
            {
                var existing = this._context.Mod.Find(id);
                if (existing == null)
                {
                    return(this.NotFound());
                }

                var user = await this._userManager.GetUserAsync(this.User);

                if (!existing.Author.Split(", ").Contains(user.AuthorName))
                {
                    return(this.Forbid());
                }

                existing.ExtractInfo(buffer, true);

                entry.UpdateTimeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss");

                this._logger.LogInformation($"User {user.UserName} ({user.AuthorName}) Update {existing.DisplayName} ({existing.Name})");
                try
                {
                    var filename = existing.FilePath();
                    if (FileIO.Exists(filename))
                    {
                        FileIO.Delete(filename);
                    }

                    FileIO.WriteAllBytes(filename, buffer);

                    this._context.Update(existing);
                    await this._context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                return(this.RedirectToAction(nameof(Details), new { id = entry.Name }));
            }
            return(this.View(mod));
        }
示例#2
0
        public async Task <IActionResult> Create(ModVM mod)
        {
            if (this.ModelState.IsValid)
            {
                var length = (int)mod.File.Length;
                var buffer = new byte[length];
                mod.File.OpenReadStream().Read(buffer, 0, length);

                var entry = new Mod();
                if (!entry.ExtractInfo(buffer, true))
                {
                    return(this.UnprocessableEntity());
                }

                if (this._context.Mod.Find(entry.Name) != null)
                {
                    return(this.Conflict());
                }

                var user = await this._userManager.GetUserAsync(this.User);

                if (!entry.Author.Split(", ").Contains(user.AuthorName))
                {
                    entry.Author += ", " + user.AuthorName;
                }

                this._logger.LogInformation($"User {user.UserName} ({user.AuthorName}) Create {entry.DisplayName} ({entry.Name})");

                var filename = entry.FilePath();
                if (FileIO.Exists(filename))
                {
                    FileIO.Delete(filename);
                }

                FileIO.WriteAllBytes(filename, buffer);

                entry.UpdateTimeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss");

                this._context.Add(entry);
                await this._context.SaveChangesAsync();

                return(this.RedirectToAction(nameof(Details), new { id = entry.Name }));
            }
            return(this.View(mod));
        }
 protected override void OnActivate()
 {
     ModVM.Reload();
     ResourcePackVM.Reload();
     ShaderPackVM.Reload();
 }