summaryrefslogtreecommitdiff
path: root/modules/gallery/models/item.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gallery/models/item.php')
-rw-r--r--modules/gallery/models/item.php105
1 files changed, 71 insertions, 34 deletions
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index c00b7972..1db766e9 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -316,7 +316,7 @@ class Item_Model extends ORM_MPTT {
unset($significant_changes["relative_url_cache"]);
unset($significant_changes["relative_path_cache"]);
- if (!empty($this->changed) && $significant_changes) {
+ if ((!empty($this->changed) && $significant_changes) || isset($this->data_file)) {
$this->updated = time();
if (!$this->loaded()) {
// Create a new item.
@@ -341,30 +341,19 @@ class Item_Model extends ORM_MPTT {
}
// Get the width, height and mime type from our data file for photos and movies.
- if ($this->is_movie() || $this->is_photo()) {
- $pi = pathinfo($this->data_file);
-
+ if ($this->is_photo() || $this->is_movie()) {
if ($this->is_photo()) {
- $image_info = getimagesize($this->data_file);
- $this->width = $image_info[0];
- $this->height = $image_info[1];
- $this->mime_type = $image_info["mime"];
-
- // Force an extension onto the name if necessary
- if (empty($pi["extension"])) {
- $pi["extension"] = image_type_to_extension($image_info[2], false);
- $this->name .= "." . $pi["extension"];
- }
- } else {
- list ($this->width, $this->height) = movie::getmoviesize($this->data_file);
-
- // No extension? Assume FLV.
- if (empty($pi["extension"])) {
- $pi["extension"] = "flv";
- $this->name .= "." . $pi["extension"];
- }
+ list ($this->width, $this->height, $this->mime_type, $extension) =
+ photo::get_file_metadata($this->data_file);
+ } else if ($this->is_movie()) {
+ list ($this->width, $this->height, $this->mime_type, $extension) =
+ movie::get_file_metadata($this->data_file);
+ }
- $this->mime_type = in_array(strtolower($pi["extension"]), array("mp4", "m4v")) ? "video/mp4" : "video/x-flv";
+ // Force an extension onto the name if necessary
+ $pi = pathinfo($this->data_file);
+ if (empty($pi["extension"])) {
+ $this->name = "{$this->name}.$extension";
}
}
@@ -422,7 +411,9 @@ class Item_Model extends ORM_MPTT {
}
// This will almost definitely trigger another save, so put it at the end so that we're
- // tail recursive.
+ // tail recursive. Null out the data file variable first, otherwise the next save will
+ // trigger an item_updated_data_file event.
+ $this->data_file = null;
module::event("item_created", $this);
} else {
// Update an existing item
@@ -479,7 +470,30 @@ class Item_Model extends ORM_MPTT {
->execute();
}
+ // Replace the data file, if requested.
+ // @todo: we don't handle the case where you swap in a file of a different mime type
+ // should we prevent that in validation? or in set_data_file()
+ if ($this->data_file && ($this->is_photo() || $this->is_movie())) {
+ copy($this->data_file, $this->file_path());
+
+ // Get the width, height and mime type from our data file for photos and movies.
+ if ($this->is_photo()) {
+ list ($this->width, $this->height) = photo::get_file_metadata($this->file_path());
+ } else if ($this->is_movie()) {
+ list ($this->width, $this->height) = movie::get_file_metadata($this->file_path());
+ }
+ $this->thumb_dirty = 1;
+ $this->resize_dirty = 1;
+ }
+
module::event("item_updated", $original, $this);
+
+ if ($this->data_file) {
+ // Null out the data file variable here, otherwise this event will trigger another
+ // save() which will think that we're doing another file move.
+ $this->data_file = null;
+ module::event("item_updated_data_file", $this);
+ }
}
} else if (!empty($this->changed)) {
// Insignificant changes only. Don't fire events or do any special checking to try to keep
@@ -656,9 +670,9 @@ class Item_Model extends ORM_MPTT {
public function resize_img($extra_attrs) {
$attrs = array_merge($extra_attrs,
array("src" => $this->resize_url(),
- "alt" => $this->title,
- "width" => $this->resize_width,
- "height" => $this->resize_height)
+ "alt" => $this->title,
+ "width" => $this->resize_width,
+ "height" => $this->resize_height)
);
// html::image forces an absolute url which we don't want
return "<img" . html::attributes($attrs) . "/>";
@@ -765,8 +779,9 @@ class Item_Model extends ORM_MPTT {
$this->rules["slug"] = array();
}
- // Movies and photos must have data files
- if (($this->is_photo() || $this->is_movie()) && !$this->loaded()) {
+ // Movies and photos must have data files. Verify the data file on new items, or if it has
+ // been replaced.
+ if (($this->is_photo() || $this->is_movie()) && $this->data_file) {
$this->rules["name"]["callbacks"][] = array($this, "valid_data_file");
}
}
@@ -842,6 +857,17 @@ class Item_Model extends ORM_MPTT {
} else if (filesize($this->data_file) == 0) {
$v->add_error("name", "empty_data_file");
}
+
+ if ($this->loaded()) {
+ if ($this->is_photo()) {
+ list ($a, $b, $mime_type) = photo::get_file_metadata($this->data_file);
+ } else if ($this->is_movie()) {
+ list ($a, $b, $mime_type) = movie::get_file_metadata($this->data_file);
+ }
+ if ($mime_type != $this->mime_type) {
+ $v->add_error("name", "cant_change_mime_type");
+ }
+ }
}
/**
@@ -949,14 +975,25 @@ class Item_Model extends ORM_MPTT {
}
unset($data["album_cover_item_id"]);
- if (access::can("view_full", $this) && $this->is_photo()) {
- $data["file_url"] = $this->file_url(true);
+ if (access::can("view_full", $this) && !$this->is_album()) {
+ $data["file_url"] = rest::url("data", $this, "full");
+ }
+ if (access::user_can(identity::guest(), "view_full", $this)) {
+ $data["file_url_public"] = $this->file_url(true);
+ }
+
+ if ($this->is_photo()) {
+ $data["resize_url"] = rest::url("data", $this, "resize");
+ if (access::user_can(identity::guest(), "view", $this)) {
+ $data["resize_url_public"] = $this->resize_url(true);
+ }
}
- if (($tmp = $this->resize_url(true)) && $this->is_photo()) {
- $data["resize_url"] = $tmp;
+ $data["thumb_url"] = rest::url("data", $this, "thumb");
+ if (access::user_can(identity::guest(), "view", $this)) {
+ $data["thumb_url_public"] = $this->thumb_url(true);
}
- $data["thumb_url"] = $this->thumb_url(true);
+
$data["can_edit"] = access::can("edit", $this);
// Elide some internal-only data that is going to cause confusion in the client.