Refactored some attachment code and fixed upload bug in interface.

This commit is contained in:
Robin Millette 2009-05-31 21:03:55 -04:00
parent abe68f4318
commit f8dae2bbc9
3 changed files with 32 additions and 34 deletions

View File

@ -131,37 +131,10 @@ class NewnoticeAction extends Action
}
function isRespectsQuota($user) {
if ($_FILES['attach']['size'] > common_config('attachments', 'file_quota')) {
$this->clientError(sprintf(_('No file may be larger than %d bytes ' .
'and the file you sent was %d bytes. Try to upload a smaller version.'),
common_config('attachments', 'file_quota'), $_FILES['attach']['size']));
}
$query = "select sum(size) as total from file join file_to_post on file_to_post.file_id = file.id join notice on file_to_post.post_id = notice.id where profile_id = {$user->id} and file.url like '%/notice/%/file'";
$file = new File;
$file->query($query);
$file->fetch();
$total = $file->total + $_FILES['attach']['size'];
if ($total > common_config('attachments', 'user_quota')) {
$this->clientError(sprintf(_('A file this large would exceed your user quota of %d bytes.'), common_config('attachments', 'user_quota')));
}
$query .= ' month(modified) = month(now()) and year(modified) = year(now())';
$file2 = new File;
$file2->query($query);
$file2->fetch();
$total2 = $file2->total + $_FILES['attach']['size'];
if ($total2 > common_config('attachments', 'monthly_quota')) {
$this->clientError(sprintf(_('A file this large would exceed your monthly quota of %d bytes.'), common_config('attachments', 'monthly_quota')));
}
return true;
}
function isValidFileAttached($user) {
return isset($_FILES['attach']['error'])
&& ($_FILES['attach']['error'] === UPLOAD_ERR_OK)
&& $this->isSupportedFileType()
&& $this->isRespectsQuota($user);
$ret = $file->isRespectsQuota($user);
if (true === $ret) return true;
$this->clientError($ret);
}
/**
@ -212,6 +185,7 @@ class NewnoticeAction extends Action
$replyto = 'false';
}
if (isset($_FILES['attach']['error'])) {
switch ($_FILES['attach']['error']) {
case UPLOAD_ERR_NO_FILE:
// no file uploaded
@ -223,8 +197,7 @@ class NewnoticeAction extends Action
// lets check if we really support its format
// and it doesn't go over quotas
if (!$this->isValidFileAttached($user)) {
if (!$this->isSupportedFileType() || !$this->isRespectsQuota($user)) {
die('clientError() should trigger an exception before reaching here.');
}
break;
@ -250,6 +223,7 @@ class NewnoticeAction extends Action
default:
die('Should never reach here.');
}
}
$notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
($replyto == 'false') ? null : $replyto);

View File

@ -120,4 +120,30 @@ class File extends Memcached_DataObject
File_to_post::processNew($file_id, $notice_id);
return $x;
}
function isRespectsQuota($user) {
if ($_FILES['attach']['size'] > common_config('attachments', 'file_quota')) {
return sprintf(_('No file may be larger than %d bytes ' .
'and the file you sent was %d bytes. Try to upload a smaller version.'),
common_config('attachments', 'file_quota'), $_FILES['attach']['size']);
}
$query = "select sum(size) as total from file join file_to_post on file_to_post.file_id = file.id join notice on file_to_post.post_id = notice.id where profile_id = {$user->id} and file.url like '%/notice/%/file'";
$this->query($query);
$this->fetch();
$total = $this->total + $_FILES['attach']['size'];
if ($total > common_config('attachments', 'user_quota')) {
return sprintf(_('A file this large would exceed your user quota of %d bytes.'), common_config('attachments', 'user_quota'));
}
$query .= ' month(modified) = month(now()) and year(modified) = year(now())';
$this->query($query);
$this->fetch();
$total = $this->total + $_FILES['attach']['size'];
if ($total > common_config('attachments', 'monthly_quota')) {
return sprintf(_('A file this large would exceed your monthly quota of %d bytes.'), common_config('attachments', 'monthly_quota'));
}
return true;
}
}

View File

@ -156,9 +156,7 @@ class NoticeForm extends Form
$this->out->hidden('notice_return-to', $this->action, 'returnto');
}
$this->out->hidden('notice_in-reply-to', $this->action, 'inreplyto');
$this->out->hidden('MAX_FILE_SIZE', common_config('attachments', 'file_quota'));
}
/**