Merge branch 'MDL-54647-master' of git://github.com/damyon/moodle

This commit is contained in:
Andrew Nicols
2016-08-16 08:01:47 +08:00
2 changed files with 31 additions and 11 deletions
+16 -7
View File
@@ -184,16 +184,17 @@ class file_storage {
*
* @param stored_file $file the file we want to preview
* @param string $format The desired format - e.g. 'pdf'. Formats are specified by file extension.
* @param boolean $forcerefresh If true, the file will be converted every time (not cached).
* @return stored_file|bool false if unable to create the conversion, stored file otherwise
*/
public function get_converted_document(stored_file $file, $format) {
public function get_converted_document(stored_file $file, $format, $forcerefresh = false) {
$context = context_system::instance();
$path = '/' . $format . '/';
$conversion = $this->get_file($context->id, 'core', 'documentconversion', 0, $path, $file->get_contenthash());
if (!$conversion) {
$conversion = $this->create_converted_document($file, $format);
if (!$conversion || $forcerefresh) {
$conversion = $this->create_converted_document($file, $format, $forcerefresh);
if (!$conversion) {
return false;
}
@@ -259,7 +260,7 @@ class file_storage {
}
/**
* If the test pdf has been generated correctly and send it direct to the browser.
* Regenerate the test pdf and send it direct to the browser.
*/
public static function send_test_pdf() {
global $CFG;
@@ -286,7 +287,7 @@ class file_storage {
}
// Convert the doc file to pdf and send it direct to the browser.
$result = $fs->get_converted_document($testdocx, 'pdf');
$result = $fs->get_converted_document($testdocx, 'pdf', true);
readfile_accel($result, 'application/pdf', true);
}
@@ -334,7 +335,7 @@ class file_storage {
* @param string $format The desired format - e.g. 'pdf'. Formats are specified by file extension.
* @return stored_file|bool false if unable to create the conversion, stored file otherwise
*/
protected function create_converted_document(stored_file $file, $format) {
protected function create_converted_document(stored_file $file, $format, $forcerefresh = false) {
global $CFG;
if (empty($CFG->pathtounoconv) || !file_is_executable(trim($CFG->pathtounoconv))) {
@@ -394,15 +395,23 @@ class file_storage {
}
$context = context_system::instance();
$path = '/' . $format . '/';
$record = array(
'contextid' => $context->id,
'component' => 'core',
'filearea' => 'documentconversion',
'itemid' => 0,
'filepath' => '/' . $format . '/',
'filepath' => $path,
'filename' => $file->get_contenthash(),
);
if ($forcerefresh) {
$existing = $this->get_file($context->id, 'core', 'documentconversion', 0, $path, $file->get_contenthash());
if ($existing) {
$existing->delete();
}
}
$convertedfile = $this->create_file_from_pathname($record, $newtmpfile);
// Cleanup.
remove_dir($tmp);
+15 -4
View File
@@ -79,11 +79,16 @@ class core_unoconv_testcase extends advanced_testcase {
$result = $fs->get_converted_document($this->testfile1, 'pdf');
$this->assertNotFalse($result);
$this->assertSame($result->get_mimetype(), 'application/pdf');
$this->assertSame('application/pdf', $result->get_mimetype());
$this->assertGreaterThan(0, $result->get_filesize());
$result = $fs->get_converted_document($this->testfile2, 'pdf');
$this->assertNotFalse($result);
$this->assertSame($result->get_mimetype(), 'application/pdf');
$this->assertSame('application/pdf', $result->get_mimetype());
$this->assertGreaterThan(0, $result->get_filesize());
// Test forcing a refresh of the document.
$result = $fs->get_converted_document($this->testfile2, 'pdf', true);
$this->assertNotFalse($result);
$this->assertSame('application/pdf', $result->get_mimetype());
$this->assertGreaterThan(0, $result->get_filesize());
}
@@ -98,11 +103,17 @@ class core_unoconv_testcase extends advanced_testcase {
$result = $fs->get_converted_document($this->testfile1, 'txt');
$this->assertNotFalse($result);
$this->assertSame($result->get_mimetype(), 'text/plain');
$this->assertSame('text/plain', $result->get_mimetype());
$this->assertGreaterThan(0, $result->get_filesize());
$result = $fs->get_converted_document($this->testfile2, 'txt');
$this->assertNotFalse($result);
$this->assertSame($result->get_mimetype(), 'text/plain');
$this->assertSame('text/plain', $result->get_mimetype());
$this->assertGreaterThan(0, $result->get_filesize());
// Test forcing a refresh of the document.
$result = $fs->get_converted_document($this->testfile2, 'txt', true);
$this->assertNotFalse($result);
$this->assertSame('text/plain', $result->get_mimetype());
$this->assertGreaterThan(0, $result->get_filesize());
}
}