Esempio n. 1
0
        public async Task <ServiceResult <SavedImage, SaveImageError> > SaveImageAsync(string filePath, Stream imageStream)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(ServiceResult <SavedImage, SaveImageError> .CreateFailed(SaveImageError.EmptyFilePath));
            }

            if (imageStream.Length > _imageStorageConfiguration.MaximumImageSize)
            {
                return(ServiceResult <SavedImage, SaveImageError> .CreateFailed(SaveImageError.TooBigImage));
            }

            var imageExtension     = Path.GetExtension(filePath);
            var mediaType          = MimeMapping.MimeUtility.GetMimeMapping(filePath);
            var isMediaTypeAllowed = _imageStorageConfiguration.AllowedImageMediaTypes.Any(t => t == mediaType);

            if (!isMediaTypeAllowed)
            {
                return(ServiceResult <SavedImage, SaveImageError> .CreateFailed(SaveImageError.NotAllowedMediaType));
            }
            var externalId        = Guid.NewGuid().ToString();
            var uploadedImageName = $"{externalId}{imageExtension}";

            var firebaseStorage = new FirebaseStorage(_firebaseImageStorageConfiguration.StorageBucket)
                                  .Child(_firebaseImageStorageConfiguration.ImagesDirectory)
                                  .Child(uploadedImageName);

            string downloadUri;

            try
            {
                downloadUri = await firebaseStorage.PutAsync(imageStream);
            }
            catch (FirebaseStorageException)
            {
                return(ServiceResult <SavedImage, SaveImageError> .CreateFailed(SaveImageError.StorageError));
            }

            var uploadedImage = new UploadedImage
            {
                FilePath   = downloadUri,
                ExternalId = externalId,
                MediaType  = mediaType
            };

            var imagesRepository = _unitOfWork.GetRepository <UploadedImage>();

            imagesRepository.Create(uploadedImage);
            await _unitOfWork.SaveAsync();

            return(ServiceResult <SavedImage, SaveImageError> .CreateSuccess(new SavedImage(uploadedImage.ExternalId)));
        }
Esempio n. 2
0
        public async Task <SaveImageResult> SaveImageAsync(string filePath, Stream imageStream)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(SaveImageResult.CreateUnsuccessful(SaveImageError.EmptyFilePath));
            }
            if (imageStream.Length > _configuration.MaximumImageSize)
            {
                return(SaveImageResult.CreateUnsuccessful(SaveImageError.TooBigImage));
            }
            var imageExtension     = Path.GetExtension(filePath);
            var mediaType          = MimeMapping.MimeUtility.GetMimeMapping(filePath);
            var isMediaTypeAllowed = _configuration.AllowedImageMediaTypes.Any(t => t == mediaType);

            if (!isMediaTypeAllowed)
            {
                return(SaveImageResult.CreateUnsuccessful(SaveImageError.NotAllowedMediaType));
            }
            var externalId        = Guid.NewGuid().ToString();
            var uploadedImageName = $"{externalId}{imageExtension}";

            var uploadedImage = new UploadedImage
            {
                FilePath   = uploadedImageName,
                ExternalId = externalId,
                MediaType  = mediaType
            };

            Directory.CreateDirectory(_configuration.ImagesDirectory);
            var uploadPath = Path.Combine(_configuration.ImagesDirectory, uploadedImageName);

            using (var outputStream = new FileStream(uploadPath, FileMode.Create))
            {
                await imageStream.CopyToAsync(outputStream);
            }

            var imagesRepository = _unitOfWork.GetRepository <UploadedImage>();

            imagesRepository.Create(uploadedImage);
            await _unitOfWork.SaveAsync();

            return(SaveImageResult.CreateSuccessful(uploadedImage.ExternalId));
        }
        public async Task <ServiceResult <UserThreadsCount, GetUserThreadsCountError> > GetUserThreadsCount(string username)
        {
            var userRepository = _unitOfWork.UserRepository;
            var currentUser    = await userRepository.FirstOrDefaultAsync(u => u.Username == username);

            if (currentUser == null)
            {
                return(ServiceResult <UserThreadsCount, GetUserThreadsCountError> .CreateFailed(GetUserThreadsCountError.UserNotFound));
            }

            var threadsRepository = _unitOfWork.GetRepository <Thread>();
            var threads           = await threadsRepository.WhereAsync(t => t.Author.Username == username, t => t.Author, t => t.Section);

            var count  = threads.Count();
            var result = new UserThreadsCount
            {
                Count = count
            };

            return(ServiceResult <UserThreadsCount, GetUserThreadsCountError> .CreateSuccess(result));
        }
Esempio n. 4
0
        public async Task <ServiceResult <UpdateProfileErrorType> > UpdateUserProfileAsync(CurrentUserIdentityProvider currentUserIdentityProvider, UpdateUserProfileDto dto)
        {
            var userRepository  = _unitOfWork.UserRepository;
            var imageRepostiory = _unitOfWork.GetRepository <UploadedImage>();

            var newImage = await imageRepostiory.GetFirstOrDefaultAsync(i => i.ExternalId == dto.UserProfileImageExternalId);

            if (newImage == null)
            {
                return(ServiceResult.CreateFailed(UpdateProfileErrorType.InvalidProfileImage));
            }

            var currentUser = await GetCurrentUserAsync(currentUserIdentityProvider);

            currentUser.ProfileImageId = newImage.Id;
            userRepository.Update(currentUser);
            await _unitOfWork.SaveAsync();

            return(ServiceResult.CreateSuccess <UpdateProfileErrorType>());
        }
        public async Task <ServiceResult <SectionsInfo, GetSectionsError> > GetSections(int startIndex, int size)
        {
            if (startIndex < 0 || size > MaxGetQuerySize)
            {
                return(ServiceResult <SectionsInfo, GetSectionsError> .CreateFailed(GetSectionsError.InvalidPaginationArguments));
            }

            var sectionRepository = _unitOfWork.GetRepository <Section>();
            var sections          = await sectionRepository.All(null)
                                    .OrderBy(s => s.Name).Skip(startIndex).Take(size).ToListAsync();

            var result = new SectionsInfo
            {
                Amount   = sections.Count,
                Sections = sections.Select(s => new SectionInfo
                {
                    Name = s.Name
                }).ToList(),
                Size       = size,
                StartIndex = startIndex
            };

            return(ServiceResult <SectionsInfo, GetSectionsError> .CreateSuccess(result));
        }
Esempio n. 6
0
        public async Task <IReadOnlyCollection <Section> > GetSectionsAsync()
        {
            var sectionsRepository = _unitOfWork.GetRepository <Section>();
            var sections           = await sectionsRepository.GetWhereAsync(s => !string.IsNullOrEmpty(s.Name));

            return(sections);
        }
Esempio n. 7
0
        public async Task <ServiceResult <ThreadsInfo, GetThreadsInfoError> > GetPopularThreads(int startIndex, int size)
        {
            if (startIndex < 0 || size > MaxGetQuerySize)
            {
                return(ServiceResult <ThreadsInfo, GetThreadsInfoError> .CreateFailed(GetThreadsInfoError.InvalidPaginationArguments));
            }

            var threadRepository = _unitOfWork.GetRepository <Thread>();
            var threads          = await threadRepository.All(null, t => t.Likes, t => t.Author, t => t.Section)
                                   .OrderByDescending(t => t.Likes.Where(l => l.IsLike).Count() - t.Likes.Where(l => !l.IsLike).Count())
                                   .Skip(startIndex).Take(size).ToListAsync();

            var result = new ThreadsInfo
            {
                Amount  = threads.Count,
                Threads = threads.Select(t => new ThreadInfo
                {
                    Id                  = t.Id,
                    Title               = t.Title,
                    Content             = t.Content,
                    SectionName         = t.Section.Name,
                    CreationDateTimeUtc = t.CreationDateTimeUtc,
                    AuthorUsername      = t.Author.Username,
                    LikesDislikes       = t.Likes.Where(l => l.IsLike).Count() - t.Likes.Where(l => !l.IsLike).Count()
                }).ToList(),
                Size       = size,
                StartIndex = startIndex
            };

            return(ServiceResult <ThreadsInfo, GetThreadsInfoError> .CreateSuccess(result));
        }