Пример #1
0
 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);
 }
Пример #2
0
        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}");
        }
Пример #3
0
 public IHttpActionResult PutImageFile(ImageFileDTO file)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     return(Ok(_imageFileService.UpdateOrCreateImageFile(file)));
 }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }