Migrate logos to use Laravel Filesystem

This commit is contained in:
Joshua Dwire 2016-03-22 16:19:55 -04:00
parent 9fbee962de
commit 5640c74f35
13 changed files with 143 additions and 55 deletions

View File

@ -9,6 +9,7 @@ use Cache;
use App; use App;
use File; use File;
use App\Events\UserSettingsChanged; use App\Events\UserSettingsChanged;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
@ -384,26 +385,64 @@ class Account extends Eloquent
public function hasLogo() 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() public function getLogoRaw(){
{ if(!$this->hasLogo()){
$fileName = 'logo/' . $this->account_key; return null;
}
return file_exists($fileName.'.png') ? $fileName.'.png' : $fileName.'.jpg';
$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() 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) public function getToken($name)
@ -419,24 +458,20 @@ class Account extends Eloquent
public function getLogoWidth() public function getLogoWidth()
{ {
$path = $this->getLogoFullPath(); if(!$this->hasLogo()){
if (!file_exists($path)) { return null;
return 0;
} }
list($width, $height) = getimagesize($path);
return $width; return $this->logo_width;
} }
public function getLogoHeight() public function getLogoHeight()
{ {
$path = $this->getLogoFullPath(); if(!$this->hasLogo()){
if (!file_exists($path)) { return null;
return 0;
} }
list($width, $height) = getimagesize($path);
return $height; return $this->logo_height;
} }
public function createInvoice($entityType = ENTITY_INVOICE, $clientId = null) public function createInvoice($entityType = ENTITY_INVOICE, $clientId = null)
@ -815,12 +850,11 @@ class Account extends Eloquent
public function getLogoSize() public function getLogoSize()
{ {
if (!$this->hasLogo()) { if(!$this->hasLogo()){
return 0; return null;
} }
$filename = $this->getLogoFullPath(); return round($this->logo_size / 1000);
return round(File::size($filename) / 1000);
} }
public function isLogoTooLarge() public function isLogoTooLarge()

View File

@ -475,7 +475,7 @@ class AccountRepository
$item->account_id = $user->account->id; $item->account_id = $user->account->id;
$item->account_name = $user->account->getDisplayName(); $item->account_name = $user->account->getDisplayName();
$item->pro_plan_paid = $user->account->pro_plan_paid; $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; $data[] = $item;
} }

View File

@ -22,8 +22,15 @@ class AppServiceProvider extends ServiceProvider {
*/ */
public function boot() public function boot()
{ {
Form::macro('image_data', function($imagePath) { Form::macro('image_data', function($image, $contents = false) {
return 'data:image/jpeg;base64,' . base64_encode(file_get_contents($imagePath)); 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 = '') { Form::macro('nav_link', function($url, $text, $url2 = '', $extra = '') {

View File

@ -47,23 +47,33 @@ return [
'driver' => 'local', 'driver' => 'local',
'root' => storage_path().'/app', 'root' => storage_path().'/app',
], ],
'logos' => [
'driver' => 'local',
'root' => env('LOGO_PATH', public_path().'/logo'),
],
'documents' => [
'driver' => 'local',
'root' => storage_path().'/documents',
],
's3' => [ 's3' => [
'driver' => 's3', 'driver' => 's3',
'key' => 'your-key', 'key' => env('S3_KEY', ''),
'secret' => 'your-secret', 'secret' => env('S3_SECRET', ''),
'region' => 'your-region', 'region' => env('S3_REGION', ''),
'bucket' => 'your-bucket', 'bucket' => env('S3_BUCKET', ''),
], ],
'rackspace' => [ 'rackspace' => [
'driver' => 'rackspace', 'driver' => 'rackspace',
'username' => 'your-username', 'username' => env('RACKSPACE_USERNAME', ''),
'key' => 'your-key', 'key' => env('RACKSPACE_KEY', ''),
'container' => 'your-container', 'container' => env('RACKSPACE_CONTAINER', ''),
'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/', 'endpoint' => env('RACKSPACE_ENDPOINT', 'https://identity.api.rackspacecloud.com/v2.0/'),
'region' => 'IAD', 'region' => env('RACKSPACE_REGION', 'https://identity.api.rackspacecloud.com/v2.0/'),
'url_type' => 'publicURL' 'url_type' => env('RACKSPACE_URL_TYPE', 'publicURL')
], ],
], ],

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use DB;
class AddDocuments extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
/*Schema::table('accounts', function($table) {
$table->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');
});
}
}

View File

@ -51,8 +51,8 @@
<div class="form-group"> <div class="form-group">
<div class="col-lg-4 col-sm-4"></div> <div class="col-lg-4 col-sm-4"></div>
<div class="col-lg-8 col-sm-8"> <div class="col-lg-8 col-sm-8">
<a href="/{{ $account->getLogoPath().'?no_cache='.time() }}" target="_blank"> <a href="{{ $account->getLogoUrl().'?no_cache='.time() }}" target="_blank">
{!! HTML::image($account->getLogoPath().'?no_cache='.time(), 'Logo', ['max-width' => 200]) !!} {!! HTML::image($account->getLogoUrl().'?no_cache='.time(), 'Logo', ['max-width' => 200]) !!}
</a> &nbsp; </a> &nbsp;
<a href="#" onclick="deleteLogo()">{{ trans('texts.remove_logo') }}</a> <a href="#" onclick="deleteLogo()">{{ trans('texts.remove_logo') }}</a>
</div> </div>

View File

@ -3,7 +3,7 @@
<a href="{{ $account->website }}" style="color: #19BB40; text-decoration: underline;"> <a href="{{ $account->website }}" style="color: #19BB40; text-decoration: underline;">
@endif @endif
<img src="{{ $message->embed($account->getLogoFullPath()) }}" style="max-height:50px; max-width:140px; margin-left: 33px;" /> <img src="{{ $message->embed($account->getLogoURL()) }}" style="max-height:50px; max-width:140px; margin-left: 33px;" />
@if ($account->website) @if ($account->website)
</a> </a>

View File

@ -448,7 +448,7 @@
'user_id' => $item->user_id, 'user_id' => $item->user_id,
'account_name' => $item->account_name, 'account_name' => $item->account_name,
'user_name' => $item->user_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, 'selected' => true,
]) ])
@endif @endif
@ -460,7 +460,7 @@
'user_id' => $item->user_id, 'user_id' => $item->user_id,
'account_name' => $item->account_name, 'account_name' => $item->account_name,
'user_name' => $item->user_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, 'selected' => false,
]) ])
@endif @endif
@ -469,7 +469,7 @@
@include('user_account', [ @include('user_account', [
'account_name' => Auth::user()->account->name ?: trans('texts.untitled'), 'account_name' => Auth::user()->account->name ?: trans('texts.untitled'),
'user_name' => Auth::user()->getDisplayName(), 'user_name' => Auth::user()->getDisplayName(),
'logo_path' => Auth::user()->account->getLogoPath(), 'logo_url' => Auth::user()->account->getLogoURL(),
'selected' => true, 'selected' => true,
]) ])
@endif @endif

View File

@ -95,7 +95,7 @@
<div class="row"> <div class="row">
<div class="col-md-3 logo"> <div class="col-md-3 logo">
@if ($account->hasLogo()) @if ($account->hasLogo())
{!! HTML::image($account->getLogoPath()) !!} {!! HTML::image($account->getLogoURL()) !!}
@else @else
<h2>{{ $account->name}}</h2> <h2>{{ $account->name}}</h2>
@endif @endif

View File

@ -971,7 +971,7 @@
@endif @endif
@if ($account->hasLogo()) @if ($account->hasLogo())
invoice.image = "{{ Form::image_data($account->getLogoPath()) }}"; invoice.image = "{{ Form::image_data($account->getLogoRaw(), true) }}";
invoice.imageWidth = {{ $account->getLogoWidth() }}; invoice.imageWidth = {{ $account->getLogoWidth() }};
invoice.imageHeight = {{ $account->getLogoHeight() }}; invoice.imageHeight = {{ $account->getLogoHeight() }};
@endif @endif

View File

@ -75,7 +75,7 @@
logoImages.imageLogoHeight3 = 81/2; logoImages.imageLogoHeight3 = 81/2;
@if ($account->hasLogo()) @if ($account->hasLogo())
window.accountLogo = "{{ Form::image_data($account->getLogoPath()) }}"; window.accountLogo = "{{ Form::image_data($account->getLogoRaw(), true) }}";
if (window.invoice) { if (window.invoice) {
invoice.image = window.accountLogo; invoice.image = window.accountLogo;
invoice.imageWidth = {{ $account->getLogoWidth() }}; invoice.imageWidth = {{ $account->getLogoWidth() }};

View File

@ -9,9 +9,9 @@
<a href="{{ URL::to("/settings/user_details") }}"> <a href="{{ URL::to("/settings/user_details") }}">
@endif @endif
@if (file_exists($logo_path)) @if (!empty($logo_url))
<div class="pull-left" style="height: 40px; margin-right: 16px;"> <div class="pull-left" style="height: 40px; margin-right: 16px;">
<img style="width: 40px; margin-top:6px" src="{{ asset($logo_path) }}"/> <img style="width: 40px; margin-top:6px" src="{{ asset($logo_url) }}"/>
</div> </div>
@else @else
<div class="pull-left" style="width: 40px; min-height: 40px; margin-right: 16px">&nbsp;</div> <div class="pull-left" style="width: 40px; min-height: 40px; margin-right: 16px">&nbsp;</div>

View File

@ -24,8 +24,8 @@
@foreach (Session::get(SESSION_USER_ACCOUNTS) as $account) @foreach (Session::get(SESSION_USER_ACCOUNTS) as $account)
<tr> <tr>
<td> <td>
@if (isset($account->logo_path)) @if (isset($account->logo_url))
{!! HTML::image($account->logo_path.'?no_cache='.time(), 'Logo', ['width' => 100]) !!} {!! HTML::image($account->logo_url.'?no_cache='.time(), 'Logo', ['width' => 100]) !!}
@endif @endif
</td> </td>
<td> <td>