Skip to content
Commits on Source (2)
......@@ -2,6 +2,13 @@
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
## [0.4.5](https://dev.solidev.net/solidev/rustize/compare/v0.4.4...v0.4.5) (2024-01-19)
### Bug Fixes
* **misc:** clean (clippy) code, move to bookworm base, bump version to 0.4.5 ([052c6f6](https://dev.solidev.net/solidev/rustize/commit/052c6f658da3bc30c81dc904a7f6b91e2871b10a))
## [0.4.4](https://dev.solidev.net/solidev/rustize/compare/v0.4.3...v0.4.4) (2023-11-03)
......
......@@ -37,6 +37,54 @@ dependencies = [
"memchr",
]
[[package]]
name = "anstream"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "async-trait"
version = "0.1.74"
......@@ -194,6 +242,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "core-foundation"
version = "0.9.3"
......@@ -339,6 +393,16 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "env_filter"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.10.0"
......@@ -352,6 +416,19 @@ dependencies = [
"termcolor",
]
[[package]]
name = "env_logger"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9eeb342678d785662fd2514be38c459bb925f02b68dd2a3e0f21d7ef82d979dd"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]]
name = "errno"
version = "0.3.5"
......@@ -359,7 +436,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860"
dependencies = [
"libc",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
......@@ -763,7 +840,7 @@ checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [
"hermit-abi",
"rustix",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
......@@ -914,7 +991,7 @@ checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0"
dependencies = [
"libc",
"wasi",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
......@@ -1064,7 +1141,7 @@ dependencies = [
"libc",
"redox_syscall 0.4.1",
"smallvec",
"windows-targets",
"windows-targets 0.48.5",
]
[[package]]
......@@ -1122,7 +1199,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c"
dependencies = [
"env_logger",
"env_logger 0.10.0",
"log",
]
......@@ -1340,14 +1417,14 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
name = "rustize"
version = "0.4.3"
version = "0.4.5"
dependencies = [
"env_logger",
"env_logger 0.11.0",
"futures",
"hyper",
"image",
......@@ -1388,7 +1465,7 @@ version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88"
dependencies = [
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
......@@ -1532,7 +1609,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9"
dependencies = [
"libc",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
......@@ -1609,7 +1686,7 @@ dependencies = [
"fastrand",
"redox_syscall 0.4.1",
"rustix",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
......@@ -1712,7 +1789,7 @@ dependencies = [
"signal-hook-registry",
"socket2 0.5.5",
"tokio-macros",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
......@@ -1836,6 +1913,12 @@ version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "vcpkg"
version = "0.2.15"
......@@ -2004,7 +2087,16 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
......@@ -2013,13 +2105,28 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]]
......@@ -2028,42 +2135,84 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "winreg"
version = "0.50.0"
......@@ -2071,7 +2220,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1"
dependencies = [
"cfg-if",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
......
[package]
name = "rustize"
version = "0.4.3"
version = "0.4.5"
edition = "2021"
repository = "https://dev.solidev.net/solidev/rustize"
......@@ -12,13 +12,8 @@ tokio = { version = "1", features = ["full"] }
pretty_env_logger = "0.5"
rust-s3 = "0.33"
futures = "0.3"
image = { version = "0.24", features = [
"webp",
"webp-encoder",
"jpeg",
"png",
] }
image = { version = "0.24", features = ["webp", "webp-encoder", "jpeg", "png"] }
log = "0.4"
env_logger = "0.10"
env_logger = "0.11.0"
path-clean = "1.0"
urlencoding = "2.1"
FROM rust:bullseye AS builder
FROM rust:bookworm AS builder
COPY . .
RUN cargo build --release
FROM debian:bullseye
FROM debian:bookworm
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=builder ./target/release/rustize /usr/local/bin/rustize
CMD ["/usr/local/bin/rustize"]
\ No newline at end of file
......@@ -2,5 +2,5 @@ apiVersion: v2
name: rustize
description: Rustize is an image processing service written in Rust.
type: application
version: 0.4.3
appVersion: "0.4.3"
version: 0.4.5
appVersion: "0.4.5"
......@@ -58,7 +58,7 @@ impl Current {
}
pub fn have_action(&self) -> bool {
return !self.actions.is_empty();
!self.actions.is_empty()
}
}
......@@ -93,7 +93,7 @@ fn get_actions(path: &str) -> Option<Vec<Action>> {
for part_data in parts.skip(1) {
debug!("Checking action: {}", part_data);
let action_data: Vec<&str> = part_data.split("_").collect();
let action_data: Vec<&str> = part_data.split('_').collect();
match action_data[0] {
"resize" => out.push(Action::Resize(ResizeAction::new(&action_data))),
"size" => out.push(Action::Resize(ResizeAction::new(&action_data))),
......@@ -122,7 +122,7 @@ fn get_actions(path: &str) -> Option<Vec<Action>> {
_ => debug!("Action {} not found", action_data[0]),
};
}
return Some(out);
Some(out)
}
/// Get the parameters from the path
......@@ -141,7 +141,7 @@ pub fn get_params(path: &str) -> Option<Current> {
format: format.into(),
actions,
};
return Some(cur);
Some(cur)
}
pub fn process_actions(image: DynamicImage, params: &Current) -> DynamicImage {
......@@ -160,7 +160,7 @@ pub fn process_actions(image: DynamicImage, params: &Current) -> DynamicImage {
Action::Grayscale(act) => img = act.process(img),
}
}
return img;
img
}
#[cfg(test)]
mod tests {
......
......@@ -6,10 +6,10 @@ pub struct BlurAction {
impl BlurAction {
pub fn process(&self, image: DynamicImage) -> DynamicImage {
debug!("Blurring image");
return image.blur(self.radius);
image.blur(self.radius)
}
pub fn new(params: &Vec<&str>) -> Self {
pub fn new(params: &[&str]) -> Self {
Self {
radius: params.get(1).unwrap_or(&"").parse().unwrap_or(3.0),
}
......
......@@ -19,7 +19,7 @@ impl BorderAction {
/// - RRGGBB (hex)
/// - RRGGBBAA (hex)
/// - html named color (https://www.w3schools.com/colors/colors_names.asp)
pub fn new(params: &Vec<&str>) -> Self {
pub fn new(params: &[&str]) -> Self {
Self {
size: params.get(1).unwrap_or(&"").parse().unwrap_or(5),
color: params.get(2).unwrap_or(&"black").to_string(),
......@@ -51,6 +51,6 @@ impl BorderAction {
}
}
return img;
img
}
}
use image::Rgba;
pub fn get_color_from_string(color: &String) -> Rgba<u8> {
let colors: Vec<&str> = color.split(",").collect();
let colors: Vec<&str> = color.split(',').collect();
let mut px = Rgba([0, 0, 0, 255]);
if colors.len() == 4 {
let colors: Vec<u8> = colors
......@@ -15,7 +15,7 @@ pub fn get_color_from_string(color: &String) -> Rgba<u8> {
.iter()
.map(|f| -> u8 { f.parse().unwrap_or(255) })
.collect();
return Rgba([colors[0], colors[1], colors[2], 255]);
Rgba([colors[0], colors[1], colors[2], 255])
} else {
px = match color.as_str() {
// All html named colors
......@@ -175,32 +175,32 @@ pub fn get_color_from_string(color: &String) -> Rgba<u8> {
let r = u8::from_str_radix(&color[0..1], 16).unwrap_or(0);
let g = u8::from_str_radix(&color[1..2], 16).unwrap_or(0);
let b = u8::from_str_radix(&color[2..3], 16).unwrap_or(0);
return Rgba([r, g, b, 255]);
Rgba([r, g, b, 255])
} else if color.len() == 4 {
// Parse as RGBA hex
let r = u8::from_str_radix(&color[0..1], 16).unwrap_or(0);
let g = u8::from_str_radix(&color[1..2], 16).unwrap_or(0);
let b = u8::from_str_radix(&color[2..3], 16).unwrap_or(0);
let a = u8::from_str_radix(&color[3..4], 16).unwrap_or(0);
return Rgba([r, g, b, a]);
Rgba([r, g, b, a])
} else if color.len() == 6 {
// Parse as RRGGBB hex
let r = u8::from_str_radix(&color[0..2], 16).unwrap_or(0);
let g = u8::from_str_radix(&color[2..4], 16).unwrap_or(0);
let b = u8::from_str_radix(&color[4..6], 16).unwrap_or(0);
return Rgba([r, g, b, 255]);
Rgba([r, g, b, 255])
} else if color.len() == 8 {
// Parse as RRGGBBAA hex
let r = u8::from_str_radix(&color[0..2], 16).unwrap_or(0);
let g = u8::from_str_radix(&color[2..4], 16).unwrap_or(0);
let b = u8::from_str_radix(&color[4..6], 16).unwrap_or(0);
let a = u8::from_str_radix(&color[6..8], 16).unwrap_or(0);
return Rgba([r, g, b, a]);
Rgba([r, g, b, a])
} else {
return px;
px
}
}
};
return px;
px
}
}
......@@ -12,7 +12,7 @@ impl ContainAction {
/// 1. width: number (destination width, default = 128)
/// 2. height: number (destination height, default = width)
/// 3. color: string (background color, default = transparent)
pub fn new(params: &Vec<&str>) -> Self {
pub fn new(params: &[&str]) -> Self {
let width = params.get(1).unwrap_or(&"").parse().unwrap_or(128);
Self {
width,
......@@ -49,7 +49,7 @@ impl ContainAction {
.get_pixel_mut(x + offset_x as u32, y + offset_y as u32)
.blend(&pixel);
}
return DynamicImage::ImageRgba8(background);
DynamicImage::ImageRgba8(background)
} else {
let new_width = (image_width as f32 / w_ratio) as u32;
let new_height = (image_height as f32 / w_ratio) as u32;
......@@ -64,7 +64,7 @@ impl ContainAction {
.get_pixel_mut(x + offset_x as u32, y + offset_y as u32)
.blend(&pixel);
}
return DynamicImage::ImageRgba8(background);
DynamicImage::ImageRgba8(background)
}
}
}
......@@ -11,7 +11,7 @@ impl CoverAction {
/// 1. width: number (destination width, default = 128)
/// 2. height: number (destination height, default = width)
/// 3. align: string (align position, default = center) - NOT IMPLEMENTED
pub fn new(params: &Vec<&str>) -> Self {
pub fn new(params: &[&str]) -> Self {
let width = params.get(1).unwrap_or(&"").parse().unwrap_or(128);
Self {
width,
......@@ -51,7 +51,7 @@ impl CoverAction {
background.get_pixel_mut(x as u32, y as u32).blend(&pixel);
}
}
return DynamicImage::ImageRgba8(background);
DynamicImage::ImageRgba8(background)
} else {
let new_width = (image_width as f32 / w_ratio) as u32 + 1;
let new_height = (image_height as f32 / w_ratio) as u32 + 1;
......@@ -68,7 +68,7 @@ impl CoverAction {
background.get_pixel_mut(x as u32, y as u32).blend(&pixel);
}
}
return DynamicImage::ImageRgba8(background);
DynamicImage::ImageRgba8(background)
}
}
}
......@@ -19,9 +19,9 @@ impl CropAction {
if height == 0 {
height = image.height() as usize
}
return image.crop_imm(x as u32, y as u32, width as u32, height as u32);
image.crop_imm(x as u32, y as u32, width as u32, height as u32)
}
pub fn new(params: &Vec<&str>) -> Self {
pub fn new(params: &[&str]) -> Self {
Self {
x: params.get(1).unwrap_or(&"").parse().unwrap_or(0),
y: params.get(2).unwrap_or(&"").parse().unwrap_or(0),
......
......@@ -3,13 +3,13 @@ pub struct GrayscaleAction {}
impl GrayscaleAction {
/// Create a new GrayscaleAction. No parameters are needed.
pub fn new(_params: &Vec<&str>) -> Self {
pub fn new(_params: &[&str]) -> Self {
Self {}
}
/// Convert the image to grayscale.
pub fn process(&self, image: DynamicImage) -> DynamicImage {
debug!("Converting image to grayscale");
return image.grayscale();
image.grayscale()
}
}
......@@ -23,7 +23,7 @@ impl ResizeAction {
///
///
/// If both width and height are 0, the image will be resized to 128x0
pub fn new(params: &Vec<&str>) -> Self {
pub fn new(params: &[&str]) -> Self {
Self {
width: params.get(1).unwrap_or(&"").parse().unwrap_or(0),
height: params.get(2).unwrap_or(&"").parse().unwrap_or(0),
......@@ -55,6 +55,6 @@ impl ResizeAction {
_ => FilterType::Lanczos3,
};
return image.resize(width as u32, height as u32, filter);
image.resize(width as u32, height as u32, filter)
}
}
......@@ -8,7 +8,7 @@ impl RotateAction {
/// 1. angle: number (angle in degrees, default = 90; allowed values: 90, 180, 270)
/// (must be a multiple of 90, if not, it will be rounded to the nearest multiple of 90)
///
pub fn new(params: &Vec<&str>) -> Self {
pub fn new(params: &[&str]) -> Self {
let mut angle: isize = params.get(1).unwrap_or(&"").parse().unwrap_or(90);
angle = (angle.abs().div_euclid(90) * 90) % 360;
Self {
......@@ -18,11 +18,11 @@ impl RotateAction {
/// Rotate the image to the given angle.
pub fn process(&self, image: DynamicImage) -> DynamicImage {
error!("Rotating image");
return match self.angle {
match self.angle {
90 => image.rotate90(),
180 => image.rotate180(),
270 => image.rotate270(),
_ => image,
};
}
}
}
......@@ -19,7 +19,7 @@ pub fn encode_image(image: &DynamicImage, params: &Current) -> Vec<u8> {
image.color(),
)
.unwrap();
return bytes;
bytes
} else if params.format.eq("png") {
info!("Encoding image to png format");
let mut bytes: Vec<u8> = Vec::new();
......
use crate::actions::Current;
use image::io::Reader;
use image::DynamicImage;
use path_clean::clean;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use image::DynamicImage;
use image::io::Reader;
use path_clean::clean;
use crate::actions::Current;
// Tries to get the current image from source directory, using `extension` first then default extensions
// "jpg", "png", "gif", "jpeg", "webp"
pub async fn fs_download(cur: &Current, extension: &str) -> Option<DynamicImage> {
// Base path from RUSTIZE_FS_SOURCE_PATH, or RUSTIZE_FS_PATH if not set,
// or FS_PATH if not set, or "." if not set
let mut path = PathBuf::from(
&std::env::var("RUSTIZE_FS_SOURCE_PATH")
.unwrap_or(
std::env::var("RUSTIZE_FS_PATH")
.unwrap_or(std::env::var("FS_PATH").unwrap_or("".to_string())),
)
.to_string(),
&std::env::var("RUSTIZE_FS_SOURCE_PATH").unwrap_or(
std::env::var("RUSTIZE_FS_PATH").unwrap_or(std::env::var("FS_PATH").unwrap_or("".to_string())),
).to_string(),
);
let base_path = path.clone();
// Join base path to image path
path = path.join(PathBuf::from(&cur.path).strip_prefix("/").unwrap());
path = clean(path);
// Ensure path is inside base path
if !path.starts_with(&base_path) {
if !path.starts_with(base_path) {
error!("Path is outside base path");
return Option::None;
return None;
}
path.push(&cur.filename);
path.set_extension(extension);
......@@ -38,17 +38,17 @@ pub async fn fs_download(cur: &Current, extension: &str) -> Option<DynamicImage>
path.set_extension(ext);
if path.exists() {
let img = Reader::open(&path);
if !img.is_ok() {
if img.is_err() {
error!("Error opening image");
continue;
}
let img = img.unwrap().with_guessed_format();
if !img.is_ok() {
if img.is_err() {
error!("Error guessing image format");
continue;
}
let img = img.unwrap().decode();
if !img.is_ok() {
if img.is_err() {
error!("Error decoding image");
continue;
}
......@@ -57,19 +57,16 @@ pub async fn fs_download(cur: &Current, extension: &str) -> Option<DynamicImage>
}
error!("No image found");
return Option::None;
None
}
pub async fn fs_upload(bytes: &Vec<u8>, params: &Current) -> bool {
// Base path from RUSTIZE_FS_DEST_PATH, or RUSTIZE_FS_PATH if not set,
// or FS_PATH if not set, or "." if not set
let mut path = PathBuf::from(
&std::env::var("RUSTIZE_FS_DEST_PATH")
.unwrap_or(
std::env::var("RUSTIZE_FS_PATH")
.unwrap_or(std::env::var("FS_PATH").unwrap_or("".to_string())),
)
.to_string(),
&std::env::var("RUSTIZE_FS_DEST_PATH").unwrap_or(
std::env::var("RUSTIZE_FS_PATH").unwrap_or(std::env::var("FS_PATH").unwrap_or("".to_string())),
).to_string(),
);
let base_path = path.clone();
debug!("Base path: {}", path.to_str().unwrap());
......@@ -77,7 +74,7 @@ pub async fn fs_upload(bytes: &Vec<u8>, params: &Current) -> bool {
path = path.join(PathBuf::from(&params.orig_path).strip_prefix("/").unwrap());
path = clean(path);
// Ensure path is inside base path
if !path.starts_with(&base_path) {
if !path.starts_with(base_path) {
error!("Path is outside base path");
return false;
}
......@@ -85,7 +82,7 @@ pub async fn fs_upload(bytes: &Vec<u8>, params: &Current) -> bool {
// Create path if it doesn't exist
let parent = path.parent().unwrap();
if !parent.exists() {
if let Err(e) = std::fs::create_dir_all(parent.clone()) {
if let Err(e) = std::fs::create_dir_all(parent) {
error!("Error creating directory: {}", e);
return false;
}
......@@ -98,7 +95,7 @@ pub async fn fs_upload(bytes: &Vec<u8>, params: &Current) -> bool {
error!("Error saving image: {}", e);
return false;
}
return true;
true
}
pub async fn fs_get_existing(cur: &Current) -> Option<Vec<u8>> {
......@@ -106,21 +103,18 @@ pub async fn fs_get_existing(cur: &Current) -> Option<Vec<u8>> {
// or FS_PATH if not set, or "." if not set
debug!("Looking for existing file {}", cur.orig_path);
let mut path = PathBuf::from(
&std::env::var("RUSTIZE_FS_DEST_PATH")
.unwrap_or(
std::env::var("RUSTIZE_FS_PATH")
.unwrap_or(std::env::var("FS_PATH").unwrap_or("".to_string())),
)
.to_string(),
&std::env::var("RUSTIZE_FS_DEST_PATH").unwrap_or(
std::env::var("RUSTIZE_FS_PATH").unwrap_or(std::env::var("FS_PATH").unwrap_or("".to_string())),
).to_string(),
);
let base_path = path.clone();
// Join base path to image path
path = path.join(PathBuf::from(&cur.orig_path).strip_prefix("/").unwrap());
path = clean(path);
// Ensure path is inside base path
if !path.starts_with(&base_path) {
if !path.starts_with(base_path) {
error!("Path is outside base path");
return Option::None;
return None;
}
if path.exists() {
let f = File::open(path);
......@@ -130,20 +124,20 @@ pub async fn fs_get_existing(cur: &Current) -> Option<Vec<u8>> {
let res = f.read_to_end(&mut data);
match res {
Ok(_) => {
return Some(data);
Some(data)
}
Err(e) => {
error!("Error reading file: {}", e);
return Option::None;
None
}
}
}
Err(e) => {
error!("Error opening file: {}", e);
return Option::None;
None
}
};
}
info!("No existing file found for {}", cur.orig_path);
return Option::None;
None
}
......@@ -11,14 +11,14 @@ pub async fn download(cur: &Current, extension: &str) -> Option<DynamicImage> {
let source = env::var("RUSTIZE_SOURCE").unwrap_or("none".to_string());
match source.as_str() {
"s3" => {
return s3utils::s3_download(cur, extension).await;
s3utils::s3_download(cur, extension).await
}
"fs" => {
return fsutils::fs_download(cur, extension).await;
fsutils::fs_download(cur, extension).await
}
_ => {
error!("No source defined, image download cancelled");
return Option::None;
None
}
}
}
......@@ -27,14 +27,14 @@ pub async fn save(bytes: &Vec<u8>, params: &Current) -> bool {
let dest = env::var("RUSTIZE_DEST").unwrap_or("none".to_string());
match dest.as_str() {
"s3" => {
return s3utils::s3_upload(bytes, params).await;
s3utils::s3_upload(bytes, params).await
}
"fs" => {
return fsutils::fs_upload(bytes, params).await;
fsutils::fs_upload(bytes, params).await
}
_ => {
error!("No dest defined, image upload cancelled");
return false;
false
}
}
}
......@@ -43,14 +43,14 @@ pub async fn get_existing(params: &Current) -> Option<Vec<u8>> {
let dest = env::var("RUSTIZE_DEST").unwrap_or("none".to_string());
match dest.as_str() {
"s3" => {
return s3utils::s3_get_existing(params).await;
s3utils::s3_get_existing(params).await
}
"fs" => {
return fsutils::fs_get_existing(params).await;
fsutils::fs_get_existing(params).await
}
_ => {
error!("No dest defined, image upload cancelled");
return Option::None;
None
}
}
}
......@@ -69,7 +69,7 @@ async fn process_request(req: Request<Body>) -> Result<Response<Body>, Infallibl
// If we are here, then something is wrong
let mut resp = Response::new("Image not found".into());
*resp.status_mut() = StatusCode::NOT_FOUND;
return Ok(resp);
Ok(resp)
}
#[tokio::main]
......
......@@ -39,7 +39,7 @@ pub fn get_source_bucket() -> Bucket {
let bucket = Bucket::new(bucket_name.as_str(), region, credentials)
.unwrap()
.with_path_style();
return bucket;
bucket
}
pub fn get_dest_bucket() -> Bucket {
......@@ -75,15 +75,15 @@ pub fn get_dest_bucket() -> Bucket {
let bucket = Bucket::new(bucket_name.as_str(), region, credentials)
.unwrap()
.with_path_style();
return bucket;
bucket
}
pub async fn s3_upload(data: &Vec<u8>, params: &Current) -> bool {
pub async fn s3_upload(data: &[u8], params: &Current) -> bool {
let bucket = get_dest_bucket();
let response_data = bucket
.put_object_with_content_type(
params.orig_path.clone(), // SEE: reprocess path for path mapping ?
&*data,
data,
params.get_content_type(),
)
.await;
......@@ -92,7 +92,7 @@ pub async fn s3_upload(data: &Vec<u8>, params: &Current) -> bool {
return true;
}
println!("error");
return false;
false
}
// Tries to download the current image from source, using `extension` first then default extensions
......@@ -106,7 +106,7 @@ pub async fn s3_download(cur: &Current, extension: &str) -> Option<DynamicImage>
let mut ext: Option<&str> = None;
info!("downloading to get {}/{}.???", cur.path, cur.filename);
for e in extensions {
if e == "" {
if e.is_empty() {
continue;
}
let filename = format!("{}/{}.{}", cur.path, cur.filename, e);
......@@ -126,13 +126,13 @@ pub async fn s3_download(cur: &Current, extension: &str) -> Option<DynamicImage>
if let Some(rd) = resp {
trace!("Status code {} for ext {}", rd.status_code(), ext.unwrap());
let bytes = rd.bytes();
if let Ok(image) = load_from_memory(&*bytes) {
if let Ok(image) = load_from_memory(bytes) {
return Some(image);
}
} else {
warn!("Not found")
}
return None;
None
}
// Tries to download the current image from source, using `extension` first then default extensions
......@@ -156,5 +156,5 @@ pub async fn s3_get_existing(cur: &Current) -> Option<Vec<u8>> {
} else {
warn!("Not found")
}
return None;
None
}