Merge branch 'fix-mimetype-subsonic-api' into develop
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
add_library(lmsav SHARED
|
||||
impl/AudioFile.cpp
|
||||
impl/RawResourceHandlerCreator.cpp
|
||||
impl/Transcoder.cpp
|
||||
impl/TranscodeResourceHandler.cpp
|
||||
impl/Types.cpp
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "av/RawResourceHandlerCreator.hpp"
|
||||
|
||||
#include "av/IAudioFile.hpp"
|
||||
#include "utils/FileResourceHandlerCreator.hpp"
|
||||
|
||||
namespace Av
|
||||
{
|
||||
std::unique_ptr<IResourceHandler> createRawResourceHandler(const std::filesystem::path& path)
|
||||
{
|
||||
std::string mimeType;
|
||||
const auto guessedAudioFormat{ Av::guessAudioFileFormat(path) };
|
||||
if (guessedAudioFormat)
|
||||
mimeType = guessedAudioFormat->mimeType;
|
||||
else
|
||||
mimeType = "application/octet-stream";
|
||||
|
||||
return createFileResourceHandler(path, mimeType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
|
||||
#include "utils/IResourceHandler.hpp"
|
||||
|
||||
namespace Av
|
||||
{
|
||||
std::unique_ptr<IResourceHandler> createRawResourceHandler(const std::filesystem::path& path);
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "MediaRetrieval.hpp"
|
||||
|
||||
#include "av/IAudioFile.hpp"
|
||||
#include "av/RawResourceHandlerCreator.hpp"
|
||||
#include "av/TranscodeParameters.hpp"
|
||||
#include "av/TranscodeResourceHandlerCreator.hpp"
|
||||
#include "av/Types.hpp"
|
||||
@@ -160,7 +161,7 @@ namespace API::Subsonic
|
||||
trackPath = track->getPath();
|
||||
}
|
||||
|
||||
resourceHandler = createFileResourceHandler(trackPath);
|
||||
resourceHandler = Av::createRawResourceHandler(trackPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -185,7 +186,7 @@ namespace API::Subsonic
|
||||
if (streamParameters.transcodeParameters)
|
||||
resourceHandler = Av::createTranscodeResourceHandler(streamParameters.inputFileParameters, *streamParameters.transcodeParameters, streamParameters.estimateContentLength);
|
||||
else
|
||||
resourceHandler = createFileResourceHandler(streamParameters.inputFileParameters.trackPath);
|
||||
resourceHandler = Av::createRawResourceHandler(streamParameters.inputFileParameters.trackPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -24,23 +24,22 @@
|
||||
#include "utils/Logger.hpp"
|
||||
|
||||
std::unique_ptr<IResourceHandler>
|
||||
createFileResourceHandler(const std::filesystem::path& path)
|
||||
createFileResourceHandler(const std::filesystem::path& path, std::string_view mimeType)
|
||||
{
|
||||
return std::make_unique<FileResourceHandler>(path);
|
||||
return std::make_unique<FileResourceHandler>(path, mimeType);
|
||||
}
|
||||
|
||||
|
||||
FileResourceHandler::FileResourceHandler(const std::filesystem::path& path)
|
||||
: _path {path}
|
||||
FileResourceHandler::FileResourceHandler(const std::filesystem::path& path, std::string_view mimeType)
|
||||
: _path{ path }
|
||||
, _mimeType{ mimeType }
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Wt::Http::ResponseContinuation*
|
||||
FileResourceHandler::processRequest(const Wt::Http::Request& request, Wt::Http::Response& response)
|
||||
{
|
||||
::uint64_t startByte {_offset};
|
||||
std::ifstream ifs {_path.string().c_str(), std::ios::in | std::ios::binary};
|
||||
::uint64_t startByte{ _offset };
|
||||
std::ifstream ifs{ _path.string().c_str(), std::ios::in | std::ios::binary };
|
||||
|
||||
if (startByte == 0)
|
||||
{
|
||||
@@ -50,18 +49,14 @@ FileResourceHandler::processRequest(const Wt::Http::Request& request, Wt::Http::
|
||||
response.setStatus(404);
|
||||
return {};
|
||||
}
|
||||
else
|
||||
{
|
||||
response.setStatus(200);
|
||||
}
|
||||
|
||||
ifs.seekg(0, std::ios::end);
|
||||
const ::uint64_t fileSize {static_cast<::uint64_t>(ifs.tellg())};
|
||||
const ::uint64_t fileSize{ static_cast<::uint64_t>(ifs.tellg()) };
|
||||
ifs.seekg(0, std::ios::beg);
|
||||
|
||||
LMS_LOG(UTILS, DEBUG) << "File '" << _path.string() << "', fileSize = " << fileSize;
|
||||
|
||||
const Wt::Http::Request::ByteRangeSpecifier ranges {request.getRanges(fileSize)};
|
||||
const Wt::Http::Request::ByteRangeSpecifier ranges{ request.getRanges(fileSize) };
|
||||
if (!ranges.isSatisfiable())
|
||||
{
|
||||
std::ostringstream contentRange;
|
||||
@@ -92,9 +87,13 @@ FileResourceHandler::processRequest(const Wt::Http::Request& request, Wt::Http::
|
||||
{
|
||||
LMS_LOG(UTILS, DEBUG) << "No range requested";
|
||||
|
||||
response.setStatus(200);
|
||||
_beyondLastByte = fileSize;
|
||||
response.setContentLength(_beyondLastByte);
|
||||
}
|
||||
|
||||
LMS_LOG(UTILS, DEBUG) << "Mimetype set to '" << _mimeType << "'";
|
||||
response.setMimeType(_mimeType);
|
||||
}
|
||||
else if (!ifs)
|
||||
{
|
||||
@@ -111,7 +110,7 @@ FileResourceHandler::processRequest(const Wt::Http::Request& request, Wt::Http::
|
||||
::uint64_t pieceSize = buf.size() > restSize ? restSize : buf.size();
|
||||
|
||||
ifs.read(&buf[0], pieceSize);
|
||||
const ::uint64_t actualPieceSize {static_cast<::uint64_t>(ifs.gcount())};
|
||||
const ::uint64_t actualPieceSize{ static_cast<::uint64_t>(ifs.gcount()) };
|
||||
response.out().write(&buf[0], actualPieceSize);
|
||||
|
||||
LMS_LOG(UTILS, DEBUG) << "Written " << actualPieceSize << " bytes";
|
||||
@@ -128,4 +127,3 @@ FileResourceHandler::processRequest(const Wt::Http::Request& request, Wt::Http::
|
||||
LMS_LOG(UTILS, DEBUG) << "Job complete!";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,21 +20,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include "utils/IResourceHandler.hpp"
|
||||
|
||||
class FileResourceHandler final : public IResourceHandler
|
||||
{
|
||||
public:
|
||||
FileResourceHandler(const std::filesystem::path& filePath);
|
||||
public:
|
||||
FileResourceHandler(const std::filesystem::path& filePath, std::string_view mimeType);
|
||||
|
||||
private:
|
||||
private:
|
||||
Wt::Http::ResponseContinuation* processRequest(const Wt::Http::Request& request, Wt::Http::Response& response) override;
|
||||
void abort() override {};
|
||||
|
||||
static constexpr std::size_t _chunkSize {65536};
|
||||
static constexpr std::size_t _chunkSize{ 65536 };
|
||||
|
||||
std::filesystem::path _path;
|
||||
::uint64_t _beyondLastByte {};
|
||||
::uint64_t _offset {};
|
||||
std::string _mimeType;
|
||||
::uint64_t _beyondLastByte{};
|
||||
::uint64_t _offset{};
|
||||
};
|
||||
|
||||
|
||||
@@ -21,8 +21,9 @@
|
||||
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
#include "utils/IResourceHandler.hpp"
|
||||
|
||||
std::unique_ptr<IResourceHandler> createFileResourceHandler(const std::filesystem::path& path);
|
||||
std::unique_ptr<IResourceHandler> createFileResourceHandler(const std::filesystem::path& path, std::string_view mimeType);
|
||||
|
||||
|
||||
@@ -23,9 +23,9 @@
|
||||
#include <Wt/Http/Response.h>
|
||||
|
||||
#include "av/IAudioFile.hpp"
|
||||
#include "av/RawResourceHandlerCreator.hpp"
|
||||
#include "services/database/Session.hpp"
|
||||
#include "services/database/Track.hpp"
|
||||
#include "utils/FileResourceHandlerCreator.hpp"
|
||||
#include "utils/Logger.hpp"
|
||||
#include "utils/String.hpp"
|
||||
#include "LmsApplication.hpp"
|
||||
@@ -94,16 +94,7 @@ AudioFileResource::handleRequest(const Wt::Http::Request& request,
|
||||
if (!trackPath)
|
||||
return;
|
||||
|
||||
fileResourceHandler = createFileResourceHandler(*trackPath);
|
||||
|
||||
const auto guessedAudioFormat {Av::guessAudioFileFormat(*trackPath)};
|
||||
if (guessedAudioFormat)
|
||||
{
|
||||
LOG(DEBUG) << "Set mime type to " << guessedAudioFormat->mimeType;
|
||||
response.setMimeType(guessedAudioFormat->mimeType);
|
||||
}
|
||||
else
|
||||
response.setMimeType("application/octet-stream");
|
||||
fileResourceHandler = Av::createRawResourceHandler(*trackPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user