mirror of
https://codeberg.org/armin/monoslicer.git
synced 2026-11-20 13:07:56 +01:00
Normalise file:// URLs for drag-and-drop loading
This commit is contained in:
parent
16322a2bb6
commit
6a8d2aa84d
2 changed files with 51 additions and 11 deletions
|
|
@ -1,5 +1,35 @@
|
||||||
#include "PluginEditor.h"
|
#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)
|
MonoslicerEditor::MonoslicerEditor(MonoslicerProcessor& p)
|
||||||
: AudioProcessorEditor(p),
|
: AudioProcessorEditor(p),
|
||||||
processorRef(p),
|
processorRef(p),
|
||||||
|
|
@ -585,7 +615,7 @@ void MonoslicerEditor::comboBoxChanged(juce::ComboBox* comboBoxThatHasChanged)
|
||||||
bool MonoslicerEditor::isInterestedInFileDrag(const juce::StringArray& files)
|
bool MonoslicerEditor::isInterestedInFileDrag(const juce::StringArray& files)
|
||||||
{
|
{
|
||||||
for (const auto& f : files)
|
for (const auto& f : files)
|
||||||
if (processorRef.canLoadFileExtension(f))
|
if (processorRef.canLoadFileExtension (normaliseDragPath (f)))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -618,8 +648,8 @@ void MonoslicerEditor::filesDropped(const juce::StringArray& files, int, int)
|
||||||
|
|
||||||
for (const auto& f : files)
|
for (const auto& f : files)
|
||||||
{
|
{
|
||||||
juce::File file(f);
|
juce::File file (normaliseDragPath (f));
|
||||||
if (file.existsAsFile() && processorRef.canLoadFileExtension(f))
|
if (file.existsAsFile() && processorRef.canLoadFileExtension (file.getFullPathName()))
|
||||||
{
|
{
|
||||||
handleDroppedSample(file);
|
handleDroppedSample(file);
|
||||||
break;
|
break;
|
||||||
|
|
@ -629,11 +659,9 @@ void MonoslicerEditor::filesDropped(const juce::StringArray& files, int, int)
|
||||||
|
|
||||||
bool MonoslicerEditor::isInterestedInTextDrag(const juce::String& text)
|
bool MonoslicerEditor::isInterestedInTextDrag(const juce::String& text)
|
||||||
{
|
{
|
||||||
juce::File file(text);
|
juce::File file (normaliseDragPath (text));
|
||||||
if (text.startsWithIgnoreCase("file://"))
|
|
||||||
file = juce::URL(text).getLocalFile();
|
|
||||||
|
|
||||||
return file.existsAsFile() && processorRef.canLoadFileExtension(file.getFullPathName());
|
return file.existsAsFile() && processorRef.canLoadFileExtension (file.getFullPathName());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MonoslicerEditor::textDragEnter(const juce::String& text, int, int)
|
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);
|
dropOverlay.setVisible(false);
|
||||||
|
|
||||||
juce::File file(text);
|
juce::File file (normaliseDragPath (text));
|
||||||
if (text.startsWithIgnoreCase("file://"))
|
|
||||||
file = juce::URL(text).getLocalFile();
|
|
||||||
|
|
||||||
if (processorRef.canLoadFileExtension(file.getFullPathName()))
|
if (file.existsAsFile() && processorRef.canLoadFileExtension (file.getFullPathName()))
|
||||||
handleDroppedSample(file);
|
handleDroppedSample(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -164,6 +164,10 @@ MonoslicerProcessor processor;
|
||||||
|
|
||||||
std::cout << "\n\033[1mAcceptance\033[0m\n";
|
std::cout << "\n\033[1mAcceptance\033[0m\n";
|
||||||
check (editor.isInterestedInFileDrag ({ path }), "accepts a .wav file");
|
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");
|
check (editor.isInterestedInFileDrag ({ "/tmp/notes.txt" }) == false, "rejects a .txt file");
|
||||||
// The old hardcoded list only accepted ".aiff"; anything the format manager can
|
// The old hardcoded list only accepted ".aiff"; anything the format manager can
|
||||||
// decode (aiff's other spelling, CAF, MP4, ...) is now accepted too.
|
// 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,
|
// Note: dropping does not auto-slice - that is a separate explicit action,
|
||||||
// and matches the behaviour of the "Load" button.
|
// 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";
|
std::cout << "\n\033[1mText drop\033[0m\n";
|
||||||
editor.textDropped (path, info.position.x, info.position.y);
|
editor.textDropped (path, info.position.x, info.position.y);
|
||||||
check (processor.getSampleBuffer().getNumSamples() == 4096,
|
check (processor.getSampleBuffer().getNumSamples() == 4096,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue