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 common_debug('in MediaFile constructor');
52 $this->filename = $filename;
53 $this->mimetype = $mimetype;
55 common_debug('storing file');
56 $this->fileRecord = $this->storeFile();
57 common_debug('finished storing file');
59 $this->fileurl = common_local_url('attachment',
60 array('attachment' => $this->fileRecord->id));
62 common_debug('$this->fileurl() = ' . $this->fileurl);
64 // not sure this is necessary -- Zach
65 $this->maybeAddRedir($this->fileRecord->id, $this->fileurl);
67 common_debug('shortening file url');
68 $this->short_fileurl = common_shorten_url($this->fileurl);
69 common_debug('shortened file url = ' . $short_fileurl);
71 // Also, not sure this is necessary -- Zach
72 $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl);
74 common_debug("MediaFile: end of constructor");
77 function attachToNotice($notice)
79 common_debug('MediaFile::attachToNotice() -- doing File_to_post');
80 File_to_post::processNew($this->fileRecord->id, $notice->id);
81 common_debug('MediaFile done doing File_to_post');
83 $this->maybeAddRedir($this->fileRecord->id,
84 common_local_url('file', array('notice' => $notice->id)));
89 return $this->short_fileurl;
94 $filepath = File::path($this->filename);
98 function storeFile() {
101 $file->filename = $this->filename;
103 common_debug('storing ' . $this->filename);
105 $file->url = File::url($this->filename);
106 common_debug('file->url = ' . $file->url);
108 $filepath = File::path($this->filename);
109 common_debug('filepath = ' . $filepath);
111 $file->size = filesize($filepath);
112 $file->date = time();
113 $file->mimetype = $this->mimetype;
115 $file_id = $file->insert();
119 common_debug("storeFile: problem inserting new file");
120 common_log_db_error($file, "INSERT", __FILE__);
121 throw new ClientException(_('There was a database error while saving your file. Please try again.'));
124 common_debug('finished storing file');
129 function rememberFile($file, $short)
131 $this->maybeAddRedir($file->id, $short);
134 function maybeAddRedir($file_id, $url)
137 common_debug("maybeAddRedir: looking up url: $url for file id $file_id");
139 $file_redir = File_redirection::staticGet('url', $url);
141 if (empty($file_redir)) {
143 common_debug("maybeAddRedir: $url is not in the db");
145 $file_redir = new File_redirection;
146 $file_redir->url = $url;
147 $file_redir->file_id = $file_id;
149 $result = $file_redir->insert();
152 common_log_db_error($file_redir, "INSERT", __FILE__);
153 throw new ClientException(_('There was a database error while saving your file. Please try again.'));
157 common_debug("maybeAddRedir: no need to add $url, it's already in the db");
161 static function fromUpload($param = 'media')
163 common_debug("fromUpload: param = $param");
165 if (!isset($_FILES[$param]['error'])){
166 common_debug('no file found');
170 switch ($_FILES[$param]['error']) {
171 case UPLOAD_ERR_OK: // success, jump out
173 case UPLOAD_ERR_INI_SIZE:
174 throw new ClientException(_('The uploaded file exceeds the ' .
175 'upload_max_filesize directive in php.ini.'));
177 case UPLOAD_ERR_FORM_SIZE:
178 throw new ClientException(
179 _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
180 ' that was specified in the HTML form.'));
182 case UPLOAD_ERR_PARTIAL:
183 @unlink($_FILES[$param]['tmp_name']);
184 throw new ClientException(_('The uploaded file was only' .
185 ' partially uploaded.'));
187 case UPLOAD_ERR_NO_TMP_DIR:
188 throw new ClientException(_('Missing a temporary folder.'));
190 case UPLOAD_ERR_CANT_WRITE:
191 throw new ClientException(_('Failed to write file to disk.'));
193 case UPLOAD_ERR_EXTENSION:
194 throw new ClientException(_('File upload stopped by extension.'));
197 throw new ClientException(_('System error uploading file.'));
201 $user = common_current_user();
203 if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) {
205 // Should never actually get here
207 @unlink($_FILES[$param]['tmp_name']);
208 throw new ClientException(_('File exceeds user\'s quota!'));
212 $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']);
216 if (isset($mimetype)) {
218 $basename = basename($_FILES[$param]['name']);
219 $filename = File::filename($user->getProfile(), $basename, $mimetype);
220 $filepath = File::path($filename);
222 common_debug("filepath = " . $filepath);
224 $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
227 throw new ClientException(_('File could not be moved to destination directory.'));
232 throw new ClientException(_('Could not determine file\'s mime-type!'));
236 return new MediaFile($user, $filename, $mimetype);
239 static function fromFilehandle($user, $fh) {
241 $stream = stream_get_meta_data($fh);
243 if (MediaFile::respectsQuota($user, filesize($stream['uri']))) {
245 // Should never actually get here
247 throw new ClientException(_('File exceeds user\'s quota!'));
251 $mimetype = MediaFile::getUploadedFileType($fh);
255 if (isset($mimetype)) {
257 $filename = File::filename($user->getProfile(), "email", $mimetype);
259 $filepath = File::path($filename);
261 $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
264 throw new ClientException(_('File could not be moved to destination directory.' .
265 $stream['uri'] . ' ' . $filepath));
268 throw new ClientException(_('Could not determine file\'s mime-type!'));
272 return new MediaFile($user, $filename, $mimetype);
275 static function getUploadedFileType($f) {
276 require_once 'MIME/Type.php';
278 common_debug("in getUploadedFileType");
280 $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
281 $cmd = common_config('attachments', 'filecommand');
287 // assuming a filename
289 $filetype = MIME_Type::autoDetect($f);
292 // assuming a filehandle
294 $stream = stream_get_meta_data($f);
295 $filetype = MIME_Type::autoDetect($stream['uri']);
298 if (in_array($filetype, common_config('attachments', 'supported'))) {
301 $media = MIME_Type::getMedia($filetype);
302 if ('application' !== $media) {
303 $hint = sprintf(_(' Try using another %s format.'), $media);
307 throw new ClientException(sprintf(
308 _('%s is not a supported filetype on this server.'), $filetype) . $hint);
311 static function respectsQuota($user, $filesize)
314 $result = $file->isRespectsQuota($user, $filesize);
315 if ($result === true) {
318 throw new ClientException($result);