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);
}
}