mirror of
https://github.com/lempamo/Project-Unite.git
synced 2025-02-02 04:57:35 +00:00
Avatar system 😃
This commit is contained in:
parent
84285d2ffb
commit
9f238cc47f
6 changed files with 120 additions and 1 deletions
|
@ -20,6 +20,67 @@ namespace Project_Unite.Controllers
|
|||
{
|
||||
}
|
||||
|
||||
public ActionResult SetAvatar(string id)
|
||||
{
|
||||
var db = new ApplicationDbContext();
|
||||
var usr = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId());
|
||||
var avtr = db.UserAvatars.FirstOrDefault(x => x.Id == id);
|
||||
usr.AvatarUrl = avtr.AvatarUrl;
|
||||
db.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
public ActionResult UploadAvatar()
|
||||
{
|
||||
var model = new UploadImageViewModel();
|
||||
return View(model);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult UploadAvatar(UploadImageViewModel model)
|
||||
{
|
||||
string[] allowedTypes = new[] { ".png", ".jpg", ".bmp", ".jpeg", ".gif" };
|
||||
|
||||
bool containsAllowedType = !string.IsNullOrWhiteSpace(allowedTypes.FirstOrDefault(x => model.Image.FileName.EndsWith(x)));
|
||||
if (containsAllowedType == false)
|
||||
ModelState.AddModelError("UploadImageViewModel", new Exception("File type not allowed."));
|
||||
|
||||
if (ModelState.IsValid == true)
|
||||
{
|
||||
var db = new ApplicationDbContext();
|
||||
var usr = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId());
|
||||
string avatarRoot = $"~/Uploads/{usr.DisplayName}/Avatars";
|
||||
string serverPath = Server.MapPath(avatarRoot);
|
||||
if (!System.IO.Directory.Exists(serverPath))
|
||||
System.IO.Directory.CreateDirectory(serverPath);
|
||||
|
||||
avatarRoot += "/" + model.Image.FileName;
|
||||
serverPath += "\\" + model.Image.FileName;
|
||||
|
||||
model.Image.SaveAs(serverPath);
|
||||
|
||||
var avatar = new Avatar();
|
||||
avatar.Id = Guid.NewGuid().ToString();
|
||||
avatar.AvatarUrl = avatarRoot.Remove(0, 1);
|
||||
avatar.UserId = usr.Id;
|
||||
avatar.UploadedAt = DateTime.Now;
|
||||
usr.AvatarUrl = avatar.AvatarUrl;
|
||||
db.UserAvatars.Add(avatar);
|
||||
db.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
public ActionResult ListAvatars()
|
||||
{
|
||||
var avatars = new ApplicationDbContext().UserAvatars.Where(x => x.UserId == User.Identity.GetUserId());
|
||||
return View(avatars);
|
||||
}
|
||||
|
||||
public ManageController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
|
||||
{
|
||||
UserManager = userManager;
|
||||
|
|
|
@ -6,6 +6,7 @@ using System.Data.Entity.Infrastructure;
|
|||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNet.Identity.EntityFramework;
|
||||
|
@ -19,6 +20,12 @@ namespace Project_Unite.Models
|
|||
public string Followed { get; set; }
|
||||
}
|
||||
|
||||
public class UploadImageViewModel
|
||||
{
|
||||
[Required(ErrorMessage = "Please select an image to upload.")]
|
||||
public HttpPostedFileBase Image { get; set; }
|
||||
}
|
||||
|
||||
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
|
||||
public class ApplicationUser : IdentityUser
|
||||
{
|
||||
|
@ -189,11 +196,14 @@ namespace Project_Unite.Models
|
|||
((IObjectContextAdapter)this).ObjectContext.DeleteObject(obj);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static ApplicationDbContext Create()
|
||||
{
|
||||
return new ApplicationDbContext();
|
||||
}
|
||||
|
||||
public DbSet<Avatar> UserAvatars { get; set; }
|
||||
public DbSet<Skin> Skins { get; set; }
|
||||
public DbSet<Configuration> Configs { get; set; }
|
||||
public DbSet<ShiftoriumUpgrade> ShiftoriumUpgrades { get; set; }
|
||||
|
@ -259,4 +269,12 @@ namespace Project_Unite.Models
|
|||
public string UserId { get; set; }
|
||||
public string EngineStoryId { get; set; }
|
||||
}
|
||||
|
||||
public class Avatar
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string UserId { get; set; }
|
||||
public string AvatarUrl { get; set; }
|
||||
public DateTime UploadedAt { get; set; }
|
||||
}
|
||||
}
|
|
@ -525,6 +525,8 @@
|
|||
<Content Include="Views\Skins\ViewSkin.cshtml" />
|
||||
<Content Include="Views\Skins\Index.cshtml" />
|
||||
<Content Include="Views\Skins\PostSkin.cshtml" />
|
||||
<Content Include="Views\Manage\ListAvatars.cshtml" />
|
||||
<Content Include="Views\Manage\UploadAvatar.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="App_Data\" />
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
<div class="tab-pane active" id="t_profile">
|
||||
<table class="table">
|
||||
<tr>
|
||||
<td><strong>Full name:</strong></td>
|
||||
<td style="width:35%;"><strong>Full name:</strong></td>
|
||||
<td>
|
||||
@using(Html.BeginForm("ChangeFullName", "Manage"))
|
||||
{
|
||||
|
|
22
Project-Unite/Views/Manage/ListAvatars.cshtml
Normal file
22
Project-Unite/Views/Manage/ListAvatars.cshtml
Normal file
|
@ -0,0 +1,22 @@
|
|||
@model IEnumerable<Project_Unite.Models.Avatar>
|
||||
|
||||
@{
|
||||
ViewBag.Title = "Change avatar";
|
||||
}
|
||||
|
||||
<h2>Change avatar</h2>
|
||||
|
||||
<p>You can choose a new avatar on this page - you can set an existing avatar that you've uploaded or upload a new one.</p>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-2">
|
||||
<a href="@Url.Action("UploadAvatar")" class="btn btn-default" style="width:128px;height:128px;"><span class="glyphicon glyphicon-plus"></span></a>
|
||||
</div>
|
||||
|
||||
@foreach(var avatar in Model.OrderByDescending(x=>x.UploadedAt))
|
||||
{
|
||||
<div class="col-sm-2">
|
||||
<a href="@Url.Action("SetAvatar", "Manage", new { id = avatar.Id })")"><img src="@avatar.AvatarUrl" width="128" height="128" /></a>
|
||||
</div>
|
||||
}
|
||||
</div>
|
16
Project-Unite/Views/Manage/UploadAvatar.cshtml
Normal file
16
Project-Unite/Views/Manage/UploadAvatar.cshtml
Normal file
|
@ -0,0 +1,16 @@
|
|||
@model Project_Unite.Models.UploadImageViewModel
|
||||
@{
|
||||
ViewBag.Title = "Upload avatar";
|
||||
}
|
||||
|
||||
<h2>Upload an avatar</h2>
|
||||
|
||||
<p>Let's put a face on your profile, shall we? Upload an image from your local computer and we'll display it as your avatar.</p>
|
||||
|
||||
@using (Html.BeginForm())
|
||||
{
|
||||
@Html.AntiForgeryToken()
|
||||
@Html.ValidationSummary()
|
||||
<p><strong>File to upload: </strong>@Html.TextBoxFor(Model=>Model.Image, new { @class = "form-control", type = "file" })</p>
|
||||
<input type="submit" class="form-control" value="Upload!" />
|
||||
}
|
Loading…
Add table
Reference in a new issue