Commit 454de594 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Set mime type in db when uploading files

parent 7554bafc
......@@ -625,6 +625,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
$tempdir = self::$blogattachmentroot . $dirname;
$result->error = $um->process_file_upload($tempdir, $filename);
$result->oldextension = $um->original_filename_extension();
$result->filetype = $um->file['type'];
$tempfile = $tempdir . '/' . $filename;
safe_require('artefact', 'file');
$result->type = ArtefactTypeFile::detect_artefact_type($tempfile);
......@@ -635,7 +636,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
/**
* Save a temporary uploaded file to the myfiles area.
*/
public function save_attachment($directory, $filename, $title, $description, $oldextension, $tags) {
public function save_attachment($directory, $filename, $uploaddata) {
// Create the blogfiles folder if it doesn't exist yet.
$blogfilesid = self::blogfiles_folder_id();
......@@ -648,12 +649,13 @@ class ArtefactTypeBlogPost extends ArtefactType {
safe_require('artefact', 'file');
$data = new StdClass;
$data->title = $title;
$data->description = $description;
$data->tags = $tags;
$data->title = $uploaddata->title;
$data->description = $uploaddata->description;
$data->tags = $uploaddata->tags;
$data->owner = $USER->get('id');
$data->parent = $blogfilesid;
$data->oldextension = $oldextension;
$data->oldextension = $uploaddata->oldextension;
$data->filetype = $uploaddata->filetype;
$path = self::$blogattachmentroot . $directory . '/' . $filename;
......
......@@ -250,6 +250,8 @@ function redrawAttachList() {
// Add a newly uploaded file to the attached files list.
var uploaddata = {};
// Currently this function does not check whether names of files
// attached from my files clash with files already in the attached
// files list. This should be done here if names of attached files
......@@ -279,14 +281,12 @@ function attachtopost(data) {
data.title,
data.description,
tags,
[SPAN({'style':'display: none;'}, ext),
INPUT(
{'type':'button', 'class':'button', 'value':{$getstring['remove']},
'onclick':"removefrompost('"+rowid+"')"})]
INPUT({'type':'button', 'class':'button', 'value':{$getstring['remove']}, 'onclick':"removefrompost('"+rowid+"')"})
]
)
)
);
uploaddata[rowid] = data;
redrawAttachList();
}
......@@ -334,10 +334,7 @@ function saveblogpost() {
else { // uploaded file
var record = {
'id':idparts[1],
'title':scrapeText(attached.tbody.childNodes[i].childNodes[1]),
'description':scrapeText(attached.tbody.childNodes[i].childNodes[2]),
'extn':scrapeText(attached.tbody.childNodes[i].childNodes[4].childNodes[0]),
'tags':scrapeText(attached.tbody.childNodes[i].childNodes[3])
'data':uploaddata[attached.tbody.childNodes[i].id],
};
uploads.push(record);
}
......
......@@ -115,9 +115,7 @@ $uploadartefact = array();
if (!empty($uploads)) {
foreach ($uploads as $upload) {
if (!$fileid = $postobj->save_attachment(session_id() . $createid, $upload->id,
$upload->title, $upload->description, $upload->extn,
$upload->tags)) {
if (!$fileid = $postobj->save_attachment(session_id() . $createid, $upload->id, $upload->data)) {
json_reply('local', get_string('errorsavingattachments', 'artefact.blog'));
// Things could be in a bad state.
}
......
......@@ -48,6 +48,7 @@ if (!$attach->error) {
$result->error = false;
$result->artefacttype = $attach->type;
$result->oldextension = $attach->oldextension;
$result->filetype = $attach->filetype;
$result->message = get_string('uploadoffilecomplete', 'artefact.file', $result->title);
}
else {
......
......@@ -293,6 +293,8 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
// file is a copy of another file artefact.
protected $fileid;
protected $filetype; // Mime type
public function __construct($id = 0, $data = null) {
parent::__construct($id, $data);
......@@ -365,6 +367,7 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
'size' => $this->get('size'),
'oldextension' => $this->get('oldextension'),
'fileid' => $this->get('fileid'),
'filetype' => $this->get('filetype'),
);
if ($new) {
......@@ -728,6 +731,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
}
$f = self::new_file($pathname, $data);
$f->set('size', $size);
// @todo: Set mime type! (and old extension)
$f->commit();
$id = $f->get('id');
......@@ -774,6 +778,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$f = self::new_file($um->file['tmp_name'], $data);
$f->set('size', $size);
$f->set('oldextension', $um->original_filename_extension());
$f->set('filetype', $um->file['type']);
$f->commit();
$id = $f->get('id');
// Save the file using its id as the filename, and use its id modulo
......@@ -1003,6 +1008,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$new->set('fileid', $this->get('fileid'));
$new->set('size', $this->get('size'));
$new->set('oldextension', $this->get('oldextension'));
$new->set('filetype', $this->get('filetype'));
global $USER;
if ($new->get('owner') && $new->get('owner') == $USER->get('id')) {
$USER->quota_add($new->get('size'));
......
......@@ -165,7 +165,7 @@ $filesize = 0;
function upload_validate(Pieform $form, $values) {
global $USER, $filesize;
require_once('file.php');
if (!is_image_mime_type(get_mime_type($values['file']['tmp_name']))) {
if (!is_image_mime_type($values['file']['type'])) {
$form->set_error('file', get_string('filenotimage'));
}
......@@ -211,6 +211,11 @@ function upload_submit(Pieform $form, $values) {
$artefact->set('owner', $USER->id);
$artefact->set('title', ($values['title']) ? $values['title'] : $values['file']['name']);
$artefact->set('note', $values['file']['name']);
$artefact->set('filetype', $values['file']['type']);
if (preg_match("/\.([^\.]+)$/", $values['file']['name'], $saved)) {
$artefact->set('oldextension', $saved[1]);
}
$artefact->set('size', $filesize);
$artefact->commit();
$id = $artefact->get('id');
......
Markdown is supported
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