134 lines
No EOL
3.6 KiB
PHP
134 lines
No EOL
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use Response;
|
|
use Storage;
|
|
use Util;
|
|
use Validator;
|
|
|
|
class FileRepository extends BaseRepository {
|
|
|
|
|
|
protected $rules;
|
|
protected $messages;
|
|
|
|
protected $disk;
|
|
protected $dir;
|
|
|
|
protected $filename;
|
|
protected $allowed_filename;
|
|
protected $originalName;
|
|
protected $extension;
|
|
protected $mine;
|
|
protected $size;
|
|
|
|
protected $redirect;
|
|
|
|
|
|
public function __construct(){
|
|
|
|
$this->rules = [
|
|
'file' => 'required|mimes:pdf,jpeg,png|max:32768'
|
|
];
|
|
$this->messages = [
|
|
'file.mimes' => 'Datei ist kein PDF/JPG/PNG Format',
|
|
'file.required' => 'PDF/JPG/PNG-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'];
|
|
|
|
$this->originalName = $file->getClientOriginalName();
|
|
$this->extension = strtolower($file->getClientOriginalExtension());
|
|
$this->mine = $file->getClientMimeType();
|
|
$this->size = $file->getSize();
|
|
|
|
$this->makeFilename();
|
|
//$dir = $this->model->getInvoiceStorageAttDir();
|
|
|
|
$this->store(file_get_contents($file->getRealPath()));
|
|
$this->save();
|
|
return $this->response();
|
|
|
|
}
|
|
|
|
public function storeFile( $content )
|
|
{
|
|
$this->makeFilename();
|
|
$this->store($content);
|
|
$this->size = Storage::disk($this->disk)->size($this->dir.$this->allowed_filename);
|
|
$this->save();
|
|
return $this->response();
|
|
}
|
|
|
|
public function storeReturnFile( $content )
|
|
{
|
|
$this->makeFilename();
|
|
$this->store($content);
|
|
$this->size = Storage::disk($this->disk)->size($this->dir.$this->allowed_filename);
|
|
$this->save();
|
|
return $this->returnFile();
|
|
}
|
|
|
|
public function store($content){
|
|
if(!Storage::disk($this->disk)->exists( $this->dir )){
|
|
Storage::disk($this->disk)->makeDirectory($this->dir); //creates directory
|
|
}
|
|
Storage::disk($this->disk)->put($this->dir.$this->allowed_filename, $content);
|
|
}
|
|
|
|
public function save(){
|
|
|
|
}
|
|
|
|
public function returnFile(){
|
|
|
|
}
|
|
|
|
|
|
public function response(){
|
|
return Response::json([
|
|
'error' => false,
|
|
'filename' => $this->allowed_filename,
|
|
'originalName' => $this->originalName,
|
|
'file_data' => $this->extension,
|
|
'mine' => $this->mine,
|
|
'size' => $this->size,
|
|
'redirect' => $this->redirect,
|
|
'code' => 200
|
|
], 200);
|
|
}
|
|
|
|
public function delete(){
|
|
if($this->model){
|
|
if(Storage::disk($this->disk)->exists( $this->model->dir.$this->model->filename )){
|
|
Storage::disk($this->disk)->delete($this->model->dir.$this->model->filename);
|
|
}
|
|
$this->model->delete();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function makeFilename(){
|
|
$originalNameWithoutExt = substr($this->originalName, 0, strlen($this->originalName) - strlen($this->extension) - 1);
|
|
$this->filename = Util::sanitize($originalNameWithoutExt, true, false, true);
|
|
$this->allowed_filename = uniqid("", true) . '_' . $this->filename.".".$this->extension;
|
|
}
|
|
} |