Working on Client Uploads

This commit is contained in:
David Bomba 2019-08-08 21:07:26 +10:00
parent 37e8f41bff
commit 9823df51b3
5 changed files with 132 additions and 19 deletions

View File

@ -55,17 +55,15 @@ class DocumentController extends Controller
Storage::makeDirectory('public/' . $contact->client->client_hash, 0755);
$path = Storage::putFile('public/' . $contact->client->client_hash, $request->file('file'));
$url = Storage::url($path);
Log::error($path);
Log::error($url);
$size = $request->file('file')->getSize();
$type = $request->file('file')->getClientOriginalExtension();
$contact = auth()->user();
tap($contact)->update([
'avatar' => $url,
]);
$contact->avatar_size = $size;
$contact->avatar_type = $type;
$contact->avatar = $url;
$contact->save();
/*
[2019-08-07 05:50:23] local.ERROR: array (
@ -82,6 +80,9 @@ class DocumentController extends Controller
)),
)
*/
return response()->json($contact);
}
/**
@ -124,8 +125,22 @@ class DocumentController extends Controller
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
public function destroy()
{
//
$contact = auth()->user();
$file = basename($contact->avatar);
$image_path = 'public/' . $contact->client->client_hash . '/' . $file;
Log::error($image_path);
Storage::delete($image_path);
$contact->avatar = '';
$contact->avatar_type = '';
$contact->avatar_size = '';
$contact->save();
return response()->json($contact);
}
}

View File

@ -55,6 +55,14 @@ class ClientContact extends Authenticatable
protected $hidden = [
'password',
'remember_token',
'user_id',
'company_id',
'client_id',
'google_2fa_secret',
'id',
'oauth_provider_id',
'oauth_user_id',
'token',
];
@ -84,7 +92,7 @@ class ClientContact extends Authenticatable
public function setAvatarAttribute($value)
{
if(!filter_var($value, FILTER_VALIDATE_URL))
if(!filter_var($value, FILTER_VALIDATE_URL) && $value)
$this->attributes['avatar'] = url('/') . $value;
else
$this->attributes['avatar'] = $value;

View File

@ -329,9 +329,8 @@ class CreateUsersTable extends Migration
$table->string('google_2fa_secret')->nullable();
$table->string('accepted_terms_version')->nullable();
$table->string('avatar', 255)->nullable();
$table->unsignedInteger('avatar_width')->nullable();
$table->unsignedInteger('avatar_height')->nullable();
$table->unsignedInteger('avatar_size')->nullable();
$table->string('avatar_type',255)->nullable();
$table->string('avatar_size',255)->nullable();
$table->string('password');
$table->string('token')->nullable();
$table->boolean('is_locked')->default(false);

View File

@ -30,6 +30,9 @@
<script src="/vendors/js/dropzone.min.js"></script>
<script src="/vendors/js/sweetalert.min.js"></script>
<script>
var contact = {!! auth()->user() !!};
Dropzone.autoDiscover = false;
window.countUploadingDocuments = 0;
@ -70,13 +73,39 @@
if (dropzone instanceof Dropzone) {
//dropzone.on('addedfile', handleDocumentAdded);
//dropzone.on('removedfile', handleDocumentRemoved);
//dropzone.on('success', handleDocumentUploaded);
dropzone.on('addedfile', handleDocumentAdded);
dropzone.on('removedfile', handleDocumentRemoved);
dropzone.on('success', handleDocumentUploaded);
//dropzone.on('canceled', handleDocumentCanceled);
dropzone.on('error', handleDocumentError);
var mockFile = {
name: contact.avatar,
size: contact.avatar_size,
type: contact.avatar_type,
status: Dropzone.SUCCESS,
accepted: true,
url: contact.avatar,
mock: true
};
if(contact.avatar) {
dropzone.emit('addedfile', mockFile);
dropzone.emit('complete', mockFile);
}
var documentType = contact.avatar_type;
var previewUrl = contact.avatar;
var documentUrl = contact.avatar;
if (contact && previewUrl) {
dropzone.emit('thumbnail', mockFile, previewUrl);
} else if (documentType == 'jpeg' || documentType == 'png' || documentType == 'svg') {
dropzone.emit('thumbnail', mockFile, documentUrl);
}
dropzone.files.push(mockFile);
}
function handleDocumentError(file, responseText) {
@ -89,5 +118,66 @@
}
}
function handleDocumentAdded(file){
if (contact.avatar) {
file.previewElement.addEventListener("click", function() {
window.open(contact.avatar, '_blank');
});
}
if(file.mock)return;
if (window.addDocument) {
addDocument(file);
}
}
function handleDocumentUploaded(file, response){
contact = response;
dropzone.files = [];
var mockFile = {
name: contact.avatar,
size: contact.avatar_size,
type: contact.avatar_type,
status: Dropzone.SUCCESS,
accepted: true,
url: contact.avatar,
mock: true
};
if(contact.avatar) {
dropzone.emit('complete', mockFile);
}
var documentType = contact.avatar_type;
var previewUrl = contact.avatar;
var documentUrl = contact.avatar;
if (contact && previewUrl) {
dropzone.emit('thumbnail', mockFile, previewUrl);
} else if (documentType == 'jpeg' || documentType == 'png' || documentType == 'svg') {
dropzone.emit('thumbnail', mockFile, documentUrl);
}
dropzone.files.push(mockFile);
}
function handleDocumentRemoved(file){
if (window.deleteDocument) {
deleteDocument(file);
}
$.ajax({
url: '{!! $url !!}',
type: 'DELETE',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
},
success: function(result) {
// Do something with the result
}
});
}
</script>
@endpush
@endpush

View File

@ -20,6 +20,7 @@ Route::group(['middleware' => ['auth:contact'], 'prefix' => 'client', 'as' => 'c
Route::put('profile/{client_contact}/edit', 'ClientPortal\ProfileController@update')->name('profile.update');
Route::post('document', 'ClientPortal\DocumentController@store')->name('document.store');
Route::delete('document', 'ClientPortal\DocumentController@destroy')->name('document.destroy');
Route::get('logout', 'Auth\ContactLoginController@logout')->name('logout');