summaryrefslogtreecommitdiffstats
path: root/src/config
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-07-10 10:48:02 +0800
committerGitHub <noreply@github.com>2024-07-10 10:48:02 +0800
commit48219d2808bafc88fa4fa4d6997aa9dc92e6f686 (patch)
tree65fefbd6e1c883a8ea55568bd24ac67c678b4a7c /src/config
parent0afe5fa24b8990991635c7023f0fb89403f55d90 (diff)
downloadaichat-48219d2808bafc88fa4fa4d6997aa9dc92e6f686.tar.gz
refactor: fetch image as data url (#694)
Diffstat (limited to 'src/config')
-rw-r--r--src/config/input.rs34
1 files changed, 16 insertions, 18 deletions
diff --git a/src/config/input.rs b/src/config/input.rs
index ee85c2b..1fce6d0 100644
--- a/src/config/input.rs
+++ b/src/config/input.rs
@@ -379,13 +379,14 @@ async fn load_paths(
}
}
for file_url in remote_urls {
- if is_image(&file_url) {
- medias.push(file_url)
+ let (contents, extension) = fetch(&loaders, &file_url, true)
+ .await
+ .with_context(|| format!("Failed to load url '{file_url}'"))?;
+ if extension == MEDIA_URL_EXTENSION {
+ data_urls.insert(sha256(&contents), file_url);
+ medias.push(contents)
} else {
- let (text, _) = fetch(&loaders, &file_url)
- .await
- .with_context(|| format!("Failed to load url '{file_url}'"))?;
- files.push((file_url, text));
+ files.push((file_url, contents));
}
}
Ok((files, medias, data_urls))
@@ -416,22 +417,19 @@ fn resolve_local_path(path: &str) -> Option<String> {
}
fn is_image(path: &str) -> bool {
- path_extension(path)
+ get_patch_extension(path)
.map(|v| IMAGE_EXTS.contains(&v.as_str()))
.unwrap_or_default()
}
-fn read_media_to_data_url<P: AsRef<Path>>(image_path: P) -> Result<String> {
- let image_path = image_path.as_ref();
- let mime_type = match image_path.extension().and_then(|v| v.to_str()) {
- Some(extension) => match extension {
- "png" => "image/png",
- "jpg" | "jpeg" => "image/jpeg",
- "webp" => "image/webp",
- "gif" => "image/gif",
- _ => bail!("Unsupported media type"),
- },
- None => bail!("Unknown media type"),
+fn read_media_to_data_url(image_path: &str) -> Result<String> {
+ let extension = get_patch_extension(image_path).unwrap_or_default();
+ let mime_type = match extension.as_str() {
+ "png" => "image/png",
+ "jpg" | "jpeg" => "image/jpeg",
+ "webp" => "image/webp",
+ "gif" => "image/gif",
+ _ => bail!("Unexpected media type"),
};
let mut file = File::open(image_path)?;
let mut buffer = Vec::new();