Commit 7cc354f2 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Make save_file static

parent fabd7c05
......@@ -588,23 +588,22 @@ class ArtefactTypeBlogPost extends ArtefactType {
$data = new StdClass;
$data->title = $title;
$data->description = $description;
$data->owner = $USER->get('id');
$data->adminfiles = 0; // No admin blogs yet...
$data->parent = self::blogfiles_folder_id();
$path = self::$blogattachmentroot . $directory . '/' . $filename;
$f = new ArtefactTypeFile(0, $data);
$f->set('owner', $USER->get('id'));
$f->set('adminfiles', 0); // No admin blogs yet...
$f->set('parent', self::blogfiles_folder_id());
if (!$f->save_file(self::$blogattachmentroot . $directory . '/' . $filename)) {
$f->delete();
if (!$fileid = ArtefactTypeFile::save_file($path, $data)) {
return false;
}
$fileid = $f->get('id');
$data = new StdClass;
$data->blogpost = $this->id;
$data->file = $fileid;
insert_record('artefact_blog_blogpost_file', $data);
return $fileid;
return true;
}
public static function blogfiles_folder_id() {
......
......@@ -110,6 +110,6 @@ if (!empty($uploads)) {
json_reply(false, 'foo');
json_reply(false, get_string('blogpostsaved', 'artefact.blog'));
?>
......@@ -282,56 +282,42 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
}
// public function save_uploaded_file($inputname) {
// require_once('uploadmanager.php');
// $um = new upload_manager($inputname);
// if ($error = $um->preprocess_file()) {
// return $error;
// }
// global $USER;
// $this->owner = $USER->get('id');
// $this->size = $um->file['size'];
// $this->dirty = true;
// $this->commit();
// // Save the file using its id as the filename, and use its id modulo
// // the number of subdirectories as the directory name.
// if ($error = $um->save_file(self::get_file_directory($this->id) , $this->id)) {
// $this->delete();
// }
// return $error;
// }
/**
* Moves a file into the myfiles area.
* Takes the name of a file outside the myfiles area.
* Returns a boolean indicating success or failure.
*/
public function save_file($pathname) {
public static function save_file($pathname, $data) {
$dataroot = get_config('dataroot');
$pathname = $dataroot . $pathname;
if (!$size = filesize($pathname)) {
$this->delete();
return false;
}
if (empty($this->id)) {
$this->commit();
//if (is_image($pathname)) {
if (false) {
$f = new ArtefactTypeImage(0, $data);
}
else {
$f = new ArtefactTypeFile(0, $data);
}
$newdir = $dataroot . self::get_file_directory($this->id);
$f->set('size', $size);
$f->commit();
$id = $f->get('id');
$newdir = $dataroot . self::get_file_directory($id);
check_dir_exists($newdir);
$newname = $newdir . '/' . $this->id;
$newname = $newdir . '/' . $id;
if (!rename($pathname, $newname)) {
$this->delete();
$f->delete();
return false;
}
$this->set('size',$size);
return true;
return $id;
}
/**
* Processes a newly uploaded file, copies it to disk, and associates it with
* the artefact object.
* Processes a newly uploaded file, copies it to disk, and creates
* a new artefact object.
* Takes the name of a file input.
* Returns false for no errors, or a string describing the error.
*/
......@@ -360,33 +346,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
return $error;
}
// /**
// * Processes a newly uploaded file, copies it to disk, creates a new artefact object and
// * associates the newly uploaded file with it.
// * Takes the name of a file input.
// * Returns a boolean indicating success or failure.
// */
// public static function uploaded_file_to_artefact_file($inputname, $data) {
// // Get the new file object first, because its id is used to
// // determine where the file goes on the filesystem
// $f = new ArtefactTypeFile(0, $data);
// require_once('uploadmanager.php');
// $um = new upload_manager($inputname);
// if (!$um->preprocess_file()) {
// return false;
// }
// $this->size = $um->file['size'];
// $this->dirty = true;
// $this->commit();
// // Save the file using its id as the filename, and use its id modulo
// // the number of subdirectories as the directory name.
// if (!$um->save_file(self::get_file_directory($this->id) , $this->id)) {
// $this->delete();
// return false;
// }
// return true;
// }
public function delete() {
if (empty($this->id)) {
return;
......
......@@ -93,6 +93,7 @@ class upload_manager {
function file_is_image() {
// For testing only: trust the browser
return preg_match('/^image\//', $this->file['type']);
// Later: call a function in lib/file.php on the file.
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment