summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/gallery/helpers/graphics.php2
-rw-r--r--modules/gallery/helpers/legal_file.php12
-rw-r--r--modules/gallery/models/item.php4
-rw-r--r--modules/gallery/tests/Legal_File_Helper_Test.php32
4 files changed, 47 insertions, 3 deletions
diff --git a/modules/gallery/helpers/graphics.php b/modules/gallery/helpers/graphics.php
index 7e0bbbea..27ee124a 100644
--- a/modules/gallery/helpers/graphics.php
+++ b/modules/gallery/helpers/graphics.php
@@ -156,7 +156,7 @@ class graphics_Core {
foreach ($ops as $target => $output_file) {
if ($input_item->is_movie()) {
// Convert the movie to a JPG first
- $output_file = preg_replace("/...$/", "jpg", $output_file);
+ $output_file = legal_file::change_extension($output_file, "jpg");
try {
movie::extract_frame($input_file, $output_file);
} catch (Exception $e) {
diff --git a/modules/gallery/helpers/legal_file.php b/modules/gallery/helpers/legal_file.php
index 6ec65e97..af6472ca 100644
--- a/modules/gallery/helpers/legal_file.php
+++ b/modules/gallery/helpers/legal_file.php
@@ -80,4 +80,16 @@ class legal_file_Core {
module::event("legal_movie_types", $types_wrapper);
return $types_wrapper->types;
}
+
+ /**
+ * Convert the extension of a filename. If the original filename has no
+ * extension, add the new one to the end.
+ */
+ static function change_extension($filename, $new_ext) {
+ if (strpos($filename, ".") === false) {
+ return "{$filename}.{$new_ext}";
+ } else {
+ return preg_replace("/\..*?$/", ".{$new_ext}", $filename);
+ }
+ }
}
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index 0e3f0fb8..98a2c4df 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -188,7 +188,7 @@ class Item_Model_Core extends ORM_MPTT {
return $base . "/.album.jpg";
} else if ($this->is_movie()) {
// Replace the extension with jpg
- return preg_replace("/...$/", "jpg", $base);
+ return legal_file::change_extension($base, "jpg");
}
}
@@ -213,7 +213,7 @@ class Item_Model_Core extends ORM_MPTT {
return $base . "/.album.jpg" . $cache_buster;
} else if ($this->is_movie()) {
// Replace the extension with jpg
- $base = preg_replace("/...$/", "jpg", $base);
+ $base = legal_file::change_extension($base, "jpg");
return $base . $cache_buster;
}
}
diff --git a/modules/gallery/tests/Legal_File_Helper_Test.php b/modules/gallery/tests/Legal_File_Helper_Test.php
new file mode 100644
index 00000000..c101de10
--- /dev/null
+++ b/modules/gallery/tests/Legal_File_Helper_Test.php
@@ -0,0 +1,32 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2012 Bharat Mediratta
+ *
+ * This program 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.
+ *
+ * This program 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 Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Legal_File_Helper_Test extends Gallery_Unit_Test_Case {
+ public function change_extension_test() {
+ $this->assert_equal("foo.jpg", legal_file::change_extension("foo.png", "jpg"));
+ }
+
+ public function change_four_letter_extension_test() {
+ $this->assert_equal("foo.flv", legal_file::change_extension("foo.mpeg", "flv"));
+ }
+
+ public function change_extension_with_no_extension_test() {
+ $this->assert_equal("foo.flv", legal_file::change_extension("foo", "flv"));
+ }
+} \ No newline at end of file