Skip to content

Add ability to put image in specified subfolder (issue #26) #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 23 additions & 10 deletions project/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ use iron::status;

use regex::*;

lazy_static! {
static ref MEDIA_ORIGINAL: String = get_media_directory("MEDIA_ORIGINAL");
static ref MEDIA_REGULAR: String = get_media_directory("MEDIA_REGULAR");
}
use std::fs::DirBuilder;

fn main() {
fn handler(req: &mut Request) -> IronResult<Response> {
let regex = Regex::new(r"^transform/([^\\/]+)/(.+)").unwrap();
fn handler(req: &mut Request) -> IronResult<Response> {
let regex = Regex::new(r"^transform/([^\\/]+)/([^\\/]+)/(.+)").unwrap();
match regex.captures(&req.url.path.join("/")) {
Some(cap) => {
match matchers::create_operations(cap.at(1).unwrap()) {
Ok(operations) => match load::load_image(cap.at(2).unwrap(), &operations, &MEDIA_ORIGINAL, &MEDIA_REGULAR) {
let folder_param = cap.at(1).unwrap().to_string();
let media_original = get_media_directory("MEDIA_ORIGINAL", &folder_param);
let media_regular = get_media_directory("MEDIA_REGULAR", &folder_param);

if folder_param != "-" {
let mut builder = DirBuilder::new();
builder
.recursive(true)
.create(["/media/" ,&folder_param, "/"].concat()).unwrap();
}

match matchers::create_operations(cap.at(2).unwrap()) {
Ok(operations) => match load::load_image(cap.at(3).unwrap(), &operations, &media_original, &media_regular) {
Ok(image) => {
let buffer = transformation::apply_operations(&image, &operations);
let content_type = load::ext_to_content_type(image.ext).parse::<Mime>().unwrap();
Expand All @@ -44,11 +52,16 @@ fn main() {
Iron::new(handler).http("0.0.0.0:3000").unwrap();
}

fn get_media_directory(key: &str) -> String {
fn get_media_directory(key: &str, folder: &str) -> String {
let value = std::env::var_os(key);
if value.is_none() {

if value.is_none() && folder == "-" {
return "/media/".to_string();
}

if value.is_none() && folder != "-" {
return ["/media/", folder, "/"].concat();
}

value.unwrap().into_string().unwrap()
}