Example #1
0
        public async Task <IActionResult> Create([Bind("Name,Description")] Thing thing, GroupSelection[] groupSelections, List <IFormFile> files, string __RequestVerificationToken)
        {
            //var file = files.First();

            if (ModelState.IsValid)
            {
                thing.UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
                _context.Add(thing);
                thing.GroupThings = groupSelections.Where(gs => gs.Selected).Select(gs => new GroupThings()
                {
                    GroupId = gs.Id,
                    ThingId = thing.Id
                }
                                                                                    ).ToList();

                //ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", thing.UserId);

                //---Lägga till bild
                DirectoryInfo d          = new DirectoryInfo(_host.WebRootPath + "\\CameraPhotos\\");
                FileInfo[]    webcamImgs = d.GetFiles(__RequestVerificationToken + "*");



                if (files.Count > 0 || webcamImgs.Count() > 0)
                {
                    var picList   = new List <ThingPic>();
                    var filePaths = new List <string>();
                    //Lägger till bilder som användaren har tagit med kamera

                    foreach (FileInfo img in webcamImgs)
                    {
                        var pic = AddPicFromWebCam(img, filePaths);
                        picList.Add(pic);
                        _context.ThingPic.Add(pic);
                    }

                    //Lägger till bilder som användaren lägger upp
                    foreach (var file in files)
                    {
                        if (file.Length > 0)
                        {
                            bool isImage = IsAnImage(file);

                            if (!isImage)
                            {
                                continue;
                            }
                            var thingGuid = Guid.NewGuid().ToString();

                            var pic = await AddPicFromFileAsync(file, filePaths);

                            picList.Add(pic);
                            _context.ThingPic.Add(pic);
                        }
                    }

                    //var EnglishTagList = new List<string>();
                    var imageTags = await GenerateTagsAndThumbs(filePaths);

                    var SwedishTagList = await _translationService.TranslateText(imageTags.Select(tag => tag.Name).ToList());

                    for (int i = 0; i < imageTags.Count(); i++)
                    {
                        Tag tag;

                        if (!_context.Tag.Any(t => t.EnglishTag == imageTags[i].Name))
                        {
                            tag = new Tag()
                            {
                                EnglishTag = imageTags[i].Name,
                                SwedishTag = SwedishTagList[i]
                            };

                            _context.Add(tag);
                        }
                        else
                        {
                            tag = _context.Tag.First(t => t.EnglishTag == imageTags[i].Name);
                        }

                        var thingTag = new ThingTags()
                        {
                            TagId      = tag.Id,
                            ThingId    = thing.Id,
                            Confidence = imageTags[i].Confidence
                        };
                        _context.Add(thingTag);
                    }
                    thing.ThingPics = picList;
                }
                //_context.Thing.Add(thing);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(MyThings)));
            }

            return(View(nameof(MyThings)));
        }
Example #2
0
        public async Task <IActionResult> Edit(string id, [Bind("Id,Name,UserId,Description,ThingPic")] Thing thing, List <IFormFile> files, string __RequestVerificationToken, GroupSelection[] groupSelections)
        {
            var thingToUpdate = _context.Thing.FirstOrDefault(t => t.Id == thing.Id);

            if (thingToUpdate == null)
            {
                return(NotFound());
            }

            _context.Update(thingToUpdate);

            if (ModelState.IsValid)
            {
                thingToUpdate.Name        = thing.Name;
                thingToUpdate.Description = thing.Description;

                //Tar bort tidigare gruppkopplingar och ersätter de med de nya
                _context.GroupThing.RemoveRange(thingToUpdate.GroupThings);
                thingToUpdate.GroupThings = groupSelections.Where(gs => gs.Selected).Select(gs => new GroupThings()
                {
                    GroupId = gs.Id,
                    ThingId = thingToUpdate.Id
                }
                                                                                            ).ToList();

                DirectoryInfo d          = new DirectoryInfo(_host.WebRootPath + "\\CameraPhotos\\");
                FileInfo[]    webcamImgs = d.GetFiles(__RequestVerificationToken + "*");

                if (files.Count > 0 || webcamImgs.Count() > 0)
                {
                    var picList   = new List <ThingPic>();
                    var filePaths = new List <string>();
                    //Lägger till bilder som användaren har tagit med kamera

                    foreach (FileInfo img in webcamImgs)
                    {
                        var pic = AddPicFromWebCam(img, filePaths);

                        picList.Add(pic);
                        _context.ThingPic.Add(pic);
                    }

                    //Lägger till bilder som användaren lägger upp
                    foreach (var file in files)
                    {
                        if (file.Length > 0)
                        {
                            bool isImage = IsAnImage(file);

                            if (!isImage)
                            {
                                continue;
                            }
                            var thingGuid = Guid.NewGuid().ToString();

                            var pic = await AddPicFromFileAsync(file, filePaths);

                            picList.Add(pic);
                            _context.ThingPic.Add(pic);
                        }
                    }

                    //var EnglishTagList = new List<string>();
                    List <ImageTag> imageTags = await GenerateTagsAndThumbs(filePaths);

                    List <string> SwedishTagList = await _translationService.TranslateText(imageTags.Select(tag => tag.Name).ToList());

                    for (int i = 0; i < imageTags.Count(); i++)
                    {
                        Tag tag;

                        if (!_context.Tag.Any(t => t.EnglishTag == imageTags[i].Name))
                        {
                            tag = new Tag()
                            {
                                EnglishTag = imageTags[i].Name,
                                SwedishTag = SwedishTagList[i]
                            };

                            _context.Add(tag);
                        }
                        else
                        {
                            tag = _context.Tag.First(t => t.EnglishTag == imageTags[i].Name);
                        }

                        if (thingToUpdate.ThingTags.Any(tt => tt.Tag.EnglishTag == tag.EnglishTag))
                        {
                            thingToUpdate.ThingTags.First(tt => tt.Tag.EnglishTag == tag.EnglishTag).Confidence += imageTags[i].Confidence;
                        }
                        else
                        {
                            var thingTag = new ThingTags()
                            {
                                TagId      = tag.Id,
                                ThingId    = thingToUpdate.Id,
                                Confidence = imageTags[i].Confidence
                            };
                            _context.Add(thingTag);
                        }
                    }
                    thingToUpdate.ThingPics.AddRange(picList);
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(MyThings)));
            }
            //ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", thing.UserId);
            return(View(thing));
        }