c3lf-system-3/app/File.php

54 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2019-11-28 03:52:53 +00:00
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
2020-01-19 00:07:08 +00:00
use TheSeer\Tokenizer\Exception;
2019-11-28 03:52:53 +00:00
class File extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'hash', 'iid'
];
2019-12-24 12:52:10 +00:00
protected $primaryKey = 'hash';
2020-01-19 00:07:08 +00:00
public $incrementing = false;
protected $keyType = 'string';
2019-12-24 12:52:10 +00:00
2019-11-28 03:52:53 +00:00
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
2019-12-01 17:37:11 +00:00
protected $hidden = ['created_at','updated_at'];
2020-01-19 00:07:08 +00:00
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);
}
2019-11-28 03:52:53 +00:00
}