-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from thomaswelton/upload-dir
Upload dir
- Loading branch information
Showing
7 changed files
with
239 additions
and
6 deletions.
There are no files selected for viewing
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
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
110 changes: 110 additions & 0 deletions
110
src/Thomaswelton/LaravelRackspaceOpencloud/Commands/CdnSyncCommand.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php namespace Thomaswelton\LaravelRackspaceOpencloud\Commands; | ||
|
||
use \File; | ||
use \Str; | ||
|
||
use Illuminate\Console\Command; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
class CdnSyncCommand extends Command { | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'cdn:sync'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Upload a file or directory to a CDN'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
*/ | ||
public function fire() | ||
{ | ||
$opencloud = \App::make('open-cloud'); | ||
$container_name = \Config::get('laravel-rackspace-opencloud::container'); | ||
$container = $opencloud->getContainer($container_name); | ||
|
||
// Get directory or file path | ||
$path = base_path() . '/' . $this->argument('path'); | ||
$path_trim = base_path() . '/' . $this->option('trim'); | ||
|
||
$this->info('Syncing to CDN: ' . $path); | ||
|
||
// Exit if not exists | ||
if(!File::isDirectory($path)){ | ||
return $this->error('Path is not a directory'); | ||
} | ||
|
||
$files = File::allFiles($path); | ||
|
||
// Get an md5 of a concatenated md5_file hash of all files | ||
$directoryHash = md5(array_reduce($files, function($hash, $file){ | ||
// Do not include .cdn.json files in the directory hash | ||
if(substr($file, -9) == '.cdn.json'){ | ||
return $hash; | ||
} | ||
|
||
$hash .= md5_file($file); | ||
return $hash; | ||
})); | ||
|
||
$fileCount = count($files); | ||
$this->info('Found ' . $fileCount . ' ' . Str::plural('file', $fileCount)); | ||
|
||
$cdnFile = $opencloud->uploadDir($container_name, $path, $directoryHash, $path_trim); | ||
|
||
$cdnJsonArray = array( | ||
'http' => $container->PublicURL(), | ||
'https' => $container->SSLURI(), | ||
'prefix' => $directoryHash, | ||
'created' => time() | ||
); | ||
|
||
File::put($path . '.cdn.json', json_encode($cdnJsonArray)); | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments() | ||
{ | ||
return array( | ||
array('path', InputArgument::REQUIRED, 'File or directory path'), | ||
); | ||
} | ||
|
||
/** | ||
* Get the console command options. | ||
* | ||
* @return array | ||
*/ | ||
protected function getOptions() | ||
{ | ||
return array( | ||
array('trim', '', InputOption::VALUE_OPTIONAL, 'String to trim from directory when uploading', null), | ||
); | ||
} | ||
|
||
} |
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
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
41 changes: 41 additions & 0 deletions
41
src/Thomaswelton/LaravelRackspaceOpencloud/UrlGenerator.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php namespace Thomaswelton\LaravelRackspaceOpencloud; | ||
|
||
use \File; | ||
use \Request; | ||
use Illuminate\Routing\UrlGenerator as LaravelGenerator; | ||
|
||
class UrlGenerator extends LaravelGenerator{ | ||
|
||
/** | ||
* Generate a URL to an application asset. | ||
* | ||
* @param string $path | ||
* @param bool $secure | ||
* @return string | ||
*/ | ||
public function asset($path, $secure = null) | ||
{ | ||
// Start looking for a CDN json file | ||
$checkDir = dirname(public_path() . '/' . $path); | ||
|
||
// Look up through the directories looking for a | ||
// CDN json file | ||
while($checkDir !== public_path()){ | ||
$cdnJsonPath = $checkDir . '.cdn.json'; | ||
|
||
if(File::isFile($cdnJsonPath)){ | ||
$json = File::get($cdnJsonPath); | ||
$cdnObject = json_decode($json); | ||
|
||
$baseUrl = ($secure || Request::secure()) ? $cdnObject->https : $cdnObject->http; | ||
|
||
return $baseUrl . '/'. $cdnObject->prefix . '/' . $path; | ||
} | ||
|
||
$checkDir = dirname($checkDir); | ||
} | ||
|
||
return parent::asset($path, $secure); | ||
} | ||
|
||
} |
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