Skip to content

Commit b993c31

Browse files
vmikloscaolanm
authored andcommitted
cool#13419 convert-to template option: more strict para name, generalize filenames
See <#13425 (review)>, it's better if ConvertToPartHandler just deals with a set of filenames, and lets the callers to decide how to handle them. It's not ConvertToPartHandler's job to be aware of the template option of convert-to. Another concern is that till we had a single "stream" parameter, the parameter name didn't matter, though SDK examples always used "data". So now that we have a "template" parameter, good to enforce the "data" name at least in case we have multiple stream parameters. Fix this by adding a new ConvertToPartHandler::_filenames, and let the caller ClientRequestDispatcher::handlePostRequest() deal with the actual parameter names. This requires (straightforward) changes to the "insertfile" endpoint, too -- keep it functionally unchanged: just take the first stream parameter as the file to be inserted. Signed-off-by: Miklos Vajna <[email protected]> Change-Id: Ic8e71f933fc8f185460135b60b59adb57665ad6d
1 parent 3916a7f commit b993c31

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

wsd/ClientRequestDispatcher.cpp

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -241,31 +241,24 @@ class ClipboardPartHandler : public Poco::Net::PartHandler
241241
/// Also owns the file - cleaning it up when destroyed.
242242
class ConvertToPartHandler : public Poco::Net::PartHandler
243243
{
244-
std::string _filename;
245-
std::string _templateOptionFilename;
244+
/// Parameter name -> filename map.
245+
std::map<std::string, std::string> _filenames;
246246

247247
public:
248-
std::string getFilename() const { return _filename; }
249-
std::string getTemplateOptionFilename() const { return _templateOptionFilename; }
248+
const std::map<std::string, std::string>& getFilenames() const { return _filenames; }
250249

251250
/// Afterwards someone else is responsible for cleaning that up.
252-
void takeFile() { _filename.clear(); }
253-
void takeTemplateOptionFile() { _templateOptionFilename.clear(); }
251+
void takeFiles() { _filenames.clear(); }
254252

255253
ConvertToPartHandler() {}
256254

257255
virtual ~ConvertToPartHandler()
258256
{
259-
if (!_filename.empty())
260-
{
261-
LOG_TRC("Remove un-handled temporary file '" << _filename << '\'');
262-
StatelessBatchBroker::removeFile(_filename);
263-
}
264-
265-
if (!_templateOptionFilename.empty())
257+
for (const auto& it : _filenames)
266258
{
267-
LOG_TRC("Remove un-handled template option file '" << _templateOptionFilename << '\'');
268-
StatelessBatchBroker::removeFile(_templateOptionFilename);
259+
const std::string& filename = it.second;
260+
LOG_TRC("Remove un-handled temporary file '" << filename << '\'');
261+
StatelessBatchBroker::removeFile(filename);
269262
}
270263
}
271264

@@ -304,17 +297,13 @@ class ConvertToPartHandler : public Poco::Net::PartHandler
304297
tempPath.setFileName("incoming_file"); // A sensible name.
305298
else
306299
tempPath.setFileName(filenameParam.getFileName()); //TODO: Sanitize.
307-
bool isTemplateOption = params.has("name") && params.get("name") == "template";
308-
if (isTemplateOption)
300+
std::string paramName = "data";
301+
if (params.has("name"))
309302
{
310-
_templateOptionFilename = tempPath.toString();
311-
LOG_DBG("Storing incoming template option file to: " << _templateOptionFilename);
312-
}
313-
else
314-
{
315-
_filename = tempPath.toString();
316-
LOG_DBG("Storing incoming file to: " << _filename);
303+
paramName = params.get("name");
317304
}
305+
_filenames[paramName] = tempPath.toString();
306+
LOG_DBG("Storing incoming file to: " << _filenames[paramName]);
318307

319308
// Copy the stream to the temp path.
320309
std::ofstream fileStream;
@@ -2157,13 +2146,30 @@ bool ClientRequestDispatcher::handlePostRequest(const RequestDetails& requestDet
21572146
if (requestDetails.equals(1, "convert-to") && format.empty())
21582147
hasRequiredParameters = false;
21592148

2160-
const std::string fromPath = handler.getFilename();
2149+
const std::map<std::string, std::string> fromPaths = handler.getFilenames();
2150+
std::string fromPath;
2151+
auto it = fromPaths.find("data");
2152+
if (it != fromPaths.end())
2153+
{
2154+
fromPath = it->second;
2155+
}
2156+
if (fromPath.empty() && fromPaths.size() == 1)
2157+
{
2158+
// Compatibility: if there is a single stream, then allow any name and assume 'data'.
2159+
it = fromPaths.begin();
2160+
fromPath = it->second;
2161+
}
21612162
LOG_INF("Conversion request for URI [" << fromPath << "] format [" << format << "].");
21622163
if (!fromPath.empty() && hasRequiredParameters)
21632164
{
21642165
Poco::URI uriPublic = RequestDetails::sanitizeURI(fromPath);
21652166
Poco::URI templateOptionUriPublic;
2166-
const std::string templateOptionFromPath = handler.getTemplateOptionFilename();
2167+
std::string templateOptionFromPath;
2168+
it = fromPaths.find("template");
2169+
if (it != fromPaths.end())
2170+
{
2171+
templateOptionFromPath = it->second;
2172+
}
21672173
if (!templateOptionFromPath.empty())
21682174
{
21692175
templateOptionUriPublic = RequestDetails::sanitizeURI(templateOptionFromPath);
@@ -2232,8 +2238,7 @@ bool ClientRequestDispatcher::handlePostRequest(const RequestDetails& requestDet
22322238
auto docBroker = getConvertToBrokerImplementation(
22332239
requestDetails[1], fromPath, uriPublic, docKey, format, options, lang, target,
22342240
filter, encodedTransformJSON);
2235-
handler.takeFile();
2236-
handler.takeTemplateOptionFile();
2241+
handler.takeFiles();
22372242

22382243
COOLWSD::cleanupDocBrokers();
22392244

@@ -2298,14 +2303,22 @@ bool ClientRequestDispatcher::handlePostRequest(const RequestDetails& requestDet
22982303
LOG_INF("Perform insertfile: " << formChildid << ", " << formName
22992304
<< ", filename: " << fileName);
23002305
Poco::File(dirPath).createDirectories();
2301-
Poco::File(handler.getFilename()).moveTo(fileName);
2306+
std::string filename;
2307+
const std::map<std::string, std::string>& filenames = handler.getFilenames();
2308+
if (!filenames.empty())
2309+
{
2310+
// Expect a single parameter, don't care about the name.
2311+
auto it = filenames.begin();
2312+
filename = it->second;
2313+
}
2314+
Poco::File(filename).moveTo(fileName);
23022315

23032316
// Cleanup the directory after moving.
2304-
const std::string dir = Poco::Path(handler.getFilename()).parent().toString();
2317+
const std::string dir = Poco::Path(filename).parent().toString();
23052318
if (FileUtil::isEmptyDirectory(dir))
23062319
FileUtil::removeFile(dir);
23072320

2308-
handler.takeFile();
2321+
handler.takeFiles();
23092322

23102323
http::Response httpResponse(http::StatusCode::OK);
23112324
FileServerRequestHandler::hstsHeaders(httpResponse);

0 commit comments

Comments
 (0)