93 lines
No EOL
2.4 KiB
PHP
93 lines
No EOL
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\File;
|
|
use Request;
|
|
use Response;
|
|
use Storage;
|
|
use Util;
|
|
use Validator;
|
|
|
|
|
|
class FileRepository extends BaseRepository {
|
|
|
|
|
|
protected $rules;
|
|
protected $messages;
|
|
|
|
protected $disk;
|
|
protected $dir;
|
|
protected $user_id;
|
|
protected $identifier;
|
|
|
|
public function __construct(File $model){
|
|
|
|
$this->model = $model;
|
|
|
|
$this->rules = [
|
|
'file' => 'required|mimes:pdf,jpeg,png|max:32768'
|
|
];
|
|
$this->messages = [
|
|
'file.mimes' => 'Datei ist kein PDF Format',
|
|
'file.required' => 'PDF-Datei wird benötigt'
|
|
];
|
|
}
|
|
|
|
public function _set($name, $value){
|
|
$this->{$name} = $value;
|
|
}
|
|
|
|
public function uploadFile( $form_data )
|
|
{
|
|
|
|
$validator = Validator::make($form_data, $this->rules, $this->messages);
|
|
|
|
if ($validator->fails()) {
|
|
return Response::json([
|
|
'error' => true,
|
|
'message' => $validator->messages()->first(),
|
|
'code' => 400
|
|
], 400);
|
|
}
|
|
$file = $form_data['file'];
|
|
|
|
$originalName = $file->getClientOriginalName();
|
|
$extension = $file->getClientOriginalExtension();
|
|
$mine = $file->getClientMimeType();
|
|
$size = $file->getSize();
|
|
|
|
|
|
$originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - strlen($extension) - 1);
|
|
$filename = Util::sanitize($originalNameWithoutExt, true, false, true);
|
|
$allowed_filename = uniqid() . '_' . $filename.".".$extension;
|
|
|
|
//$dir = $this->model->getInvoiceStorageAttDir();
|
|
|
|
if(!Storage::disk($this->disk)->exists( $this->dir )){
|
|
Storage::disk($this->disk)->makeDirectory($this->dir); //creates directory
|
|
}
|
|
Storage::disk($this->disk)->put($this->dir.$allowed_filename, file_get_contents($file->getRealPath()));
|
|
|
|
File::create([
|
|
'user_id' => $this->user_id,
|
|
'identifier' => $this->identifier,
|
|
'filename' => $allowed_filename,
|
|
'dir' => $this->dir,
|
|
'original_name' => $originalName,
|
|
'ext' => $extension,
|
|
'mine' => $mine,
|
|
'size' => $size
|
|
]);
|
|
|
|
return Response::json([
|
|
'error' => false,
|
|
'filename' => $allowed_filename,
|
|
'filedata' => 'pdf',
|
|
'redirect' => $form_data['redirect'],
|
|
'code' => 200
|
|
], 200);
|
|
}
|
|
|
|
|
|
} |