private void ValidateFile(ImageFileDTO imageFile)
 {
     if (imageFile == null)
     {
         throw new ServiceException(ExceptionType.NullException, $"Image file is null!");
     }
     if (!(imageFile.Name?.Length > 0))
     {
         throw new ServiceException("File name is empty!")
               {
                   Type = ExceptionType.EmptyStringException
               }
     }
     ;
     if (imageFile.FolderPath == null)
     {
         throw new ServiceException("Folder name is empty!")
               {
                   Type           = ExceptionType.EmptyStringException,
                   ExceptionValue = nameof(imageFile.FolderPath)
               }
     }
     ;
     CheckFormatAndGetExtension(imageFile.Name);
 }
        public string GetImagePath(int fileId)
        {
            ImageFileDTO file = GetImageFileById(fileId)
                                ?? throw new ServiceException(ExceptionType.NotFoundException,
                                                              $"File with id = {fileId} was not found!");

            return($"{file.FolderPath}/{file.Name}");
        }
Beispiel #3
0
 public IHttpActionResult PutImageFile(ImageFileDTO file)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     return(Ok(_imageFileService.UpdateOrCreateImageFile(file)));
 }
        public int AddImageFile(ImageFileDTO file)
        {
            ValidateFile(file);
            if (GetFileByNameAndFolder(file.Name, file.FolderPath) == null)
            {
                throw new ServiceException(ExceptionType.UniqueException,
                                           "Such image file already exists!");
            }
            File fileDAL = _db.FileRepository.Add(_mapper.Map <File>(file));

            _db.Save();
            return(fileDAL.Id);
        }
        public int UpdateOrCreateImageFile(ImageFileDTO file)
        {
            if (file == null)
            {
                throw new ServiceException(ExceptionType.NullException, "Image file is null");
            }
            var unique = GetFileByNameAndFolder(file.Name, file.FolderPath);

            if ((unique != null) && (unique.Id != file.Id))
            {
                return(unique.Id);
            }
            File fileDAL = _db.FileRepository.Get(file.Id);

            if (fileDAL == null)
            {
                return(AddImageFile(file));
            }
            ValidateFile(file);
            fileDAL.Folder = file.FolderPath;
            fileDAL.Name   = file.Name;
            _db.Save();
            return(fileDAL.Id);
        }