Commit e5dea9b0 authored by ZeMKI's avatar ZeMKI
Browse files

Study Controller optimization

* Now the token save is a function of the Token model.
parent 94b7b32a
......@@ -221,36 +221,6 @@ class StudyController extends Controller
return $arrayOfFiles;
}
/**
* @param $image
* @param string $studyPath
* @param $name
* @param $base64FirstPart
* @param Study $newStudy
* @param $tokenToSave
* @param $token
*/
public function saveTokenImage($image, string $studyPath, $name, $base64FirstPart, Study $newStudy, $tokenToSave, &$token): void
{
// open file a image resource
Image::make($image)->fit(100, 100)->save($studyPath . $name);
$path = $studyPath . $name;
$encryptedContent = encrypt($base64FirstPart . "," . base64_encode(file_get_contents($path)));
$encryptedName = 'study' . $newStudy->id . '/tokens/' . $name . '.mtoken';
// Store the encrypted Content
Storage::put($encryptedName, $encryptedContent);
File::delete($path);
$token = new Token;
$token->name = $tokenToSave['name'];
$token->image_path = $encryptedName;
$token->author = Auth::user()->id;
$token->save();
$token->studies()->sync($newStudy->id);
}
/**
* @param Study $study
*/
......@@ -275,42 +245,7 @@ class StudyController extends Controller
public function saveTokens(Study $study, Request $request, &$token): void
{
foreach ($request->get('sorting')['tokens'] as $tokenToSave) {
$imageIsPreset = (isset($tokenToSave['ispreset']) && $tokenToSave['ispreset'])
|| (isset($tokenToSave['image_path'])
&& strpos($tokenToSave['image_path'], 'presets') !== false);
if ($imageIsPreset) {
// save only preset path
$token = new Token;
$token->name = $tokenToSave['name'];
$token->image_path = isset($tokenToSave['file']) ? $tokenToSave['file']['dirname'] : $tokenToSave['image_path'];
$token->author = Auth::user()->id;
$token->save();
$token->studies()->sync($study->id);
} else {
// create new token
if (isset($tokenToSave['base64']))
$image = $tokenToSave['base64'];
else
$image = config('utilities.base64logo');
$name = $tokenToSave['name'];
$arr = explode(",", $image, 2);
$base64FirstPart = $arr[0];
$studyPath = storage_path('app/study' . $study->id . '/tokens/');
File::isDirectory($studyPath) or File::makeDirectory($studyPath, 0775, true, true);
$this->saveTokenImage($image, $studyPath, $name, $base64FirstPart, $study, $tokenToSave, $token);
}
$token->save();
Token::store($tokenToSave,$study);
}
}
......
......@@ -2,7 +2,11 @@
namespace App;
use File;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Image;
use Storage;
class Token extends Model
{
......@@ -25,4 +29,74 @@ class Token extends Model
{
return $this->belongsTo(\App\User::class)->withTimestamps();
}
public static function store($tokenToSave,$study)
{
$imageIsPreset = (isset($tokenToSave['ispreset']) && $tokenToSave['ispreset'])
|| (isset($tokenToSave['image_path'])
&& strpos($tokenToSave['image_path'], 'presets') !== false);
if ($imageIsPreset) {
// save only preset path
$token = new Token;
$token->name = $tokenToSave['name'];
$token->image_path = isset($tokenToSave['file']) ? $tokenToSave['file']['dirname'] : $tokenToSave['image_path'];
$token->author = Auth::user()->id;
$token->save();
$token->studies()->sync($study->id);
} else {
// create new token
if (isset($tokenToSave['base64']))
$image = $tokenToSave['base64'];
else
$image = config('utilities.base64logo');
$name = $tokenToSave['name'];
$arr = explode(",", $image, 2);
$base64FirstPart = $arr[0];
$studyPath = storage_path('app/study' . $study->id . '/tokens/');
File::isDirectory($studyPath) or File::makeDirectory($studyPath, 0775, true, true);
Token::saveImage($image, $studyPath, $name, $base64FirstPart, $study, $tokenToSave, $token);
}
$token->save();
}
/**
* @param $image
* @param string $studyPath
* @param $name
* @param $base64FirstPart
* @param Study $newStudy
* @param $tokenToSave
* @param $token
*/
public static function saveImage($image, string $studyPath, $name, $base64FirstPart, Study $newStudy, $tokenToSave, &$token): void
{
// open file a image resource
Image::make($image)->fit(100, 100)->save($studyPath . $name);
$path = $studyPath . $name;
$encryptedContent = encrypt($base64FirstPart . "," . base64_encode(file_get_contents($path)));
$encryptedName = 'study' . $newStudy->id . '/tokens/' . $name . '.mtoken';
// Store the encrypted Content
Storage::put($encryptedName, $encryptedContent);
File::delete($path);
$token = new Token;
$token->name = $tokenToSave['name'];
$token->image_path = $encryptedName;
$token->author = Auth::user()->id;
$token->save();
$token->studies()->sync($newStudy->id);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment