/** * @dataProvider fileTypeCases * */ public function testUploadedFileType($filename, $expectedType) { if (!file_exists($filename)) { throw new Exception("WTF? {$filename} test file missing"); } $tmp = tmpfile(); fwrite($tmp, file_get_contents($filename)); $type = MediaFile::getUploadedFileType($tmp, basename($filename)); $this->assertEquals($expectedType, $type); }
static function fromFilehandle($fh, $user) { $stream = stream_get_meta_data($fh); if (!MediaFile::respectsQuota($user, 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); $filename = null; if (isset($mimetype)) { $filename = File::filename($user->getProfile(), "email", $mimetype); $filepath = File::path($filename); $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); 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.' . $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); }
static function fromFilehandle($fh, $user) { $stream = stream_get_meta_data($fh); if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) { // Should never actually get here throw new ClientException(_('File exceeds user\'s quota.')); return; } $mimetype = MediaFile::getUploadedFileType($fh); $filename = null; if (isset($mimetype)) { $filename = File::filename($user->getProfile(), "email", $mimetype); $filepath = File::path($filename); $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); if (!$result) { throw new ClientException(_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath)); } } else { throw new ClientException(_('Could not determine file\'s MIME type.')); return; } return new MediaFile($user, $filename, $mimetype); }