Skip to content

Commit

Permalink
Add support for temporary file suffix
Browse files Browse the repository at this point in the history
HFP-1100
  • Loading branch information
icc committed Jul 7, 2017
1 parent f07ce1b commit b1531bc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions h5p-default-storage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,18 @@ public function removeContentFile($file, $contentId) {
$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
}
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions h5p.classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3400,6 +3400,11 @@ private function _validateFilelike(&$file, $semantics, $typeValidKeys = array())
$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)) {
Expand Down
3 changes: 2 additions & 1 deletion js/h5p.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down

0 comments on commit b1531bc

Please sign in to comment.