infer icon indicating copy to clipboard operation
infer copied to clipboard

[FEATURE] Get Mime from URL

Open ollyde opened this issue 1 year ago • 1 comments

As title. We want to be able to set mime type for users on a could service without having to download 100gb files. It's possible with Node mime.

ollyde avatar Feb 06 '24 13:02 ollyde

I tried to do this myself but it fails

pub async fn get_mime_from_url(public_link: &str) -> Result<String, ApiError> {
    // Create a reqwest client
    let client = Client::new();

    // Make a GET request to fetch the first 512 bytes of the file
    // Replace with the actual URL
    let resp = client
        .get(public_link)
        .header(reqwest::header::RANGE, "bytes=0-2048")
        .send()
        .await
        .map_err(|e| {
            eprintln!("Error fetching file: {}", e);
            ApiError {
                status: "issue-fetching-file".to_string(),
                message: "Error fetching file for get_mime_from_url".to_string(),
            }
        })?
        .bytes()
        .await
        .map_err(|e| {
            eprintln!("Error fetching bytes file: {}", e);
            ApiError {
                status: "issue-fetching-file".to_string(),
                message: "Error fetching bytes for get_mime_from_url".to_string(),
            }
        })?;

    // Infer expects a &[u8]
    let bytes = resp.as_ref();

    println!("RES: {:?}", resp.len());

    // Create an instance of Infer to guess the file type
    let infer = Infer::new();
    let kind = infer.get(bytes);

    println!("KIND {:?}", kind);

    // Match the result and print the file type
    match kind {
        Some(kind) => println!("File type: {}", kind.mime_type()),
        None => println!("Could not determine file type"),
    }

    Ok("".to_string())
}

ollyde avatar Feb 06 '24 14:02 ollyde