Repository navigation
Expose webUtils.getPathForFile as window.Native.getPathForFile for dropped files - #170
Open
PunchRockgroin wants to merge 1 commit into
Open
PunchRockgroin wants to merge 1 commit into
PunchRockgroin wants to merge 1 commit into
Conversation
…opped files Electron 32 removed File.path in favour of webUtils.getPathForFile(), which is only available in a preload script. With contextIsolation enabled and NativePHP's preload always set, apps had no way to get the path of a file dropped into a window or picked with <input type="file">.
|
Tested on an M5 MacBook Pro, macOS Tahoe 26.6, with php artisan native:run: Dropping a single file from Finder: full path, e.g. /Users/lexie/Desktop/NativePHPTestFile.jpeg Didn't try a network share. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
Adds
getPathForFile(file)to thewindow.Nativeobject the preload exposes. It returns the file system path of aFilefrom a drag-and-drop or an<input type="file">.Why
Electron 32 removed the non-standard
File.pathand replaced it withwebUtils.getPathForFile(). That function can only be called from a preload script.A NativePHP app can't currently get the path of a file a user drags into a window:
contextIsolation: trueandnodeIntegration: false, so the page has no access towebUtils.webPreferences.preloadis a required preference, so an app can't add its own preload alongside NativePHP's.Desktop apps commonly need the original path rather than an upload of the contents. Examples include working with the file where it is, watching it, checking whether another program has it open, or handling large files without copying them through a request.
Notes
Fileobjects the user has dropped or picked, so it doesn't let the page read arbitrary paths.getPathForFilereturns an empty string for aFilethat isn't backed by a file on disk, such as one built in JavaScript.getPathForFileis involved. Firefox saves a temporary copy (for exampleC:\Users\<user>\AppData\Local\Temp\image.jpgon Windows) and that's the path returned. Chrome on Windows gives aFilewith no file on disk behind it, sogetPathForFilereturns''. Chrome on macOS gives a drop with no files at all.dist/preload/index.mjsis updated to match. It's whatnpm run plugin:build(tsc) emits for the source change.Nativeobject, sowindow.Native.on()andwindow.Native.contextMenu()are unchanged.Testing
Tested with
php artisan native:runand a page that listens fordrop, on Windows 11 and on macOS Tahoe 26.6 (M5 MacBook Pro). Thanks to @lexchristopher03 for the macOS testing.npm run plugin:buildinresources/electrongives the samedist/preload/index.mjsas in this PR<input type="file">gives its full path (macOS)\\server\share\...) gives its path (Windows). Not tried on a macOS network share.''on Windows, and a drop with no files on macOSFilebuilt in JavaScript gives''(Windows, macOS)Docs
I have a matching docs addition for
resources/views/docs/desktop/2/digging-deeper/files.mdin NativePHP/nativephp.com, and I'm happy to open it as a separate PR there.