storage updates

This commit is contained in:
Vijay Sebastian
2017-05-12 11:45:21 +05:30
parent f06f07c10e
commit 03cdb3976b
3 changed files with 90 additions and 88 deletions

View File

@@ -9,8 +9,8 @@ use App\Model\helpdesk\Ticket\Ticket_Thread;
use Config; use Config;
use Storage; use Storage;
class StorageController extends Controller class StorageController extends Controller {
{
protected $default; protected $default;
protected $driver; protected $driver;
protected $root; protected $root;
@@ -25,8 +25,7 @@ class StorageController extends Controller
protected $rackspace_endpoint; protected $rackspace_endpoint;
protected $rackspace_url_type; protected $rackspace_url_type;
public function __construct() public function __construct() {
{
$this->default = $this->defaults(); $this->default = $this->defaults();
$this->driver = $this->driver(); $this->driver = $this->driver();
$this->root = $this->root(); $this->root = $this->root();
@@ -41,8 +40,7 @@ class StorageController extends Controller
$this->rackspace_username = $this->rackspaceUsername(); $this->rackspace_username = $this->rackspaceUsername();
} }
protected function settings($option) protected function settings($option) {
{
$settings = new CommonSettings(); $settings = new CommonSettings();
$setting = $settings->getOptionValue('storage', $option); $setting = $settings->getOptionValue('storage', $option);
$value = ''; $value = '';
@@ -53,8 +51,7 @@ class StorageController extends Controller
return $value; return $value;
} }
public function defaults() public function defaults() {
{
$default = 'local'; $default = 'local';
if ($this->settings('default')) { if ($this->settings('default')) {
$default = $this->settings('default'); $default = $this->settings('default');
@@ -63,13 +60,11 @@ class StorageController extends Controller
return $default; return $default;
} }
public function driver() public function driver() {
{
return $this->settings('default'); return $this->settings('default');
} }
public function root() public function root() {
{
$root = storage_path('app'); $root = storage_path('app');
if ($this->settings('root')) { if ($this->settings('root')) {
$root = $this->settings('root'); $root = $this->settings('root');
@@ -78,58 +73,47 @@ class StorageController extends Controller
return $root; return $root;
} }
public function s3Key() public function s3Key() {
{
return $this->settings('s3_key'); return $this->settings('s3_key');
} }
public function s3Region() public function s3Region() {
{
return $this->settings('s3_region'); return $this->settings('s3_region');
} }
public function s3Secret() public function s3Secret() {
{
return $this->settings('s3_secret'); return $this->settings('s3_secret');
} }
public function s3Bucket() public function s3Bucket() {
{
return $this->settings('s3_bucket'); return $this->settings('s3_bucket');
} }
public function rackspaceKey() public function rackspaceKey() {
{
return $this->settings('root'); return $this->settings('root');
} }
public function rackspaceRegion() public function rackspaceRegion() {
{
return $this->settings('rackspace_region'); return $this->settings('rackspace_region');
} }
public function rackspaceUsername() public function rackspaceUsername() {
{
return $this->settings('rackspace_username'); return $this->settings('rackspace_username');
} }
public function rackspaceContainer() public function rackspaceContainer() {
{
return $this->settings('rackspace_container'); return $this->settings('rackspace_container');
} }
public function rackspaceEndpoint() public function rackspaceEndpoint() {
{
return $this->settings('rackspace_endpoint'); return $this->settings('rackspace_endpoint');
} }
public function rackspaceUrlType() public function rackspaceUrlType() {
{
return $this->settings('rackspace_url_type'); return $this->settings('rackspace_url_type');
} }
protected function setFileSystem() protected function setFileSystem() {
{
$config = $this->config(); $config = $this->config();
//dd($config); //dd($config);
foreach ($config as $key => $con) { foreach ($config as $key => $con) {
@@ -144,8 +128,7 @@ class StorageController extends Controller
return Config::get('filesystem'); return Config::get('filesystem');
} }
protected function config() protected function config() {
{
return [ return [
'default' => $this->default, 'default' => $this->default,
'cloud' => 's3', 'cloud' => 's3',
@@ -153,12 +136,11 @@ class StorageController extends Controller
]; ];
} }
protected function disks() protected function disks() {
{
return [ return [
'local' => [ 'local' => [
'driver' => 'local', 'driver' => 'local',
'root' => $this->root.'/attachments', 'root' => $this->root . '/attachments',
], ],
's3' => [ 's3' => [
'driver' => 's3', 'driver' => 's3',
@@ -179,8 +161,7 @@ class StorageController extends Controller
]; ];
} }
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 = new Ticket_attachments();
$upload->thread_id = $thread_id; $upload->thread_id = $thread_id;
$upload->name = $filename; $upload->name = $filename;
@@ -192,6 +173,10 @@ class StorageController extends Controller
if ($this->default !== 'database') { if ($this->default !== 'database') {
$this->setFileSystem(); $this->setFileSystem();
Storage::disk($this->default)->put($filename, $data); 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 { } else {
$upload->file = $data; $upload->file = $data;
} }
@@ -200,27 +185,36 @@ class StorageController extends Controller
} }
} }
public function saveAttachments($thread_id, $attachments = []) public function saveAttachments($thread_id, $attachments = []) {
{
if (is_array($attachments) && count($attachments) > 0) {
foreach ($attachments as $attachment) {
$structure = $attachment->getStructure();
$disposition = 'ATTACHMENT'; $disposition = 'ATTACHMENT';
foreach ($attachments as $attachment) {
if (is_object($attachment)) {
if (method_exists($attachment, 'getStructure')) {
$structure = $attachment->getStructure();
if (isset($structure->disposition)) { if (isset($structure->disposition)) {
$disposition = $structure->disposition; $disposition = $structure->disposition;
} }
$filename = str_random(16).'-'.$attachment->getFileName(); $filename = rand(1111, 9999) . "_" . $attachment->getFileName();
$type = $attachment->getMimeType(); $type = $attachment->getMimeType();
$size = $attachment->getSize(); $size = $attachment->getSize();
$data = $attachment->getData(); $data = $attachment->getData();
$this->upload($data, $filename, $type, $size, $disposition, $thread_id); } else {
$this->updateBody($attachment, $thread_id, $filename); $filename = rand(1111, 9999) . "_" . $attachment->getClientOriginalName();
$type = $attachment->getMimeType();
$size = $attachment->getSize();
$data = file_get_contents($attachment->getRealPath());
}
$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(); $structure = $attachment->getStructure();
$disposition = 'ATTACHMENT'; $disposition = 'ATTACHMENT';
if (isset($structure->disposition)) { if (isset($structure->disposition)) {
@@ -228,23 +222,22 @@ class StorageController extends Controller
} }
if ($disposition == 'INLINE' || $disposition == 'inline') { if ($disposition == 'INLINE' || $disposition == 'inline') {
$id = str_replace('>', '', str_replace('<', '', $structure->id)); $id = str_replace('>', '', str_replace('<', '', $structure->id));
$threads = new Ticket_Thread();
$thread = $threads->find($thread_id);
$body = $thread->body; $body = $thread->body;
$body = str_replace('cid:'.$id, $filename, $body); $body = str_replace('cid:' . $id, $filename, $body);
$thread->body = $body; $thread->body = $body;
$thread->save(); $thread->save();
} }
return $thread;
} }
public function getFile($drive, $name) public function getFile($drive, $name, $root) {
{ if ($drive != "database") {
//dd($drive,$name); $root = $root . "/" . $name;
if ($drive != 'database') { if (\File::exists($root)) {
$this->setFileSystem(); chmod($root, 0755);
if (Storage::disk($this->default)->exists($name)) { return \File::get($root);
return Storage::disk($this->default)->get($name);
} }
} }
} }
} }

View File

@@ -229,16 +229,18 @@ class FormController extends Controller
$ticketId = Tickets::where('ticket_number', '=', $result[0])->first(); $ticketId = Tickets::where('ticket_number', '=', $result[0])->first();
$thread = Ticket_Thread::where('ticket_id', '=', $ticketId->id)->first(); $thread = Ticket_Thread::where('ticket_id', '=', $ticketId->id)->first();
if ($attachments != null) { if ($attachments != null) {
foreach ($attachments as $attachment) { $storage = new \App\FaveoStorage\Controllers\StorageController();
if ($attachment != null) { $storage->saveAttachments($thread->id, $attachments);
$name = $attachment->getClientOriginalName(); // foreach ($attachments as $attachment) {
$type = $attachment->getClientOriginalExtension(); // if ($attachment != null) {
$size = $attachment->getSize(); // $name = $attachment->getClientOriginalName();
$data = file_get_contents($attachment->getRealPath()); // $type = $attachment->getClientOriginalExtension();
$attachPath = $attachment->getRealPath(); // $size = $attachment->getSize();
$ta->create(['thread_id' => $thread->id, 'name' => $name, 'size' => $size, 'type' => $type, 'file' => $data, 'poster' => 'ATTACHMENT']); // $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); // 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')); 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'));

View File

@@ -30,8 +30,15 @@ class Ticket_attachments extends Model
} }
if ($drive && $drive !== 'database') { if ($drive && $drive !== 'database') {
$storage = new \App\FaveoStorage\Controllers\StorageController(); $storage = new \App\FaveoStorage\Controllers\StorageController();
$content = $storage->getFile($drive, $name); $content = $storage->getFile($drive, $name, $root);
if ($content) {
$value = base64_encode($content); $value = base64_encode($content);
//dd($content);
if (mime($this->type) != 'image') {
$root = $root . "/" . $name;
chmod($root, 1204);
}
}
} }
return $value; return $value;