Started to add pack download
This commit is contained in:
@@ -350,8 +350,9 @@ auto DownloadManager::downloadPack(const QString &id) -> void
|
||||
|
||||
QNetworkRequest request(downloadUrl);
|
||||
request.setRawHeader(QByteArray("uuid"), id.toUtf8());
|
||||
QString tempFile(QString("%1.tar.gz").arg(id));
|
||||
|
||||
QFuture<QUrl> downloadUri = download(request, id, Post);
|
||||
QFuture<QUrl> downloadUri = download(request, id, Post, tempFile);
|
||||
|
||||
downloadUri
|
||||
.then
|
||||
@@ -378,31 +379,35 @@ auto DownloadManager::downloadPack(const QString &id) -> void
|
||||
)
|
||||
.onCanceled
|
||||
(
|
||||
[this]()
|
||||
[this, tempFile]()
|
||||
{
|
||||
QFile::remove(QStringLiteral("/tmp/") + tempFile);
|
||||
reset();
|
||||
}
|
||||
)
|
||||
.onFailed
|
||||
(
|
||||
[this] (const NetworkException &e)
|
||||
[this, tempFile] (const NetworkException &e)
|
||||
{
|
||||
QFile::remove(QStringLiteral("/tmp/") + tempFile);
|
||||
reset();
|
||||
setError(QStringLiteral("Network Exception %1").arg(QString::number(e.errorCode)), e.message);
|
||||
}
|
||||
)
|
||||
.onFailed
|
||||
(
|
||||
[this] (const FileException &e)
|
||||
[this, tempFile] (const FileException &e)
|
||||
{
|
||||
QFile::remove(QStringLiteral("/tmp/") + tempFile);
|
||||
reset();
|
||||
setError(QStringLiteral("File Exception %1").arg(QString::number(e.errorCode)), e.message);
|
||||
}
|
||||
)
|
||||
.onFailed
|
||||
(
|
||||
[this] ()
|
||||
[this, tempFile] ()
|
||||
{
|
||||
QFile::remove(QStringLiteral("/tmp/") + tempFile);
|
||||
reset();
|
||||
setError(QStringLiteral("Unknown Exception"), QString());
|
||||
}
|
||||
@@ -503,7 +508,7 @@ auto DownloadManager::install(const QUrl &uri) noexcept(false) -> QUrl
|
||||
|
||||
QDir tempDirectory(uri.toLocalFile());
|
||||
|
||||
QUrl installLocation = QStringLiteral("%1/.local/share/komplex/packs/%2").arg
|
||||
QUrl installLocation = QStringLiteral("file://%1/.local/share/komplex/packs/%2").arg
|
||||
(
|
||||
QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
|
||||
tempDirectory.dirName()
|
||||
@@ -537,24 +542,28 @@ auto DownloadManager::install(const QUrl &uri) noexcept(false) -> QUrl
|
||||
return installLocation;
|
||||
}
|
||||
|
||||
auto DownloadManager::download(const QNetworkRequest &request, const QString &id, RequestType type) -> QFuture<QUrl>
|
||||
auto DownloadManager::download(const QNetworkRequest &request, const QString &id, RequestType type, const QString &filename) -> QFuture<QUrl>
|
||||
{
|
||||
return QtConcurrent::run
|
||||
(
|
||||
[this, request, id, type]() -> QUrl
|
||||
[this, request, id, type, _filename = filename]() -> QUrl
|
||||
{
|
||||
QEventLoop loop;
|
||||
QString filename = id;
|
||||
QString filename = _filename;
|
||||
|
||||
if(filename.isEmpty())
|
||||
if(filename.isEmpty() && !request.url().fileName().isEmpty())
|
||||
{
|
||||
filename = QUuid::createUuidV7().toString();
|
||||
filename = request.url().fileName();
|
||||
}
|
||||
else if(filename.isEmpty())
|
||||
{
|
||||
filename = id;
|
||||
}
|
||||
|
||||
QUrl downloadUri = QStringLiteral("file://%1/%2").arg
|
||||
(
|
||||
QStandardPaths::writableLocation(QStandardPaths::TempLocation),
|
||||
request.url().fileName()
|
||||
filename
|
||||
);
|
||||
|
||||
auto manager = CoreServices::networkAccessManager().toStrongRef();
|
||||
@@ -578,13 +587,13 @@ auto DownloadManager::download(const QNetworkRequest &request, const QString &id
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case Post:
|
||||
reply = manager->post(request, nullptr);
|
||||
break;
|
||||
default:
|
||||
case Get:
|
||||
reply = manager->get(request, nullptr);
|
||||
break;
|
||||
case Post:
|
||||
reply = manager->post(request, nullptr);
|
||||
break;
|
||||
default:
|
||||
case Get:
|
||||
reply = manager->get(request, nullptr);
|
||||
break;
|
||||
}
|
||||
|
||||
QObject::connect
|
||||
@@ -624,8 +633,9 @@ auto DownloadManager::download(const QNetworkRequest &request, const QString &id
|
||||
reply,
|
||||
&QNetworkReply::readyRead,
|
||||
this,
|
||||
[this, downloadUri, reply]()
|
||||
[this, downloadUri/*, reply, &downloadFile*/]()
|
||||
{
|
||||
QMutexLocker locker(&m_fileMutex);
|
||||
QFile downloadFile(downloadUri.toLocalFile());
|
||||
|
||||
if(!downloadFile.open(QFile::ReadWrite | QFile::Append))
|
||||
@@ -633,8 +643,19 @@ auto DownloadManager::download(const QNetworkRequest &request, const QString &id
|
||||
throw FileException(QStringLiteral("Could not open temp file location"));
|
||||
}
|
||||
|
||||
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
||||
|
||||
if(reply == nullptr)
|
||||
{
|
||||
throw NetworkException
|
||||
{
|
||||
QStringLiteral("Reply is not a qobject")
|
||||
};
|
||||
}
|
||||
|
||||
quint64 bytes = reply->bytesAvailable();
|
||||
quint64 bytesWritten = downloadFile.write(reply->readAll()/*bytes)*/);
|
||||
QByteArray data = reply->readAll();
|
||||
quint64 bytesWritten = downloadFile.write(data/*bytes)*/);
|
||||
|
||||
downloadFile.close();
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ signals:
|
||||
private:
|
||||
auto compile(const QUrl &uri) noexcept(false) -> QUrl;
|
||||
auto install(const QUrl &uri) noexcept(false) -> QUrl;
|
||||
auto download(const QNetworkRequest &request, const QString &id, RequestType type = Get) -> QFuture<QUrl>;
|
||||
auto download(const QNetworkRequest &request, const QString &id, RequestType type = Get, const QString &filename = QString()) -> QFuture<QUrl>;
|
||||
auto readShaderToyEntry(const QUrl &uri) noexcept(false) -> ShaderToyEntry;
|
||||
|
||||
QString m_lastInstalledFile;
|
||||
@@ -159,6 +159,7 @@ private:
|
||||
State m_state;
|
||||
|
||||
QMutex m_downloadMutex;
|
||||
QMutex m_fileMutex;
|
||||
|
||||
PackCompiler *m_compiler = nullptr;
|
||||
|
||||
|
||||
@@ -60,29 +60,22 @@ auto PackCompiler::process(const QUrl &uri) -> QFuture<QUrl>
|
||||
setStatus(QStringLiteral("Compiling %1").arg(uri.fileName()));
|
||||
setState(Compiling);
|
||||
|
||||
if(!validateDirectory(uri))
|
||||
{
|
||||
throw FileException
|
||||
(
|
||||
QStringLiteral("Source directory is invalid")
|
||||
);
|
||||
}
|
||||
// this was a leftover from ThumbnailGenerator
|
||||
// if(!validateDirectory(uri))
|
||||
// {
|
||||
// throw FileException
|
||||
// (
|
||||
// QStringLiteral("Source directory is invalid")
|
||||
// );
|
||||
// }
|
||||
|
||||
QUrl buildUri
|
||||
(
|
||||
QStringLiteral("file://%1/komplex/build/%2").arg
|
||||
(
|
||||
QStandardPaths::writableLocation
|
||||
(
|
||||
QStandardPaths::TempLocation
|
||||
),
|
||||
uri.fileName()
|
||||
)
|
||||
);
|
||||
QFileInfo info(uri.toLocalFile());
|
||||
|
||||
QUrl buildUri;
|
||||
|
||||
try
|
||||
{
|
||||
extract(uri, buildUri);
|
||||
buildUri = extract(uri);
|
||||
prepareShaders(buildUri);
|
||||
compile(buildUri);
|
||||
}
|
||||
@@ -98,6 +91,10 @@ auto PackCompiler::process(const QUrl &uri) -> QFuture<QUrl>
|
||||
setError(QStringLiteral("Compiler Error"), e.message);
|
||||
throw e;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
removeDirectory(buildUri);
|
||||
}
|
||||
|
||||
return buildUri;
|
||||
}
|
||||
@@ -457,18 +454,89 @@ auto PackCompiler::validateDirectory(const QUrl &uri) -> bool
|
||||
);
|
||||
}
|
||||
|
||||
auto PackCompiler::extract(const QUrl &sourceUri, const QUrl &destinationUri) noexcept(false) -> void
|
||||
auto PackCompiler::createDirectory(const QUrl &uri) -> void
|
||||
{
|
||||
if(!uri.isLocalFile() || !uri.isValid())
|
||||
{
|
||||
throw FileException(QStringLiteral("Uri needs to be a local file"), 0);
|
||||
}
|
||||
|
||||
QStringList arguments =
|
||||
{
|
||||
QStringLiteral("-p"),
|
||||
uri.toLocalFile()
|
||||
};
|
||||
|
||||
QProcess *process = new QProcess(this);
|
||||
|
||||
QObject::connect
|
||||
(
|
||||
process,
|
||||
&QProcess::readyReadStandardOutput,
|
||||
this,
|
||||
[this, process]()
|
||||
{
|
||||
QByteArray processData = process->readAllStandardOutput();
|
||||
setCompilerOutput(m_compilerOutput + processData);
|
||||
}
|
||||
);
|
||||
|
||||
QObject::connect
|
||||
(
|
||||
process,
|
||||
&QProcess::readyReadStandardError,
|
||||
this,
|
||||
[this, process]()
|
||||
{
|
||||
QByteArray processData = process->readAllStandardError();
|
||||
|
||||
if(!processData.isValidUtf8())
|
||||
{
|
||||
qWarning() << QStringLiteral("Process output not valid UTF8 data");
|
||||
return;
|
||||
}
|
||||
|
||||
setCompilerOutput(m_compilerOutput + processData);
|
||||
}
|
||||
);
|
||||
|
||||
process->start(QStringLiteral("mkdir"), arguments);
|
||||
|
||||
if(!process->waitForStarted(3000))
|
||||
{
|
||||
process->deleteLater();
|
||||
throw FileException(QStringLiteral("Could not start preprocessor"));
|
||||
}
|
||||
|
||||
if(!process->waitForFinished())
|
||||
{
|
||||
process->deleteLater();
|
||||
throw FileException(QStringLiteral("Preprocessor timeout"));
|
||||
}
|
||||
|
||||
if(process->exitCode() != 0)
|
||||
{
|
||||
process->deleteLater();
|
||||
throw ShaderCompilerException(m_compilerOutput);
|
||||
}
|
||||
|
||||
process->deleteLater();
|
||||
}
|
||||
|
||||
auto PackCompiler::extract(const QUrl &sourceUri) noexcept(false) -> QUrl
|
||||
{
|
||||
if(!sourceUri.isLocalFile() || !sourceUri.isValid())
|
||||
{
|
||||
throw FileException(QStringLiteral("Uri needs to be a local file"), 0);
|
||||
}
|
||||
|
||||
QFileInfo info(sourceUri.toLocalFile());
|
||||
|
||||
QUrl outputUri
|
||||
(
|
||||
QStringLiteral("file://%1_proc").arg
|
||||
QStringLiteral("file:///tmp/%1").arg
|
||||
(
|
||||
sourceUri.toLocalFile()
|
||||
info.baseName()
|
||||
)
|
||||
);
|
||||
|
||||
@@ -480,6 +548,13 @@ auto PackCompiler::extract(const QUrl &sourceUri, const QUrl &destinationUri) no
|
||||
outputUri.toLocalFile()
|
||||
};
|
||||
|
||||
QDir outputDir(outputUri.toLocalFile());
|
||||
|
||||
if(!outputDir.exists())
|
||||
{
|
||||
createDirectory(outputUri);
|
||||
}
|
||||
|
||||
QProcess *process = new QProcess(this);
|
||||
|
||||
QObject::connect
|
||||
@@ -534,6 +609,10 @@ auto PackCompiler::extract(const QUrl &sourceUri, const QUrl &destinationUri) no
|
||||
}
|
||||
|
||||
process->deleteLater();
|
||||
|
||||
QFile::remove(sourceUri.toLocalFile());
|
||||
|
||||
return outputUri;
|
||||
}
|
||||
|
||||
auto PackCompiler::copyFile(const QUrl &sourceUri, const QUrl &destinationUri) noexcept(false) -> void
|
||||
|
||||
@@ -112,7 +112,9 @@ protected:
|
||||
*/
|
||||
auto validateDirectory(const QUrl &uri) -> bool;
|
||||
|
||||
auto extract(const QUrl &sourceUri, const QUrl &destinationUri) noexcept(false) -> void;
|
||||
auto createDirectory(const QUrl &uri) -> void;
|
||||
|
||||
auto extract(const QUrl &sourceUri) noexcept(false) -> QUrl;
|
||||
auto copyFile(const QUrl &sourceUri, const QUrl &destinationUri) noexcept(false) -> void;
|
||||
auto compile(const QUrl &uri) noexcept(false) -> void;
|
||||
auto preprocess(const QUrl &uri) noexcept(false) -> void;
|
||||
|
||||
Reference in New Issue
Block a user