Created
April 17, 2014 01:50
-
-
Save adamgoose/10947642 to your computer and use it in GitHub Desktop.
Importing media into Flights
This file contains 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 namespace Flight\Importers\Drivers; | |
use File; | |
use Flight\Asset; | |
use Flight\Flight; | |
class Image implements ImporterDriverInterface { | |
/** | |
* Open Graph Objects | |
* @var array | |
*/ | |
protected $graphObjects; | |
/** | |
* Flight to import to | |
* @var Flight\Flight | |
*/ | |
protected $flight; | |
/** | |
* Setup the driver | |
* @param array $graphObjects | |
*/ | |
public function __construct(array $graphObjects, Flight $flight) | |
{ | |
$this->graphObjects = $graphObjects; | |
$this->flight = $flight; | |
} | |
/** | |
* Import the image, and create the asset model | |
* @return Asset | |
*/ | |
public function doImport() | |
{ | |
if($this->getImage() != '') { | |
// Delete any existing asset files | |
if($this->flight->asset) | |
File::delete(public_path().$this->flight->asset->file); | |
$asset = $this->flight->asset ?: new Asset; | |
$asset->fill([ | |
'type' => 'image', | |
'file' => $this->getImageName(), | |
]); | |
File::put(public_path().$this->flight->path.$this->getImageName(), file_get_contents($this->getImage())); | |
$this->flight->asset()->save($asset); | |
return $asset; | |
} | |
} | |
/** | |
* Get the image URL from the OpenGraph info | |
* @return string URL of image | |
*/ | |
private function getImage() | |
{ | |
return $this->graphObjects['og:image'][0]['og:image:url']; | |
} | |
/** | |
* Get the image's filename | |
* @return string Image filename | |
*/ | |
private function getImageName() | |
{ | |
return pathinfo(parse_url($this->getImage(), PHP_URL_PATH), PATHINFO_BASENAME); | |
} | |
} |
This file contains 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 namespace Flight\Importers; | |
use Str; | |
use Flight\Asset; | |
use Flight\Flight; | |
use Opengraph\Reader as Graph; | |
class Import { | |
/** | |
* Domains for import type detection | |
* @var array | |
*/ | |
protected $domains = [ | |
'youtube' => ['youtube.com', 'youtu.be'], | |
'vimeo' => ['vimeo.com'], | |
]; | |
/** | |
* URL of asset to import | |
* @var string | |
*/ | |
protected $url; | |
/** | |
* Flight to import to | |
* @var Flight\Flight | |
*/ | |
protected $flight; | |
/** | |
* Import type | |
* @var string | |
*/ | |
protected $type; | |
/** | |
* Import Driver | |
* @var ImporterDriverInterface | |
*/ | |
protected $driver; | |
/** | |
* Setup the class | |
* @param string $url | |
*/ | |
public function __construct($url, Flight $flight) | |
{ | |
$this->url = $url; | |
$this->flight = $flight; | |
$this->graph = new Graph; | |
$this->graph->parse(file_get_contents($url)); | |
$this->detectImportType(); | |
$this->fetchDriver(); | |
} | |
/** | |
* Helper static method to instantiate and preform import. | |
* Only useful in production! | |
* @param string $url | |
* @param Flight $flight | |
* @return Asset | |
*/ | |
public static function to($url, Flight $flight) | |
{ | |
$importer = new self($url, $flight); | |
return $importer->doImport(); | |
} | |
/** | |
* Do the import (on the driver) | |
* @return Asset | |
*/ | |
public function doImport() | |
{ | |
return $this->driver->doImport(); | |
} | |
/** | |
* Get OpenGraph META tags | |
* @return array | |
*/ | |
public function getGraphObjects() | |
{ | |
return $this->graph->getArrayCopy(); | |
} | |
/** | |
* Return the import type | |
* @return string | |
*/ | |
public function getType() | |
{ | |
return $this->type; | |
} | |
/** | |
* Return the import driver | |
* @return ImporterDriverInterface | |
*/ | |
public function getDriver() | |
{ | |
return $this->driver; | |
} | |
/** | |
* Detect import type | |
* @return void | |
*/ | |
private function detectImportType() | |
{ | |
if(Str::contains($this->getImportHostname(), $this->domains['youtube'])) | |
$this->type = 'Youtube'; | |
elseif(Str::contains($this->getImportHostname(), $this->domains['vimeo'])) | |
$this->type = 'Vimeo'; | |
else | |
$this->type = 'Image'; | |
} | |
/** | |
* Parse the import URL's hostname | |
* @return string Hostname of URL | |
*/ | |
private function getImportHostname() | |
{ | |
return parse_url($this->url, PHP_URL_HOST); | |
} | |
/** | |
* Fetch the appropriate import driver | |
* @return void | |
*/ | |
private function fetchDriver() | |
{ | |
$driver = 'Flight\Importers\Drivers\\'.$this->type; | |
$this->driver = new $driver($this->getGraphObjects(), $this->flight); | |
} | |
} |
This file contains 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 namespace Flight\Importers\Drivers; | |
use Flight\Flight; | |
interface ImporterDriverInterface { | |
/** | |
* Setup the driver | |
* @param array $graphObjects | |
*/ | |
public function __construct(array $graphObjects, Flight $flight); | |
/** | |
* Preform any necessary actions, then create the asset model | |
* @return Flight\Asset | |
*/ | |
public function doImport(); | |
} |
This file contains 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 namespace Flight\Importers\Drivers; | |
use File; | |
use Flight\Asset; | |
use Flight\Flight; | |
class Vimeo { | |
/** | |
* Open Graph Objects | |
* @var array | |
*/ | |
protected $graphObjects; | |
/** | |
* Flight to import to | |
* @var Flight\Flight | |
*/ | |
protected $flight; | |
/** | |
* Setup the driver | |
* @param array $graphObjects | |
*/ | |
public function __construct(array $graphObjects, Flight $flight) | |
{ | |
$this->graphObjects = $graphObjects; | |
$this->flight = $flight; | |
} | |
/** | |
* Create the asset model | |
* @return Asset | |
*/ | |
public function doImport() | |
{ | |
if($this->getVideoId() != '') { | |
// Delete any existing asset files | |
if($this->flight->asset) | |
File::delete(public_path().$this->flight->asset->file); | |
$asset = $this->flight->asset ?: new Asset; | |
$asset->fill([ | |
'type' => 'vimeo', | |
'file' => $this->getVideoId(), | |
]); | |
$this->flight->asset()->save($asset); | |
return $asset; | |
} | |
} | |
/** | |
* Get the video URL | |
* @return string | |
*/ | |
private function getVideoUrl() | |
{ | |
return $this->graphObjects['og:url']; | |
} | |
/** | |
* Get the video ID | |
* @return string | |
* @throws Exception If cannot determine the YouTube Video ID | |
*/ | |
private function getVideoId() | |
{ | |
return substr(parse_url($this->getVideoUrl(), PHP_URL_PATH), 1); | |
} | |
} |
This file contains 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 namespace Flight\Importers\Drivers; | |
use File; | |
use Flight\Asset; | |
use Flight\Flight; | |
class Youtube { | |
/** | |
* Open Graph Objects | |
* @var array | |
*/ | |
protected $graphObjects; | |
/** | |
* Flight to import to | |
* @var Flight\Flight | |
*/ | |
protected $flight; | |
/** | |
* Regex for extracting a video URL from a YouTube URL | |
* @var string | |
*/ | |
protected $regex = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i'; | |
/** | |
* Setup the driver | |
* @param array $graphObjects | |
*/ | |
public function __construct(array $graphObjects, Flight $flight) | |
{ | |
$this->graphObjects = $graphObjects; | |
$this->flight = $flight; | |
} | |
/** | |
* Create the asset model | |
* @return Asset | |
*/ | |
public function doImport() | |
{ | |
if($this->getVideoId() != '') { | |
// Delete any existing asset files | |
if($this->flight->asset) | |
File::delete(public_path().$this->flight->asset->file); | |
$asset = $this->flight->asset ?: new Asset; | |
$asset->fill([ | |
'type' => 'youtube', | |
'file' => $this->getVideoId(), | |
]); | |
$this->flight->asset()->save($asset); | |
return $asset; | |
} | |
} | |
/** | |
* Get the video URL | |
* @return string | |
*/ | |
private function getVideoUrl() | |
{ | |
return $this->graphObjects['og:video'][0]['og:video:url']; | |
} | |
/** | |
* Get the video ID | |
* @return string | |
* @throws Exception If cannot determine the YouTube Video ID | |
*/ | |
private function getVideoId() | |
{ | |
if (preg_match($this->regex, $this->getVideoUrl(), $match)) { | |
return $match[1]; | |
} | |
throw new Exception("Cannot determine the YouTube Video ID"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: