4 * GuessContentType addon
6 * A lot of the built-in File objects just return application/octet-stream
7 * as a content-type by default. This is a problem for some clients, because
8 * they expect a correct contenttype.
10 * There's really no accurate, fast and portable way to determine the contenttype
11 * so this extension does what the rest of the world does, and guesses it based
12 * on the file extension.
16 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
17 * @author Evert Pot (http://www.rooftopsolutions.nl/)
18 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
20 class Sabre_DAV_Browser_GuessContentType extends Sabre_DAV_ServerPlugin {
23 * List of recognized file extensions
25 * Feel free to add more
29 public $extensionMap = array(
32 'jpg' => 'image/jpeg',
37 'ics' => 'text/calendar',
38 'vcf' => 'text/x-vcard',
41 'txt' => 'text/plain',
46 * Initializes the addon
48 * @param Sabre_DAV_Server $server
51 public function initialize(Sabre_DAV_Server $server) {
53 // Using a relatively low priority (200) to allow other extensions
54 // to set the content-type first.
55 $server->subscribeEvent('afterGetProperties',array($this,'afterGetProperties'),200);
60 * Handler for teh afterGetProperties event
63 * @param array $properties
66 public function afterGetProperties($path, &$properties) {
68 if (array_key_exists('{DAV:}getcontenttype', $properties[404])) {
70 list(, $fileName) = Sabre_DAV_URLUtil::splitPath($path);
71 $contentType = $this->getContentType($fileName);
74 $properties[200]['{DAV:}getcontenttype'] = $contentType;
75 unset($properties[404]['{DAV:}getcontenttype']);
83 * Simple method to return the contenttype
85 * @param string $fileName
88 protected function getContentType($fileName) {
90 // Just grabbing the extension
91 $extension = strtolower(substr($fileName,strrpos($fileName,'.')+1));
92 if (isset($this->extensionMap[$extension]))
93 return $this->extensionMap[$extension];