Do not crash when transcoding a non existing file (regression from eea27b8), fixes #141

This commit is contained in:
emeric
2021-04-17 15:00:14 +02:00
parent 68107311e3
commit 05f0213a9f
5 changed files with 53 additions and 47 deletions
@@ -33,7 +33,6 @@ namespace Av
TranscodeResourceHandler::TranscodeResourceHandler(const std::filesystem::path& trackPath, const TranscodeParameters& parameters)
: _transcoder {trackPath, parameters}
{
_transcoder.start();
}
Wt::Http::ResponseContinuation*
+9 -16
View File
@@ -48,11 +48,12 @@ Transcoder::Transcoder(const std::filesystem::path& filePath, const TranscodePar
, _filePath {filePath}
, _parameters {parameters}
{
start();
}
Transcoder::~Transcoder() = default;
bool
void
Transcoder::start()
{
if (ffmpegPath.empty())
@@ -61,20 +62,13 @@ Transcoder::start()
try
{
if (!std::filesystem::exists(_filePath))
{
LOG(ERROR) << "File '" << _filePath << "' does not exist!";
return false;
}
throw Exception {"File '" + _filePath.string() + "' does not exist!"};
else if (!std::filesystem::is_regular_file( _filePath) )
{
LOG(ERROR) << "File '" << _filePath << "' is not regular!";
return false;
}
throw Exception {"File '" + _filePath.string() + "' is not regular!"};
}
catch (const std::filesystem::filesystem_error& e)
{
LOG(ERROR) << "File error on '" << _filePath.string() << "': " << e.what();
return false;
throw Exception {"File error '" + _filePath.string() + "': " + e.what()};
}
LOG(INFO) << "Transcoding file '" << _filePath.string() << "'";
@@ -163,7 +157,7 @@ Transcoder::start()
break;
default:
return false;
throw Exception {"Unhandled format (" + std::to_string(static_cast<int>(_parameters.format)) + ")"};
}
_outputMimeType = formatToMimetype(_parameters.format);
@@ -181,11 +175,8 @@ Transcoder::start()
}
catch (ChildProcessException& exception)
{
LOG(ERROR) << "Cannot execute '" << ffmpegPath << "': " << exception.what();
return false;
throw Exception {"Cannot execute '" + ffmpegPath.string() + "': " + exception.what()};
}
return true;
}
void
@@ -223,6 +214,8 @@ Transcoder::readSome(std::byte* buffer, std::size_t bufferSize)
bool
Transcoder::finished() const
{
assert(_childProcess);
return _childProcess->finished();
}
+2 -2
View File
@@ -40,8 +40,6 @@ namespace Av
Transcoder(Transcoder&&) = delete;
Transcoder& operator=(Transcoder&&) = delete;
bool start();
using WaitCallback = std::function<void()>;
void asyncWaitForData(WaitCallback cb);
@@ -58,6 +56,8 @@ namespace Av
private:
static void init();
void start();
const std::size_t _id {};
const std::filesystem::path _filePath;
const TranscodeParameters _parameters;
+21 -14
View File
@@ -26,6 +26,7 @@
#include "database/Track.hpp"
#include "database/User.hpp"
#include "utils/IResourceHandler.hpp"
#include "utils/Logger.hpp"
#include "utils/FileResourceHandlerCreator.hpp"
#include "utils/Utils.hpp"
#include "ParameterParsing.hpp"
@@ -147,24 +148,30 @@ handleStream(RequestContext& context, const Wt::Http::Request& request, Wt::Http
{
std::shared_ptr<IResourceHandler> resourceHandler;
Wt::Http::ResponseContinuation* continuation = request.continuation();
if (!continuation)
try
{
StreamParameters streamParameters {getStreamParameters(context)};
if (streamParameters.transcodeParameters)
resourceHandler = Av::createTranscodeResourceHandler(streamParameters.trackPath, *streamParameters.transcodeParameters);
Wt::Http::ResponseContinuation* continuation = request.continuation();
if (!continuation)
{
StreamParameters streamParameters {getStreamParameters(context)};
if (streamParameters.transcodeParameters)
resourceHandler = Av::createTranscodeResourceHandler(streamParameters.trackPath, *streamParameters.transcodeParameters);
else
resourceHandler = createFileResourceHandler(streamParameters.trackPath);
}
else
resourceHandler = createFileResourceHandler(streamParameters.trackPath);
{
resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<IResourceHandler>>(continuation->data());
}
continuation = resourceHandler->processRequest(request, response);
if (continuation)
continuation->setData(resourceHandler);
}
else
catch (const Av::Exception& e)
{
resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<IResourceHandler>>(continuation->data());
LMS_LOG(API_SUBSONIC, ERROR) << "Caught Av exception: " << e.what();
}
continuation = resourceHandler->processRequest(request, response);
if (continuation)
continuation->setData(resourceHandler);
}
}
} // namespace API::Subsonic::Stream
+21 -14
View File
@@ -180,23 +180,30 @@ AudioTranscodeResource::handleRequest(const Wt::Http::Request& request,
{
std::shared_ptr<IResourceHandler> resourceHandler;
Wt::Http::ResponseContinuation* continuation {request.continuation()};
if (!continuation)
try
{
const std::optional<TranscodeParameters>& parameters {readTranscodeParameters(request)};
if (parameters)
resourceHandler = Av::createTranscodeResourceHandler(parameters->file, parameters->transcodeParameters);
}
else
{
resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<IResourceHandler>>(continuation->data());
}
Wt::Http::ResponseContinuation* continuation {request.continuation()};
if (!continuation)
{
const std::optional<TranscodeParameters>& parameters {readTranscodeParameters(request)};
if (parameters)
resourceHandler = Av::createTranscodeResourceHandler(parameters->file, parameters->transcodeParameters);
}
else
{
resourceHandler = Wt::cpp17::any_cast<std::shared_ptr<IResourceHandler>>(continuation->data());
}
if (resourceHandler)
if (resourceHandler)
{
continuation = resourceHandler->processRequest(request, response);
if (continuation)
continuation->setData(resourceHandler);
}
}
catch (const Av::Exception& e)
{
continuation = resourceHandler->processRequest(request, response);
if (continuation)
continuation->setData(resourceHandler);
LOG(ERROR) << "Caught Av exception: " << e.what();
}
}