[LyX/master] xHTML export: change filenames of exported images.

Pavel Sanda sanda at lyx.org
Sat Aug 1 19:50:56 UTC 2020


commit eef0c8e8ed53bc1aaa176b5a00c78624281b0548
Author: Pavel Sanda <sanda at lyx.org>
Date:   Sat Aug 1 21:26:36 2020 +0200

    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 b4ddd77..62efa84 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 179fef4..a6b8494 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 <QCryptographicHash>
 #include <QDateTime>
 #include <QDir>
 #include <QFile>
@@ -956,6 +957,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.
 
 	// We need to make sure that every DocFileName instance for a given
@@ -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 ac351c2..a0da841 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;
 


More information about the lyx-cvs mailing list