From 03cdb3976b0d024e4a47321da2b4abfc4a7c4c04 Mon Sep 17 00:00:00 2001 From: Vijay Sebastian Date: Fri, 12 May 2017 11:45:21 +0530 Subject: [PATCH] storage updates --- .../Controllers/StorageController.php | 145 +++++++++--------- .../Client/helpdesk/FormController.php | 22 +-- .../helpdesk/Ticket/Ticket_attachments.php | 11 +- 3 files changed, 90 insertions(+), 88 deletions(-) diff --git a/app/FaveoStorage/Controllers/StorageController.php b/app/FaveoStorage/Controllers/StorageController.php index 7c6178377..38a8567ec 100644 --- a/app/FaveoStorage/Controllers/StorageController.php +++ b/app/FaveoStorage/Controllers/StorageController.php @@ -9,8 +9,8 @@ use App\Model\helpdesk\Ticket\Ticket_Thread; use Config; use Storage; -class StorageController extends Controller -{ +class StorageController extends Controller { + protected $default; protected $driver; protected $root; @@ -25,8 +25,7 @@ class StorageController extends Controller protected $rackspace_endpoint; protected $rackspace_url_type; - public function __construct() - { + public function __construct() { $this->default = $this->defaults(); $this->driver = $this->driver(); $this->root = $this->root(); @@ -41,8 +40,7 @@ class StorageController extends Controller $this->rackspace_username = $this->rackspaceUsername(); } - protected function settings($option) - { + protected function settings($option) { $settings = new CommonSettings(); $setting = $settings->getOptionValue('storage', $option); $value = ''; @@ -53,8 +51,7 @@ class StorageController extends Controller return $value; } - public function defaults() - { + public function defaults() { $default = 'local'; if ($this->settings('default')) { $default = $this->settings('default'); @@ -63,13 +60,11 @@ class StorageController extends Controller return $default; } - public function driver() - { + public function driver() { return $this->settings('default'); } - public function root() - { + public function root() { $root = storage_path('app'); if ($this->settings('root')) { $root = $this->settings('root'); @@ -78,58 +73,47 @@ class StorageController extends Controller return $root; } - public function s3Key() - { + public function s3Key() { return $this->settings('s3_key'); } - public function s3Region() - { + public function s3Region() { return $this->settings('s3_region'); } - public function s3Secret() - { + public function s3Secret() { return $this->settings('s3_secret'); } - public function s3Bucket() - { + public function s3Bucket() { return $this->settings('s3_bucket'); } - public function rackspaceKey() - { + public function rackspaceKey() { return $this->settings('root'); } - public function rackspaceRegion() - { + public function rackspaceRegion() { return $this->settings('rackspace_region'); } - public function rackspaceUsername() - { + public function rackspaceUsername() { return $this->settings('rackspace_username'); } - public function rackspaceContainer() - { + public function rackspaceContainer() { return $this->settings('rackspace_container'); } - public function rackspaceEndpoint() - { + public function rackspaceEndpoint() { return $this->settings('rackspace_endpoint'); } - public function rackspaceUrlType() - { + public function rackspaceUrlType() { return $this->settings('rackspace_url_type'); } - protected function setFileSystem() - { + protected function setFileSystem() { $config = $this->config(); //dd($config); foreach ($config as $key => $con) { @@ -144,43 +128,40 @@ class StorageController extends Controller return Config::get('filesystem'); } - protected function config() - { + protected function config() { return [ 'default' => $this->default, - 'cloud' => 's3', - 'disks' => $this->disks(), + 'cloud' => 's3', + 'disks' => $this->disks(), ]; } - protected function disks() - { + protected function disks() { return [ 'local' => [ 'driver' => 'local', - 'root' => $this->root.'/attachments', + 'root' => $this->root . '/attachments', ], 's3' => [ 'driver' => 's3', - 'key' => $this->s3_key, + 'key' => $this->s3_key, 'secret' => $this->s3_secret, 'region' => $this->s3_region, 'bucket' => $this->s3_bucket, ], 'rackspace' => [ - 'driver' => 'rackspace', - 'username' => $this->rackspace_username, - 'key' => $this->rackspace_key, + 'driver' => 'rackspace', + 'username' => $this->rackspace_username, + 'key' => $this->rackspace_key, 'container' => $this->rackspace_container, - 'endpoint' => $this->rackspace_endpoint, - 'region' => $this->rackspace_region, - 'url_type' => $this->rackspace_url_type, + 'endpoint' => $this->rackspace_endpoint, + 'region' => $this->rackspace_region, + 'url_type' => $this->rackspace_url_type, ], ]; } - public function upload($data, $filename, $type, $size, $disposition, $thread_id) - { + public function upload($data, $filename, $type, $size, $disposition, $thread_id) { $upload = new Ticket_attachments(); $upload->thread_id = $thread_id; $upload->name = $filename; @@ -192,6 +173,10 @@ class StorageController extends Controller if ($this->default !== 'database') { $this->setFileSystem(); Storage::disk($this->default)->put($filename, $data); + $storagePath = Storage::disk($this->default)->getDriver()->getAdapter()->getPathPrefix() . $filename; + if (mime(\File::mimeType($storagePath)) != 'image' || mime(\File::extension($storagePath)) != 'image') { + chmod($storagePath, 1204); + } } else { $upload->file = $data; } @@ -200,27 +185,36 @@ class StorageController extends Controller } } - public function saveAttachments($thread_id, $attachments = []) - { - if (is_array($attachments) && count($attachments) > 0) { - foreach ($attachments as $attachment) { - $structure = $attachment->getStructure(); - $disposition = 'ATTACHMENT'; - if (isset($structure->disposition)) { - $disposition = $structure->disposition; + public function saveAttachments($thread_id, $attachments = []) { + $disposition = 'ATTACHMENT'; + foreach ($attachments as $attachment) { + if (is_object($attachment)) { + if (method_exists($attachment, 'getStructure')) { + + $structure = $attachment->getStructure(); + if (isset($structure->disposition)) { + $disposition = $structure->disposition; + } + $filename = rand(1111, 9999) . "_" . $attachment->getFileName(); + $type = $attachment->getMimeType(); + $size = $attachment->getSize(); + $data = $attachment->getData(); + } else { + $filename = rand(1111, 9999) . "_" . $attachment->getClientOriginalName(); + $type = $attachment->getMimeType(); + $size = $attachment->getSize(); + $data = file_get_contents($attachment->getRealPath()); } - $filename = str_random(16).'-'.$attachment->getFileName(); - $type = $attachment->getMimeType(); - $size = $attachment->getSize(); - $data = $attachment->getData(); - $this->upload($data, $filename, $type, $size, $disposition, $thread_id); - $this->updateBody($attachment, $thread_id, $filename); + $filename = $this->upload($data, $filename, $type, $size, $disposition, $thread_id); + $thread = $this->updateBody($attachment, $thread_id, $filename); } } + return $thread; } - public function updateBody($attachment, $thread_id, $filename) - { + public function updateBody($attachment, $thread_id, $filename) { + $threads = new Ticket_Thread(); + $thread = $threads->find($thread_id); $structure = $attachment->getStructure(); $disposition = 'ATTACHMENT'; if (isset($structure->disposition)) { @@ -228,23 +222,22 @@ class StorageController extends Controller } if ($disposition == 'INLINE' || $disposition == 'inline') { $id = str_replace('>', '', str_replace('<', '', $structure->id)); - $threads = new Ticket_Thread(); - $thread = $threads->find($thread_id); $body = $thread->body; - $body = str_replace('cid:'.$id, $filename, $body); + $body = str_replace('cid:' . $id, $filename, $body); $thread->body = $body; $thread->save(); } + return $thread; } - public function getFile($drive, $name) - { - //dd($drive,$name); - if ($drive != 'database') { - $this->setFileSystem(); - if (Storage::disk($this->default)->exists($name)) { - return Storage::disk($this->default)->get($name); + public function getFile($drive, $name, $root) { + if ($drive != "database") { + $root = $root . "/" . $name; + if (\File::exists($root)) { + chmod($root, 0755); + return \File::get($root); } } } + } diff --git a/app/Http/Controllers/Client/helpdesk/FormController.php b/app/Http/Controllers/Client/helpdesk/FormController.php index 2f95ee986..4b48a341d 100644 --- a/app/Http/Controllers/Client/helpdesk/FormController.php +++ b/app/Http/Controllers/Client/helpdesk/FormController.php @@ -229,16 +229,18 @@ class FormController extends Controller $ticketId = Tickets::where('ticket_number', '=', $result[0])->first(); $thread = Ticket_Thread::where('ticket_id', '=', $ticketId->id)->first(); if ($attachments != null) { - foreach ($attachments as $attachment) { - if ($attachment != null) { - $name = $attachment->getClientOriginalName(); - $type = $attachment->getClientOriginalExtension(); - $size = $attachment->getSize(); - $data = file_get_contents($attachment->getRealPath()); - $attachPath = $attachment->getRealPath(); - $ta->create(['thread_id' => $thread->id, 'name' => $name, 'size' => $size, 'type' => $type, 'file' => $data, 'poster' => 'ATTACHMENT']); - } - } + $storage = new \App\FaveoStorage\Controllers\StorageController(); + $storage->saveAttachments($thread->id, $attachments); +// foreach ($attachments as $attachment) { +// if ($attachment != null) { +// $name = $attachment->getClientOriginalName(); +// $type = $attachment->getClientOriginalExtension(); +// $size = $attachment->getSize(); +// $data = file_get_contents($attachment->getRealPath()); +// $attachPath = $attachment->getRealPath(); +// $ta->create(['thread_id' => $thread->id, 'name' => $name, 'size' => $size, 'type' => $type, 'file' => $data, 'poster' => 'ATTACHMENT']); +// } +// } } // dd($result); return Redirect::back()->with('success', Lang::get('lang.Ticket-has-been-created-successfully-your-ticket-number-is').' '.$result[0].'. '.Lang::get('lang.Please-save-this-for-future-reference')); diff --git a/app/Model/helpdesk/Ticket/Ticket_attachments.php b/app/Model/helpdesk/Ticket/Ticket_attachments.php index 1172eac02..3f9d72c51 100644 --- a/app/Model/helpdesk/Ticket/Ticket_attachments.php +++ b/app/Model/helpdesk/Ticket/Ticket_attachments.php @@ -30,8 +30,15 @@ class Ticket_attachments extends Model } if ($drive && $drive !== 'database') { $storage = new \App\FaveoStorage\Controllers\StorageController(); - $content = $storage->getFile($drive, $name); - $value = base64_encode($content); + $content = $storage->getFile($drive, $name, $root); + if ($content) { + $value = base64_encode($content); + //dd($content); + if (mime($this->type) != 'image') { + $root = $root . "/" . $name; + chmod($root, 1204); + } + } } return $value;