Example #1
0
        private void SaveImage(long id, HttpPostedFileBase file)
        {
            if (file != null)
            {
                try
                {
                    string[] allowedMimeTypes = GlobalConfig.Get().AllowMimeType.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (allowedMimeTypes.Contains(file.ContentType.ToLower()))
                    {
                        Image image = Image.FromStream(file.InputStream);

                        string imagePath = HttpContext.Current.Request.MapPath(GlobalConfig.Get().ImagePath);
                        string imageName = String.Format("img_{0}.png", id);

                        //save to file system
                        image.Save(String.Format("{0}{1}", imagePath, imageName), System.Drawing.Imaging.ImageFormat.Png);

                        //update the database
                        BookRepository bRepo = new BookRepository(_dal);

                        long ret = bRepo.SetImage(id, imageName);
                        if(ret == 0)
                            throw new Exception("Book was not updated");
                    }
                    else
                    {
                        throw new Exception("The image format is not allowed.");
                    }
                }
                catch (Exception ex)
                {
                    log.Fatal(ex);
                    throw;
                }
            }
        }
Example #2
0
        public void RemoveImage(long id)
        {
            BookRepository bRepo = new BookRepository(_dal);

            Book book = bRepo.Get(id);
            if(book == null)
                throw new HttpException(404, "Ressource not found");

            if(String.IsNullOrEmpty(book.image))
                throw new HttpException(404, "Book doesn't contain an image");

            try
            {
                long ret = bRepo.SetImage(id, null);
                if(ret == 0)
                    throw new Exception("Book was not updated.");
            }
            catch(Exception ex)
            {
                log.Error(ex);
                throw;
            }
        }