Skip to content

Commit

Permalink
refactor qr_code util
Browse files Browse the repository at this point in the history
  • Loading branch information
robatipoor committed Feb 24, 2024
1 parent 895821f commit cb29de1
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 81 deletions.
2 changes: 0 additions & 2 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ fake = { workspace = true }
futures-util = { workspace = true }
hyper = { workspace = true }
once_cell = { workspace = true }
qrcode = { workspace = true }
base64 = { workspace = true }
rand = { workspace = true }
reqwest = { workspace = true }
Expand All @@ -43,5 +42,4 @@ tower-http = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
url = { workspace = true }
image = { workspace = true }
garde = { workspace = true }
14 changes: 0 additions & 14 deletions api/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ pub enum ApiError {
#[error(transparent)]
Utf8Error(#[from] std::str::Utf8Error),
#[error(transparent)]
QrCodeError(#[from] qrcode::types::QrError),
#[error(transparent)]
MultipartError(#[from] axum::extract::multipart::MultipartError),
#[error(transparent)]
UrlError(#[from] url::ParseError),
#[error(transparent)]
ImageError(#[from] image::ImageError),
#[error("lock error: {0}")]
LockError(String),
#[error("duration out of range error: {0}")]
Expand Down Expand Up @@ -134,11 +130,6 @@ impl ApiError {
err.to_string(),
StatusCode::INTERNAL_SERVER_ERROR,
),
QrCodeError(err) => (
"QRCODE_ERROR",
err.to_string(),
StatusCode::INTERNAL_SERVER_ERROR,
),
MultipartError(err) => (
"MULTIPART_ERROR",
err.to_string(),
Expand All @@ -154,11 +145,6 @@ impl ApiError {
err.to_string(),
StatusCode::INTERNAL_SERVER_ERROR,
),
ImageError(err) => (
"IMAGE_ERROR",
err.to_string(),
StatusCode::INTERNAL_SERVER_ERROR,
),
UnknownError(err) => (
"UNKNOWN_ERROR",
err.to_string(),
Expand Down
8 changes: 6 additions & 2 deletions api/src/handler/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ pub fn generate_qr_code(
input: &str,
) -> ApiResult<Option<String>> {
match qr_code_format {
Some(QrCodeFormat::Text) => Ok(Some(crate::util::qr_code::encode_to_text_format(input)?)),
Some(QrCodeFormat::Image) => Ok(Some(crate::util::qr_code::encode_to_image_format(input)?)),
Some(QrCodeFormat::Text) => Ok(Some(sdk::util::qr_code::generate_base64_text_qr_code(
input,
)?)),
Some(QrCodeFormat::Image) => Ok(Some(sdk::util::qr_code::generate_base64_png_qr_code(
input,
)?)),
None => Ok(None),
}
}
Expand Down
1 change: 0 additions & 1 deletion api/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub mod file_name;
pub mod hash;
pub mod http;
pub mod multipart;
pub mod qr_code;
pub mod secret;
pub mod task;
pub mod test;
Expand Down
45 changes: 0 additions & 45 deletions api/src/util/qr_code.rs

This file was deleted.

25 changes: 8 additions & 17 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::path::{Path, PathBuf};

use base64::{engine::general_purpose::STANDARD, Engine};
use sdk::{
dto::{
request::{QrCodeFormat, UploadQueryParam},
request::UploadQueryParam,
response::{ApiResponseResult, BodyResponseError, MessageResponse},
FileUrlPath,
},
util::file::{add_extension, rm_extra_extension},
};
use std::path::{Path, PathBuf};
use url::Url;

use crate::{args::UploadOutput, client::CommandLineClient, util::crypto::KeyNonce};
Expand Down Expand Up @@ -44,25 +42,20 @@ pub async fn upload(args: UploadArguments) {
.await
.unwrap();
}
let qr_code_format = if args.output == UploadOutput::Json || args.output == UploadOutput::QrCode {
Some(QrCodeFormat::Text)
} else {
None
};
let query = UploadQueryParam {
let param = UploadQueryParam {
max_download: args.max_download,
code_length: args.code_length,
expire_secs: args.expire,
delete_manually: args.delete_manually,
qr_code_format,
qr_code_format: None,
};
let client = CommandLineClient::new(args.server_addr);
let (_, resp) = if args.progress_bar {
client
.upload_with_progress_bar(&source_file, &query, args.auth)
.upload_with_progress_bar(&source_file, &param, args.auth)
.await
} else {
client.upload_from(&source_file, &query, args.auth).await
client.upload_from(&source_file, &param, args.auth).await
}
.unwrap();
match resp {
Expand All @@ -71,10 +64,8 @@ pub async fn upload(args: UploadArguments) {
println!("{}", serde_json::to_string(&resp).unwrap());
}
UploadOutput::QrCode => {
println!(
"{}",
std::str::from_utf8(&STANDARD.decode(resp.qr_code.unwrap()).unwrap()).unwrap()
);
let qr_code = sdk::util::qr_code::generate_text_qr_code(&resp.url).unwrap();
println!("{qr_code}");
}
UploadOutput::Url => {
println!("{}", resp.url);
Expand Down
2 changes: 2 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ log = { workspace = true }
async-stream = { workspace = true }
mime_guess = { workspace = true }
url = { workspace = true }
qrcode = { workspace = true }
image = { workspace = true }
1 change: 1 addition & 0 deletions sdk/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod dir;
pub mod file;
pub mod qr_code;
pub mod random;
pub mod retry;
48 changes: 48 additions & 0 deletions sdk/src/util/qr_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::io::Cursor;

use base64::{engine::general_purpose::STANDARD, Engine};
use image::{ImageOutputFormat, Luma};
use qrcode::QrCode;

pub fn generate_text_qr_code(input: &str) -> anyhow::Result<String> {
Ok(
QrCode::new(input.as_bytes())?
.render::<char>()
.quiet_zone(true)
.module_dimensions(2, 1)
.build(),
)
}

pub fn generate_base64_text_qr_code(input: &str) -> anyhow::Result<String> {
Ok(STANDARD.encode(generate_text_qr_code(input)?))
}

pub fn generate_base64_png_qr_code(input: &str) -> anyhow::Result<String> {
let qr_code: image::ImageBuffer<Luma<u8>, Vec<u8>> = QrCode::new(input.as_bytes())?
.render::<Luma<u8>>()
.quiet_zone(true)
.module_dimensions(20, 20)
.build();
let mut buff = Cursor::new(Vec::new());
qr_code.write_to(&mut buff, ImageOutputFormat::Png)?;
Ok(STANDARD.encode(buff.into_inner()))
}

#[cfg(test)]
mod tests {
use super::*;
use fake::{Fake, Faker};

#[test]
pub fn test_generate_base64_text_qr_code() {
let qr_code = generate_base64_text_qr_code(&Faker.fake::<String>()).unwrap();
let _content = STANDARD.decode(qr_code.as_bytes()).unwrap();
}

#[test]
pub fn test_generate_base64_png_qr_code() {
let qr_code = generate_base64_png_qr_code(&Faker.fake::<String>()).unwrap();
let _content = STANDARD.decode(qr_code.as_bytes()).unwrap();
}
}

0 comments on commit cb29de1

Please sign in to comment.