Ver código fonte

Expose sounds to the local http server

You can now request /sounds/$nameofsound that will first grab
sounds from the dataDir/sounds dir and secondly from the internal
resources.
Tobias Hieta 8 anos atrás
pai
commit
f370a58458

+ 23 - 1
src/server/HTTPServer.cpp

@@ -73,8 +73,13 @@ bool HttpServer::writeFile(const QString& file, QHttpResponse* response)
     QFile fp(file);
     if (fp.open(QFile::ReadOnly))
     {
+      auto mime = m_mime.mimeTypeForFile(fp);
+
       response->setStatusCode(qhttp::ESTATUS_OK);
+      response->addHeader("Content-Type", mime.name().toUtf8());
+      response->addHeader("Content-Length", QByteArray::number(fp.size()));
       response->write(fp.readAll());
+
       fp.close();
       return true;
     }
@@ -140,6 +145,20 @@ void HttpServer::handleFilesRequest(QHttpRequest* request, QHttpResponse* respon
   response->end();
 }
 
+/////////////////////////////////////////////////////////////////////////////////////////
+void HttpServer::handleSoundsRequest(QHttpRequest* request, QHttpResponse* response)
+{
+  auto sound = QFileInfo(request->url().path()).fileName();
+  auto soundPath = Paths::soundsPath(sound);
+
+  if (soundPath.isEmpty())
+    writeError(response, qhttp::ESTATUS_NOT_FOUND);
+  else
+    writeFile(Paths::soundsPath(sound), response);
+
+  response->end();
+}
+
 /////////////////////////////////////////////////////////////////////////////////////////
 void HttpServer::handleRequest(QHttpRequest* request, QHttpResponse* response)
 {
@@ -162,6 +181,10 @@ void HttpServer::handleRequest(QHttpRequest* request, QHttpResponse* response)
   {
     handleFilesRequest(request, response);
   }
+  else if (path.startsWith("/sounds"))
+  {
+    handleSoundsRequest(request, response);
+  }
   else if (path == "/")
   {
     response->setStatusCode(qhttp::ESTATUS_OK);
@@ -173,4 +196,3 @@ void HttpServer::handleRequest(QHttpRequest* request, QHttpResponse* response)
     response->end();
   }
 }
-

+ 4 - 2
src/server/HTTPServer.h

@@ -3,6 +3,8 @@
 
 #include <QObject>
 #include <QString>
+#include <QMimeDatabase>
+
 #include "qhttpserverrequest.hpp"
 #include "qhttpserver.hpp"
 #include "qhttpserverresponse.hpp"
@@ -26,12 +28,12 @@ private slots:
 
 private:
   bool writeFile(const QString& file, QHttpResponse* response);
+  void handleSoundsRequest(QHttpRequest* request, QHttpResponse* response);
 
   QHttpServer* m_server;
-
   QString m_baseUrl;
-
   quint16 m_port;
+  QMimeDatabase m_mime;
 };
 
 #endif // HTTPSERVER_H

+ 21 - 1
src/shared/Paths.cpp

@@ -89,4 +89,24 @@ QString Paths::socketName(const QString& serverName)
 #else
   return QString("pmp_%1_%2.sock").arg(serverName).arg(userName);
 #endif
-}
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+QString Paths::soundsPath(const QString& sound)
+{
+  // check local filesystem first
+  auto localSound = dataDir("sounds/" + sound);
+
+  QFileInfo f(localSound);
+  if (f.exists())
+    return f.absoluteFilePath();
+
+  f = QFileInfo(":/sounds/" + sound);
+  if (!f.exists())
+  {
+    QLOG_WARN() << "Can't find sound:" << sound;
+    return QString();
+  }
+
+  return f.absoluteFilePath();
+}

+ 1 - 0
src/shared/Paths.h

@@ -16,6 +16,7 @@ namespace Paths
   QString cacheDir(const QString& file = QString());
   QString logDir(const QString& file = QString());
   QString socketName(const QString& serverName);
+  QString soundsPath(const QString& sound);
 };
 
 #endif //KONVERGO_PATHS_H

+ 1 - 0
src/system/SystemComponent.cpp

@@ -71,6 +71,7 @@ SystemComponent::SystemComponent(QObject* parent) : ComponentBase(parent), m_pla
 bool SystemComponent::componentInitialize()
 {
   QDir().mkpath(Paths::dataDir("scripts"));
+  QDir().mkpath(Paths::dataDir("sounds"));
 
   // Hide mouse pointer on any keyboard input
   connect(&InputComponent::Get(), &InputComponent::receivedInput, [=]() { setCursorVisibility(false); });