public static function get_mediatype($mime, $filename = '') { $mediatype = static::mimetype2mediatype($mime); if ($mediatype === 'other' && !empty($filename)) { $ext = \pdyn\filesystem\FilesystemUtils::get_ext($filename); $mimefromext = \pdyn\filesystem\Mimetype::ext2mime($ext); $mediatype = static::mimetype2mediatype($mimefromext); } return $mediatype; }
/** * Download the file at $url and store locally. * * @param string $url The URL to fetch. * @param string $local_path The path to store the file, should be a full path, and include the desired filename * @param boolean $append_ext_based_on_mime If true, append an appropriate extension to $local_path. * @return array Array of The $local_path (with appended extension, if $append_ext_based_on_mime is true), and the response object */ public function download($url, $local_path, $append_ext_based_on_mime = false) { $fp = fopen($local_path, 'w+'); $additional_opts = [CURLOPT_FILE => &$fp, CURLOPT_TIMEOUT => 1800, CURLOPT_CONNECTTIMEOUT => 15]; $response = $this->request($url, 'get', array(), $additional_opts); fclose($fp); $responseerrors = $response->errors(); if (!empty($responseerrors)) { if (!empty($this->logger)) { $this->logger->debug('httpclient download failed: ' . $response->get_debug_string()); } return [false, $response]; } if ($append_ext_based_on_mime === true) { $mime = $response->mime_type(); $ext = \pdyn\filesystem\Mimetype::mime2ext($mime); if (empty($ext) || $ext === 'unknown') { $ext = \pdyn\filesystem\FilesystemUtils::get_ext($url); $mime = \pdyn\filesystem\Mimetype::ext2mime($ext); } rename($local_path, $local_path . '.' . $ext); $local_path = $local_path . '.' . $ext; } return [$local_path, $response]; }
/** * Test the mimetypes can be imported. */ public function test_import() { $mimemap = \pdyn\filesystem\Mimetype::get_mime_map(); $this->assertNotEmpty($mimemap); $this->assertInternalType('array', $mimemap); }
/** * Get the mime type of a file using only the filename. * * This will return the mime type of a file using the files extension. It will not check if the file exists, open the file, * analyze the file, etc. Therefore, understand that the returned mime type may not reflect what is actually in the file. * * @param string $fn The filename. * @return string The mime type. */ public static function get_mime_from_filename($fn) { return \pdyn\filesystem\Mimetype::ext2mime(static::get_ext($fn)); }
/** * Get the mediatype of the file. * * @return string The mediatype of the file. */ public function get_mediatype() { return \pdyn\filesystem\Mimetype::get_mediatype($this->type, $this->stored_filename); }