public async Task <ImageFile> AddAsync(Image image)
        {
            var libraryDirectory = FileService.GetFileInfo(image.ImageFile.LibraryFullName).Directory.FullName;

            if (!libraryDirectory.EndsWith("\\"))
            {
                libraryDirectory += "\\";
            }

            var filePath = FileSystemInfo.FileSystemInfo.RootDirectory + libraryDirectory + FileSystemInfo.FileSystemInfo.ImagesDirectory + Guid.NewGuid().ToString() + ImageExtensionHelper.ExtensionToString(image.ImageFile.Extension);
            var path     = await Task.Run(() => FileService.AddFile(filePath, image.ImageContent));

            var fileInfo = FileService.GetFileInfo(path);

            var imageFile =
                ImageFileBuilder
                .StartBuilding()
                .WithName(image.ImageFile.Name)
                .WithExtension(image.ImageFile.Extension)
                .WithFullName(path)
                .WithLibraryFullName(image.ImageFile.LibraryFullName)
                .WithCreationTime(fileInfo.CreationTimeUtc)
                .WithLastAccessTime(fileInfo.LastAccessTimeUtc)
                .WithLastWriteTime(fileInfo.LastWriteTimeUtc)
                .WithSize(fileInfo.Length)
                .WithTags(new List <Tag>())
                .Build();

            Logger.LogInformation("New added image: " + imageFile.FullName);

            return(imageFile);
        }
        public async Task <ImageFile> UpdateAsync(Image entity)
        {
            if (!FileService.FileExists(entity.ImageFile.FullName))
            {
                throw new ContentNotFoundException("Couldn't find specified file.");
            }

            // overwrites the file
            var path = await Task.Run(() => FileService.AddFile(entity.ImageFile.FullName, entity.ImageContent));

            Logger.LogInformation("Updated content of an image: " + path);

            return(entity.ImageFile);
        }
        public async Task <IActionResult> PutImage([FromBody] Image image)
        {
            try
            {
                var library = await LibraryRepository.GetBySourceAsync(image.ImageFile.LibraryFullName);

                if (library == null)
                {
                    return(BadRequest("Library doesn't exist"));
                }

                var userId = User?.Identity.Name;
                if (!library.Owners.Where(x => x.ToString() == userId).Any())
                {
                    return(Unauthorized());
                }

                bool imageExistsInLibrary = library.Images.Remove(library.Images.Find(x => x.FullName == image.ImageFile.FullName));

                if (!imageExistsInLibrary)
                {
                    return(BadRequest());
                }

                var updatedImage = await ImageRepository.UpdateAsync(image);

                library.Images.Add(updatedImage);

                await LibraryRepository.UpdateAsync(library);
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }
            catch (ContentNotFoundException e)
            {
                return(NotFound(new { message = e.Message }));
            }
            catch (Exception e)
            {
                Logger.LogError(e, e.Message);
                return(StatusCode(500));
            }

            return(NoContent());
        }
        public async Task <ActionResult <ImageFile> > PostImage([FromBody] Image image)
        {
            ImageFile imageFile = null;

            try
            {
                var library = await LibraryRepository.GetBySourceAsync(image.ImageFile.LibraryFullName);

                if (library == null)
                {
                    return(BadRequest("Library doesn't exist"));
                }

                var userId = User?.Identity.Name;
                if (!library.Owners.Where(x => x.ToString() == userId).Any())
                {
                    return(Unauthorized());
                }

                imageFile = await ImageRepository.AddAsync(image);

                library.Images.Add(imageFile);
                await LibraryRepository.UpdateAsync(library);
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }
            catch (ContentNotFoundException e)
            {
                return(NotFound(new { message = e.Message }));
            }
            catch (Exception e)
            {
                Logger.LogError(e, e.Message);
                return(StatusCode(500));
            }

            return(CreatedAtAction("GetImage", new { name = imageFile.Name }, imageFile));
        }