protected function make_file_record(array $fileinfo) {
$defaultrecord = array(
- 'contenthash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709', // sha1 of an empty file
+ 'contenthash' => file_storage::hash_from_string(''),
'contextid' => $this->contextid,
'component' => $this->component,
'filearea' => $this->filearea,
throw new moodle1_convert_exception('file_not_readable');
}
- $contenthash = sha1_file($pathname);
+ $contenthash = file_storage::hash_from_path($pathname);
$filesize = filesize($pathname);
$hashpath = $this->converter->get_workdir_path().'/files/'.substr($contenthash, 0, 2);
$hashfile = "$hashpath/$contenthash";
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/icon.gif"
);
- $this->iconhash = sha1_file($CFG->tempdir.'/backup/'.$this->tempdir.'/moddata/unittest/4/icon.gif');
+ $this->iconhash = file_storage::hash_from_path($CFG->tempdir.'/backup/'.$this->tempdir.'/moddata/unittest/4/icon.gif');
copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/icon.gif"
$this->assertEquals('array', gettype($files['/file1.gif']));
$this->assertEquals('array', gettype($files['/sub1/.']));
$this->assertEquals('array', gettype($files['/sub1/file2.gif']));
- $this->assertEquals(sha1(''), $files['/.']['contenthash']);
- $this->assertEquals(sha1(''), $files['/sub1/.']['contenthash']);
+ $this->assertEquals(file_storage::hash_from_string(''), $files['/.']['contenthash']);
+ $this->assertEquals(file_storage::hash_from_string(''), $files['/sub1/.']['contenthash']);
$this->assertEquals($this->iconhash, $files['/file1.gif']['contenthash']);
$this->assertEquals($this->iconhash, $files['/sub1/file2.gif']['contenthash']);
'filename' => '.',
'userid' => 0, // Don't rely on any particular user for these system records.
'filesize' => 0,
- 'contenthash' => sha1(''));
+ // Note: This does not use the file_storage API's hash calculator
+ // because access to core APIs is not allowed during upgrade.
+ 'contenthash' => sha1(''),
+ );
foreach ($rs as $r) {
$r->pathnamehash = sha1("/$r->contextid/user/draft/$r->itemid/.");
$DB->insert_record('files', (array)$r + $defaults);
$contenthash = '';
if (is_array($file)) {
$content = reset($file);
- $contenthash = sha1($content);
+ $contenthash = file_storage::hash_from_string($content);
} else {
$contenthash = $file->get_contenthash();
}
$fs = get_file_storage();
$issyncing = true;
if (file_extension_in_typegroup($filepath, 'web_image')) {
- $contenthash = sha1_file($filepath);
+ $contenthash = file_storage::hash_from_path($filepath);
if ($file->get_contenthash() == $contenthash) {
// File did not change since the last synchronisation.
$filesize = filesize($filepath);
}
} else {
// Update only file size so file will NOT be copied into moodle filepool.
- $emptyfile = $contenthash = sha1('');
- $currentcontenthash = $file->get_contenthash();
- if ($currentcontenthash !== $emptyfile && $currentcontenthash === sha1_file($filepath)) {
+ if ($file->compare_to_string('') || !$file->compare_to_path($filepath)) {
+ // File is not synchronized or the file has changed.
+ $contenthash = file_storage::hash_from_string('');
+ } else {
// File content was synchronised and has not changed since then, leave it.
$contenthash = null;
}
// File is not found or is not readable.
return null;
}
- $filename = sha1($filecontents);
+ $filename = file_storage::hash_from_string($filecontents);
// Try to get generated thumbnail for this file.
$fs = get_file_storage();
foreach ($files as $filepath => $filesinpath) {
if ($filecontents = @file_get_contents($this->get_rootpath() . trim($filepath, '/'))) {
// The 'filename' in Moodle file storage is contenthash of the file in filesystem repository.
- $filename = sha1($filecontents);
+ $filename = file_storage::hash_from_string($filecontents);
foreach ($filesinpath as $file) {
if ($file->get_filename() !== $filename && $file->get_filename() !== '.') {
// Contenthash does not match, this is an old thumbnail.
// size, and a contenthash which does not related to empty content.
// If thereis no file size, or the contenthash is for an empty file, then the file has
// yet to be successfully downloaded.
- $contentexists = $file->get_filesize() && $file->get_contenthash() !== sha1('');
+ $contentexists = $file->get_filesize() && !$file->compare_to_string('');
if (!$file->get_status() && $contentexists) {
// we already have the content in moodle filepool and it was synchronised recently.