Dont panic.
Handle errors in image scaling properly.
This commit is contained in:
parent
1a421537fb
commit
88b0d8b57b
25
src/main.rs
25
src/main.rs
@ -169,15 +169,22 @@ fn show_image<'mw>(req: &mut Request, mut res: Response<'mw>)
|
||||
if let Ok(photo) = req.orm_get::<Photo>("id", &id) {
|
||||
if req.authorized_user().is_some() || photo.is_public() {
|
||||
if let Some(size) = match req.param("size").unwrap() {
|
||||
"s" => Some(200),
|
||||
"m" => Some(800),
|
||||
"l" => Some(1200),
|
||||
_ => None
|
||||
} {
|
||||
let buf = req.photos().get_scaled_image(photo, size, size);
|
||||
res.set(MediaType::Jpeg);
|
||||
res.set(Expires(HttpDate(time::now() + Duration::days(14))));
|
||||
return res.send(buf);
|
||||
"s" => Some(200),
|
||||
"m" => Some(800),
|
||||
"l" => Some(1200),
|
||||
_ => None
|
||||
} {
|
||||
match req.photos().get_scaled_image(photo, size, size) {
|
||||
Ok(buf) => {
|
||||
res.set(MediaType::Jpeg);
|
||||
res.set(Expires(HttpDate(time::now() + Duration::days(14))));
|
||||
return res.send(buf);
|
||||
},
|
||||
Err(err) => {
|
||||
return res.error(StatusCode::InternalServerError,
|
||||
format!("{}", err));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use image::open as image_open;
|
||||
use image::{FilterType, ImageFormat, GenericImage};
|
||||
use image::{FilterType, ImageFormat, GenericImage, ImageError};
|
||||
use rexif::{self, ExifData};
|
||||
|
||||
use models::Photo;
|
||||
@ -20,10 +20,10 @@ impl PhotosDir {
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_scaled_image(&self, photo: Photo, width: u32, height: u32)
|
||||
-> Vec<u8> {
|
||||
-> Result<Vec<u8>, ImageError> {
|
||||
let path = self.basedir.join(photo.path);
|
||||
info!("Should open {:?}", path);
|
||||
let img = image_open(path).unwrap();
|
||||
let img = try!(image_open(path));
|
||||
let img =
|
||||
if width < img.width() || height < img.height() {
|
||||
img.resize(width, height, FilterType::CatmullRom)
|
||||
@ -43,8 +43,8 @@ impl PhotosDir {
|
||||
};
|
||||
// TODO Put the icon in some kind of cache!
|
||||
let mut buf : Vec<u8> = Vec::new();
|
||||
img.save(&mut buf, ImageFormat::JPEG).unwrap();
|
||||
buf
|
||||
try!(img.save(&mut buf, ImageFormat::JPEG));
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
Loading…
Reference in New Issue
Block a user