xHTML export: change filenames of exported images.

This patch aims at:
1. replacing absolute paths by their hashes (do not leak directory structures)
2. not using counters anymore so that changing figures order in the document
   does not lead to large number of obsolete images in export directory.

Other changes than in xHTML export of images are unintended.
This commit is contained in:
Pavel Sanda 2020-08-01 21:26:36 +02:00
parent 50f8ed4169
commit eef0c8e8ed
3 changed files with 34 additions and 4 deletions

View File

@ -575,7 +575,7 @@ copyToDirIfNeeded(DocFileName const & file, string const & dir)
if (rtrim(only_path, "/") == rtrim(dir, "/"))
return make_pair(IDENTICAL_PATHS, FileName(file_in));
string mangled = file.mangledFileName();
string mangled = file.mangledFileName(empty_string(), false, true);
if (theFormats().isZippedFile(file)) {
// We need to change _eps.gz to .eps.gz. The mangled name is
// still unique because of the counter in mangledFileName().

View File

@ -22,6 +22,7 @@
#include "support/Package.h"
#include "support/qstring_helpers.h"
#include <QCryptographicHash>
#include <QDateTime>
#include <QDir>
#include <QFile>
@ -955,6 +956,11 @@ string DocFileName::outputFileName(string const & path) const
string DocFileName::mangledFileName(string const & dir) const
{
return mangledFileName(dir, true, false);
};
string DocFileName::mangledFileName(string const & dir, bool use_counter, bool encrypt_path) const
{
// Concurrent access to these variables is possible.
@ -972,6 +978,19 @@ string DocFileName::mangledFileName(string const & dir) const
string const name = absFileName();
// Now the real work. Remove the extension.
string mname = support::changeExtension(name, string());
if (encrypt_path) {
QString qname = toqstr(mname);
#if QT_VERSION >= 0x050000
QByteArray hash = QCryptographicHash::hash(qname.toLocal8Bit(),QCryptographicHash::Sha256);
#else
QByteArray hash = QCryptographicHash::hash(qname.toLocal8Bit(),QCryptographicHash::Sha1);
#endif
hash = hash.toHex();
mname = fromqstr(QString(hash));
mname = mname + "_" + onlyFileName();
}
// The mangled name must be a valid LaTeX name.
// The list of characters to keep is probably over-restrictive,
// but it is not really a problem.
@ -991,9 +1010,12 @@ string DocFileName::mangledFileName(string const & dir) const
// Prepend a counter to the filename. This is necessary to make
// the mangled name unique.
static int counter = 0;
if (use_counter) {
ostringstream s;
s << counter++ << mname;
mname = s.str();
}
// MiKTeX's YAP (version 2.4.1803) crashes if the file name
// is longer than about 160 characters. MiKTeX's pdflatex

View File

@ -291,6 +291,14 @@ public:
std::string
mangledFileName(std::string const & dir = empty_string()) const;
/** Identical to mangledFileName, wit the following additions:
*
* @encrypt_path allows using hash (SHA-256) instead of full path.
* @use_counter allows disabling the counter in the filename.
*/
std::string
mangledFileName(std::string const & dir, bool use_counter, bool encrypt_path) const;
/// \return the absolute file name without its .gz, .z, .Z extension
std::string unzippedFileName() const;