MediaFile code improvements, preparing to implement multi-attachments

Maybe in the future we can use this for anonymous file uploads too?
With some kind of anonymous/pseudonymous profile. That'd be neat.
This commit is contained in:
Mikael Nordfeldth 2013-10-05 18:43:41 +02:00
parent b43be41643
commit 48da97f204
7 changed files with 59 additions and 106 deletions

View File

@ -85,7 +85,7 @@ class ApiMediaUploadAction extends ApiAuthAction
$upload = null; $upload = null;
try { try {
$upload = MediaFile::fromUpload('media', $this->auth_user); $upload = MediaFile::fromUpload('media', $this->auth_user->getProfile());
} catch (Exception $e) { } catch (Exception $e) {
$this->clientError($e->getMessage(), $e->getCode()); $this->clientError($e->getMessage(), $e->getCode());
return; return;

View File

@ -269,7 +269,7 @@ class ApiStatusesUpdateAction extends ApiAuthAction
$upload = null; $upload = null;
try { try {
$upload = MediaFile::fromUpload('media', $this->auth_user); $upload = MediaFile::fromUpload('media', $this->auth_user->getProfile());
} catch (Exception $e) { } catch (Exception $e) {
$this->clientError($e->getMessage(), $e->getCode(), $this->format); $this->clientError($e->getMessage(), $e->getCode(), $this->format);
return; return;

View File

@ -114,8 +114,7 @@ class NewnoticeAction extends FormAction
$options['reply_to'] = $replyto; $options['reply_to'] = $replyto;
} }
$upload = null; $upload = MediaFile::fromUpload('attach', $this->scoped);
$upload = MediaFile::fromUpload('attach');
if (isset($upload)) { if (isset($upload)) {

View File

@ -211,8 +211,7 @@ class File extends Managed_DataObject
return $x; return $x;
} }
function isRespectsQuota($user,$fileSize) { public static function respectsQuota(Profile $scoped, $fileSize) {
if ($fileSize > common_config('attachments', 'file_quota')) { if ($fileSize > common_config('attachments', 'file_quota')) {
// TRANS: Message used to be inserted as %2$s in the text "No file may // TRANS: Message used to be inserted as %2$s in the text "No file may
// TRANS: be larger than %1$d byte and the file you sent was %2$s.". // TRANS: be larger than %1$d byte and the file you sent was %2$s.".
@ -224,35 +223,40 @@ class File extends Managed_DataObject
// TRANS: %1$d (used for plural) is the byte limit for uploads, // TRANS: %1$d (used for plural) is the byte limit for uploads,
// TRANS: %2$s is the proper form of "n bytes". This is the only ways to have // TRANS: %2$s is the proper form of "n bytes". This is the only ways to have
// TRANS: gettext support multiple plurals in the same message, unfortunately... // TRANS: gettext support multiple plurals in the same message, unfortunately...
return sprintf(_m('No file may be larger than %1$d byte and the file you sent was %2$s. Try to upload a smaller version.', throw new ClientException(
sprintf(_m('No file may be larger than %1$d byte and the file you sent was %2$s. Try to upload a smaller version.',
'No file may be larger than %1$d bytes and the file you sent was %2$s. Try to upload a smaller version.', 'No file may be larger than %1$d bytes and the file you sent was %2$s. Try to upload a smaller version.',
$fileQuota), $fileQuota),
$fileQuota, $fileSizeText); $fileQuota, $fileSizeText));
} }
$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;
$this->query($query);
$this->fetch(); $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 = {$scoped->id} and file.url like '%/notice/%/file'";
$total = $this->total + $fileSize; $file->query($query);
$file->fetch();
$total = $file->total + $fileSize;
if ($total > common_config('attachments', 'user_quota')) { if ($total > common_config('attachments', 'user_quota')) {
// TRANS: Message given if an upload would exceed user quota. // TRANS: Message given if an upload would exceed user quota.
// TRANS: %d (number) is the user quota in bytes and is used for plural. // TRANS: %d (number) is the user quota in bytes and is used for plural.
return sprintf(_m('A file this large would exceed your user quota of %d byte.', throw new ClientException(
sprintf(_m('A file this large would exceed your user quota of %d byte.',
'A file this large would exceed your user quota of %d bytes.', 'A file this large would exceed your user quota of %d bytes.',
common_config('attachments', 'user_quota')), common_config('attachments', 'user_quota')),
common_config('attachments', 'user_quota')); common_config('attachments', 'user_quota')));
} }
$query .= ' AND EXTRACT(month FROM file.modified) = EXTRACT(month FROM now()) and EXTRACT(year FROM file.modified) = EXTRACT(year FROM now())'; $query .= ' AND EXTRACT(month FROM file.modified) = EXTRACT(month FROM now()) and EXTRACT(year FROM file.modified) = EXTRACT(year FROM now())';
$this->query($query); $file->query($query);
$this->fetch(); $file->fetch();
$total = $this->total + $fileSize; $total = $file->total + $fileSize;
if ($total > common_config('attachments', 'monthly_quota')) { if ($total > common_config('attachments', 'monthly_quota')) {
// TRANS: Message given id an upload would exceed a user's monthly quota. // TRANS: Message given id an upload would exceed a user's monthly quota.
// TRANS: $d (number) is the monthly user quota in bytes and is used for plural. // TRANS: $d (number) is the monthly user quota in bytes and is used for plural.
return sprintf(_m('A file this large would exceed your monthly quota of %d byte.', throw new ClientException(
sprintf(_m('A file this large would exceed your monthly quota of %d byte.',
'A file this large would exceed your monthly quota of %d bytes.', 'A file this large would exceed your monthly quota of %d bytes.',
common_config('attachments', 'monthly_quota')), common_config('attachments', 'monthly_quota')),
common_config('attachments', 'monthly_quota')); common_config('attachments', 'monthly_quota')));
} }
return true; return true;
} }

View File

@ -73,7 +73,7 @@ class MailHandler
$mf = null; $mf = null;
try { try {
$mf = MediaFile::fromFileHandle($attachment, $user); $mf = MediaFile::fromFilehandle($attachment, $user->getProfile());
} catch(ClientException $ce) { } catch(ClientException $ce) {
$this->error($from, $ce->getMessage()); $this->error($from, $ce->getMessage());
} }

View File

@ -36,21 +36,17 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
class MediaFile class MediaFile
{ {
protected $scoped = null;
var $filename = null; var $filename = null;
var $fileRecord = null; var $fileRecord = null;
var $user = null;
var $fileurl = null; var $fileurl = null;
var $short_fileurl = null; var $short_fileurl = null;
var $mimetype = null; var $mimetype = null;
function __construct($user = null, $filename = null, $mimetype = null) function __construct(Profile $scoped, $filename = null, $mimetype = null)
{ {
if ($user == null) { $this->scoped = $scoped;
$this->user = common_current_user();
} else {
$this->user = $user;
}
$this->filename = $filename; $this->filename = $filename;
$this->mimetype = $mimetype; $this->mimetype = $mimetype;
@ -124,7 +120,7 @@ class MediaFile
return null; return null;
} }
$outname = File::filename($this->user->getProfile(), 'thumb-' . $this->filename, $this->mimetype); $outname = File::filename($this->scoped, 'thumb-' . $this->filename, $this->mimetype);
$outpath = File::path($outname); $outpath = File::path($outname);
$maxWidth = common_config('attachments', 'thumb_width'); $maxWidth = common_config('attachments', 'thumb_width');
@ -176,10 +172,10 @@ class MediaFile
} }
} }
static function fromUpload($param = 'media', $user = null) static function fromUpload($param = 'media', Profile $scoped)
{ {
if (empty($user)) { if (is_null($scoped)) {
$user = common_current_user(); $scoped = Profile::current();
} }
if (!isset($_FILES[$param]['error'])){ if (!isset($_FILES[$param]['error'])){
@ -229,85 +225,49 @@ class MediaFile
return; return;
} }
if (!MediaFile::respectsQuota($user, $_FILES[$param]['size'])) { // Throws exception if additional size does not respect quota
File::respectsQuota($scoped, $_FILES[$param]['size']);
// Should never actually get here
@unlink($_FILES[$param]['tmp_name']);
// TRANS: Client exception thrown when a file upload operation would cause a user to exceed a set quota.
throw new ClientException(_('File exceeds user\'s quota.'));
return;
}
$mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name'], $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name'],
$_FILES[$param]['name']); $_FILES[$param]['name']);
$filename = null; $basename = basename($_FILES[$param]['name']);
$filename = File::filename($scoped, $basename, $mimetype);
$filepath = File::path($filename);
if (isset($mimetype)) { $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
$basename = basename($_FILES[$param]['name']); if (!$result) {
$filename = File::filename($user->getProfile(), $basename, $mimetype); // TRANS: Client exception thrown when a file upload operation fails because the file could
$filepath = File::path($filename); // TRANS: not be moved from the temporary folder to the permanent file location.
throw new ClientException(_('File could not be moved to destination directory.'));
$result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
if (!$result) {
// TRANS: Client exception thrown when a file upload operation fails because the file could
// TRANS: not be moved from the temporary folder to the permanent file location.
throw new ClientException(_('File could not be moved to destination directory.'));
return;
}
} else {
// TRANS: Client exception thrown when a file upload operation has been stopped because the MIME
// TRANS: type of the uploaded file could not be determined.
throw new ClientException(_('Could not determine file\'s MIME type.'));
return;
} }
return new MediaFile($user, $filename, $mimetype); return new MediaFile($scoped, $filename, $mimetype);
} }
static function fromFilehandle($fh, $user) { static function fromFilehandle($fh, Profile $scoped) {
$stream = stream_get_meta_data($fh); $stream = stream_get_meta_data($fh);
if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) { File::respectsQuota($scoped, filesize($stream['uri']));
// Should never actually get here
// TRANS: Client exception thrown when a file upload operation would cause a user to exceed a set quota.
throw new ClientException(_('File exceeds user\'s quota.'));
return;
}
$mimetype = MediaFile::getUploadedFileType($fh); $mimetype = MediaFile::getUploadedFileType($fh);
$filename = null; $filename = File::filename($scoped, "email", $mimetype);
if (isset($mimetype)) { $filepath = File::path($filename);
$filename = File::filename($user->getProfile(), "email", $mimetype); $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
$filepath = File::path($filename); if (!$result) {
// TRANS: Client exception thrown when a file upload operation fails because the file could
$result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); // TRANS: not be moved from the temporary folder to the permanent file location.
throw new ClientException(_('File could not be moved to destination directory.' .
if (!$result) { $stream['uri'] . ' ' . $filepath));
// TRANS: Client exception thrown when a file upload operation fails because the file could
// TRANS: not be moved from the temporary folder to the permanent file location.
throw new ClientException(_('File could not be moved to destination directory.' .
$stream['uri'] . ' ' . $filepath));
}
} else {
// TRANS: Client exception thrown when a file upload operation has been stopped because the MIME
// TRANS: type of the uploaded file could not be determined.
throw new ClientException(_('Could not determine file\'s MIME type.'));
return;
} }
return new MediaFile($user, $filename, $mimetype); return new MediaFile($scoped, $filename, $mimetype);
} }
/** /**
@ -325,14 +285,16 @@ class MediaFile
* @throws ClientException if type is known, but not supported for local uploads * @throws ClientException if type is known, but not supported for local uploads
*/ */
static function getUploadedFileType($f, $originalFilename=false) { static function getUploadedFileType($f, $originalFilename=false) {
global $_PEAR;
require_once 'MIME/Type.php'; require_once 'MIME/Type.php';
require_once 'MIME/Type/Extension.php'; require_once 'MIME/Type/Extension.php';
// We have to disable auto handling of PEAR errors // We have to disable auto handling of PEAR errors
PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); $_PEAR->staticPushErrorHandling(PEAR_ERROR_RETURN);
$mte = new MIME_Type_Extension(); $mte = new MIME_Type_Extension();
$cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); $cmd = &$_PEAR->getStaticProperty('MIME_Type', 'fileCmd');
$cmd = common_config('attachments', 'filecommand'); $cmd = common_config('attachments', 'filecommand');
$filetype = null; $filetype = null;
@ -385,7 +347,7 @@ class MediaFile
} }
if ($supported === true || in_array($filetype, $supported)) { if ($supported === true || in_array($filetype, $supported)) {
// Restore PEAR error handlers for our DB code... // Restore PEAR error handlers for our DB code...
PEAR::staticPopErrorHandling(); $_PEAR->staticPopErrorHandling();
return $filetype; return $filetype;
} }
$media = MIME_Type::getMedia($filetype); $media = MIME_Type::getMedia($filetype);
@ -401,19 +363,7 @@ class MediaFile
$hint = sprintf(_('"%s" is not a supported file type on this server.'), $filetype); $hint = sprintf(_('"%s" is not a supported file type on this server.'), $filetype);
} }
// Restore PEAR error handlers for our DB code... // Restore PEAR error handlers for our DB code...
PEAR::staticPopErrorHandling(); $_PEAR->staticPopErrorHandling();
throw new ClientException($hint); throw new ClientException($hint);
} }
static function respectsQuota($user, $filesize)
{
$file = new File;
$result = $file->isRespectsQuota($user, $filesize);
if ($result === true) {
return true;
} else {
throw new ClientException($result);
}
}
} }

View File

@ -492,7 +492,7 @@ class YammerImporter
$temp = tmpfile(); $temp = tmpfile();
fwrite($temp, $body); fwrite($temp, $body);
try { try {
$upload = MediaFile::fromFileHandle($temp, $user); $upload = MediaFile::fromFilehandle($temp, $user->getProfile());
fclose($temp); fclose($temp);
return $upload; return $upload;
} catch (Exception $e) { } catch (Exception $e) {