Improved packaging. Made sure zip format is followed by always using forward slashes. This will make h5p export work on Windows.

semantics-font
Frode Petterson 2015-03-19 16:58:48 +01:00
parent 5931ed5d76
commit 51a1d333e2
1 changed files with 38 additions and 13 deletions

View File

@ -1519,28 +1519,53 @@ Class H5PExport {
$results = print_r(json_encode($h5pJson), true); $results = print_r(json_encode($h5pJson), true);
file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'h5p.json', $results); file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'h5p.json', $results);
// Get a complete file list from our tmp dir
$files = array();
self::populateFileList($tempPath, $files);
// Create new zip instance. // Create new zip instance.
$zip = new ZipArchive(); $zip = new ZipArchive();
$zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE); $zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
// Get all files and folders in $tempPath // Add all the files from the tmp dir.
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempPath . DIRECTORY_SEPARATOR)); foreach ($files as $file) {
// Add files to zip // Please note that the zip format has no concept of folders, we must
foreach ($iterator as $key => $value) { // use forward slashes to separate our directories.
$test = '.'; $zip->addFile($file->absolutePath, $file->relativePath);
// Do not add the folders '.' and '..' to the zip. This will make zip invalid.
if (substr_compare($key, $test, -strlen($test), strlen($test)) !== 0) {
// Get files path in $tempPath
$filePath = explode($tempPath . DIRECTORY_SEPARATOR, $key);
// Add files to the zip with the intended file-structure
$zip->addFile($key, $filePath[1]);
}
} }
// Close zip and remove temp dir // Close zip and remove temp dir
$zip->close(); $zip->close();
H5PCore::deleteFileTree($tempPath); H5PCore::deleteFileTree($tempPath);
} }
/**
* Recursive function the will add the files of the given directory to the
* given files list. All files are objects with an absolute path and
* a relative path. The relative path is forward slashes only! Great for
* use in zip files and URLs.
*
* @param string $dir path
* @param array $files list
* @param string $relative prefix. Optional
*/
private static function populateFileList($dir, &$files, $relative = '') {
$strip = strlen($dir) + 1;
foreach (glob($dir . DIRECTORY_SEPARATOR . '*') as $file) {
$rel = $relative . substr($file, $strip);
if (is_dir($file)) {
self::populateFileList($file, $files, $rel . '/');
}
else {
// TODO: Should we filter out files that aren't in the whitelist?
$files[] = (object) array(
'absolutePath' => $file,
'relativePath' => $rel
);
}
}
}
/** /**
* Delete .h5p file * Delete .h5p file
* *