示例#1
0
        private async Task <UserModel> IsValidUser(string email, string password)
        {
            var json = await _fileUploader.GetFileFromS3(userFilePath);

            var users = JsonConvert.DeserializeObject <AdminUsers>(json);

            if (users.Users.Any(u => string.Equals(u.Email, email, StringComparison.CurrentCultureIgnoreCase) && string.Equals(u.Password, password, StringComparison.CurrentCultureIgnoreCase)))
            {
                return(users.Users.First(
                           u => string.Equals(u.Email, email, StringComparison.CurrentCultureIgnoreCase) && string.Equals(u.Password, password, StringComparison.CurrentCultureIgnoreCase)));
            }

            return(new UserModel());
        }
示例#2
0
        private async Task <List <NewsModel> > GetNewsFromS3()
        {
            var newsCollection = new List <NewsModel>();

            try
            {
                var req = new ListObjectsV2Request
                {
                    BucketName = BucketName,
                    Prefix     = "News/NewsItems"
                };

                var listing = await _s3Client.ListObjectsV2Async(req);

                foreach (var s3 in listing.S3Objects)
                {
                    var responseBody = await _fileUploader.GetFileFromS3(s3.Key);

                    var newsModel = JsonConvert.DeserializeObject <NewsModel>(responseBody);
                    newsModel.MainImg  = newsModel.Images.FirstOrDefault();
                    newsModel.NewsBody = newsModel.NewsBody.Length > 100 ? newsModel.NewsBody.Substring(0, 99) : newsModel.NewsBody;
                    newsCollection.Add(newsModel);
                }
            }
            catch (Exception ex)
            {
                //ignore
            }

            return(newsCollection.OrderByDescending(n => n.CreatedDate).ToList());
        }
        public async Task <ActionResult> SaveAdvertisement(AdvertiseModel model)
        {
            if (model.MainImage == null)
            {
                return(View("Advertisement"));
            }

            using (var readStream = model.MainImage.OpenReadStream())
            {
                var result = await _fileUploader.UploadFileAsync($@"Advertisement/{model.MainImage.FileName}", readStream)
                             .ConfigureAwait(false);

                if (!result)
                {
                    throw new Exception($"Could not upload the image to file repository. Please see the logs for details.");
                }
            }

            var advertisements     = new List <AdvertiseModel>();
            var advertisementsJson = await _fileUploader.GetFileFromS3(@"Advertisement/Advertisements.json");

            if (!string.IsNullOrEmpty(advertisementsJson))
            {
                advertisements = DeserializeObject <List <AdvertiseModel> >(advertisementsJson);
            }

            model.FileName  = model.MainImage.FileName;
            model.MainImage = null;

            advertisements.Add(model);

            var jsonString = JsonSerializer.Serialize(advertisements);

            await _fileUploader.SaveFileAsync($@"Advertisement/Advertisements.json", jsonString);



            return(RedirectToAction("Index", "Home"));
        }
        private async Task <List <NewsModel> > GetNewsFromS3()
        {
            var newsFromCache = _cache.Get <List <NewsModel> >("NewsItems");

            if (newsFromCache != null && newsFromCache.Any())
            {
                return(newsFromCache);
            }

            var newsItemsFromFile = await _fileUploader.GetFileFromS3(@"News/NewsItems.json"); // _fileUploader.ReadFile("NewsItems.json");

            if (!string.IsNullOrWhiteSpace(newsItemsFromFile))
            {
                //  await _fileUploader.UpdateS3ReaderCount(true);
                var newsModel     = JsonConvert.DeserializeObject <List <NewsModel> >(newsItemsFromFile);
                var newsItemsFile = newsModel.OrderByDescending(n => n.DisplayDate).ToList();
                _cache.Set("NewsItems", newsItemsFile);
                return(newsItemsFile);
            }

            var newsCollection = new List <NewsModel>();

            try
            {
                var req = new ListObjectsV2Request
                {
                    BucketName = BucketName,
                    Prefix     = "News/NewsItems"
                };

                var listing = await _s3Client.ListObjectsV2Async(req);

                //   await _fileUploader.UpdateS3ReaderCount(false);
                foreach (var s3 in listing.S3Objects)
                {
                    var responseBody = await _fileUploader.GetFileFromS3(s3.Key);

                    if (string.IsNullOrEmpty(responseBody))
                    {
                        continue;
                    }
                    var newsModel = JsonConvert.DeserializeObject <NewsModel>(responseBody);
                    newsModel.MainImg     = newsModel.Images.FirstOrDefault();
                    newsModel.DisplayDate = newsModel.CreatedDate.AddDays(newsModel.TopNewsForDays);

                    if (newsModel.DisplayDate >= DateTime.Today)
                    {
                        newsModel.CreatedDate = DateTime.Today;
                    }
                    else
                    {
                        newsModel.DisplayDate = newsModel.CreatedDate;
                    }

                    newsCollection.Add(newsModel);
                }
            }
            catch (Exception ex)
            {
                //ignore
            }

            var newsItems = newsCollection.OrderByDescending(n => n.DisplayDate).ToList();

            _cache.Set("NewsItems", newsItems);

            await _fileUploader.SaveFileAsync("News/NewsItems.json", JsonConvert.SerializeObject(newsItems));

            return(_cache.Get <List <NewsModel> >("NewsItems"));
        }