summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-17 04:45:35 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-17 04:45:35 +0000
commitfc7b78492bc671f6dd78e04f5d42de958cdfb1d7 (patch)
tree9ee1738b0869cd183f688180eedc144bb4fa385a /core
parentf8a0c91ce689c5e0ae7bf05cc75b9982a3b26baf (diff)
Separate thumbnails out into var/thumbs. This clears up some ambiguity in Item_Model and simplifies
file_proxy. It also means we can stop munging file names in the var/resizes hierarchy. In the process, rename "thumbnail" to "thumb" everywhere in honor of Chad (well, ok because it's shorter)..
Diffstat (limited to 'core')
-rw-r--r--core/controllers/file_proxy.php32
-rw-r--r--core/controllers/welcome.php13
-rw-r--r--core/helpers/access.php4
-rw-r--r--core/helpers/album.php6
-rw-r--r--core/helpers/core_installer.php18
-rw-r--r--core/helpers/photo.php4
-rw-r--r--core/libraries/Theme_View.php6
-rw-r--r--core/models/item.php106
-rw-r--r--core/tests/Album_Helper_Test.php13
-rw-r--r--core/tests/Photo_Helper_Test.php12
10 files changed, 98 insertions, 116 deletions
diff --git a/core/controllers/file_proxy.php b/core/controllers/file_proxy.php
index c3b92e99..5bb15ebb 100644
--- a/core/controllers/file_proxy.php
+++ b/core/controllers/file_proxy.php
@@ -50,21 +50,10 @@ class File_Proxy_Controller extends Controller {
// We only handle var/resizes and var/albums
$paths = explode("/", $file);
$type = array_shift($paths);
- if ($type != "resizes" && $type != "albums") {
+ if ($type != "resizes" && $type != "albums" && $type != "thumbs") {
kohana::show_404();
}
- // Pull the last item off of the list, explode it out to get the "resize" or "thumb" tag, then
- // put it back together without that tag. This will give us the matching item name.
- $exploded_last = explode(".", array_pop($paths));
- $extension = array_pop($exploded_last);
- $image_type = array_pop($exploded_last);
- if ($image_type != "resize" && $image_type != "thumb") {
- kohana::show_404();
- }
- array_push($exploded_last, $extension);
- array_push($paths, implode(".", $exploded_last));
-
// Walk down from the root until we find the item that matches this path
$item = ORM::factory("item", 1);
while ($path = array_shift($paths)) {
@@ -77,11 +66,8 @@ class File_Proxy_Controller extends Controller {
kohana::show_404();
}
- // Try to detect when we're asking for an album thumbnail or resize. In that case, the
- // second to last element will be an album and the last element will be .thumb.jpg or
- // .resize.jpg except we'll have stripped the .thumb and .resize parts so it'll just be .jpg
- if ($item->type == "album" && count($paths) == 1 &&
- $paths[0][0] == '.' && strlen($paths[0]) == 4) {
+ // If the last element is _album.jpg then we're done.
+ if (count($paths) == 1 && $paths[0] == "_album.jpg") {
break;
}
}
@@ -91,7 +77,17 @@ class File_Proxy_Controller extends Controller {
kohana::show_404();
}
- $path = $image_type == "thumb" ? $item->thumbnail_path() : $item->resize_path();
+ if ($type == "albums") {
+ if ($item->is_album()) {
+ kohana::show_404();
+ }
+ $path = $item->file_path();
+ } else if ($type == "resizes") {
+ $path = $item->resize_path();
+ } else {
+ $path = $item->thumb_path();
+ }
+
if (!file_exists($path)) {
kohana::show_404();
}
diff --git a/core/controllers/welcome.php b/core/controllers/welcome.php
index 7235e2c0..b341b1af 100644
--- a/core/controllers/welcome.php
+++ b/core/controllers/welcome.php
@@ -92,10 +92,15 @@ class Welcome_Controller extends Template_Controller {
try {
call_user_func(array("{$module->name}_installer", "uninstall"));
} catch (Exception $e) {
+ print $e;
}
}
}
- } catch (Exception $e) { }
+ core_installer::uninstall();
+ } catch (Exception $e) {
+ print $e;
+ }
+
// Since we're in a state of flux, it's possible that other stuff went wrong with the
// uninstall, so back off and nuke it from orbit. It's the only way to be sure.
@@ -206,15 +211,15 @@ class Welcome_Controller extends Template_Controller {
$type = rand(0, 10) ? "photo" : "album";
}
if ($type == "album") {
- $thumbnail_size = module::get_var("core", "thumbnail_size");
+ $thumb_size = module::get_var("core", "thumb_size");
$parents[] = album::create(
$parent->id, "rnd_" . rand(), "Rnd $i", "random album $i", $owner_id)
- ->set_thumbnail(DOCROOT . "core/tests/test.jpg", $thumbnail_size, $thumbnail_size)
+ ->set_thumb(DOCROOT . "core/tests/test.jpg", $thumb_size, $thumb_size)
->save();
} else {
$photo_index = rand(0, count($test_images) - 1);
photo::create($parent->id, $test_images[$photo_index], basename($test_images[$photo_index]),
- "rnd_" . rand(), "sample thumbnail", $owner_id);
+ "rnd_" . rand(), "sample thumb", $owner_id);
}
}
url::redirect("welcome");
diff --git a/core/helpers/access.php b/core/helpers/access.php
index b8aee683..05799bf5 100644
--- a/core/helpers/access.php
+++ b/core/helpers/access.php
@@ -467,7 +467,9 @@ class access_Core {
* Create .htaccess files to prevent direct access to the given album and its hierarchy.
*/
private static function _create_htaccess_files($album) {
- foreach (array($album->file_path(), dirname($album->resize_path())) as $dir) {
+ foreach (array($album->file_path(),
+ dirname($album->resize_path()),
+ dirname($album->thumb_path())) as $dir) {
$base_url = url::site("file_proxy");
$fp = fopen("$dir/.htaccess", "w+");
fwrite($fp, "<IfModule mod_rewrite.c>\n");
diff --git a/core/helpers/album.php b/core/helpers/album.php
index 9373c5d5..faf0fd98 100644
--- a/core/helpers/album.php
+++ b/core/helpers/album.php
@@ -54,10 +54,8 @@ class album_Core {
$album = $album->add_to_parent($parent);
mkdir($album->file_path());
- $thumbnail_dir = dirname($album->thumbnail_path());
- if (!file_exists($thumbnail_dir)) {
- mkdir($thumbnail_dir);
- }
+ mkdir(dirname($album->thumb_path()));
+ mkdir(dirname($album->resize_path()));
module::event("album_created", $album);
diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php
index 1c5f92ea..de4c51d9 100644
--- a/core/helpers/core_installer.php
+++ b/core/helpers/core_installer.php
@@ -56,8 +56,8 @@ class core_installer {
`resize_height` int(9) default NULL,
`resize_width` int(9) default NULL,
`right` int(9) NOT NULL,
- `thumbnail_height` int(9) default NULL,
- `thumbnail_width` int(9) default NULL,
+ `thumb_height` int(9) default NULL,
+ `thumb_width` int(9) default NULL,
`title` char(255) default NULL,
`type` char(32) NOT NULL,
`width` int(9) default NULL,
@@ -91,7 +91,7 @@ class core_installer {
UNIQUE KEY(`module_id`, `name`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- foreach (array("albums", "resizes") as $dir) {
+ foreach (array("albums", "resizes", "thumbs", "uploads") as $dir) {
@mkdir(VARPATH . $dir);
}
@@ -106,8 +106,8 @@ class core_installer {
$root->right = 2;
$root->parent_id = 0;
$root->level = 1;
- $root->set_thumbnail(DOCROOT . "core/tests/test.jpg", 200, 200)
- ->save();
+ $root->set_thumb(DOCROOT . "core/tests/test.jpg", 200, 200);
+ $root->save();
access::add_item($root);
// Save this before setting vars so that module id is set
@@ -116,9 +116,8 @@ class core_installer {
module::set_var("core", "active_theme", "default");
module::set_var("core", "active_admin_theme", "admin_default");
module::set_var("core", "page_size", 9);
- module::set_var("core", "thumbnail_size", 200);
+ module::set_var("core", "thumb_size", 200);
module::set_var("core", "resize_size", 640);
-
}
}
@@ -129,11 +128,10 @@ class core_installer {
$db->query("DROP TABLE IF EXISTS `permissions`;");
$db->query("DROP TABLE IF EXISTS `items`;");
$db->query("DROP TABLE IF EXISTS `modules`;");
- // @todo remove before release
- $db->query("DROP TABLE IF EXISTS `parameters`;");
- // @todo-end
$db->query("DROP TABLE IF EXISTS `vars`;");
system("/bin/rm -rf " . VARPATH . "albums");
system("/bin/rm -rf " . VARPATH . "resizes");
+ system("/bin/rm -rf " . VARPATH . "thumbs");
+ system("/bin/rm -rf " . VARPATH . "uploads");
}
}
diff --git a/core/helpers/photo.php b/core/helpers/photo.php
index da4ab0ac..00aff447 100644
--- a/core/helpers/photo.php
+++ b/core/helpers/photo.php
@@ -78,10 +78,10 @@ class photo_Core {
copy($filename, $photo->file_path());
// This saves the photo a second time, which is unfortunate but difficult to avoid.
- $thumbnail_size = module::get_var("core", "thumbnail_size");
+ $thumb_size = module::get_var("core", "thumb_size");
$resize_size = module::get_var("core", "resize_size");
- $result = $photo->set_thumbnail($filename, $thumbnail_size, $thumbnail_size)
+ $result = $photo->set_thumb($filename, $thumb_size, $thumb_size)
->set_resize($filename, $resize_size, $resize_size)
->save();
diff --git a/core/libraries/Theme_View.php b/core/libraries/Theme_View.php
index 7fc400ce..933fa2c0 100644
--- a/core/libraries/Theme_View.php
+++ b/core/libraries/Theme_View.php
@@ -105,9 +105,9 @@ class Theme_View_Core extends View {
case "sidebar_top":
case "tag_bottom":
case "tag_top":
- case "thumbnail_bottom":
- case "thumbnail_info":
- case "thumbnail_top":
+ case "thumb_bottom":
+ case "thumb_info":
+ case "thumb_top":
case "photo_bottom":
// @todo: restrict access to this option
$debug = Session::instance()->get("debug", false);
diff --git a/core/models/item.php b/core/models/item.php
index a4d72fad..9ab230b7 100644
--- a/core/models/item.php
+++ b/core/models/item.php
@@ -19,6 +19,7 @@
*/
class Item_Model extends ORM_MPTT {
protected $children = 'items';
+ private $relative_path = null;
var $rules = array();
@@ -61,13 +62,18 @@ class Item_Model extends ORM_MPTT {
function move_to($target) {
$original_path = $this->file_path();
$original_resize_path = $this->resize_path();
- $original_thumbnail_path = $this->thumbnail_path();
+ $original_thumb_path = $this->thumb_path();
parent::move_to($target, true);
rename($original_path, $this->file_path());
- rename($original_resize_path, $this->resize_path());
- rename($original_thumbnail_path, $this->thumbnail_path());
+ if ($this->is_album()) {
+ rename(dirname($original_resize_path), dirname($this->resize_path()));
+ rename(dirname($original_thumb_path), dirname($this->thumb_path()));
+ } else {
+ rename($original_resize_path, $this->resize_path());
+ rename($original_thumb_path, $this->thumb_path());
+ }
return $this;
}
@@ -77,11 +83,7 @@ class Item_Model extends ORM_MPTT {
* photo: /var/albums/album1/album2/photo.jpg
*/
public function file_path() {
- if ($this->is_album()) {
- return $this->_relative_path(VARPATH . "albums", "", "");
- } else {
- return $this->_relative_path(VARPATH . "albums", "", "");
- }
+ return VARPATH . "albums/" . $this->_relative_path();
}
/**
@@ -89,33 +91,29 @@ class Item_Model extends ORM_MPTT {
* photo: http://example.com/gallery3/var/albums/album1/photo.jpg
*/
public function file_url($full_uri=false) {
- $func = $full_uri ? "abs_file" : "file";
- return $this->_relative_path(url::$func("") . "var/albums", "", "");
+ return $full_uri ?
+ url::abs_file("var/albums/" . $this->_relative_path()) :
+ url::file("var/albums/" . $this->_relative_path());
}
/**
* album: /var/resizes/album1/.thumb.jpg
* photo: /var/albums/album1/photo.thumb.jpg
*/
- public function thumbnail_path() {
- if ($this->is_album()) {
- return $this->_relative_path(VARPATH . "resizes", "", "/.thumb.jpg");
- } else {
- return $this->_relative_path(VARPATH . "resizes", ".thumb", "");
- }
+ public function thumb_path() {
+ return VARPATH . "thumbs/" . $this->_relative_path() .
+ ($this->type == "album" ? "/_album.jpg" : "");
}
/**
* album: http://example.com/gallery3/var/resizes/album1/.thumb.jpg
* photo: http://example.com/gallery3/var/albums/album1/photo.thumb.jpg
*/
- public function thumbnail_url($full_uri=true) {
- $func = $full_uri ? "abs_file" : "file";
- if ($this->is_album()) {
- return $this->_relative_path(url::$func("") . "var/resizes", "", "/.thumb.jpg");
- } else {
- return $this->_relative_path(url::$func("") . "var/resizes", ".thumb", "");
- }
+ public function thumb_url($full_uri=true) {
+ return ($full_uri ?
+ url::abs_file("var/thumbs/" . $this->_relative_path()) :
+ url::file("var/thumbs/" . $this->_relative_path())) .
+ ($this->type == "album" ? "/_album.jpg" : "");
}
/**
@@ -123,11 +121,8 @@ class Item_Model extends ORM_MPTT {
* photo: /var/albums/album1/photo.resize.jpg
*/
public function resize_path() {
- if ($this->is_album()) {
- return $this->_relative_path(VARPATH . "resizes", "", "/.resize.jpg");
- } else {
- return $this->_relative_path(VARPATH . "resizes", ".resize", "");
- }
+ return VARPATH . "resizes/" . $this->_relative_path() .
+ ($this->type == "album" ? "/_album.jpg" : "");
}
/**
@@ -135,12 +130,10 @@ class Item_Model extends ORM_MPTT {
* photo: http://example.com/gallery3/var/albums/album1/photo.resize.jpg
*/
public function resize_url($full_uri=true) {
- $func = $full_uri ? "abs_file" : "file";
- if ($this->is_album()) {
- return $this->_relative_path(url::$func("") . "var/resizes", "", "/.resize.jpg");
- } else {
- return $this->_relative_path(url::$func("") . "var/resizes", ".resize", "");
- }
+ return ($full_uri ?
+ url::abs_file("var/resizes/" . $this->_relative_path()) :
+ url::file("var/resizes/" . $this->_relative_path())) .
+ ($this->type == "album" ? "/_album.jpg" : "");
}
/**
@@ -149,18 +142,18 @@ class Item_Model extends ORM_MPTT {
*
* @chainable
* @param string $filename the path to an image
- * @param integer $width the desired width of the thumbnail
- * @param integer $height the desired height of the thumbnail
+ * @param integer $width the desired width of the thumb
+ * @param integer $height the desired height of the thumb
* @return ORM
*/
- public function set_thumbnail($filename, $width, $height) {
+ public function set_thumb($filename, $width, $height) {
Image::factory($filename)
->resize($width, $height, Image::AUTO)
- ->save($this->thumbnail_path());
+ ->save($this->thumb_path());
- $dims = getimagesize($this->thumbnail_path());
- $this->thumbnail_width = $dims[0];
- $this->thumbnail_height = $dims[1];
+ $dims = getimagesize($this->thumb_path());
+ $this->thumb_width = $dims[0];
+ $this->thumb_height = $dims[1];
return $this;
}
@@ -187,30 +180,19 @@ class Item_Model extends ORM_MPTT {
/**
* Return the relative path to this item's file.
- * @param string $prefix prefix to the path (eg "/var" or "http://foo.com/var")
- * @param string $tag a tag to specify before the extension (eg ".thumb", ".resize")
- * @param string $suffix suffix to add to end of the path
- * @return a path
+ * @return string
*/
- private function _relative_path($prefix, $tag, $suffix) {
- $paths = array($prefix);
- foreach ($this->parents() as $parent) {
- if ($parent->id > 1) {
- $paths[] = $parent->name;
+ private function _relative_path() {
+ if (empty($this->relative_path)) {
+ foreach ($this->parents() as $parent) {
+ if ($parent->id > 1) {
+ $paths[] = $parent->name;
+ }
}
+ $paths[] = $this->name;
+ $this->relative_path = implode($paths, "/");
}
- $paths[] = $this->name;
- $path = implode($paths, "/");
-
- if ($tag) {
- $pi = pathinfo($path);
- $path = "{$pi['dirname']}/{$pi['filename']}{$tag}.{$pi['extension']}";
- }
-
- if ($suffix) {
- $path .= $suffix;
- }
- return $path;
+ return $this->relative_path;
}
/**
diff --git a/core/tests/Album_Helper_Test.php b/core/tests/Album_Helper_Test.php
index 9927b66a..e01ebcbc 100644
--- a/core/tests/Album_Helper_Test.php
+++ b/core/tests/Album_Helper_Test.php
@@ -23,11 +23,12 @@ class Album_Helper_Test extends Unit_Test_Case {
$album = album::create(1, $rand, $rand, $rand);
$this->assert_equal(VARPATH . "albums/$rand", $album->file_path());
- $this->assert_equal(VARPATH . "resizes/$rand/.thumb.jpg", $album->thumbnail_path());
- $this->assert_true(is_dir(VARPATH . "resizes/$rand"), "missing thumbnail dir");
+ $this->assert_equal(VARPATH . "thumbs/$rand/_album.jpg", $album->thumb_path());
+ $this->assert_true(is_dir(VARPATH . "thumbs/$rand"), "missing thumb dir");
// It's unclear that a resize makes sense for an album. But we have one.
- $this->assert_equal(VARPATH . "resizes/$rand/.resize.jpg", $album->resize_path());
+ $this->assert_equal(VARPATH . "resizes/$rand/_album.jpg", $album->resize_path());
+ $this->assert_true(is_dir(VARPATH . "resizes/$rand"), "missing resizes dir");
$this->assert_equal(1, $album->parent_id); // MPTT tests will cover other hierarchy checks
$this->assert_equal($rand, $album->name);
@@ -42,15 +43,15 @@ class Album_Helper_Test extends Unit_Test_Case {
$this->assert_true($album1->name != $album2->name);
}
- public function thumbnail_url_test() {
+ public function thumb_url_test() {
$rand = rand();
$album = album::create(1, $rand, $rand, $rand);
- $this->assert_equal("http://./var/resizes/$rand/.thumb.jpg", $album->thumbnail_url());
+ $this->assert_equal("http://./var/thumbs/$rand/_album.jpg", $album->thumb_url());
}
public function resize_url_test() {
$rand = rand();
$album = album::create(1, $rand, $rand, $rand);
- $this->assert_equal("http://./var/resizes/$rand/.resize.jpg", $album->resize_url());
+ $this->assert_equal("http://./var/resizes/$rand/_album.jpg", $album->resize_url());
}
}
diff --git a/core/tests/Photo_Helper_Test.php b/core/tests/Photo_Helper_Test.php
index 4c804a6f..f31509a3 100644
--- a/core/tests/Photo_Helper_Test.php
+++ b/core/tests/Photo_Helper_Test.php
@@ -27,12 +27,12 @@ class Photo_Helper_Test extends Unit_Test_Case {
$photo = photo::create(1, $filename, "$rand.jpg", $rand, $rand);
$this->assert_equal(VARPATH . "albums/$rand.jpg", $photo->file_path());
- $this->assert_equal(VARPATH . "resizes/{$rand}.thumb.jpg", $photo->thumbnail_path());
- $this->assert_equal(VARPATH . "resizes/{$rand}.resize.jpg", $photo->resize_path());
+ $this->assert_equal(VARPATH . "thumbs/{$rand}.jpg", $photo->thumb_path());
+ $this->assert_equal(VARPATH . "resizes/{$rand}.jpg", $photo->resize_path());
$this->assert_true(is_file($photo->file_path()), "missing: {$photo->file_path()}");
$this->assert_true(is_file($photo->resize_path()), "missing: {$photo->resize_path()}");
- $this->assert_true(is_file($photo->thumbnail_path()), "missing: {$photo->thumbnail_path()}");
+ $this->assert_true(is_file($photo->thumb_path()), "missing: {$photo->thumb_path()}");
$this->assert_equal(1, $photo->parent_id); // MPTT tests will cover other hierarchy checks
$this->assert_equal("$rand.jpg", $photo->name);
@@ -62,10 +62,10 @@ class Photo_Helper_Test extends Unit_Test_Case {
}
}
- public function thumbnail_url_test() {
+ public function thumb_url_test() {
$rand = rand();
$photo = photo::create(1, DOCROOT . "core/tests/test.jpg", "$rand.jpg", $rand, $rand);
- $this->assert_equal("http://./var/resizes/{$rand}.thumb.jpg", $photo->thumbnail_url());
+ $this->assert_equal("http://./var/thumbs/{$rand}.jpg", $photo->thumb_url());
}
public function resize_url_test() {
@@ -73,6 +73,6 @@ class Photo_Helper_Test extends Unit_Test_Case {
$album = album::create(1, $rand, $rand, $rand);
$photo = photo::create($album->id, DOCROOT . "core/tests/test.jpg", "$rand.jpg", $rand, $rand);
- $this->assert_equal("http://./var/resizes/{$rand}/{$rand}.resize.jpg", $photo->resize_url());
+ $this->assert_equal("http://./var/resizes/{$rand}/{$rand}.jpg", $photo->resize_url());
}
}