Added Tagging, Moving with Replace, Optimised Delete

This commit is contained in:
Deon George
2015-07-12 21:23:57 +10:00
parent c88d289e82
commit 08f823460e
8 changed files with 272 additions and 52 deletions

View File

@@ -15,7 +15,10 @@ class Model_Photo extends ORM {
protected $_has_many = array(
'album'=>array('through'=>'album_photo'),
'people'=>array('through'=>'people_photo','far_key'=>'people_id'),
'people'=>array('through'=>'photo_people','far_key'=>'people_id'),
'tags'=>array('through'=>'photo_tag','far_key'=>'tag_id'),
'photo_tag'=>array('far_key'=>'id'),
'photo_people'=>array('far_key'=>'id'),
);
/**
@@ -65,6 +68,27 @@ class Model_Photo extends ORM {
return trim(chunk_split(sprintf("%0{$depth}s",$this->id),$sep,'/'),'/');
}
public function file_path_short($path) {
return preg_replace(":^{$this->_path}".DIRECTORY_SEPARATOR.":",'',$path);
}
public function delete() {
if (unlink($this->file_path())) {
// Remove any tags
foreach ($this->photo_tag->find_all() as $o)
$o->delete();
// Remove any people
foreach ($this->photo_people->find_all() as $o)
$o->delete();
return parent::delete();
}
// If unlink failed...
return FALSE;
}
public function file_size() {
return filesize($this->file_path());
}
@@ -121,15 +145,22 @@ class Model_Photo extends ORM {
if (! $path)
$path = $this->file_path(FALSE,TRUE);
$po = ORM::factory('Photo',$path);
// Check if there is already a phoot here - and if it is pending delete.
$po = ORM::factory('Photo',array('filename'=>$this->file_path_short($path)));
// If the file already exists, we'll ignore the move.
if ($po->loaded() OR file_exists($path) OR ! File::ParentDirExist(dirname($path),TRUE))
// @todo Move any tags if we are replacing an existing photo.
if ($po->loaded() AND (! $po->remove OR ($po->remove AND ! $po->delete())))
return FALSE;
unset($po);
// If the file already exists, or we cant create the dir structure, we'll ignore the move
if (file_exists($path) OR ! File::ParentDirExist(dirname($path),TRUE))
return FALSE;
if (rename($this->file_path(),$path)) {
// Convert the path to a relative one.
$this->filename = preg_replace(":^{$this->_path}/:",'',$path);
$this->filename = $this->file_path_short($path);
// If the DB update failed, move it back.
if (! $this->save() AND ! rename($path,$this->file_path()))
@@ -176,11 +207,14 @@ class Model_Photo extends ORM {
}
}
public static function SignatureTrim($signature) {
return sprintf('%s...%s',substr($signature,0,6),substr($signature,-6));
public static function SignatureTrim($signature,$chars=6) {
return sprintf('%s...%s',substr($signature,0,$chars),substr($signature,-1*$chars));
}
public function thumbnail($rotate=TRUE) {
if (! $this->thumbnail)
return NULL;
$imo = new Imagick();
$imo->readImageBlob($this->thumbnail);
@@ -191,6 +225,10 @@ class Model_Photo extends ORM {
return $imo->getImageBlob();
}
public function thumbnail_sig() {
return md5($this->thumbnail()).':'.strlen($this->thumbnail());
}
public function type($mime=FALSE) {
return strtolower($mime ? File::mime_by_ext(pathinfo($this->filename,PATHINFO_EXTENSION)) : pathinfo($this->filename,PATHINFO_EXTENSION));
}