systemprompt_cli/commands/core/files/
upload.rs1use std::path::{Path, PathBuf};
2
3use anyhow::{Result, anyhow};
4use base64::Engine;
5use base64::engine::general_purpose::STANDARD;
6use clap::Args;
7use sha2::{Digest, Sha256};
8use systemprompt_files::{FileUploadRequest, FileUploadService, FilesConfig};
9use systemprompt_identifiers::{ContextId, SessionId, UserId};
10use systemprompt_runtime::AppContext;
11use tokio::fs;
12
13use super::types::FileUploadOutput;
14use crate::CliConfig;
15use crate::shared::CommandResult;
16
17#[derive(Debug, Clone, Args)]
18pub struct UploadArgs {
19 #[arg(help = "Path to file to upload")]
20 pub file_path: PathBuf,
21
22 #[arg(long, help = "Context ID (required)")]
23 pub context: String,
24
25 #[arg(long, help = "User ID")]
26 pub user: Option<String>,
27
28 #[arg(long, help = "Session ID")]
29 pub session: Option<String>,
30
31 #[arg(long, help = "Mark as AI-generated content")]
32 pub ai: bool,
33}
34
35pub async fn execute(
36 args: UploadArgs,
37 _config: &CliConfig,
38) -> Result<CommandResult<FileUploadOutput>> {
39 let ctx = AppContext::new().await?;
40 let files_config = FilesConfig::get()?;
41 let service = FileUploadService::new(ctx.db_pool(), files_config.clone())?;
42
43 if !service.is_enabled() {
44 return Err(anyhow!("File uploads are disabled in configuration"));
45 }
46
47 let file_path = args
48 .file_path
49 .canonicalize()
50 .map_err(|e| anyhow!("File not found: {} - {}", args.file_path.display(), e))?;
51
52 let bytes = fs::read(&file_path).await?;
53 let bytes_base64 = STANDARD.encode(&bytes);
54 let digest = Sha256::digest(&bytes);
55 let checksum_sha256 = digest.iter().fold(String::with_capacity(64), |mut acc, b| {
56 use std::fmt::Write;
57 let _ = write!(acc, "{b:02x}");
58 acc
59 });
60 let size_bytes = bytes.len() as i64;
61
62 let mime_type = detect_mime_type(&file_path);
63 let filename = file_path
64 .file_name()
65 .and_then(|n| n.to_str())
66 .map(String::from);
67
68 let context_id = ContextId::new(args.context);
69
70 let request = FileUploadRequest {
71 name: filename,
72 mime_type: mime_type.clone(),
73 bytes_base64,
74 context_id,
75 user_id: args.user.map(UserId::new),
76 session_id: args.session.map(SessionId::new),
77 trace_id: None,
78 };
79
80 let result = service.upload_file(request).await?;
81
82 let output = FileUploadOutput {
83 file_id: result.file_id,
84 path: result.path,
85 public_url: result.public_url,
86 size_bytes,
87 mime_type,
88 checksum_sha256,
89 };
90
91 Ok(CommandResult::card(output).with_title("File Uploaded"))
92}
93
94pub fn detect_mime_type(path: &Path) -> String {
95 let extension = path
96 .extension()
97 .and_then(|e| e.to_str())
98 .map(str::to_lowercase);
99
100 match extension.as_deref() {
101 Some("jpg" | "jpeg") => "image/jpeg".to_string(),
102 Some("png") => "image/png".to_string(),
103 Some("gif") => "image/gif".to_string(),
104 Some("webp") => "image/webp".to_string(),
105 Some("svg") => "image/svg+xml".to_string(),
106 Some("bmp") => "image/bmp".to_string(),
107 Some("tiff" | "tif") => "image/tiff".to_string(),
108 Some("ico") => "image/x-icon".to_string(),
109 Some("pdf") => "application/pdf".to_string(),
110 Some("doc") => "application/msword".to_string(),
111 Some("docx") => {
112 "application/vnd.openxmlformats-officedocument.wordprocessingml.document".to_string()
113 },
114 Some("xls") => "application/vnd.ms-excel".to_string(),
115 Some("xlsx") => {
116 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".to_string()
117 },
118 Some("ppt") => "application/vnd.ms-powerpoint".to_string(),
119 Some("pptx") => {
120 "application/vnd.openxmlformats-officedocument.presentationml.presentation".to_string()
121 },
122 Some("txt") => "text/plain".to_string(),
123 Some("csv") => "text/csv".to_string(),
124 Some("md") => "text/markdown".to_string(),
125 Some("html" | "htm") => "text/html".to_string(),
126 Some("json") => "application/json".to_string(),
127 Some("xml") => "application/xml".to_string(),
128 Some("rtf") => "application/rtf".to_string(),
129 Some("mp3") => "audio/mpeg".to_string(),
130 Some("wav") => "audio/wav".to_string(),
131 Some("ogg") => "audio/ogg".to_string(),
132 Some("aac") => "audio/aac".to_string(),
133 Some("flac") => "audio/flac".to_string(),
134 Some("m4a") => "audio/mp4".to_string(),
135 Some("mp4") => "video/mp4".to_string(),
136 Some("webm") => "video/webm".to_string(),
137 Some("mov") => "video/quicktime".to_string(),
138 Some("avi") => "video/x-msvideo".to_string(),
139 Some("mkv") => "video/x-matroska".to_string(),
140 _ => "application/octet-stream".to_string(),
141 }
142}