From e056b6776a17d7d7d15e19c6de507d7b62d427c1 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Wed, 4 May 2016 16:52:02 +0200 Subject: [PATCH] More functions for handling content files Added three more functions to File Storage Interface h5p/h5p-moodle-plugin#49 HFJ-1846 --- h5p-default-storage.class.php | 62 ++++++++++++++++++++++++++++++++++ h5p-file-storage.interface.php | 30 ++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/h5p-default-storage.class.php b/h5p-default-storage.class.php index f78b829..fb58f30 100644 --- a/h5p-default-storage.class.php +++ b/h5p-default-storage.class.php @@ -277,6 +277,68 @@ class H5PDefaultStorage implements \H5PFileStorage { } } + /** + * Copy a file from another content or editor tmp dir. + * Used when copy pasting content in H5P Editor. + * + * @param string $file path + name + * @param string|int $fromid Content ID or 'editor' string + * @param int $toid Target Content ID + */ + public function cloneContentFile($file, $fromId, $toId) { + // Determine source path + if ($formId === 'editor') { + $sourcepath = (empty($this->editorpath) ? "{$this->path}/editor" : $this->editorpath); + } + else { + $sourcepath = "{$this->path}/content/{$fromId}"; + } + $sourcepath .= '/' . $file; + + // Determine target path + $targetpath = "{$this->path}/content/{$toId}"; + + // Make sure it's ready + self::dirReady($targetpath); + + $targetpath .= '/' . $file; + + // Check to see if source exist and if target doesn't + if (!file_exists($sourcepath) || file_exists($targetpath)) { + return; // Nothing to copy from or target already exists + } + + copy($sourcepath, $targetpath); + } + + /** + * Checks to see if content has the given file. + * Used when saving content. + * + * @param string $file path + name + * @param int $contentId + * @return string File ID or NULL if not found + */ + public function getContentFile($file, $contentId) { + $path = "{$this->path}/content/{$contentId}/{$file}"; + return file_exists($path) ? $path : NULL; + } + + /** + * Checks to see if content has the given file. + * Used when saving content. + * + * @param string $file path + name + * @param int $contentid + * @return string|int File ID or NULL if not found + */ + public function removeContentFile($file, $contentId) { + $path = "{$this->path}/content/{$contentId}/{$file}"; + if (file_exists($path)) { + unlink($path); + } + } + /** * Recursive function for copying directories. * diff --git a/h5p-file-storage.interface.php b/h5p-file-storage.interface.php index 9f44f94..1ae1b47 100644 --- a/h5p-file-storage.interface.php +++ b/h5p-file-storage.interface.php @@ -134,4 +134,34 @@ interface H5PFileStorage { * @param int $contentid */ public function saveFile($file, $contentId); + + /** + * Copy a file from another content or editor tmp dir. + * Used when copy pasting content in H5P. + * + * @param string $file path + name + * @param string|int $fromid Content ID or 'editor' string + * @param int $toid Target Content ID + */ + public function cloneContentFile($file, $fromId, $toId); + + /** + * Checks to see if content has the given file. + * Used when saving content. + * + * @param string $file path + name + * @param int $contentid + * @return string|int File ID or NULL if not found + */ + public function getContentFile($file, $contentId); + + /** + * Remove content files that are no longer used. + * Used when saving content. + * + * @param string $file path + name + * @param int $contentid + * @return string|int File ID or NULL if not found + */ + public function removeContentFile($file, $contentId); }