Allow for alternative editor path

(used by Drupal)
pull/22/head
Frode Petterson 2016-06-14 12:39:15 +02:00
parent 0ee4cc57fc
commit 89541acfa8
1 changed files with 25 additions and 12 deletions

View File

@ -16,17 +16,20 @@
* @license MIT * @license MIT
*/ */
class H5PDefaultStorage implements \H5PFileStorage { class H5PDefaultStorage implements \H5PFileStorage {
private $path; private $path, $alteditorpath;
/** /**
* The great Constructor! * The great Constructor!
* *
* @param string $path * @param string $path
* The base location of H5P files * The base location of H5P files
* @param string $alteditorpath
* Optional. Use a different editor path
*/ */
function __construct($path) { function __construct($path, $alteditorpath = NULL) {
// Set H5P storage path // Set H5P storage path
$this->path = $path; $this->path = $path;
$this->alteditorpath = $alteditorpath;
} }
/** /**
@ -266,7 +269,15 @@ class H5PDefaultStorage implements \H5PFileStorage {
*/ */
public function saveFile($file, $contentId) { public function saveFile($file, $contentId) {
// Prepare directory // Prepare directory
$path = $this->path . '/' . (empty($contentId) ? 'editor' : 'content/' . $contentId) . '/' . $file->getType() . 's'; if (empty($contentId)) {
// Should be in editor tmp folder
$path = ($this->alteditorpath !== NULL ? $this->alteditorpath : $path . '/editor');
}
else {
// Should be in content folder
$path = $this->path . '/content/' . $contentId;
}
$path .= '/' . $file->getType() . 's';
self::dirReady($path); self::dirReady($path);
// Add filename to path // Add filename to path
@ -291,8 +302,8 @@ class H5PDefaultStorage implements \H5PFileStorage {
*/ */
public function cloneContentFile($file, $fromId, $toId) { public function cloneContentFile($file, $fromId, $toId) {
// Determine source path // Determine source path
if ($formId === 'editor') { if ($fromId === 'editor') {
$sourcepath = (empty($this->editorpath) ? "{$this->path}/editor" : $this->editorpath); $sourcepath = ($this->alteditorpath !== NULL ? $this->alteditorpath : "{$this->path}/editor");
} }
else { else {
$sourcepath = "{$this->path}/content/{$fromId}"; $sourcepath = "{$this->path}/content/{$fromId}";
@ -300,12 +311,14 @@ class H5PDefaultStorage implements \H5PFileStorage {
$sourcepath .= '/' . $file; $sourcepath .= '/' . $file;
// Determine target path // Determine target path
$targetpath = "{$this->path}/content/{$toId}"; $filename = basename($file);
$filedir = str_replace($filename, '', $file);
$targetpath = "{$this->path}/content/{$toId}/{$filedir}";
// Make sure it's ready // Make sure it's ready
self::dirReady($targetpath); self::dirReady($targetpath);
$targetpath .= '/' . $file; $targetpath .= $filename;
// Check to see if source exist and if target doesn't // Check to see if source exist and if target doesn't
if (!file_exists($sourcepath) || file_exists($targetpath)) { if (!file_exists($sourcepath) || file_exists($targetpath)) {