Skip to content

Commit

Permalink
MDL-16596 file listing methods now return arrays index with pathnameh…
Browse files Browse the repository at this point in the history
…ash (useful when looking for existing files); create from stored file now supports directories too - very useful when moving files between areas
  • Loading branch information
skodak committed Sep 21, 2008
1 parent 6ed19c7 commit cd5be21
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/file/file_storage.php
Expand Up @@ -136,7 +136,7 @@ public function get_file($contextid, $filearea, $itemid, $filepath, $filename) {
* @param int $itemid (all files if not specified)
* @param string $sort
* @param bool $includedirs
* @return array of stored_files
* @return array of stored_files indexed by pathanmehash
*/
public function get_area_files($contextid, $filearea, $itemid=false, $sort="itemid, filepath, filename", $includedirs=true) {
global $DB;
Expand All @@ -152,7 +152,7 @@ public function get_area_files($contextid, $filearea, $itemid=false, $sort="item
if (!$includedirs and $file_record->filename === '.') {
continue;
}
$result[] = new stored_file($this, $file_record);
$result[$file_record->pathnamehash] = new stored_file($this, $file_record);
}
return $result;
}
Expand All @@ -166,7 +166,7 @@ public function get_area_files($contextid, $filearea, $itemid=false, $sort="item
* @param bool $recursive include all subdirectories
* @param bool $includedirs include files and directories
* @param string $sort
* @return array of stored_files
* @return array of stored_files indexed by pathanmehash
*/
public function get_directory_files($contextid, $filearea, $itemid, $filepath, $recursive=false, $includedirs=true, $sort="filepath, filename") {
global $DB;
Expand Down Expand Up @@ -194,9 +194,9 @@ public function get_directory_files($contextid, $filearea, $itemid, $filepath, $
$file_records = $DB->get_records_sql($sql, $params);
foreach ($file_records as $file_record) {
if ($file_record->filename == '.') {
$dirs[] = new stored_file($this, $file_record);
$dirs[$file_record->pathnamehash] = new stored_file($this, $file_record);
} else {
$files[] = new stored_file($this, $file_record);
$files[$file_record->pathnamehash] = new stored_file($this, $file_record);
}
}
$result = array_merge($dirs, $files);
Expand All @@ -221,7 +221,7 @@ public function get_directory_files($contextid, $filearea, $itemid, $filepath, $
if (substr_count($file_record->filepath, '/') !== $reqlevel) {
continue;
}
$result[] = new stored_file($this, $file_record);
$result[$file_record->pathnamehash] = new stored_file($this, $file_record);
}
}

Expand All @@ -233,7 +233,7 @@ public function get_directory_files($contextid, $filearea, $itemid, $filepath, $

$file_records = $DB->get_records_sql($sql, $params);
foreach ($file_records as $file_record) {
$result[] = new stored_file($this, $file_record);
$result[$file_record->pathnamehash] = new stored_file($this, $file_record);
}
}

Expand Down Expand Up @@ -414,6 +414,18 @@ public function create_file_from_storedfile($file_record, $fid) {

$newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);

if ($newrecord->filename === '.') {
// special case - only this function supports directories ;-)
$directory = $this->create_directory($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid);
// update the existing directory with the new data
$newrecord->id = $directory->get_id();
if (!$DB->update_record('files', $newrecord)) {
throw new stored_file_creation_exception($newrecord->contextid, $newrecord->filearea, $newrecord->itemid,
$newrecord->filepath, $newrecord->filename);
}
return new stored_file($this, $newrecord);
}

try {
$newrecord->id = $DB->insert_record('files', $newrecord);
} catch (database_exception $e) {
Expand All @@ -422,7 +434,7 @@ public function create_file_from_storedfile($file_record, $fid) {

if (!$newrecord->id) {
throw new stored_file_creation_exception($newrecord->contextid, $newrecord->filearea, $newrecord->itemid,
$newrecord->filepath, $newrecord->filename);
$newrecord->filepath, $newrecord->filename);
}

$this->create_directory($newrecord->contextid, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid);
Expand Down

0 comments on commit cd5be21

Please sign in to comment.