예제 #1
0
        public async Task <IActionResult> Register(ArtistRegistrationViewModel model)
        {
            ViewBag.SubsectionPages = _subsectionPages;
            ViewBag.ActiveSubpage   = _subsectionPages[1];

            if (!ModelState.IsValid)
            {
                return(View());
            }

            if (!_signInManager.IsSignedIn(User))
            {
                ModelState.AddModelError(string.Empty,
                                         "You have to be logged in to register an artist!");
                return(View());
            }

            Guid id = Guid.NewGuid();

            ImageFileManager ifmPfp = null, ifmBg = null;
            string           pfp, bg = null;

            try
            {
                // Load images
                ifmPfp = new ImageFileManager("wwwroot/img/profiles/pfps/", model.Pfp.OpenReadStream(),
                                              ImageUtils.ImgExtensionFromContentType(model.Pfp.ContentType));
                if (model.BackgroundImage != null)
                {
                    ifmBg = new ImageFileManager("wwwroot/img/profiles/bgs/",
                                                 model.BackgroundImage.OpenReadStream(),
                                                 ImageUtils.ImgExtensionFromContentType(model.BackgroundImage.ContentType));
                }

                // Save images
                pfp = await ifmPfp.SavePfp(id);

                pfp = pfp.Remove(0, 7);
                if (ifmBg != null)
                {
                    bg = await ifmBg.SaveBg(id);

                    bg = bg.Remove(0, 7);
                }
            }
            catch (InvalidArtDimensionsException e)
            {
                ModelState.AddModelError(string.Empty, "Invalid profile picture or background size! " +
                                         "Profile picture must be at least 400px by 400px and in 1:1 ratio " +
                                         "and background must be at least 1590px by 540px");
                return(View());
            }


            Artist artist = new Artist()
            {
                Id                 = id,
                ArtistName         = model.Name,
                RegisteredAt       = DateTime.Now,
                RegisteredBy       = await _userManager.GetUserAsync(User),
                ProfileViews       = 0,
                BackgroundImageUrl = bg,
                PfpUrl             = pfp,
                Country            = model.Country,
                FacebookProfileUrl = model.FacebookProfileUrl,
                TwitterProfileUrl  = model.TwitterProfileUrl,
                MailAddress        = model.MailAddress,
                Gender             = model.Gender,
            };

            await _db.Artists.AddAsync(artist);

            await _db.SaveChangesAsync();

            return(Redirect("/Artists/List"));
        }
예제 #2
0
        public async Task <IActionResult> Upload(PostUploadViewModel model)
        {
            ViewBag.SubsectionPages = _subsectionPages;
            ViewBag.ActiveSubpage   = _subsectionPages[2];
            if (ModelState.IsValid)
            {
                if (!_signInManager.IsSignedIn(User))
                {
                    ModelState.AddModelError(string.Empty, "You have to be logged in to upload an art!");
                    return(Redirect("/posts/upload"));
                }

                // Check if the artist exists
                Artist artist = null;
                if (model.Author != null)
                {
                    artist = await _db.Artists
                             .FirstOrDefaultAsync(a => a.ArtistName.Equals(model.Author));

                    if (artist == null)
                    {
                        // TODO: Ask if u can make a link to the artist registration form in here
                        ModelState.AddModelError(string.Empty, $"Could not find artist named {model.Author}." +
                                                 " Please consider adding a new artist <a href=\"#\">here</a>");
                        return(View());
                    }
                }

                // Save the files and manage get the necessary data
                string large, normal, thumbnail, hash;
                (int, int)dims;
                long size;
                try
                {
                    using (ImageFileManager ifm = new ImageFileManager("wwwroot/img/posts/", model.File.OpenReadStream(),
                                                                       ImageUtils.ImgExtensionFromContentType(model.File.ContentType)))
                    {
                        large = await ifm.SaveLarge();

                        normal = await ifm.SaveNormal();

                        thumbnail = await ifm.SaveThumbnail(0, 0);
                    }
                    hash = ImageUtils.HashFromFile(large);
                    dims = ImageUtils.DimensionsOfImage(large);
                    size = model.File.Length;

                    large     = large.Remove(0, 7);
                    normal    = normal.Remove(0, 7);
                    thumbnail = thumbnail.Remove(0, 7);
                }
                catch (InvalidArtDimensionsException exception)
                {
                    ModelState.AddModelError(string.Empty, "Invalid art size - the art should be at least 300 x 300px");
                    return(View());
                }

                // Get the user data and register unregistered tags
                var usr = await _userManager.GetUserAsync(User);

                List <string> rawTags = model.TagString.Split(' ').ToList();
                List <Tag>    tags    = new List <Tag>();
                foreach (string rawTag in rawTags)
                {
                    var tag = _db.Tags.FirstOrDefault(t => t.TagString.Equals(rawTag));
                    if (tag == null)
                    {
                        Tag newTag = new Tag {
                            Id = Guid.NewGuid(), Creator = usr, TagString = rawTag, AddedAt = DateTime.Now
                        };
                        await _db.Tags.AddAsync(newTag);

                        tags.Add(newTag);
                    }
                    else
                    {
                        tags.Add(tag);
                    }
                }
                await _db.SaveChangesAsync();

                // Create an art
                Art art = new Art()
                {
                    Id             = Guid.NewGuid(),
                    Uploader       = usr,
                    Name           = model.Name,
                    Source         = model.Source,
                    Rating         = model.Rating,
                    Author         = artist,
                    LargeFileUrl   = large,
                    FileUrl        = normal,
                    PreviewFileUrl = thumbnail,
                    Md5Hash        = hash,
                    Height         = dims.Item2,
                    Width          = dims.Item1,
                    FileSize       = (int)size,
                    Stars          = 0,
                    CreatedAt      = DateTime.Now,
                };

                // Register tag occurrences in the join table and the art
                List <TagOccurrence> occurrences = new List <TagOccurrence>();
                foreach (var tag in tags)
                {
                    occurrences.Add(new TagOccurrence()
                    {
                        Art   = art,
                        ArtId = art.Id,
                        Tag   = tag,
                        TagId = tag.Id
                    });
                }
                art.Tags = occurrences;

                await _db.Arts.AddAsync(art);

                await _db.SaveChangesAsync();

                return(Redirect("/Posts/List"));
            }
            return(View());
        }
예제 #3
0
        public async Task <IActionResult> Update(ProfileUpdateViewModel puvm)
        {
            if (!ModelState.IsValid)
            {
                return(Redirect("/Profile/Settings"));
            }

            if (!_signInManager.IsSignedIn(User))
            {
                ModelState.AddModelError(string.Empty,
                                         "You have to be logged in to change your settings!");
                return(Redirect("/Profile/Settings"));
            }

            NeobooruUser usr = await _userManager.GetUserAsync(User);

            usr.Gender             = puvm.Gender;
            usr.UserName           = puvm.Username;
            usr.ProfileDescription = puvm.ProfileDescription;

            if (puvm.PfpImage != null)
            {
                Guid id = Guid.NewGuid();

                ImageFileManager ifmPfp = null;
                try
                {
                    ifmPfp = new ImageFileManager("wwwroot/img/profiles/pfps/",
                                                  puvm.PfpImage.OpenReadStream(), ImageUtils.ImgExtensionFromContentType(puvm.PfpImage.ContentType));
                }
                catch (InvalidArtDimensionsException e)
                {
                    ModelState.AddModelError(string.Empty, "Invalid profile picture or background size! " +
                                             "Profile picture must be at least 400px by 400px");
                    return(Redirect("/Profile/Settings"));
                }

                usr.PfpUrl = await ifmPfp.SavePfp(id);

                usr.PfpUrl = usr.PfpUrl.Remove(0, 7);
            }

            if (puvm.BgImage != null)
            {
                Guid id = Guid.NewGuid();

                ImageFileManager ifmPfp = null;
                try
                {
                    ifmPfp = new ImageFileManager("wwwroot/img/profiles/bgs/",
                                                  puvm.BgImage.OpenReadStream(), ImageUtils.ImgExtensionFromContentType(puvm.BgImage.ContentType));
                }
                catch (InvalidArtDimensionsException e)
                {
                    ModelState.AddModelError(string.Empty, "Invalid profile picture or background size! " +
                                             "Background must be at least 1590px by 540px");
                    return(Redirect("/Profile/Settings"));
                }

                usr.BgUrl = await ifmPfp.SaveBg(id);

                usr.BgUrl = usr.BgUrl.Remove(0, 7);
            }

            await _db.SaveChangesAsync();

            return(Redirect("/Profile/Profile"));
        }