From eef0c8e8ed53bc1aaa176b5a00c78624281b0548 Mon Sep 17 00:00:00 2001 From: Pavel Sanda Date: Sat, 1 Aug 2020 21:26:36 +0200 Subject: [PATCH] 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. --- src/insets/InsetGraphics.cpp | 2 +- src/support/FileName.cpp | 28 +++++++++++++++++++++++++--- src/support/FileName.h | 8 ++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/insets/InsetGraphics.cpp b/src/insets/InsetGraphics.cpp index b4ddd77a1a..62efa84dc0 100644 --- a/src/insets/InsetGraphics.cpp +++ b/src/insets/InsetGraphics.cpp @@ -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(). diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 179fef46ee..a6b849407a 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -22,6 +22,7 @@ #include "support/Package.h" #include "support/qstring_helpers.h" +#include #include #include #include @@ -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; - ostringstream s; - s << counter++ << mname; - mname = s.str(); + + 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 diff --git a/src/support/FileName.h b/src/support/FileName.h index ac351c2386..a0da84175a 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -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; -- 2.39.5