c3lf-system-3/app/File.php

53 lines
1.1 KiB
PHP

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use TheSeer\Tokenizer\Exception;
class File extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'hash', 'iid'
];
protected $primaryKey = 'hash';
public $incrementing = false;
protected $keyType = 'string';
/**
* The attributes excluded from the model's JSON form.
*
* @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);
}
}