Fixed default file storage implementation

pull/17/head
Frode Petterson 2016-01-12 12:55:03 +01:00
parent 808e43bc66
commit f4ba0af1ef
1 changed files with 33 additions and 13 deletions

View File

@ -1,25 +1,34 @@
<?php <?php
/** /**
* * File info?
*/ */
namespace H5P; namespace H5P;
/** /**
* The default file storage class for H5P. Will carry out the requested file
* operations using PHP's standard file operation functions.
* *
* Some implementations of H5P that doesn't use the standard file system will
* want to create their own implementation of the \H5P\FileStorage interface.
*
* @package H5P
* @copyright 2016 Joubel AS
* @license MIT
*/ */
class DefaultStorage implements \H5P\FileStorage { class DefaultStorage implements \H5P\FileStorage {
private $path; private $path;
/** /**
* The great Constructor! * The great Constructor!
*
* @param string $path
* The base location of H5P files
*/ */
function __construct($path) { function __construct($path) {
// Set H5P storage path // Set H5P storage path
$this->path = $path; $this->path = $path;
// TODO: Check if Dirs are ready? Perhaps in each function?
} }
/** /**
@ -47,7 +56,12 @@ class DefaultStorage implements \H5P\FileStorage {
* What makes this content unique. * What makes this content unique.
*/ */
public function saveContent($source, $id) { public function saveContent($source, $id) {
self::copyFileTree($source, $this->path . '/content/' . $id); $dest = "{$this->path}/content/{$id}";
// Remove any old content
\H5PCore::deleteFileTree($dest);
self::copyFileTree($source, $dest);
} }
/** /**
@ -57,7 +71,7 @@ class DefaultStorage implements \H5P\FileStorage {
* Content identifier * Content identifier
*/ */
public function deleteContent($id) { public function deleteContent($id) {
// TODO \H5PCore::deleteFileTree("{$this->path}/content/{$id}");
} }
/** /**
@ -80,7 +94,7 @@ class DefaultStorage implements \H5P\FileStorage {
* Path * Path
*/ */
public function getTmpPath() { public function getTmpPath() {
// TODO return $this->path . '/temp/' . uniqid('h5p-');
} }
/** /**
@ -92,7 +106,7 @@ class DefaultStorage implements \H5P\FileStorage {
* Where the content folder will be saved * Where the content folder will be saved
*/ */
public function exportContent($id, $target) { public function exportContent($id, $target) {
self::copyFileTree($this->path . '/content/' . $id, $target); self::copyFileTree("{$this->path}/content/{$id}", $target);
} }
/** /**
@ -104,7 +118,8 @@ class DefaultStorage implements \H5P\FileStorage {
* Where the library folder will be saved * Where the library folder will be saved
*/ */
public function exportLibrary($library, $target) { public function exportLibrary($library, $target) {
// TODO $folder = \H5PCore::libraryToString($library, TRUE);
self::copyFileTree("{$this->path}/libraries/{$folder}", $target);
} }
/** /**
@ -116,7 +131,9 @@ class DefaultStorage implements \H5P\FileStorage {
* Name of export file. * Name of export file.
*/ */
public function saveExport($source, $filename) { public function saveExport($source, $filename) {
// TODO $this->deleteExport($filename);
self::dirReady("{$this->path}/exports");
copy($source, "{$this->path}/exports/{$filename}");
} }
/** /**
@ -125,7 +142,10 @@ class DefaultStorage implements \H5P\FileStorage {
* @param string $filename * @param string $filename
*/ */
public function deleteExport($filename) { public function deleteExport($filename) {
// TODO $target = "{$this->path}/exports/{$filename}";
if (file_exists($target)) {
unlink($target);
}
} }
/** /**
@ -151,11 +171,11 @@ class DefaultStorage implements \H5P\FileStorage {
while (false !== ($file = readdir($dir))) { while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..') && $file != '.git' && $file != '.gitignore') { if (($file != '.') && ($file != '..') && $file != '.git' && $file != '.gitignore') {
if (is_dir($source . DIRECTORY_SEPARATOR . $file)) { if (is_dir("{$source}/{$file}")) {
self::copyFileTree($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file); self::copyFileTree("{$source}/{$file}", "{$destination}/{$file}");
} }
else { else {
copy($source . DIRECTORY_SEPARATOR . $file,$destination . DIRECTORY_SEPARATOR . $file); copy("{$source}/{$file}", "{$destination}/{$file}");
} }
} }
} }