Пример #1
0
        public IActionResult Search(string tag)
        {
            var db         = new buffimgContext();
            var imageModel = new UserImagesModel();

            var tags         = db.Tags.Include(p => p.PhotoTagMap).ThenInclude(y => y.Photo).ToList();
            var relevantTags = tags.Where(t => t.Description == tag.ToLowerInvariant()).ToList();

            foreach (var relevantTag in relevantTags)
            {
                foreach (var photoTagMap in relevantTag.PhotoTagMap)
                {
                    if (!photoTagMap.Photo.IsPublic)
                    {
                        continue;
                    }
                    var path = Path.Combine("/user_images/", photoTagMap.Photo.FilePath + photoTagMap.Photo.FileExtension);

                    imageModel.imageList.Add(new DisplayImage()
                    {
                        src = path, tags = new List <string>()
                    });
                }
            }

            return(View(imageModel));
        }
Пример #2
0
        public IActionResult Index()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(View("Error"));
            }
            var user        = User.FindFirst(ClaimTypes.NameIdentifier);
            var userId      = user.Value;
            var db          = new buffimgContext();
            var currentUser = db.AspNetUsers.Include(u => u.Photos).SingleOrDefault(u => u.Id == userId);

            if (currentUser == null)
            {
                return(View("Error"));
            }

            var photos = currentUser.Photos;

            var imageModel = new UserImagesModel();

            foreach (var photo in photos)
            {
                var path = Path.Combine("/user_images/", photo.FilePath + photo.FileExtension);

                imageModel.imageList.Add(new DisplayImage()
                {
                    src = path, tags = new List <string>()
                });
            }

            return(View(imageModel));
        }
Пример #3
0
        public IActionResult Index()
        {
            var db = new buffimgContext();

            var recentPhotos = db.Photos.OrderByDescending(p => p.PhotoId).Where(p => p.IsPublic).Take(10);
            var imageModel   = new UserImagesModel();

            foreach (var recentPhoto in recentPhotos)
            {
                var path = Path.Combine("/user_images/", recentPhoto.FilePath + recentPhoto.FileExtension);

                imageModel.imageList.Add(new DisplayImage()
                {
                    src = path, tags = new List <string>()
                });
            }

            return(View(imageModel));
        }
Пример #4
0
        public IActionResult Image(string imageName)
        {
            var db = new buffimgContext();

            var photo = db.Photos.SingleOrDefault(p => p.FilePath == imageName);

            if (photo == null)
            {
                return(View("Error"));
            }

            var path = Path.Combine("/user_images/", photo.FilePath + photo.FileExtension);

            //prep the model
            DisplayImage img = new DisplayImage();

            img.src   = path;
            img.title = "";
            img.tags  = new List <string>();

            //pass the model to the page
            return(View(img));
        }
Пример #5
0
        public async Task <IActionResult> Upload(IFormFile image, string tags, bool isPrivate) //the model will automatically populate since the property names match the form names
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(View("Error"));
            }

            //check if it exists
            if (image == null)
            {
                return(View("Error"));
            }

            var user   = User.FindFirst(ClaimTypes.NameIdentifier);
            var userId = user.Value;

            var imageId       = RandomString(8);
            var fileExtension = Path.GetExtension(image.FileName);

            var tagList = new List <string>();

            if (tags != null)
            {
                tagList = tags.Split(',').Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
            }

            if (fileExtension.ToLowerInvariant() == ".png" || fileExtension.ToLowerInvariant() == ".jpg" || fileExtension.ToLowerInvariant() == ".jpeg" || fileExtension.ToLowerInvariant() == ".gif")
            {
                var fileLocation = Path.Combine(he.WebRootPath, "user_images", Path.GetFileName(imageId) + Path.GetExtension(image.FileName));

                var db = new buffimgContext();

                if (db.Photos.SingleOrDefault(p => p.FilePath == imageId) != null)
                {
                    return(View("Error"));
                }

                //copy the image to that location
                await using var stream = new FileStream(fileLocation, FileMode.Create);
                try
                {
                    await image.CopyToAsync(stream);
                }
                finally
                {
                    stream.Close();
                }

                var newPhoto = new Photos()
                {
                    FilePath      = imageId,
                    IsPublic      = !isPrivate,
                    UserId        = userId,
                    FileExtension = fileExtension
                };

                db.Photos.Add(newPhoto);

                var newTagsList      = new List <Tags>();
                var existingTagsList = new List <Tags>();

                foreach (var tag in tagList)
                {
                    var normalizedTag = tag.ToLowerInvariant();
                    var existingTag   = db.Tags.SingleOrDefault(t => t.Description == normalizedTag);

                    if (existingTag == null)
                    {
                        var tagId = new Random().Next(int.MaxValue);
                        // make sure tagId is unique
                        while (db.Tags.SingleOrDefault(t => t.TagId == tagId) != null)
                        {
                            tagId = new Random().Next(int.MaxValue);
                        }

                        newTagsList.Add(new Tags()
                        {
                            Description = normalizedTag, TagId = tagId
                        });
                    }
                    else
                    {
                        existingTagsList.Add(existingTag);
                    }
                }

                db.Tags.AddRange(newTagsList);

                // add new photo and tags
                await db.SaveChangesAsync();

                newPhoto = db.Photos.Single(p => p.FilePath == imageId);

                foreach (var tag in newTagsList)
                {
                    db.PhotoTagMap.Add(new PhotoTagMap()
                    {
                        PhotoId = newPhoto.PhotoId, TagId = tag.TagId
                    });
                }
                foreach (var existingTag in existingTagsList)
                {
                    db.PhotoTagMap.Add(new PhotoTagMap()
                    {
                        PhotoId = newPhoto.PhotoId, TagId = existingTag.TagId
                    });
                }

                await db.SaveChangesAsync();

                return(RedirectToAction("Image", "Image", new { imageName = imageId }));
            }

            //they didn't upload a file or something went wrong
            return(View("Error"));
        }