Commit 6bb29902 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add original filename extensions to db on upload

parent 0c6b05fc
...@@ -733,6 +733,7 @@ class ArtefactTypeBlogPost extends ArtefactType { ...@@ -733,6 +733,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
$result = new StdClass; $result = new StdClass;
$tempdir = self::$blogattachmentroot . $dirname; $tempdir = self::$blogattachmentroot . $dirname;
$result->error = $um->process_file_upload($tempdir, $filename); $result->error = $um->process_file_upload($tempdir, $filename);
$result->oldextension = $um->original_filename_extension();
$tempfile = $tempdir . '/' . $filename; $tempfile = $tempdir . '/' . $filename;
safe_require('artefact', 'file'); safe_require('artefact', 'file');
$result->type = ArtefactTypeFile::detect_artefact_type($tempfile); $result->type = ArtefactTypeFile::detect_artefact_type($tempfile);
...@@ -743,7 +744,7 @@ class ArtefactTypeBlogPost extends ArtefactType { ...@@ -743,7 +744,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
/** /**
* Save a temporary uploaded file to the myfiles area. * Save a temporary uploaded file to the myfiles area.
*/ */
public function save_attachment($directory, $filename, $title, $description) { public function save_attachment($directory, $filename, $title, $description, $oldextension) {
// Create the blogfiles folder if it doesn't exist yet. // Create the blogfiles folder if it doesn't exist yet.
$blogfilesid = self::blogfiles_folder_id(); $blogfilesid = self::blogfiles_folder_id();
...@@ -761,6 +762,7 @@ class ArtefactTypeBlogPost extends ArtefactType { ...@@ -761,6 +762,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
$data->owner = $USER->get('id'); $data->owner = $USER->get('id');
$data->adminfiles = 0; // No admin blogs yet... $data->adminfiles = 0; // No admin blogs yet...
$data->parent = $blogfilesid; $data->parent = $blogfilesid;
$data->oldextension = $oldextension;
$path = self::$blogattachmentroot . $directory . '/' . $filename; $path = self::$blogattachmentroot . $directory . '/' . $filename;
......
...@@ -279,6 +279,7 @@ function attachtopost(data) { ...@@ -279,6 +279,7 @@ function attachtopost(data) {
if (fileattached_id(rowid) || data.error) { if (fileattached_id(rowid) || data.error) {
return; return;
} }
var ext = data.oldextension ? data.oldextension : '';
appendChildNodes(attached.tbody, appendChildNodes(attached.tbody,
TR( TR(
{'id':rowid}, {'id':rowid},
...@@ -288,7 +289,8 @@ function attachtopost(data) { ...@@ -288,7 +289,8 @@ function attachtopost(data) {
IMG({'src':get_themeurl('images/'+data.artefacttype+'.gif'), 'alt':data.artefacttype}), IMG({'src':get_themeurl('images/'+data.artefacttype+'.gif'), 'alt':data.artefacttype}),
data.title, data.title,
data.description, data.description,
[INPUT( [SPAN({'style':'display: none;'}, ext),
INPUT(
{'type':'button', 'class':'button', 'value':{$getstring['remove']}, {'type':'button', 'class':'button', 'value':{$getstring['remove']},
'onclick':"removefrompost('"+rowid+"')"}), 'onclick':"removefrompost('"+rowid+"')"}),
removehelp()] removehelp()]
...@@ -338,7 +340,8 @@ function saveblogpost() { ...@@ -338,7 +340,8 @@ function saveblogpost() {
else { // uploaded file else { // uploaded file
var record = {'id':idparts[1], var record = {'id':idparts[1],
'title':scrapeText(attached.tbody.childNodes[i].childNodes[1]), 'title':scrapeText(attached.tbody.childNodes[i].childNodes[1]),
'description':scrapeText(attached.tbody.childNodes[i].childNodes[2])}; 'description':scrapeText(attached.tbody.childNodes[i].childNodes[2]),
'extn':scrapeText(attached.tbody.childNodes[i].childNodes[3].childNodes[0])};
uploads.push(record); uploads.push(record);
} }
} }
......
...@@ -116,7 +116,7 @@ $uploadartefact = array(); ...@@ -116,7 +116,7 @@ $uploadartefact = array();
if (!empty($uploads)) { if (!empty($uploads)) {
foreach ($uploads as $upload) { foreach ($uploads as $upload) {
if (!$fileid = $postobj->save_attachment(session_id() . $createid, $upload->id, if (!$fileid = $postobj->save_attachment(session_id() . $createid, $upload->id,
$upload->title, $upload->description)) { $upload->title, $upload->description, $upload->extn)) {
json_reply('local', get_string('errorsavingattachments', 'artefact.blog')); json_reply('local', get_string('errorsavingattachments', 'artefact.blog'));
// Things could be in a bad state. // Things could be in a bad state.
} }
......
...@@ -46,6 +46,7 @@ $attach = ArtefactTypeBlogPost::save_attachment_temporary('userfile', session_id ...@@ -46,6 +46,7 @@ $attach = ArtefactTypeBlogPost::save_attachment_temporary('userfile', session_id
if (!$attach->error) { if (!$attach->error) {
$result->error = false; $result->error = false;
$result->artefacttype = $attach->type; $result->artefacttype = $attach->type;
$result->oldextension = $attach->oldextension;
$result->message = get_string('uploadoffilecomplete', 'artefact.file', $result->title); $result->message = get_string('uploadoffilecomplete', 'artefact.file', $result->title);
} }
else { else {
......
...@@ -257,6 +257,7 @@ class ArtefactTypeFileBase extends ArtefactType { ...@@ -257,6 +257,7 @@ class ArtefactTypeFileBase extends ArtefactType {
protected $adminfiles = 0; protected $adminfiles = 0;
protected $size; protected $size;
protected $oldextension;
public function __construct($id = 0, $data = null) { public function __construct($id = 0, $data = null) {
parent::__construct($id, $data); parent::__construct($id, $data);
...@@ -300,7 +301,8 @@ class ArtefactTypeFileBase extends ArtefactType { ...@@ -300,7 +301,8 @@ class ArtefactTypeFileBase extends ArtefactType {
$data = (object)array( $data = (object)array(
'artefact' => $this->get('id'), 'artefact' => $this->get('id'),
'size' => $this->get('size'), 'size' => $this->get('size'),
'adminfiles' => $this->get('adminfiles') 'adminfiles' => $this->get('adminfiles'),
'oldextension' => $this->get('oldextension')
); );
if ($new) { if ($new) {
...@@ -519,6 +521,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase { ...@@ -519,6 +521,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$f = self::new_file($um->file['tmp_name'], $data); $f = self::new_file($um->file['tmp_name'], $data);
$f->set('owner', $USER->get('id')); $f->set('owner', $USER->get('id'));
$f->set('size', $size); $f->set('size', $size);
$f->set('oldextension', $um->original_filename_extension());
$f->commit(); $f->commit();
$id = $f->get('id'); $id = $f->get('id');
// Save the file using its id as the filename, and use its id modulo // Save the file using its id as the filename, and use its id modulo
......
...@@ -191,6 +191,16 @@ class upload_manager { ...@@ -191,6 +191,16 @@ class upload_manager {
} }
} }
public function original_filename_extension() {
if (isset($this->file)
&& !empty($this->file['name'])
&& preg_match("/\.([^\.]+)$/", $this->file['name'], $m)) {
return $m[1];
}
return null;
}
} }
/************************************************************************************** /**************************************************************************************
......
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