Skip to content

Commit

Permalink
JI-1062 Add interface function for saving files from .h5p
Browse files Browse the repository at this point in the history
Make it easier for plugins to override the unpacking of the
package files.
  • Loading branch information
icc committed Mar 15, 2019
1 parent d38b3b1 commit 366d8f2
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 121 deletions.
34 changes: 23 additions & 11 deletions h5p-default-storage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,14 @@ public function cloneContentFile($file, $fromId, $toId) {
* content from the current temporary upload folder to the editor path.
*
* @param string $source path to source directory
* @param string $contentId Id of content
*
* @return object Object containing h5p json and content json data
* @param string $contentId Id of contentarray
*/
public function moveContentDirectory($source, $contentId = NULL) {
if ($source === NULL) {
return NULL;
}

// TODO: Remove $contentId and never copy temporary files into content folder. JI-366
if ($contentId === NULL || $contentId == 0) {
$target = $this->getEditorPath();
}
Expand All @@ -385,14 +384,7 @@ public function moveContentDirectory($source, $contentId = NULL) {
}
}

// Successfully loaded content json of file into editor
$h5pJson = $this->getContent($source . DIRECTORY_SEPARATOR . 'h5p.json');
$contentJson = $this->getContent($contentSource . DIRECTORY_SEPARATOR . 'content.json');

return (object) array(
'h5pJson' => $h5pJson,
'contentJson' => $contentJson
);
// TODO: Return list of all files so that they can be marked as temporary. JI-366
}

/**
Expand Down Expand Up @@ -476,6 +468,26 @@ public function getUpgradeScript($machineName, $majorVersion, $minorVersion) {
}
}

/**
* Store the given stream into the given file.
*
* @param string $path
* @param string $file
* @param resource $stream
* @return bool
*/
public function saveFileFromZip($path, $file, $stream) {
$filePath = $path . '/' . $file;

// Make sure the directory exists first
$matches = array();
preg_match('/(.+)\/[^\/]*$/', $filePath, $matches);
self::dirReady($matches[1]);

// Store in local storage folder
return file_put_contents($filePath, $stream);
}

/**
* Recursive function for copying directories.
*
Expand Down
10 changes: 10 additions & 0 deletions h5p-file-storage.interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,14 @@ public function hasPresave($libraryName, $developmentPath = null);
* @return string Relative path
*/
public function getUpgradeScript($machineName, $majorVersion, $minorVersion);

/**
* Store the given stream into the given file.
*
* @param string $path
* @param string $file
* @param resource $stream
* @return bool
*/
public function saveFileFromZip($path, $file, $stream);
}

0 comments on commit 366d8f2

Please sign in to comment.