~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.0.13/Nette/Utils/MimeTypeDetector.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/**
4
 
 * This file is part of the Nette Framework (http://nette.org)
5
 
 *
6
 
 * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7
 
 *
8
 
 * For the full copyright and license information, please view
9
 
 * the file license.txt that was distributed with this source code.
10
 
 */
11
 
 
12
 
namespace Nette\Utils;
13
 
 
14
 
use Nette;
15
 
 
16
 
 
17
 
/**
18
 
 * Mime type detector.
19
 
 *
20
 
 * @author     David Grudl
21
 
 */
22
 
final class MimeTypeDetector
23
 
{
24
 
 
25
 
        /**
26
 
         * Static class - cannot be instantiated.
27
 
         */
28
 
        final public function __construct()
29
 
        {
30
 
                throw new Nette\StaticClassException;
31
 
        }
32
 
 
33
 
 
34
 
        /**
35
 
         * Returns the MIME content type of file.
36
 
         * @param  string
37
 
         * @return string
38
 
         */
39
 
        public static function fromFile($file)
40
 
        {
41
 
                if (!is_file($file)) {
42
 
                        throw new Nette\FileNotFoundException("File '$file' not found.");
43
 
                }
44
 
 
45
 
                $info = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
46
 
                if (isset($info['mime'])) {
47
 
                        return $info['mime'];
48
 
 
49
 
                } elseif (extension_loaded('fileinfo')) {
50
 
                        $type = preg_replace('#[\s;].*\z#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
51
 
 
52
 
                } elseif (function_exists('mime_content_type')) {
53
 
                        $type = mime_content_type($file);
54
 
                }
55
 
 
56
 
                return isset($type) && preg_match('#^\S+/\S+\z#', $type) ? $type : 'application/octet-stream';
57
 
        }
58
 
 
59
 
 
60
 
        /**
61
 
         * Returns the MIME content type of file.
62
 
         * @param  string
63
 
         * @return string
64
 
         */
65
 
        public static function fromString($data)
66
 
        {
67
 
                if (extension_loaded('fileinfo') && preg_match('#^(\S+/[^\s;]+)#', finfo_buffer(finfo_open(FILEINFO_MIME), $data), $m)) {
68
 
                        return $m[1];
69
 
 
70
 
                } elseif (strncmp($data, "\xff\xd8", 2) === 0) {
71
 
                        return 'image/jpeg';
72
 
 
73
 
                } elseif (strncmp($data, "\x89PNG", 4) === 0) {
74
 
                        return 'image/png';
75
 
 
76
 
                } elseif (strncmp($data, "GIF", 3) === 0) {
77
 
                        return 'image/gif';
78
 
 
79
 
                } else {
80
 
                        return 'application/octet-stream';
81
 
                }
82
 
        }
83
 
 
84
 
}