Fixed issue 10, add mime type when downloading a file.

This commit is contained in:
Loic d'Anterroches 2008-08-05 12:44:56 +02:00
parent b4cac893a5
commit 366b73d27e
2 changed files with 84 additions and 2 deletions

View File

@ -0,0 +1,47 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Test the source class.
*/
class IDF_Tests_TestSource extends UnitTestCase
{
public function __construct()
{
parent::__construct('Test the source class.');
}
public function testGetMimeType()
{
$files = array(
'whatever.php' => 'application/x-httpd-php',
'whatever.pht' => 'application/x-httpd-php',
'README' => 'text/plain',
);
foreach ($files as $file => $mime) {
$m = IDF_Views_Source::getMimeType($file);
$this->assertEqual($mime, $m[0]);
}
}
}

View File

@ -101,8 +101,11 @@ class IDF_Views_Source
return new Pluf_HTTP_Response_Redirect($url);
}
if ($request_file_info->type != 'tree') {
return new Pluf_HTTP_Response($git->getBlob($request_file_info->hash),
'application/octet-stream');
$info = self::getMimeType($request_file_info->file);
$rep = new Pluf_HTTP_Response($git->getBlob($request_file_info->hash),
$info[0]);
$rep->headers['Content-Disposition'] = 'attachment; filename="'.$info[1].'"';
return $rep;
}
$bc = self::makeBreadCrumb($request->project, $commit, $request_file_info->file);
$page_title = $bc.' - '.$title;
@ -198,6 +201,38 @@ class IDF_Views_Source
$rep->headers['Content-Disposition'] = 'attachment; filename="'.$base.'.zip"';
return $rep;
}
/**
* Find the mime type of a file.
*
* Use /etc/mime.types to find the type.
*
* @param string Filename/Filepath
* @param string Path to the mime types database ('/etc/mime.types')
* @param array Mime type found or 'application/octet-stream' and basename
*/
public static function getMimeType($file, $src='/etc/mime.types')
{
$mimes = preg_split("/\015\012|\015|\012/", file_get_contents($src));
$info = pathinfo($file);
if (isset($info['extension'])) {
foreach ($mimes as $mime) {
if ('#' != substr($mime, 0, 1)) {
$elts = preg_split('/ |\t/', $mime, -1, PREG_SPLIT_NO_EMPTY);
if (in_array($info['extension'], $elts)) {
return array($elts[0], $info['basename']);
}
}
}
} else {
// we consider that if no extension and base name is all
// uppercase, then we have a text file.
if ($info['basename'] == strtoupper($info['basename'])) {
return array('text/plain', $info['basename']);
}
}
return array('application/octet-stream', $info['basename']);
}
}
function IDF_Views_Source_PrettySize($size)