Exemple #1
0
        public IActionResult GetBlob(string id)
        {
            var stream = _ContentManager.GetBlobAsStream(id);
            var tag    = HashExtentions.GetSHA1AsString(id + _ContentManager.LastModified);

            // HttpContext.Response.Headers.Add("Cache-control", "public, max-age=1314000");
            return(File(
                       stream,
                       "image/jpeg"
                       ));
            // new EntityTagHeaderValue('"' + tag + '"'));
        }
        public async Task <IActionResult> GetSliderHtml([FromForm] string galleryTitle, [FromForm] string limits, [FromForm] string ratio)
        {
            _Logger.LogDebug("User1: " + HttpContext.User?.Identity?.Name
                             + Environment.NewLine + "User2: " + User.Identity.Name
                             );


            PublicLimits              limitsParsed = PublicLimits.Parse(limits);
            List <PublicModel>        meta         = _ContentManager.GetMetadata();
            IEnumerable <PublicModel> byLimits     = meta.Where(x => x.LimitValue == limitsParsed.LimitValue && x.Kind == limitsParsed.Kind);
            IEnumerable <PublicTopic> byTitle      = byLimits.SelectMany(x => x.Topics).Where(x => x.Title == galleryTitle);
            PublicTopic foundGallery = byTitle.FirstOrDefault();

            if (foundGallery == null)
            {
                throw new ArgumentException($"Gallery [{galleryTitle}] with specified limits ({limitsParsed}) not found");
            }

            decimal ratioParsed;

            if (!decimal.TryParse(ratio, out ratioParsed))
            {
                ratioParsed = 0;
            }

            if (ratioParsed < 1)
            {
                ratioParsed = 1;
            }


            // Shuffle gallery based on remote ip
            var galleryCopy = new PublicTopic()
            {
                Title = foundGallery.Title,
                Blobs = new List <PublicBlob>(foundGallery.Blobs)
            };
            var idUser = User?.Identity?.Name;

            if (string.IsNullOrEmpty(idUser))
            {
                idUser = HttpContext.Connection.RemoteIpAddress.ToString();
            }
            var seedByIp = HashExtentions.GetSHA1AsSeed(idUser);

            galleryCopy.Blobs.Shuffle(seedByIp);

            var userAgent = HttpContext.Request.Headers["User-Agent"];
            var uaInfo    = new UserAgentInfo(userAgent);

            var userPhotosByTopic = await _PhotosRepository.GetUserPhotosByTopic(galleryCopy.Title, idUser);

            var photoTotals = await _PhotosRepository.GetPhotoTotalsByTopic(galleryCopy.Title);

            List <JsPhotoModel> jsPhotos =
                galleryCopy.Blobs.Select(x => new JsPhotoModel()
            {
                Id     = x.IdContent,
                Url    = x.Id,
                Height = x.Height,
                Width  = x.Width
            }).ToList();

            foreach (var jsPhoto in jsPhotos)
            {
                userPhotosByTopic.TryGetValue(jsPhoto.Id, out var myFlags);
                photoTotals.TryGetValue(jsPhoto.Id, out var totals);
                if (myFlags != null)
                {
                    jsPhoto.MyDislikes = myFlags.Dislikes;
                    jsPhoto.MyLikes    = myFlags.Likes;
                    jsPhoto.MyShares   = myFlags.Shares;
                    jsPhoto.MyStars    = myFlags.Stars;
                }

                if (totals != null)
                {
                    jsPhoto.TotalDislikes = totals.Dislikes;
                    jsPhoto.TotalLikes    = totals.Likes;
                    jsPhoto.TotalShares   = totals.Shares;
                    jsPhoto.TotalStars    = totals.Stars;
                }
            }

            var angularModel = new
            {
                Gallery  = galleryCopy.Title,
                Ratio    = ratioParsed,
                Limits   = limitsParsed,
                IsMobile = uaInfo.IsMobile,
                Photos   = jsPhotos,
            };

            return(PartialView("GalleryPartial", new PartialGalleryModel()
            {
                Limits = limitsParsed,
                Topic = galleryCopy,
                Ratio = ratioParsed,
                IsMobile = uaInfo.IsMobile,
                AngularModel = angularModel.ToNewtonJSon(TheAppContext.IsDebug)
            }));
        }