From b1531bcbc09c57ff4965cd179cad7e3252b2e6ff Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 7 Jul 2017 10:51:57 +0200 Subject: [PATCH] Add support for temporary file suffix HFP-1100 --- h5p-default-storage.class.php | 12 ++++++++++++ h5p.classes.php | 5 +++++ js/h5p.js | 3 ++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/h5p-default-storage.class.php b/h5p-default-storage.class.php index 8d2daf8..38af307 100644 --- a/h5p-default-storage.class.php +++ b/h5p-default-storage.class.php @@ -426,6 +426,18 @@ class H5PDefaultStorage implements \H5PFileStorage { $path = "{$this->path}/content/{$contentId}/{$file}"; if (file_exists($path)) { unlink($path); + + // Clean up any empty parent directories to avoid cluttering the file system + $parts = explode('/', $path); + while (array_pop($parts) !== NULL) { + $dir = implode('/', $parts); + if (is_dir($dir) && count(scandir($dir)) === 2) { // empty contains '.' and '..' + rmdir($dir); // Remove empty parent + } + else { + return; // Not empty + } + } } } diff --git a/h5p.classes.php b/h5p.classes.php index 32aeb38..51fd853 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -3400,6 +3400,11 @@ class H5PContentValidator { $file->path = $matches[5]; } + // Remove temporary files suffix + if (substr($file->path, -4, 4) === '#tmp') { + $file->path = substr($file->path, 0, strlen($file->path) - 4); + } + // Make sure path and mime does not have any special chars $file->path = htmlspecialchars($file->path, ENT_QUOTES, 'UTF-8', FALSE); if (isset($file->mime)) { diff --git a/js/h5p.js b/js/h5p.js index 2ad916f..b2d13b3 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -678,7 +678,8 @@ H5P.getPath = function (path, contentId) { } var prefix; - if (contentId !== undefined) { + var isTmpFile = (path.substr(-4,4) === '#tmp'); + if (contentId !== undefined && !isTmpFile) { // Check for custom override URL if (H5PIntegration.contents !== undefined && H5PIntegration.contents['cid-' + contentId]) {