From 6a8d2aa84d7ee8ec055b7737407c94d1396b4114 Mon Sep 17 00:00:00 2001 From: Armin Date: Thu, 12 Nov 2026 21:52:35 +0100 Subject: [PATCH] Normalise file:// URLs for drag-and-drop loading --- Source/PluginEditor.cpp | 48 ++++++++++++++++++++++++++++++--------- Tests/DragAndDropTest.cpp | 14 ++++++++++++ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Source/PluginEditor.cpp b/Source/PluginEditor.cpp index efac7ca..73ec69e 100644 --- a/Source/PluginEditor.cpp +++ b/Source/PluginEditor.cpp @@ -1,5 +1,35 @@ #include "PluginEditor.h" +namespace +{ +// Drag sources don't always hand us a plain filesystem path: some send a +// file:// URL, with spaces and other characters percent-encoded. Normalise +// everything to a plain path before looking at the extension or opening it. +juce::String normaliseDragPath (const juce::String& rawPath) +{ + auto path = rawPath.trim(); + + if (path.startsWithIgnoreCase ("file:")) + { + juce::URL url (path); + if (url.isLocalFile()) + return url.getLocalFile().getFullPathName(); + + // Fall back to manual parsing for URLs JUCE doesn't recognise. + if (path.startsWith ("file://")) + path = path.substring (7); + else if (path.startsWith ("file:/")) + path = path.substring (5); + + path = path.replace ("%20", " ").replace ("%5B", "[").replace ("%5D", "]") + .replace ("%28", "(").replace ("%29", ")").replace ("%2C", ",") + .replace ("%27", "'").replace ("%22", "\""); + } + + return path; +} +} + MonoslicerEditor::MonoslicerEditor(MonoslicerProcessor& p) : AudioProcessorEditor(p), processorRef(p), @@ -585,7 +615,7 @@ void MonoslicerEditor::comboBoxChanged(juce::ComboBox* comboBoxThatHasChanged) bool MonoslicerEditor::isInterestedInFileDrag(const juce::StringArray& files) { for (const auto& f : files) - if (processorRef.canLoadFileExtension(f)) + if (processorRef.canLoadFileExtension (normaliseDragPath (f))) return true; return false; @@ -618,8 +648,8 @@ void MonoslicerEditor::filesDropped(const juce::StringArray& files, int, int) for (const auto& f : files) { - juce::File file(f); - if (file.existsAsFile() && processorRef.canLoadFileExtension(f)) + juce::File file (normaliseDragPath (f)); + if (file.existsAsFile() && processorRef.canLoadFileExtension (file.getFullPathName())) { handleDroppedSample(file); break; @@ -629,11 +659,9 @@ void MonoslicerEditor::filesDropped(const juce::StringArray& files, int, int) bool MonoslicerEditor::isInterestedInTextDrag(const juce::String& text) { - juce::File file(text); - if (text.startsWithIgnoreCase("file://")) - file = juce::URL(text).getLocalFile(); + juce::File file (normaliseDragPath (text)); - return file.existsAsFile() && processorRef.canLoadFileExtension(file.getFullPathName()); + return file.existsAsFile() && processorRef.canLoadFileExtension (file.getFullPathName()); } void MonoslicerEditor::textDragEnter(const juce::String& text, int, int) @@ -655,11 +683,9 @@ void MonoslicerEditor::textDropped(const juce::String& text, int, int) { dropOverlay.setVisible(false); - juce::File file(text); - if (text.startsWithIgnoreCase("file://")) - file = juce::URL(text).getLocalFile(); + juce::File file (normaliseDragPath (text)); - if (processorRef.canLoadFileExtension(file.getFullPathName())) + if (file.existsAsFile() && processorRef.canLoadFileExtension (file.getFullPathName())) handleDroppedSample(file); } diff --git a/Tests/DragAndDropTest.cpp b/Tests/DragAndDropTest.cpp index fcf8b4f..b16a9a8 100644 --- a/Tests/DragAndDropTest.cpp +++ b/Tests/DragAndDropTest.cpp @@ -164,6 +164,10 @@ MonoslicerProcessor processor; std::cout << "\n\033[1mAcceptance\033[0m\n"; check (editor.isInterestedInFileDrag ({ path }), "accepts a .wav file"); + check (editor.isInterestedInFileDrag ({ juce::String ("file://") + path }), + "accepts a .wav file via a file:// URL"); + check (editor.isInterestedInFileDrag ({ "file:///tmp/foo%20bar.wav" }), + "accepts a percent-encoded file:// URL"); check (editor.isInterestedInFileDrag ({ "/tmp/notes.txt" }) == false, "rejects a .txt file"); // The old hardcoded list only accepted ".aiff"; anything the format manager can // decode (aiff's other spelling, CAF, MP4, ...) is now accepted too. @@ -219,6 +223,16 @@ MonoslicerProcessor processor; // Note: dropping does not auto-slice - that is a separate explicit action, // and matches the behaviour of the "Load" button. + std::cout << "\n\033[1mFile drop via file:// URL\033[0m\n"; + processor.clearSample(); + check (processor.getSampleBuffer().getNumSamples() == 0, "the sample was cleared"); + + editor.filesDropped ({ juce::String ("file://") + path }, info.position.x, info.position.y); + check (processor.getSampleBuffer().getNumSamples() == 4096, + "a file:// URL dropped as a file loads the sample"); + check (processor.getCurrentFile() == wavFile, + "the file:// dropped file is recorded as the current file"); + std::cout << "\n\033[1mText drop\033[0m\n"; editor.textDropped (path, info.position.x, info.position.y); check (processor.getSampleBuffer().getNumSamples() == 4096,