3 * StatusNet, the distributed open-source mMediaFileicroblogging tool
5 * Abstraction for a media files in general
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2008-2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
38 var $fileRecord = null;
41 var $short_fileurl = null;
44 function __construct($user = null, $filename = null, $mimetype = null)
47 $this->user = common_current_user();
50 $this->filename = $filename;
51 $this->mimetype = $mimetype;
52 $this->fileRecord = $this->storeFile();
54 $this->fileurl = common_local_url('attachment',
55 array('attachment' => $this->fileRecord->id));
57 $this->maybeAddRedir($this->fileRecord->id, $this->fileurl);
58 $this->short_fileurl = common_shorten_url($this->fileurl);
59 $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl);
62 function attachToNotice($notice)
64 File_to_post::processNew($this->fileRecord->id, $notice->id);
65 $this->maybeAddRedir($this->fileRecord->id,
66 common_local_url('file', array('notice' => $notice->id)));
71 return $this->short_fileurl;
76 $filepath = File::path($this->filename);
80 function storeFile() {
84 $file->filename = $this->filename;
85 $file->url = File::url($this->filename);
86 $filepath = File::path($this->filename);
87 $file->size = filesize($filepath);
89 $file->mimetype = $this->mimetype;
91 $file_id = $file->insert();
94 common_log_db_error($file, "INSERT", __FILE__);
95 throw new ClientException(_('There was a database error while saving your file. Please try again.'));
101 function rememberFile($file, $short)
103 $this->maybeAddRedir($file->id, $short);
106 function maybeAddRedir($file_id, $url)
108 $file_redir = File_redirection::staticGet('url', $url);
110 if (empty($file_redir)) {
112 $file_redir = new File_redirection;
113 $file_redir->url = $url;
114 $file_redir->file_id = $file_id;
116 $result = $file_redir->insert();
119 common_log_db_error($file_redir, "INSERT", __FILE__);
120 throw new ClientException(_('There was a database error while saving your file. Please try again.'));
125 static function fromUpload($param = 'media', $user = null)
128 $user = common_current_user();
131 if (!isset($_FILES[$param]['error'])){
135 switch ($_FILES[$param]['error']) {
136 case UPLOAD_ERR_OK: // success, jump out
138 case UPLOAD_ERR_INI_SIZE:
139 throw new ClientException(_('The uploaded file exceeds the ' .
140 'upload_max_filesize directive in php.ini.'));
142 case UPLOAD_ERR_FORM_SIZE:
143 throw new ClientException(
144 _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
145 ' that was specified in the HTML form.'));
147 case UPLOAD_ERR_PARTIAL:
148 @unlink($_FILES[$param]['tmp_name']);
149 throw new ClientException(_('The uploaded file was only' .
150 ' partially uploaded.'));
152 case UPLOAD_ERR_NO_TMP_DIR:
153 throw new ClientException(_('Missing a temporary folder.'));
155 case UPLOAD_ERR_CANT_WRITE:
156 throw new ClientException(_('Failed to write file to disk.'));
158 case UPLOAD_ERR_EXTENSION:
159 throw new ClientException(_('File upload stopped by extension.'));
162 throw new ClientException(_('System error uploading file.'));
166 if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) {
168 // Should never actually get here
170 @unlink($_FILES[$param]['tmp_name']);
171 throw new ClientException(_('File exceeds user\'s quota!'));
175 $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']);
179 if (isset($mimetype)) {
181 $basename = basename($_FILES[$param]['name']);
182 $filename = File::filename($user->getProfile(), $basename, $mimetype);
183 $filepath = File::path($filename);
185 $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
188 throw new ClientException(_('File could not be moved to destination directory.'));
193 throw new ClientException(_('Could not determine file\'s mime-type!'));
197 return new MediaFile($user, $filename, $mimetype);
200 static function fromFilehandle($fh, $user) {
202 $stream = stream_get_meta_data($fh);
204 if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) {
206 // Should never actually get here
208 throw new ClientException(_('File exceeds user\'s quota!'));
212 $mimetype = MediaFile::getUploadedFileType($fh);
216 if (isset($mimetype)) {
218 $filename = File::filename($user->getProfile(), "email", $mimetype);
220 $filepath = File::path($filename);
222 $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
225 throw new ClientException(_('File could not be moved to destination directory.' .
226 $stream['uri'] . ' ' . $filepath));
229 throw new ClientException(_('Could not determine file\'s mime-type!'));
233 return new MediaFile($user, $filename, $mimetype);
236 static function getUploadedFileType($f) {
237 require_once 'MIME/Type.php';
239 $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
240 $cmd = common_config('attachments', 'filecommand');
246 // assuming a filename
248 $filetype = MIME_Type::autoDetect($f);
251 // assuming a filehandle
253 $stream = stream_get_meta_data($f);
254 $filetype = MIME_Type::autoDetect($stream['uri']);
257 if (in_array($filetype, common_config('attachments', 'supported'))) {
260 $media = MIME_Type::getMedia($filetype);
261 if ('application' !== $media) {
262 $hint = sprintf(_(' Try using another %s format.'), $media);
266 throw new ClientException(sprintf(
267 _('%s is not a supported filetype on this server.'), $filetype) . $hint);
270 static function respectsQuota($user, $filesize)
273 $result = $file->isRespectsQuota($user, $filesize);
274 if ($result === true) {
277 throw new ClientException($result);