diff --git a/app/Models/Account.php b/app/Models/Account.php index f6a7194bdf6f..26e1e0f6b7dc 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -9,6 +9,7 @@ use Cache; use App; use File; use App\Events\UserSettingsChanged; +use Illuminate\Support\Facades\Storage; use Illuminate\Database\Eloquent\SoftDeletes; use Laracasts\Presenter\PresentableTrait; @@ -384,26 +385,64 @@ class Account extends Eloquent public function hasLogo() { - return file_exists($this->getLogoFullPath()); + if($this->logo == ''){ + $this->calculateLogoDetails(); + } + + return !empty($this->logo); + } + + public function getLogoDisk(){ + return Storage::disk(env('LOGO_DISK', 'logos')); + } + + protected function calculateLogoDetails(){ + $disk = $this->getLogoDisk(); + + if($disk->exists($this->account_key.'.png')){ + $this->logo = $this->account_key.'.png'; + } else if($disk->exists($this->account_key.'.jpg')) { + $this->logo = $this->account_key.'.jpg'; + } + + if(!empty($this->logo)){ + $image = imagecreatefromstring($disk->get($this->logo)); + $this->logo_width = imagesx($image); + $this->logo_height = imagesy($image); + $this->logo_size = $disk->size($this->logo); + } else { + $this->logo = null; + } + $this->save(); } - public function getLogoPath() - { - $fileName = 'logo/' . $this->account_key; - - return file_exists($fileName.'.png') ? $fileName.'.png' : $fileName.'.jpg'; + public function getLogoRaw(){ + if(!$this->hasLogo()){ + return null; + } + + $disk = $this->getLogoDisk(); + return $disk->get($this->logo); } - - public function getLogoFullPath() - { - $fileName = public_path() . '/logo/' . $this->account_key; - - return file_exists($fileName.'.png') ? $fileName.'.png' : $fileName.'.jpg'; - } - + public function getLogoURL() { - return SITE_URL . '/' . $this->getLogoPath(); + if(!$this->hasLogo()){ + return null; + } + + $disk = $this->getLogoDisk(); + $adapter = $disk->getAdapter(); + + if($adapter instanceof \League\Flysystem\Adapter\Local) { + // Stored locally + $logo_url = str_replace(public_path(), url('/'), $adapter->applyPathPrefix($this->logo), $count); + if($count == 1){ + return str_replace(DIRECTORY_SEPARATOR, '/', $logo_url); + } + } + + return null; } public function getToken($name) @@ -419,24 +458,20 @@ class Account extends Eloquent public function getLogoWidth() { - $path = $this->getLogoFullPath(); - if (!file_exists($path)) { - return 0; + if(!$this->hasLogo()){ + return null; } - list($width, $height) = getimagesize($path); - return $width; + return $this->logo_width; } public function getLogoHeight() { - $path = $this->getLogoFullPath(); - if (!file_exists($path)) { - return 0; + if(!$this->hasLogo()){ + return null; } - list($width, $height) = getimagesize($path); - return $height; + return $this->logo_height; } public function createInvoice($entityType = ENTITY_INVOICE, $clientId = null) @@ -815,12 +850,11 @@ class Account extends Eloquent public function getLogoSize() { - if (!$this->hasLogo()) { - return 0; + if(!$this->hasLogo()){ + return null; } - $filename = $this->getLogoFullPath(); - return round(File::size($filename) / 1000); + return round($this->logo_size / 1000); } public function isLogoTooLarge() diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index 0e08fbba5d06..cd07a37b5db2 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -475,7 +475,7 @@ class AccountRepository $item->account_id = $user->account->id; $item->account_name = $user->account->getDisplayName(); $item->pro_plan_paid = $user->account->pro_plan_paid; - $item->logo_path = $user->account->hasLogo() ? $user->account->getLogoPath() : null; + $item->logo_url = $user->account->hasLogo() ? $user->account->getLogoUrl() : null; $data[] = $item; } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 3cd691098747..c5d597ded259 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -22,8 +22,15 @@ class AppServiceProvider extends ServiceProvider { */ public function boot() { - Form::macro('image_data', function($imagePath) { - return 'data:image/jpeg;base64,' . base64_encode(file_get_contents($imagePath)); + Form::macro('image_data', function($image, $contents = false) { + if(!$contents){ + $contents = file_get_contents($image); + } + else{ + $contents = $image; + } + + return 'data:image/jpeg;base64,' . base64_encode($contents); }); Form::macro('nav_link', function($url, $text, $url2 = '', $extra = '') { diff --git a/config/filesystems.php b/config/filesystems.php index 0221fa70dbe1..d0c683352bd5 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -47,23 +47,33 @@ return [ 'driver' => 'local', 'root' => storage_path().'/app', ], + + 'logos' => [ + 'driver' => 'local', + 'root' => env('LOGO_PATH', public_path().'/logo'), + ], + + 'documents' => [ + 'driver' => 'local', + 'root' => storage_path().'/documents', + ], 's3' => [ 'driver' => 's3', - 'key' => 'your-key', - 'secret' => 'your-secret', - 'region' => 'your-region', - 'bucket' => 'your-bucket', + 'key' => env('S3_KEY', ''), + 'secret' => env('S3_SECRET', ''), + 'region' => env('S3_REGION', ''), + 'bucket' => env('S3_BUCKET', ''), ], 'rackspace' => [ 'driver' => 'rackspace', - 'username' => 'your-username', - 'key' => 'your-key', - 'container' => 'your-container', - 'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/', - 'region' => 'IAD', - 'url_type' => 'publicURL' + 'username' => env('RACKSPACE_USERNAME', ''), + 'key' => env('RACKSPACE_KEY', ''), + 'container' => env('RACKSPACE_CONTAINER', ''), + 'endpoint' => env('RACKSPACE_ENDPOINT', 'https://identity.api.rackspacecloud.com/v2.0/'), + 'region' => env('RACKSPACE_REGION', 'https://identity.api.rackspacecloud.com/v2.0/'), + 'url_type' => env('RACKSPACE_URL_TYPE', 'publicURL') ], ], diff --git a/database/migrations/2016_03_22_168364_add_documents.php b/database/migrations/2016_03_22_168364_add_documents.php new file mode 100644 index 000000000000..97b0927570e0 --- /dev/null +++ b/database/migrations/2016_03_22_168364_add_documents.php @@ -0,0 +1,37 @@ +string('logo')->nullable()->default(null); + $table->unsignedInteger('logo_width'); + $table->unsignedInteger('logo_height'); + $table->unsignedInteger('logo_size'); + });*/ + + DB::table('accounts')->update(array('logo' => '')); + } + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('accounts', function($table) { + $table->dropColumn('logo'); + $table->dropColumn('logo_width'); + $table->dropColumn('logo_height'); + $table->dropColumn('logo_size'); + }); + } +} diff --git a/resources/views/accounts/details.blade.php b/resources/views/accounts/details.blade.php index 56dc8f33e73c..7201ed2f00ea 100644 --- a/resources/views/accounts/details.blade.php +++ b/resources/views/accounts/details.blade.php @@ -51,8 +51,8 @@
- - {!! HTML::image($account->getLogoPath().'?no_cache='.time(), 'Logo', ['max-width' => 200]) !!} + + {!! HTML::image($account->getLogoUrl().'?no_cache='.time(), 'Logo', ['max-width' => 200]) !!}   {{ trans('texts.remove_logo') }}
diff --git a/resources/views/emails/partials/account_logo.blade.php b/resources/views/emails/partials/account_logo.blade.php index cfe158153fe7..d79002d6b938 100644 --- a/resources/views/emails/partials/account_logo.blade.php +++ b/resources/views/emails/partials/account_logo.blade.php @@ -3,7 +3,7 @@ @endif - + @if ($account->website) diff --git a/resources/views/header.blade.php b/resources/views/header.blade.php index 3ce364da6441..1191aac2eee5 100644 --- a/resources/views/header.blade.php +++ b/resources/views/header.blade.php @@ -448,7 +448,7 @@ 'user_id' => $item->user_id, 'account_name' => $item->account_name, 'user_name' => $item->user_name, - 'logo_path' => isset($item->logo_path) ? $item->logo_path : "", + 'logo_url' => isset($item->logo_url) ? $item->logo_url : "", 'selected' => true, ]) @endif @@ -460,7 +460,7 @@ 'user_id' => $item->user_id, 'account_name' => $item->account_name, 'user_name' => $item->user_name, - 'logo_path' => isset($item->logo_path) ? $item->logo_path : "", + 'logo_url' => isset($item->logo_url) ? $item->logo_url : "", 'selected' => false, ]) @endif @@ -469,7 +469,7 @@ @include('user_account', [ 'account_name' => Auth::user()->account->name ?: trans('texts.untitled'), 'user_name' => Auth::user()->getDisplayName(), - 'logo_path' => Auth::user()->account->getLogoPath(), + 'logo_url' => Auth::user()->account->getLogoURL(), 'selected' => true, ]) @endif diff --git a/resources/views/invited/dashboard.blade.php b/resources/views/invited/dashboard.blade.php index a5b698c46e2e..4af2cc53c9d3 100644 --- a/resources/views/invited/dashboard.blade.php +++ b/resources/views/invited/dashboard.blade.php @@ -95,7 +95,7 @@