Allow assets of only one type

pull/17/head
Frode Petterson 2016-01-25 14:49:54 +01:00
parent 3defc7abde
commit 36ca38705f
1 changed files with 28 additions and 33 deletions

View File

@ -92,7 +92,9 @@ class H5PDefaultStorage implements \H5PFileStorage {
* Path * Path
*/ */
public function getTmpPath() { public function getTmpPath() {
return $this->path . '/temp/' . uniqid('h5p-'); $temp = "{$this->path}/temp";
self::dirReady($temp);
return "{$temp}/" . uniqid('h5p-');
} }
/** /**
@ -187,11 +189,13 @@ class H5PDefaultStorage implements \H5PFileStorage {
self::dirReady("{$this->path}/cachedassets"); self::dirReady("{$this->path}/cachedassets");
$ext = ($type === 'scripts' ? 'js' : 'css'); $ext = ($type === 'scripts' ? 'js' : 'css');
file_put_contents("{$this->path}/cachedassets/{$key}.{$ext}", $content); $outputfile = "/cachedassets/{$key}.{$ext}";
file_put_contents($this->path . $outputfile, $content);
$files[$type] = array((object) array(
'path' => $outputfile,
'version' => ''
));
} }
// Use the newly created cache
$files = self::formatCachedAssets($key);
} }
/** /**
@ -202,11 +206,25 @@ class H5PDefaultStorage implements \H5PFileStorage {
* @return array * @return array
*/ */
public function getCachedAssets($key) { public function getCachedAssets($key) {
if (!file_exists("{$this->path}/cachedassets/{$key}.js") || $files = array();
!file_exists("{$this->path}/cachedassets/{$key}.css") {
return NULL; $js = "/cachedassets/{$key}.js";
if (file_exists($this->path . $js)) {
$files['scripts'] = array((object) array(
'path' => $js,
'version' => ''
));
} }
return self::formatCachedAssets($key);
$css = "/cachedassets/{$key}.css";
if (file_exists($this->path . $css)) {
$files['styles'] = array((object) array(
'path' => $css,
'version' => ''
));
}
return empty($files) ? NULL : $files;
} }
/** /**
@ -216,12 +234,9 @@ class H5PDefaultStorage implements \H5PFileStorage {
* The hash keys of removed files * The hash keys of removed files
*/ */
public function deleteCachedAssets($keys) { public function deleteCachedAssets($keys) {
$context = \context_system::instance();
$fs = get_file_storage();
foreach ($keys as $hash) { foreach ($keys as $hash) {
foreach (array('js', 'css') as $ext) { foreach (array('js', 'css') as $ext) {
$path = "{$this->path}/cachedassets/{$key}.{$ext}"; $path = "{$this->path}/cachedassets/{$hash}.{$ext}";
if (file_exists($path)) { if (file_exists($path)) {
unlink($path); unlink($path);
} }
@ -229,26 +244,6 @@ class H5PDefaultStorage implements \H5PFileStorage {
} }
} }
/**
* Format the cached assets data the way it's supposed to be.
*
* @param string $key
* Hashed key for cached asset
* @return array
*/
private static function formatCachedAssets($key) {
return array(
'scripts' => array((object) array(
'path' => "/cachedassets/{$key}.js",
'version' => ''
)),
'styles' => array((object) array(
'path' => "/cachedassets/{$key}.css",
'version' => ''
))
);
}
/** /**
* Recursive function for copying directories. * Recursive function for copying directories.
* *