improved uploads

This commit is contained in:
denisdulici
2017-09-28 18:10:13 +03:00
parent e5e703f9fd
commit 80c278ab12
11 changed files with 107 additions and 15 deletions

View File

@@ -106,7 +106,7 @@ class Users extends Controller
// Upload picture
$picture = $request->file('picture');
if ($picture && $picture->isValid()) {
$request['picture'] = $picture->store('uploads/users');
$request['picture'] = $picture->store('users');
}
// Do not reset password if not entered/changed

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Http\Controllers\Common;
use App\Http\Controllers\Controller;
use Storage;
class Uploads extends Controller
{
/**
* Show the specified resource.
*
* @param $folder
* @param $file
* @return boolean|Response
*/
public function show($folder, $file)
{
// Get file path
if (!$path = $this->getPath($folder, $file)) {
return false;
}
return response()->file($path);
}
/**
* Download the specified resource.
*
* @param $folder
* @param $file
* @return boolean|Response
*/
public function download($folder, $file)
{
// Get file path
if (!$path = $this->getPath($folder, $file)) {
return false;
}
return response()->download($path);
}
/**
* Get the full path of resource.
*
* @param $folder
* @param $file
* @return boolean|string
*/
protected function getPath($folder, $file)
{
// Add company id
if ($folder != 'users') {
$folder = session('company_id') . '/' . $folder;
}
$path = $folder . '/' . $file;
if (!Storage::exists($path)) {
return false;
}
$full_path = Storage::path($path);
return $full_path;
}
}

View File

@@ -79,24 +79,21 @@ class User extends Authenticatable
*/
public function getPictureAttribute($value)
{
$pic = '';
if (is_file(base_path($value))) {
$pic = $value;
} elseif (setting('general.use_gravatar', '0') == '1') {
// Check if we should use gravatar
if (setting('general.use_gravatar', '0') == '1') {
// Check for gravatar
$url = 'https://www.gravatar.com/avatar/' . md5(strtolower($this->getAttribute('email'))).'?size=90&d=404';
$client = new \GuzzleHttp\Client(['verify' => false]);
try {
$pic = $client->request('GET', $url)->getBody()->getContents();
$value = $client->request('GET', $url)->getBody()->getContents();
} catch (RequestException $e) {
// 404 Not Found
}
}
return $pic;
return $value;
}
/**

View File

@@ -19,7 +19,11 @@ trait Uploads
$file_name = $file->getClientOriginalName();
$path = 'storage/app/' . $file->storeAs('uploads/' . $company_id . '/' . $folder, $file_name);
// Upload file
$file->storeAs($company_id . '/' . $folder, $file_name);
// Prepare db path
$path = $folder . '/' . $file_name;
return $path;
}