add tests for files and refactor

This commit is contained in:
j3d1 2020-01-19 01:07:08 +01:00
parent b0cb0db558
commit 010282a7bb
6 changed files with 112 additions and 49 deletions

View file

@ -3,6 +3,7 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use TheSeer\Tokenizer\Exception;
class File extends Model
{
@ -17,6 +18,8 @@ class File extends Model
];
protected $primaryKey = 'hash';
public $incrementing = false;
protected $keyType = 'string';
/**
* The attributes excluded from the model's JSON form.
@ -24,4 +27,27 @@ class File extends Model
* @var array
*/
protected $hidden = ['created_at','updated_at'];
public static function create(array $attributes = [])
{
if (!isset($attributes['data'])) {
throw new Exception("foo" );
}
$pos = strpos($attributes['data'], ",");
$image = base64_decode(substr($attributes['data'], $pos + 1), true);
if (!$image) {
throw new Exception("foo" );
}
$hash = md5(time());
if (!file_exists('staticimages'))
mkdir('staticimages', 0755, true);
file_put_contents('staticimages/' . $hash, $image);
$attributes['hash'] = $hash;
return static::query()->create($attributes);
}
}

View file

@ -20,17 +20,8 @@ class FileController extends Controller
public function create(Request $request)
{
$File = File::create($request->all());
return response()->json($File, 201);
}
public function update($id, Request $request)
{
$File = File::findOrFail($id);
$File->update($request->all());
return response()->json($File, 200);
$file = File::create($request->only(['data','iid']));
return response()->json($file, 201);
}
public function delete($id)

View file

@ -48,19 +48,11 @@ class ItemController extends Controller
$newitem['eid'] = "".$eid;
$item = Item::create($newitem);
if ($request->get('dataImage')) {
$pos = strpos($request->get('dataImage'), ",");
//$head = substr($request->get('dataImage'), 0, $pos);
$image = base64_decode(substr($request->get('dataImage'), $pos + 1), true);
if ($image) {
$hash = md5(time());
if (!file_exists('staticimages'))
mkdir('staticimages', 0755, true);
file_put_contents('staticimages/' . $hash, $image);
$file = File::create(array('hash' => $hash, 'iid' => $item['iid']));
$item['file'] = $hash;
}
if($request->get('dataImage')) {
$file = File::create(array('data' => $request->get('dataImage'), 'iid' => $item['iid']));
$item['file'] = $file['hash'];
}
return response()->json($item, 201);
}
@ -74,19 +66,11 @@ class ItemController extends Controller
$item->update(['returned_at' => DB::raw( 'current_timestamp' )]);
}
if ($request->get('dataImage')) {
$pos = strpos($request->get('dataImage'), ",");
//$head = substr($request->get('dataImage'), 0, $pos);
$image = base64_decode(substr($request->get('dataImage'), $pos + 1), true);
if ($image) {
$hash = md5(time());
if (!file_exists('staticimages'))
mkdir('staticimages', 0755, true);
file_put_contents('staticimages/' . $hash, $image);
$file = File::create(array('hash' => $hash, 'iid' => $item['iid']));
$item['file'] = $hash;
}
if($request->get('dataImage')) {
$file = File::create(array('data' => $request->get('dataImage'), 'iid' => $item['iid']));
$item['file'] = $file['hash'];
}
return response()->json(Item::find($item['uid']), 200);
}