Skip to content

Commit

Permalink
Merge pull request #80 from shadlaws/master
Browse files Browse the repository at this point in the history
[#1924, #1925, #1927 - enhance compatibility with movies (movie helper) and file types (legal_file helper)]
  • Loading branch information
bharat committed Dec 17, 2012
2 parents 989c24e + 768e6fa commit 98753bc
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 32 deletions.
6 changes: 3 additions & 3 deletions installer/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ CREATE TABLE {modules} (
KEY `weight` (`weight`)
) AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO {modules} VALUES (1,1,'gallery',50,1);
INSERT INTO {modules} VALUES (1,1,'gallery',52,1);
INSERT INTO {modules} VALUES (2,1,'user',4,2);
INSERT INTO {modules} VALUES (3,1,'comment',7,3);
INSERT INTO {modules} VALUES (4,1,'organize',4,4);
Expand Down Expand Up @@ -382,7 +382,7 @@ CREATE TABLE {vars} (
`value` text,
PRIMARY KEY (`id`),
UNIQUE KEY `module_name` (`module_name`,`name`)
) AUTO_INCREMENT=44 DEFAULT CHARSET=utf8;
) AUTO_INCREMENT=45 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO {vars} VALUES (NULL,'gallery','active_site_theme','wind');
INSERT INTO {vars} VALUES (NULL,'gallery','active_admin_theme','admin_wind');
Expand Down Expand Up @@ -411,10 +411,10 @@ INSERT INTO {vars} VALUES (NULL,'gallery','email_from','unknown@unknown.com');
INSERT INTO {vars} VALUES (NULL,'gallery','email_reply_to','unknown@unknown.com');
INSERT INTO {vars} VALUES (NULL,'gallery','email_line_length','70');
INSERT INTO {vars} VALUES (NULL,'gallery','email_header_separator','s:1:\"\n\";');
INSERT INTO {vars} VALUES (NULL,'gallery','lock_timeout','1');
INSERT INTO {vars} VALUES (NULL,'gallery','show_user_profiles_to','registered_users');
INSERT INTO {vars} VALUES (NULL,'gallery','extra_binary_paths','/usr/local/bin:/opt/local/bin:/opt/bin');
INSERT INTO {vars} VALUES (NULL,'gallery','timezone',NULL);
INSERT INTO {vars} VALUES (NULL,'gallery','lock_timeout','1');
INSERT INTO {vars} VALUES (NULL,'gallery','blocks_site_sidebar','a:4:{i:10;a:2:{i:0;s:7:\"gallery\";i:1;s:8:\"language\";}i:11;a:2:{i:0;s:4:\"info\";i:1;s:8:\"metadata\";}i:12;a:2:{i:0;s:3:\"rss\";i:1;s:9:\"rss_feeds\";}i:13;a:2:{i:0;s:3:\"tag\";i:1;s:3:\"tag\";}}');
INSERT INTO {vars} VALUES (NULL,'gallery','identity_provider','user');
INSERT INTO {vars} VALUES (NULL,'user','minimum_password_length','5');
Expand Down
19 changes: 16 additions & 3 deletions modules/gallery/helpers/gallery_installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ static function install() {
module::set_var("gallery", "timezone", null);
module::set_var("gallery", "lock_timeout", 1);

module::set_version("gallery", 51);
module::set_version("gallery", 52);
}

static function upgrade($version) {
Expand Down Expand Up @@ -688,7 +688,7 @@ static function upgrade($version) {

if ($version == 47 || $version == 48) {
// Add configuration variable to set timezone. Defaults to the currently
// used timezone (from PHP configuration). Note that in v48 we werew
// used timezone (from PHP configuration). Note that in v48 we were
// setting this value incorrectly, so we're going to stomp this value for v49.
module::set_var("gallery", "timezone", null);
module::set_version("gallery", $version = 49);
Expand Down Expand Up @@ -716,12 +716,25 @@ static function upgrade($version) {
}

if ($version == 50) {
// In v50, a lock_timeout variable was added so that administrators could edit the time out
// In v51, we added a lock_timeout variable so that administrators could edit the time out
// from 1 second to a higher variable if their system runs concurrent parallel uploads for
// instance.
module::set_var("gallery", "lock_timeout", 1);
module::set_version("gallery", $version = 51);
}

if ($version == 51) {
// In v52, we added functions to the legal_file helper that map photo and movie file
// extensions to their mime types (and allow extension of the list by other modules). During
// this process, we correctly mapped m4v files to video/x-m4v, correcting a previous error
// where they were mapped to video/mp4. This corrects the existing items.
db::build()
->update("items")
->set("mime_type", "video/x-m4v")
->where("name", "REGEXP", "\.m4v$") // case insensitive since name column is utf8_general_ci
->execute();
module::set_version("gallery", $version = 52);
}
}

static function uninstall() {
Expand Down
55 changes: 51 additions & 4 deletions modules/gallery/helpers/legal_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,54 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class legal_file_Core {
/**
* Create a default list of allowed photo MIME types paired with their extensions and then let
* modules modify it. This is an ordered map, mapping extensions to their MIME types.
* Extensions cannot be duplicated, but MIMEs can (e.g. jpeg and jpg both map to image/jpeg).
*
* @param string $extension (opt.) - return MIME of extension; if not given, return complete array
*/
static function get_photo_types_by_extension($extension=NULL) {
$types_by_extension_wrapper = new stdClass();
$types_by_extension_wrapper->types_by_extension = array(
"jpg" => "image/jpeg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
module::event("photo_types_by_extension", $types_by_extension_wrapper);
if ($extension) {
// return matching MIME type
return $types_by_extension_wrapper->types_by_extension[$extension];
} else {
// return complete array
return $types_by_extension_wrapper->types_by_extension;
}
}

/**
* Create a default list of allowed movie MIME types paired with their extensions and then let
* modules modify it. This is an ordered map, mapping extensions to their MIME types.
* Extensions cannot be duplicated, but MIMEs can (e.g. jpeg and jpg both map to image/jpeg).
*
* @param string $extension (opt.) - return MIME of extension; if not given, return complete array
*/
static function get_movie_types_by_extension($extension=NULL) {
$types_by_extension_wrapper = new stdClass();
$types_by_extension_wrapper->types_by_extension = array(
"flv" => "video/x-flv", "mp4" => "video/mp4", "m4v" => "video/x-m4v");
module::event("movie_types_by_extension", $types_by_extension_wrapper);
if ($extension) {
// return matching MIME type
return $types_by_extension_wrapper->types_by_extension[$extension];
} else {
// return complete array
return $types_by_extension_wrapper->types_by_extension;
}
}

/**
* Create a default list of allowed photo extensions and then let modules modify it.
*/
static function get_photo_extensions() {
$extensions_wrapper = new stdClass();
$extensions_wrapper->extensions = array("gif", "jpg", "jpeg", "png");
$extensions_wrapper->extensions = array_keys(legal_file::get_photo_types_by_extension());
module::event("legal_photo_extensions", $extensions_wrapper);
return $extensions_wrapper->extensions;
}
Expand All @@ -33,7 +75,7 @@ static function get_photo_extensions() {
*/
static function get_movie_extensions() {
$extensions_wrapper = new stdClass();
$extensions_wrapper->extensions = array("flv", "mp4", "m4v");
$extensions_wrapper->extensions = array_keys(legal_file::get_movie_types_by_extension());
module::event("legal_movie_extensions", $extensions_wrapper);
return $extensions_wrapper->extensions;
}
Expand Down Expand Up @@ -63,20 +105,25 @@ static function get_filters() {

/**
* Create a default list of allowed photo MIME types and then let modules modify it.
* Can be used to add legal alternatives for default MIME types.
* (e.g. flv maps to video/x-flv by default, but video/flv is still legal).
*/
static function get_photo_types() {
$types_wrapper = new stdClass();
$types_wrapper->types = array("image/jpeg", "image/gif", "image/png");
$types_wrapper->types = array_values(legal_file::get_photo_types_by_extension());
module::event("legal_photo_types", $types_wrapper);
return $types_wrapper->types;
}

/**
* Create a default list of allowed movie MIME types and then let modules modify it.
* Can be used to add legal alternatives for default MIME types.
* (e.g. flv maps to video/x-flv by default, but video/flv is still legal).
*/
static function get_movie_types() {
$types_wrapper = new stdClass();
$types_wrapper->types = array("video/flv", "video/x-flv", "video/mp4");
$types_wrapper->types = array_values(legal_file::get_movie_types_by_extension());
$types_wrapper->types[] = "video/flv";
module::event("legal_movie_types", $types_wrapper);
return $types_wrapper->types;
}
Expand Down
54 changes: 38 additions & 16 deletions modules/gallery/helpers/movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,29 @@ static function extract_frame($input_file, $output_file) {
throw new Exception("@todo MISSING_FFMPEG");
}

list($width, $height, $mime_type, $extension, $duration) = movie::get_file_metadata($input_file);

// extract frame at 0:03, unless movie is shorter than 4 sec.
$start_time_arg = ($duration > 4) ? " -ss 00:00:03" : "";

$cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($input_file) .
" -an -ss 00:00:03 -an -r 1 -vframes 1" .
" -an $start_time_arg -an -r 1 -vframes 1" .
" -s {$width}x{$height}" .
" -y -f mjpeg " . escapeshellarg($output_file) . " 2>&1";
exec($cmd);
exec($cmd, $exec_output, $exec_return);

clearstatcache(); // use $filename parameter when PHP_version is 5.3+
if (filesize($output_file) == 0) {
// Maybe the movie is shorter, fall back to the first frame.
$cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($input_file) .
" -an -an -r 1 -vframes 1" .
if (filesize($output_file) == 0 || $exec_return) {
// Maybe the movie needs the "-threads 1" argument added
// (see http://sourceforge.net/apps/trac/gallery/ticket/1924)
$cmd = escapeshellcmd($ffmpeg) . " -threads 1 -i " . escapeshellarg($input_file) .
" -an $start_time_arg -an -r 1 -vframes 1" .
" -s {$width}x{$height}" .
" -y -f mjpeg " . escapeshellarg($output_file) . " 2>&1";
exec($cmd);
exec($cmd, $exec_output, $exec_return);

clearstatcache();
if (filesize($output_file) == 0) {
if (filesize($output_file) == 0 || $exec_return) {
throw new Exception("@todo FFMPEG_FAILED");
}
}
Expand All @@ -96,7 +104,7 @@ static function find_ffmpeg() {
}

/**
* Return the width, height, mime_type and extension of the given movie file.
* Return the width, height, mime_type, extension and duration of the given movie file.
*/
static function get_file_metadata($file_path) {
$ffmpeg = movie::find_ffmpeg();
Expand All @@ -106,18 +114,32 @@ static function get_file_metadata($file_path) {

$cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($file_path) . " 2>&1";
$result = `$cmd`;
if (preg_match("/Stream.*?Video:.*?, (\d+)x(\d+)/", $result, $regs)) {
list ($width, $height) = array($regs[1], $regs[2]);
if (preg_match("/Stream.*?Video:.*?, (\d+)x(\d+)/", $result, $matches_res)) {
if (preg_match("/Stream.*?Video:.*? \[.*?DAR (\d+):(\d+).*?\]/", $result, $matches_dar) &&
$matches_dar[1] >= 1 && $matches_dar[2] >= 1) {
// DAR is defined - determine width based on height and DAR
// (should always be int, but adding round to be sure)
$matches_res[1] = round($matches_res[2] * $matches_dar[1] / $matches_dar[2]);
}
list ($width, $height) = array($matches_res[1], $matches_res[2]);
} else {
list ($width, $height) = array(0, 0);
}

$pi = pathinfo($file_path);
$extension = isset($pi["extension"]) ? $pi["extension"] : "flv"; // No extension? Assume FLV.
$mime_type = in_array(strtolower($extension), array("mp4", "m4v")) ?
"video/mp4" : "video/x-flv";
$extension = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
$extension = $extension ? $extension : "flv"; // No extension? Assume FLV.
$mime_type = legal_file::get_movie_types_by_extension($extension);
$mime_type = $mime_type ? $mime_type : "video/x-flv"; // No MIME found? Default to video/x-flv.

if (preg_match("/Duration: (\d+):(\d+):(\d+\.\d+)/", $result, $matches)) {
$duration = 3600 * $matches[1] + 60 * $matches[2] + $matches[3];
} else if (preg_match("/duration.*?:.*?(\d+)/", $result, $matches)) {
$duration = $matches[1];
} else {
$duration = 0;
}

return array($width, $height, $mime_type, $extension);
return array($width, $height, $mime_type, $extension, $duration);
}

}
17 changes: 12 additions & 5 deletions modules/gallery/helpers/photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ static function img_dimensions($width, $height, $max, $format="width=\"%d\" heig
*/
static function get_file_metadata($file_path) {
$image_info = getimagesize($file_path);
$width = $image_info[0];
$height = $image_info[1];
$mime_type = $image_info["mime"];
$extension = image_type_to_extension($image_info[2], false);
return array($width, $height, $mime_type, $extension);
if ($image_info) {
$width = $image_info[0];
$height = $image_info[1];
$mime_type = $image_info["mime"];
$extension = image_type_to_extension($image_info[2], false);
return array($width, $height, $mime_type, $extension);
} else {
// getimagesize failed - use legal_file mapping instead.
$extension = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
$mime_type = legal_file::get_photo_types_by_extension($extension);
return array(0, 0, $mime_type, $extension);
}
}
}
2 changes: 1 addition & 1 deletion modules/gallery/module.info
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Gallery 3"
description = "Gallery core application"
version = 51
version = 52
author_name = "Gallery Team"
author_url = "http://codex.gallery2.org/Gallery:Team"
info_url = "http://codex.gallery2.org/Gallery3:Modules:gallery"
Expand Down

0 comments on commit 98753bc

Please sign in to comment.