Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Get Mime from URL #95

Open
ollyde opened this issue Feb 6, 2024 · 1 comment
Open

[FEATURE] Get Mime from URL #95

ollyde opened this issue Feb 6, 2024 · 1 comment

Comments

@ollyde
Copy link

ollyde commented Feb 6, 2024

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
Copy link
Author

ollyde commented Feb 6, 2024

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())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant