Skip to content

Commit cb29de1

Browse files
committed
refactor qr_code util
1 parent 895821f commit cb29de1

File tree

9 files changed

+65
-81
lines changed

9 files changed

+65
-81
lines changed

api/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ fake = { workspace = true }
2222
futures-util = { workspace = true }
2323
hyper = { workspace = true }
2424
once_cell = { workspace = true }
25-
qrcode = { workspace = true }
2625
base64 = { workspace = true }
2726
rand = { workspace = true }
2827
reqwest = { workspace = true }
@@ -43,5 +42,4 @@ tower-http = { workspace = true }
4342
tracing = { workspace = true }
4443
tracing-subscriber = { workspace = true }
4544
url = { workspace = true }
46-
image = { workspace = true }
4745
garde = { workspace = true }

api/src/error/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,9 @@ pub enum ApiError {
4545
#[error(transparent)]
4646
Utf8Error(#[from] std::str::Utf8Error),
4747
#[error(transparent)]
48-
QrCodeError(#[from] qrcode::types::QrError),
49-
#[error(transparent)]
5048
MultipartError(#[from] axum::extract::multipart::MultipartError),
5149
#[error(transparent)]
5250
UrlError(#[from] url::ParseError),
53-
#[error(transparent)]
54-
ImageError(#[from] image::ImageError),
5551
#[error("lock error: {0}")]
5652
LockError(String),
5753
#[error("duration out of range error: {0}")]
@@ -134,11 +130,6 @@ impl ApiError {
134130
err.to_string(),
135131
StatusCode::INTERNAL_SERVER_ERROR,
136132
),
137-
QrCodeError(err) => (
138-
"QRCODE_ERROR",
139-
err.to_string(),
140-
StatusCode::INTERNAL_SERVER_ERROR,
141-
),
142133
MultipartError(err) => (
143134
"MULTIPART_ERROR",
144135
err.to_string(),
@@ -154,11 +145,6 @@ impl ApiError {
154145
err.to_string(),
155146
StatusCode::INTERNAL_SERVER_ERROR,
156147
),
157-
ImageError(err) => (
158-
"IMAGE_ERROR",
159-
err.to_string(),
160-
StatusCode::INTERNAL_SERVER_ERROR,
161-
),
162148
UnknownError(err) => (
163149
"UNKNOWN_ERROR",
164150
err.to_string(),

api/src/handler/file.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ pub fn generate_qr_code(
4646
input: &str,
4747
) -> ApiResult<Option<String>> {
4848
match qr_code_format {
49-
Some(QrCodeFormat::Text) => Ok(Some(crate::util::qr_code::encode_to_text_format(input)?)),
50-
Some(QrCodeFormat::Image) => Ok(Some(crate::util::qr_code::encode_to_image_format(input)?)),
49+
Some(QrCodeFormat::Text) => Ok(Some(sdk::util::qr_code::generate_base64_text_qr_code(
50+
input,
51+
)?)),
52+
Some(QrCodeFormat::Image) => Ok(Some(sdk::util::qr_code::generate_base64_png_qr_code(
53+
input,
54+
)?)),
5155
None => Ok(None),
5256
}
5357
}

api/src/util/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pub mod file_name;
33
pub mod hash;
44
pub mod http;
55
pub mod multipart;
6-
pub mod qr_code;
76
pub mod secret;
87
pub mod task;
98
pub mod test;

api/src/util/qr_code.rs

Lines changed: 0 additions & 45 deletions
This file was deleted.

cli/src/command.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
use std::path::{Path, PathBuf};
2-
3-
use base64::{engine::general_purpose::STANDARD, Engine};
41
use sdk::{
52
dto::{
6-
request::{QrCodeFormat, UploadQueryParam},
3+
request::UploadQueryParam,
74
response::{ApiResponseResult, BodyResponseError, MessageResponse},
85
FileUrlPath,
96
},
107
util::file::{add_extension, rm_extra_extension},
118
};
9+
use std::path::{Path, PathBuf};
1210
use url::Url;
1311

1412
use crate::{args::UploadOutput, client::CommandLineClient, util::crypto::KeyNonce};
@@ -44,25 +42,20 @@ pub async fn upload(args: UploadArguments) {
4442
.await
4543
.unwrap();
4644
}
47-
let qr_code_format = if args.output == UploadOutput::Json || args.output == UploadOutput::QrCode {
48-
Some(QrCodeFormat::Text)
49-
} else {
50-
None
51-
};
52-
let query = UploadQueryParam {
45+
let param = UploadQueryParam {
5346
max_download: args.max_download,
5447
code_length: args.code_length,
5548
expire_secs: args.expire,
5649
delete_manually: args.delete_manually,
57-
qr_code_format,
50+
qr_code_format: None,
5851
};
5952
let client = CommandLineClient::new(args.server_addr);
6053
let (_, resp) = if args.progress_bar {
6154
client
62-
.upload_with_progress_bar(&source_file, &query, args.auth)
55+
.upload_with_progress_bar(&source_file, &param, args.auth)
6356
.await
6457
} else {
65-
client.upload_from(&source_file, &query, args.auth).await
58+
client.upload_from(&source_file, &param, args.auth).await
6659
}
6760
.unwrap();
6861
match resp {
@@ -71,10 +64,8 @@ pub async fn upload(args: UploadArguments) {
7164
println!("{}", serde_json::to_string(&resp).unwrap());
7265
}
7366
UploadOutput::QrCode => {
74-
println!(
75-
"{}",
76-
std::str::from_utf8(&STANDARD.decode(resp.qr_code.unwrap()).unwrap()).unwrap()
77-
);
67+
let qr_code = sdk::util::qr_code::generate_text_qr_code(&resp.url).unwrap();
68+
println!("{qr_code}");
7869
}
7970
UploadOutput::Url => {
8071
println!("{}", resp.url);

sdk/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ log = { workspace = true }
2222
async-stream = { workspace = true }
2323
mime_guess = { workspace = true }
2424
url = { workspace = true }
25+
qrcode = { workspace = true }
26+
image = { workspace = true }

sdk/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod dir;
22
pub mod file;
3+
pub mod qr_code;
34
pub mod random;
45
pub mod retry;

sdk/src/util/qr_code.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::io::Cursor;
2+
3+
use base64::{engine::general_purpose::STANDARD, Engine};
4+
use image::{ImageOutputFormat, Luma};
5+
use qrcode::QrCode;
6+
7+
pub fn generate_text_qr_code(input: &str) -> anyhow::Result<String> {
8+
Ok(
9+
QrCode::new(input.as_bytes())?
10+
.render::<char>()
11+
.quiet_zone(true)
12+
.module_dimensions(2, 1)
13+
.build(),
14+
)
15+
}
16+
17+
pub fn generate_base64_text_qr_code(input: &str) -> anyhow::Result<String> {
18+
Ok(STANDARD.encode(generate_text_qr_code(input)?))
19+
}
20+
21+
pub fn generate_base64_png_qr_code(input: &str) -> anyhow::Result<String> {
22+
let qr_code: image::ImageBuffer<Luma<u8>, Vec<u8>> = QrCode::new(input.as_bytes())?
23+
.render::<Luma<u8>>()
24+
.quiet_zone(true)
25+
.module_dimensions(20, 20)
26+
.build();
27+
let mut buff = Cursor::new(Vec::new());
28+
qr_code.write_to(&mut buff, ImageOutputFormat::Png)?;
29+
Ok(STANDARD.encode(buff.into_inner()))
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
use fake::{Fake, Faker};
36+
37+
#[test]
38+
pub fn test_generate_base64_text_qr_code() {
39+
let qr_code = generate_base64_text_qr_code(&Faker.fake::<String>()).unwrap();
40+
let _content = STANDARD.decode(qr_code.as_bytes()).unwrap();
41+
}
42+
43+
#[test]
44+
pub fn test_generate_base64_png_qr_code() {
45+
let qr_code = generate_base64_png_qr_code(&Faker.fake::<String>()).unwrap();
46+
let _content = STANDARD.decode(qr_code.as_bytes()).unwrap();
47+
}
48+
}

0 commit comments

Comments
 (0)