Merge pull request #100 from zed-industries/fix-url-to-path-conversion

Fix URL to `PathBuf` conversion in `mac::Platform`
This commit is contained in:
Antonio Scandurra 2021-07-08 17:51:09 +02:00 committed by GitHub
commit 792926c675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ use cocoa::{
NSEventModifierFlags, NSMenu, NSMenuItem, NSModalResponse, NSOpenPanel, NSPasteboard,
NSPasteboardTypeString, NSSavePanel, NSWindow,
},
base::{id, nil, selector},
base::{id, nil, selector, YES},
foundation::{NSArray, NSAutoreleasePool, NSData, NSInteger, NSString, NSURL},
};
use core_foundation::{
@ -244,11 +244,10 @@ impl platform::ForegroundPlatform for MacForegroundPlatform {
let urls = panel.URLs();
for i in 0..urls.count() {
let url = urls.objectAtIndex(i);
let string = url.absoluteString();
let string = std::ffi::CStr::from_ptr(string.UTF8String())
.to_string_lossy()
.to_string();
if let Some(path) = string.strip_prefix("file://") {
if url.isFileURL() == YES {
let path = std::ffi::CStr::from_ptr(url.path().UTF8String())
.to_string_lossy()
.to_string();
result.push(PathBuf::from(path));
}
}
@ -281,11 +280,10 @@ impl platform::ForegroundPlatform for MacForegroundPlatform {
let block = ConcreteBlock::new(move |response: NSModalResponse| {
let result = if response == NSModalResponse::NSModalResponseOk {
let url = panel.URL();
let string = url.absoluteString();
let string = std::ffi::CStr::from_ptr(string.UTF8String())
.to_string_lossy()
.to_string();
if let Some(path) = string.strip_prefix("file://") {
if url.isFileURL() == YES {
let path = std::ffi::CStr::from_ptr(url.path().UTF8String())
.to_string_lossy()
.to_string();
Some(PathBuf::from(path))
} else {
None