2016-03-18 16:07:39 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2017-03-08 10:38:22 -06:00
|
|
|
|
2016-03-18 16:07:39 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2019-11-19 12:29:54 -06:00
|
|
|
#include <thread>
|
2019-11-06 03:07:32 -06:00
|
|
|
#include <sysexits.h>
|
2016-03-18 16:07:39 -05:00
|
|
|
|
|
|
|
#include <Poco/Net/HTMLForm.h>
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
2016-04-14 13:32:56 -05:00
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
2016-03-18 16:07:39 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/FilePartSource.h>
|
2016-04-14 13:32:56 -05:00
|
|
|
#include <Poco/Net/SSLManager.h>
|
|
|
|
#include <Poco/Net/KeyConsoleHandler.h>
|
|
|
|
#include <Poco/Net/AcceptCertificateHandler.h>
|
2016-03-18 16:07:39 -05:00
|
|
|
#include <Poco/StreamCopier.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
#include <Poco/Util/OptionSet.h>
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Common.hpp>
|
|
|
|
#include <Protocol.hpp>
|
|
|
|
#include <Util.hpp>
|
2016-03-18 16:07:39 -05:00
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Simple command-line tool for file format conversion.
|
2016-03-18 16:07:39 -05:00
|
|
|
class Tool: public Poco::Util::Application
|
|
|
|
{
|
2018-05-09 05:27:30 -05:00
|
|
|
// Display help information on the console
|
|
|
|
void displayHelp();
|
|
|
|
|
2016-03-18 16:07:39 -05:00
|
|
|
public:
|
|
|
|
Tool();
|
|
|
|
|
2019-01-03 02:05:18 -06:00
|
|
|
const std::string& getServerURI() const { return _serverURI; }
|
|
|
|
const std::string& getDestinationFormat() const { return _destinationFormat; }
|
|
|
|
const std::string& getDestinationDir() const { return _destinationDir; }
|
|
|
|
|
|
|
|
private:
|
2016-03-18 16:07:39 -05:00
|
|
|
unsigned _numWorkers;
|
|
|
|
std::string _serverURI;
|
|
|
|
std::string _destinationFormat;
|
2016-04-14 15:41:09 -05:00
|
|
|
std::string _destinationDir;
|
2016-03-18 16:07:39 -05:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void defineOptions(Poco::Util::OptionSet& options) override;
|
|
|
|
void handleOption(const std::string& name, const std::string& value) override;
|
|
|
|
int main(const std::vector<std::string>& args) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
using namespace COOLProtocol;
|
2016-03-18 16:07:39 -05:00
|
|
|
|
|
|
|
using Poco::Net::HTTPClientSession;
|
|
|
|
using Poco::Net::HTTPRequest;
|
|
|
|
using Poco::Net::HTTPResponse;
|
|
|
|
using Poco::Runnable;
|
|
|
|
using Poco::URI;
|
|
|
|
using Poco::Util::Application;
|
|
|
|
using Poco::Util::OptionSet;
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Thread class which performs the conversion.
|
2016-03-18 16:07:39 -05:00
|
|
|
class Worker: public Runnable
|
|
|
|
{
|
|
|
|
Tool& _app;
|
|
|
|
std::vector< std::string > _files;
|
2019-01-03 02:05:18 -06:00
|
|
|
public:
|
2016-03-18 16:07:39 -05:00
|
|
|
Worker(Tool& app, const std::vector< std::string > & files) :
|
|
|
|
_app(app), _files(files)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void run() override
|
|
|
|
{
|
2016-04-27 07:58:33 -05:00
|
|
|
for (const auto& i : _files)
|
2016-03-18 16:07:39 -05:00
|
|
|
convertFile(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void convertFile(const std::string& document)
|
|
|
|
{
|
2019-01-03 02:05:18 -06:00
|
|
|
Poco::URI uri(_app.getServerURI());
|
2016-04-14 13:32:56 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPClientSession *session;
|
2019-01-03 02:05:18 -06:00
|
|
|
if (_app.getServerURI().compare(0, 5, "https") == 0)
|
2016-04-14 13:32:56 -05:00
|
|
|
session = new Poco::Net::HTTPSClientSession(uri.getHost(), uri.getPort());
|
|
|
|
else
|
|
|
|
session = new Poco::Net::HTTPClientSession(uri.getHost(), uri.getPort());
|
|
|
|
|
2021-11-15 09:37:59 -06:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/cool/convert-to");
|
2016-03-18 16:07:39 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
|
2019-01-03 02:05:18 -06:00
|
|
|
form.set("format", _app.getDestinationFormat());
|
2016-03-18 16:07:39 -05:00
|
|
|
form.addPart("data", new Poco::Net::FilePartSource(document));
|
|
|
|
form.prepareSubmit(request);
|
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
// If this results in a Poco::Net::ConnectionRefusedException, coolwsd is not running.
|
2016-04-14 13:32:56 -05:00
|
|
|
form.write(session->sendRequest(request));
|
2016-03-18 16:07:39 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception &e)
|
|
|
|
{
|
2016-04-14 13:32:56 -05:00
|
|
|
std::cerr << "Failed to write data: " << e.name() <<
|
2020-05-24 08:10:18 -05:00
|
|
|
' ' << e.message() << '\n';
|
2016-03-18 16:07:39 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-04-14 13:32:56 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
// receiveResponse() resulted in a Poco::Net::NoMessageException.
|
|
|
|
std::istream& responseStream = session->receiveResponse(response);
|
|
|
|
|
|
|
|
Poco::Path path(document);
|
2020-05-24 08:10:18 -05:00
|
|
|
std::string outPath = _app.getDestinationDir() + '/' + path.getBaseName() + '.' + _app.getDestinationFormat();
|
2016-04-14 13:32:56 -05:00
|
|
|
std::ofstream fileStream(outPath);
|
|
|
|
|
|
|
|
Poco::StreamCopier::copyStream(responseStream, fileStream);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception &e)
|
|
|
|
{
|
|
|
|
std::cerr << "Exception converting: " << e.name() <<
|
2020-05-24 08:10:18 -05:00
|
|
|
' ' << e.message() << '\n';
|
2016-04-14 13:32:56 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete session;
|
2016-03-18 16:07:39 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Tool::Tool() :
|
|
|
|
_numWorkers(4),
|
|
|
|
#if ENABLE_SSL
|
2016-04-14 13:32:56 -05:00
|
|
|
_serverURI("https://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER)),
|
2016-03-18 16:07:39 -05:00
|
|
|
#else
|
2016-04-14 13:32:56 -05:00
|
|
|
_serverURI("http://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER)),
|
2016-03-18 16:07:39 -05:00
|
|
|
#endif
|
|
|
|
_destinationFormat("txt")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:27:30 -05:00
|
|
|
void Tool::displayHelp()
|
|
|
|
{
|
2020-10-02 03:53:04 -05:00
|
|
|
std::cout << "Collabora Online document converter tool.\n"
|
2018-07-11 03:35:10 -05:00
|
|
|
<< "Usage: " << commandName() << " [options] file...\n"
|
|
|
|
<< "Options are:\n"
|
|
|
|
<< " --help Show this text\n"
|
|
|
|
<< " --extension=format File format to convert to\n"
|
|
|
|
<< " --outdir=directory Output directory for converted files\n"
|
|
|
|
<< " --parallelism=threads Number of simultaneous threads to use\n"
|
2021-11-15 10:07:24 -06:00
|
|
|
<< " --server=uri URI of COOL server\n"
|
2018-07-11 03:35:10 -05:00
|
|
|
<< " --no-check-certificate Disable checking of SSL certificate\n"
|
|
|
|
<< "In addition, the options taken by the libreoffice command for its --convert-to\n"
|
|
|
|
<< "functionality can be used (but are ignored if irrelevant to this command)." << std::endl;
|
2018-05-09 05:27:30 -05:00
|
|
|
}
|
|
|
|
|
2018-07-10 17:26:53 -05:00
|
|
|
void Tool::defineOptions(OptionSet&)
|
2016-03-18 16:07:39 -05:00
|
|
|
{
|
2018-07-10 17:26:53 -05:00
|
|
|
stopOptionsProcessing();
|
2016-03-18 16:07:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tool::handleOption(const std::string& optionName,
|
2016-04-14 15:41:09 -05:00
|
|
|
const std::string& value)
|
2016-03-18 16:07:39 -05:00
|
|
|
{
|
|
|
|
if (optionName == "help")
|
|
|
|
{
|
2018-05-09 05:27:30 -05:00
|
|
|
displayHelp();
|
2019-11-06 03:07:32 -06:00
|
|
|
std::exit(EX_OK);
|
2016-03-18 16:07:39 -05:00
|
|
|
}
|
2018-07-10 17:26:53 -05:00
|
|
|
else if (optionName == "extension"
|
|
|
|
|| optionName == "convert-to")
|
2016-03-18 16:07:39 -05:00
|
|
|
_destinationFormat = value;
|
|
|
|
else if (optionName == "outdir")
|
|
|
|
_destinationDir = value;
|
2016-04-14 15:41:09 -05:00
|
|
|
else if (optionName == "parallelism")
|
|
|
|
_numWorkers = std::max(std::stoi(value), 1);
|
2018-07-10 13:15:47 -05:00
|
|
|
else if (optionName == "server")
|
2016-03-18 16:07:39 -05:00
|
|
|
_serverURI = value;
|
2016-04-14 13:32:56 -05:00
|
|
|
else if (optionName == "no-check-certificate")
|
|
|
|
{
|
|
|
|
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler> consoleClientHandler = new Poco::Net::KeyConsoleHandler(false);
|
|
|
|
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidClientCertHandler = new Poco::Net::AcceptCertificateHandler(false);
|
|
|
|
Poco::Net::Context::Ptr sslClientContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "");
|
|
|
|
Poco::Net::SSLManager::instance().initializeClient(consoleClientHandler, invalidClientCertHandler, sslClientContext);
|
|
|
|
}
|
2016-03-18 16:07:39 -05:00
|
|
|
}
|
|
|
|
|
2018-07-10 17:26:53 -05:00
|
|
|
int Tool::main(const std::vector<std::string>& origArgs)
|
2016-03-18 16:07:39 -05:00
|
|
|
{
|
2018-07-10 17:26:53 -05:00
|
|
|
std::vector<std::string> args = origArgs;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < origArgs.size(); ++i)
|
|
|
|
{
|
|
|
|
if (origArgs[i].length() > 0 && origArgs[i][0] != '-')
|
|
|
|
break;
|
|
|
|
|
|
|
|
// It's an option. Erase it from the file name vector.
|
|
|
|
args.erase(args.begin());
|
|
|
|
|
|
|
|
std::string optionName, value;
|
|
|
|
|
|
|
|
// Accept either one or two dashes, like LibreOffice.
|
|
|
|
if (origArgs[i].length() > 1 && origArgs[i][1] != '-')
|
|
|
|
optionName = origArgs[i].substr(1);
|
|
|
|
else if (origArgs[i].length() > 1 && origArgs[i][1] == '-')
|
|
|
|
optionName = origArgs[i].substr(2);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::string::size_type equals = optionName.find('=');
|
|
|
|
|
|
|
|
// Handle LibreOffice-compatible options that don't have their value separated with an equals,
|
|
|
|
// but as the next argument.
|
|
|
|
if (equals == std::string::npos
|
|
|
|
&& (optionName == "convert-to"
|
|
|
|
|| optionName == "outdir")
|
|
|
|
&& i < origArgs.size()-1)
|
|
|
|
{
|
|
|
|
value = origArgs[i+1];
|
|
|
|
args.erase(args.begin());
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else if (equals != std::string::npos)
|
|
|
|
{
|
|
|
|
value = optionName.substr(equals+1);
|
|
|
|
optionName = optionName.substr(0, equals);
|
|
|
|
}
|
|
|
|
handleOption(optionName, value);
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:27:30 -05:00
|
|
|
if (args.empty())
|
|
|
|
{
|
|
|
|
std::cerr << "Nothing to do." << std::endl;
|
|
|
|
displayHelp();
|
2019-11-06 03:07:32 -06:00
|
|
|
return EX_NOINPUT;
|
2018-05-09 05:27:30 -05:00
|
|
|
}
|
|
|
|
|
2019-11-19 12:29:54 -06:00
|
|
|
std::vector<std::thread> clients;
|
|
|
|
clients.reserve(_numWorkers);
|
2016-03-18 16:07:39 -05:00
|
|
|
|
|
|
|
size_t chunk = (args.size() + _numWorkers - 1) / _numWorkers;
|
|
|
|
size_t offset = 0;
|
|
|
|
for (unsigned i = 0; i < _numWorkers; i++)
|
|
|
|
{
|
|
|
|
size_t toCopy = std::min(args.size() - offset, chunk);
|
|
|
|
if (toCopy > 0)
|
|
|
|
{
|
|
|
|
std::vector< std::string > files( toCopy );
|
2016-04-15 07:55:34 -05:00
|
|
|
std::copy( args.begin() + offset, args.begin() + offset + toCopy, files.begin() );
|
2016-03-18 16:07:39 -05:00
|
|
|
offset += toCopy;
|
2021-11-19 03:59:34 -06:00
|
|
|
clients.emplace_back([this, files]{Worker(*this, files).run();});
|
2016-03-18 16:07:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 12:29:54 -06:00
|
|
|
for (auto& client: clients)
|
2016-03-18 16:07:39 -05:00
|
|
|
{
|
2019-11-19 12:29:54 -06:00
|
|
|
client.join();
|
2016-03-18 16:07:39 -05:00
|
|
|
}
|
|
|
|
|
2019-11-06 03:07:32 -06:00
|
|
|
return EX_OK;
|
2016-03-18 16:07:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
POCO_APP_MAIN(Tool)
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|