Created
August 26, 2015 05:45
-
-
Save romaninsh/2da822ddec9769a07840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Model_File extends LF_Model { | |
public $table = 'file'; | |
public $storage = null; | |
public $local_copy = null; | |
function init(){ | |
parent::init(); | |
$this->addField('location'); // e.g. s3://bucket/file or http://host/file, or file://path/to/file | |
$this->addField('url'); // public access URL if file is available | |
$this->addField('storage'); // storage controller used | |
$this->addField('status')->enum(['draft','uploaded','verified']); | |
$this->containsOne(['file_metadata', 'json'=>true], [$this, 'initFileMetadata']); | |
$this->addHook('beforeDelete,afterUnload', $this); | |
} | |
function initFileMetadata($m){ | |
$m->addField('extension'); | |
$m->addField('size'); | |
$m->addField('md5'); | |
$m->addField('original_filename'); | |
$m->addField('is_image')->type('boolean'); | |
$m->addField('mime'); | |
$m->addField('width'); | |
$m->addField('height'); | |
} | |
/** | |
* Call to associate with a file storage service (such as Amazon S3) | |
*/ | |
function setStorage($controller){ | |
$controller = $this->api->normalizeClassName($controller, 'Storage'); | |
$this->storage = $this->setController($controller); | |
return $this; | |
} | |
function import($source){ | |
if(!$this->storage)throw $this->exception('Set Storage Controller first'); | |
$this->unload(); | |
$this['satus']='draft'; | |
$this->save(); | |
$m = $this->ref('file_metadata'); | |
$m['original_filename'] = basename($source); | |
$m['size'] = filesize($source); | |
$m['extension'] = pathinfo($source, PATHINFO_EXTENSION); | |
// TODO: that's only for images | |
$is = getimagesize($source); | |
if($m['is_image'] = (boolean)$is){ | |
$m['mime'] = $is['mime']; | |
$m['width'] = $is[0]; | |
$m['height'] = $is[1]; | |
$m['md5'] = md5_file($source); | |
//$m['extension'] = $is['mime']; | |
} | |
$m->save(); | |
$this->storage->put($this, $source); | |
$this['status']='uploaded'; | |
//$this->local_copy = $source; | |
$m->save(); | |
} | |
/** | |
* Verify that the file is uploaded correctly | |
* @param [type] $source [description] | |
* @return [type] [description] | |
*/ | |
function verify($source = null){ | |
// make sure, that public URL is accessible | |
if(md5_file($this['url']) == $this->ref('file_metadata')['md5']){ | |
$this['status'] = 'verified'; | |
$this->save(); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Retrieve file and store in local file | |
*/ | |
function getFile(){ | |
if($this->local_copy)return $this->local_copy; | |
return $this->local_copy = $this->storage->get($this); | |
} | |
function beforeDelete(){ | |
if($this->loaded()){ | |
$this->storage->delete($this); | |
} | |
} | |
function __destruct(){ | |
if($this->local_copy)@unlink($this->local_copy); | |
} | |
function afterUnload(){ | |
if($this->local_copy)@unlink($this->local_copy); | |
$this->local_copy=null; | |
} | |
} | |
/* | |
class Model_File extends filestore\Model_File { | |
public $table = 'filestore_file'; | |
public $title_field = 's3_url'; | |
function init() | |
{ | |
parent::init(); | |
//$this->addField('original_filename'); | |
//$this->addField('filesize'); | |
$this->addField('s3_url'); | |
$this->addField('s3_thumb_url'); | |
} | |
function getURLExpr($m,$q){ | |
$e = parent::getURLExpr($m,$q); | |
return $q->expr('coalesce([0],[1])', [$this->getElement('s3_url'), $e]); | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment