3 * StatusNet, the distributed open-source microblogging tool
5 * Abstraction for media files in general
7 * TODO: combine with ImageFile?
11 * LICENCE: This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * @author Robin Millette <robin@millette.info>
27 * @author Zach Copley <zach@status.net>
28 * @copyright 2008-2009 StatusNet, Inc.
29 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30 * @link http://status.net/
33 if (!defined('STATUSNET') && !defined('LACONICA')) {
41 var $fileRecord = null;
44 var $short_fileurl = null;
47 function __construct($user = null, $filename = null, $mimetype = null)
50 $this->user = common_current_user();
53 $this->filename = $filename;
54 $this->mimetype = $mimetype;
55 $this->fileRecord = $this->storeFile();
57 $this->fileurl = common_local_url('attachment',
58 array('attachment' => $this->fileRecord->id));
60 $this->maybeAddRedir($this->fileRecord->id, $this->fileurl);
61 $this->short_fileurl = common_shorten_url($this->fileurl);
62 $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl);
65 function attachToNotice($notice)
67 File_to_post::processNew($this->fileRecord->id, $notice->id);
68 $this->maybeAddRedir($this->fileRecord->id,
69 common_local_url('file', array('notice' => $notice->id)));
74 return $this->short_fileurl;
79 $filepath = File::path($this->filename);
83 function storeFile() {
87 $file->filename = $this->filename;
88 $file->url = File::url($this->filename);
89 $filepath = File::path($this->filename);
90 $file->size = filesize($filepath);
92 $file->mimetype = $this->mimetype;
94 $file_id = $file->insert();
97 common_log_db_error($file, "INSERT", __FILE__);
98 throw new ClientException(_('There was a database error while saving your file. Please try again.'));
104 function rememberFile($file, $short)
106 $this->maybeAddRedir($file->id, $short);
109 function maybeAddRedir($file_id, $url)
111 $file_redir = File_redirection::staticGet('url', $url);
113 if (empty($file_redir)) {
115 $file_redir = new File_redirection;
116 $file_redir->url = $url;
117 $file_redir->file_id = $file_id;
119 $result = $file_redir->insert();
122 common_log_db_error($file_redir, "INSERT", __FILE__);
123 throw new ClientException(_('There was a database error while saving your file. Please try again.'));
128 static function fromUpload($param = 'media', $user = null)
131 $user = common_current_user();
134 if (!isset($_FILES[$param]['error'])){
138 switch ($_FILES[$param]['error']) {
139 case UPLOAD_ERR_OK: // success, jump out
141 case UPLOAD_ERR_INI_SIZE:
142 throw new ClientException(_('The uploaded file exceeds the ' .
143 'upload_max_filesize directive in php.ini.'));
145 case UPLOAD_ERR_FORM_SIZE:
146 throw new ClientException(
147 _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
148 ' that was specified in the HTML form.'));
150 case UPLOAD_ERR_PARTIAL:
151 @unlink($_FILES[$param]['tmp_name']);
152 throw new ClientException(_('The uploaded file was only' .
153 ' partially uploaded.'));
155 case UPLOAD_ERR_NO_TMP_DIR:
156 throw new ClientException(_('Missing a temporary folder.'));
158 case UPLOAD_ERR_CANT_WRITE:
159 throw new ClientException(_('Failed to write file to disk.'));
161 case UPLOAD_ERR_EXTENSION:
162 throw new ClientException(_('File upload stopped by extension.'));
165 throw new ClientException(_('System error uploading file.'));
169 if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) {
171 // Should never actually get here
173 @unlink($_FILES[$param]['tmp_name']);
174 throw new ClientException(_('File exceeds user\'s quota!'));
178 $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']);
182 if (isset($mimetype)) {
184 $basename = basename($_FILES[$param]['name']);
185 $filename = File::filename($user->getProfile(), $basename, $mimetype);
186 $filepath = File::path($filename);
188 $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
191 throw new ClientException(_('File could not be moved to destination directory.'));
196 throw new ClientException(_('Could not determine file\'s mime-type!'));
200 return new MediaFile($user, $filename, $mimetype);
203 static function fromFilehandle($fh, $user) {
205 $stream = stream_get_meta_data($fh);
207 if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) {
209 // Should never actually get here
211 throw new ClientException(_('File exceeds user\'s quota!'));
215 $mimetype = MediaFile::getUploadedFileType($fh);
219 if (isset($mimetype)) {
221 $filename = File::filename($user->getProfile(), "email", $mimetype);
223 $filepath = File::path($filename);
225 $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
228 throw new ClientException(_('File could not be moved to destination directory.' .
229 $stream['uri'] . ' ' . $filepath));
232 throw new ClientException(_('Could not determine file\'s mime-type!'));
236 return new MediaFile($user, $filename, $mimetype);
239 static function getUploadedFileType($f) {
240 require_once 'MIME/Type.php';
242 $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
243 $cmd = common_config('attachments', 'filecommand');
249 // assuming a filename
251 $filetype = MIME_Type::autoDetect($f);
254 // assuming a filehandle
256 $stream = stream_get_meta_data($f);
257 $filetype = MIME_Type::autoDetect($stream['uri']);
260 if (in_array($filetype, common_config('attachments', 'supported'))) {
263 $media = MIME_Type::getMedia($filetype);
264 if ('application' !== $media) {
265 $hint = sprintf(_(' Try using another %s format.'), $media);
269 throw new ClientException(sprintf(
270 _('%s is not a supported filetype on this server.'), $filetype) . $hint);
273 static function respectsQuota($user, $filesize)
276 $result = $file->isRespectsQuota($user, $filesize);
277 if ($result === true) {
280 throw new ClientException($result);