General improvements

This commit is contained in:
Deon George
2015-06-04 14:25:58 +10:00
parent 9359564ea8
commit c88d289e82
4 changed files with 40 additions and 13 deletions

View File

@@ -51,11 +51,24 @@ class Model_Photo extends ORM {
}
public function file_path($short=FALSE,$new=FALSE) {
$file = $new ? sprintf('%s_%03s.%s',date('Y/m/d-His',$this->date_taken),$this->subsectime,$this->type()) : $this->filename;
$file = $this->filename;
if ($new)
$file = sprintf('%s.%s',((is_null($this->date_taken) OR ! $this->date_taken)
? sprintf('UNKNOWN/%07s',$this->file_path_id())
: sprintf('%s_%03s',date('Y/m/d-His',$this->date_taken),$this->subsectime).($this->subsectime ? '' : sprintf('-%05s',$this->id))),$this->type());
return (($short OR preg_match('/^\//',$file)) ? '' : $this->_path.DIRECTORY_SEPARATOR).$file;
}
public function file_path_id($sep=3,$depth=9) {
return trim(chunk_split(sprintf("%0{$depth}s",$this->id),$sep,'/'),'/');
}
public function file_size() {
return filesize($this->file_path());
}
public function gps(array $coordinate,$hemisphere) {
if (! $coordinate OR ! $hemisphere)
return NULL;
@@ -99,7 +112,7 @@ class Model_Photo extends ORM {
public function io($attr=NULL) {
if (is_nulL($this->_io))
$this->_io = new Imagick($this->_path.DIRECTORY_SEPARATOR.$this->filename);
$this->_io = new Imagick($this->file_path());
return is_null($attr) ? $this->_io : $this->_io->getImageProperty($attr);
}
@@ -114,13 +127,16 @@ class Model_Photo extends ORM {
if ($po->loaded() OR file_exists($path) OR ! File::ParentDirExist(dirname($path),TRUE))
return FALSE;
if (rename($this->file_path(FALSE,FALSE),$path)) {
if (rename($this->file_path(),$path)) {
// Convert the path to a relative one.
$this->filename = preg_replace(":^{$this->_path}/:",'',$path);
// If the DB update failed, move it back.
if (! $this->save() AND ! rename($path,$this->file_path()))
throw new Kohana_Exception('Error: Unable to move file, ID: :id, OLD: :oldname, NEW: :newname',
array(':id'=>$this->id,':oldname'=>$this->file_path(),':newname'=>$this->file_path()));
array(':id'=>$this->id,':oldname'=>$this->file_path(),':newname'=>$path));
chmod($this->file_path(),0444);
return TRUE;
}