-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gio: add content_type_guess_for_filename helper
- Loading branch information
1 parent
c8df619
commit 8ecd700
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use std::ptr; | ||
|
||
use glib::translate::*; | ||
|
||
use crate::ffi; | ||
|
||
/// Guesses the content type based on only a filename. | ||
/// | ||
/// This function is equivalent to calling `g_content_type_guess()` with the `data` parameter set to NULL. | ||
/// | ||
/// See [`crate::content_type_guess`] for more information. | ||
pub fn content_type_guess_for_filename( | ||
filename: impl AsRef<std::path::Path>, | ||
) -> (glib::GString, bool) { | ||
unsafe { | ||
let mut result_uncertain = std::mem::MaybeUninit::uninit(); | ||
let ret = from_glib_full(ffi::g_content_type_guess( | ||
filename.as_ref().to_glib_none().0, | ||
ptr::null_mut(), | ||
0, | ||
result_uncertain.as_mut_ptr(), | ||
)); | ||
(ret, from_glib(result_uncertain.assume_init())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
#[cfg(unix)] | ||
#[test] | ||
fn test_content_type_guess_for_filename() { | ||
// We only test for directory and file without extension here as we can't guarantee the | ||
// CI runners will have any mimetypes installed. | ||
let filename = std::path::Path::new("test/"); | ||
let ret: (glib::GString, bool) = gio::content_type_guess_for_filename(filename); | ||
assert_eq!(ret.0, "inode/directory"); | ||
|
||
let filename = std::path::Path::new("test"); | ||
let ret: (glib::GString, bool) = gio::content_type_guess_for_filename(filename); | ||
assert_eq!(ret.0, "application/octet-stream"); | ||
} |