Fix URL handling in PLS parser.

Make sure relative paths are resolved to a full path.

Task-number: QTBUG-40515
Change-Id: Ideb83fc3a3c4a74c84917a22e3c30162d7b6158a
Reviewed-by: Christian Stromme <christian.stromme@digia.com>
This commit is contained in:
Yoann Lopes
2014-08-27 16:25:40 +02:00
parent 973ae5e0f6
commit e26483c106
3 changed files with 57 additions and 27 deletions

View File

@@ -59,6 +59,30 @@ public:
virtual void parseLine(int lineIndex, const QString& line, const QUrl& root) = 0; virtual void parseLine(int lineIndex, const QString& line, const QUrl& root) = 0;
protected:
QUrl expandToFullPath(const QUrl &root, const QString &line)
{
// On Linux, backslashes are not converted to forward slashes :/
if (line.startsWith(QLatin1String("//")) || line.startsWith(QLatin1String("\\\\"))) {
// Network share paths are not resolved
return QUrl::fromLocalFile(line);
}
QUrl url(line);
if (url.scheme().isEmpty()) {
// Resolve it relative to root
if (root.isLocalFile())
return root.resolved(QUrl::fromLocalFile(line));
else
return root.resolved(url);
} else if (url.scheme().length() == 1) {
// Assume it's a drive letter for a Windows path
url = QUrl::fromLocalFile(line);
}
return url;
}
Q_SIGNALS: Q_SIGNALS:
void newItem(const QVariant& content); void newItem(const QVariant& content);
void finished(); void finished();
@@ -146,29 +170,6 @@ public:
return -1; return -1;
} }
QUrl expandToFullPath(const QUrl& root, const QString& line)
{
// On Linux, backslashes are not converted to forward slashes :/
if (line.startsWith(QLatin1String("//")) || line.startsWith(QLatin1String("\\\\"))) {
// Network share paths are not resolved
return QUrl::fromLocalFile(line);
}
QUrl url(line);
if (url.scheme().isEmpty()) {
// Resolve it relative to root
if (root.isLocalFile())
return root.resolved(QUrl::fromLocalFile(line));
else
return root.resolved(url);
} else if (url.scheme().length() == 1) {
// Assume it's a drive letter for a Windows path
url = QUrl::fromLocalFile(line);
}
return url;
}
private: private:
bool m_extendedFormat; bool m_extendedFormat;
QVariantMap m_extraInfo; QVariantMap m_extraInfo;
@@ -249,7 +250,7 @@ Version=2
m_readFlags |= int(flag); m_readFlags |= int(flag);
} }
void parseLine(int lineIndex, const QString& line, const QUrl&) void parseLine(int lineIndex, const QString &line, const QUrl &root)
{ {
switch (m_state) { switch (m_state) {
case Header: case Header:
@@ -260,7 +261,7 @@ Version=2
break; break;
case Track: case Track:
if (!containsFlag(FileRead) && line.startsWith(m_fileName)) { if (!containsFlag(FileRead) && line.startsWith(m_fileName)) {
m_item[QLatin1String("url")] = getValue(lineIndex, line); m_item[QLatin1String("url")] = expandToFullPath(root, getValue(lineIndex, line));
setFlag(FileRead); setFlag(FileRead);
} else if (!containsFlag(TitleRead) && line.startsWith(m_titleName)) { } else if (!containsFlag(TitleRead) && line.startsWith(m_titleName)) {
m_item[QMediaMetaData::Title] = getValue(lineIndex, line); m_item[QMediaMetaData::Title] = getValue(lineIndex, line);

View File

@@ -6,5 +6,22 @@ Length1=-1
File2= http://test.host/path File2= http://test.host/path
Title2=Second Title2=Second
Length2=-1 Length2=-1
File3=testfile
Title3=Third
Length3=-1
NumberOfEntries=2
File4=testdir/testfile
Title4=Fourth
Length4=-1
File5=/testdir/testfile
Title5=Fifth
Length5=-1
File6=file://path/name#suffix
Title6=Sixth
Length6=-1
File7=testfile2#suffix
Title7=Seventh
Length7=-1
NumberOfEntries=7

View File

@@ -488,9 +488,21 @@ void tst_QMediaPlaylist::loadPLSFile()
QTRY_VERIFY(!loadSpy.isEmpty()); QTRY_VERIFY(!loadSpy.isEmpty());
QVERIFY(loadFailedSpy.isEmpty()); QVERIFY(loadFailedSpy.isEmpty());
QCOMPARE(playlist.error(), QMediaPlaylist::NoError); QCOMPARE(playlist.error(), QMediaPlaylist::NoError);
QCOMPARE(playlist.mediaCount(), 2); QCOMPARE(playlist.mediaCount(), 7);
QCOMPARE(playlist.media(0).canonicalUrl(), QUrl(QLatin1String("http://test.host/path"))); QCOMPARE(playlist.media(0).canonicalUrl(), QUrl(QLatin1String("http://test.host/path")));
QCOMPARE(playlist.media(1).canonicalUrl(), QUrl(QLatin1String("http://test.host/path"))); QCOMPARE(playlist.media(1).canonicalUrl(), QUrl(QLatin1String("http://test.host/path")));
testFileName = QFINDTESTDATA("testdata/testfile");
QCOMPARE(playlist.media(2).canonicalUrl(),
QUrl::fromLocalFile(testFileName));
testFileName = QFINDTESTDATA("testdata");
QCOMPARE(playlist.media(3).canonicalUrl(),
QUrl::fromLocalFile(testFileName + "/testdir/testfile"));
QCOMPARE(playlist.media(4).canonicalUrl(), QUrl(QLatin1String("file:///testdir/testfile")));
QCOMPARE(playlist.media(5).canonicalUrl(), QUrl(QLatin1String("file://path/name#suffix")));
//ensure #2 suffix is not stripped from path
testFileName = QFINDTESTDATA("testdata/testfile2#suffix");
QCOMPARE(playlist.media(6).canonicalUrl(), QUrl::fromLocalFile(testFileName));
// Try to load a totem-pl generated playlist // Try to load a totem-pl generated playlist
loadSpy.clear(); loadSpy.clear();