buildadmin/app/common/model/Attachment.php

72 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace app\common\model;
use think\Model;
2022-03-14 13:46:51 +00:00
use app\admin\model\Admin;
/**
* Attachment模型
* @controllerUrl 'routineAttachment'
*/
class Attachment extends Model
{
protected $autoWriteTimestamp = 'int';
protected $createTime = 'createtime';
protected $updateTime = null;
2022-03-13 13:23:43 +00:00
protected $append = [
'suffix',
'full_url'
];
public function getSuffixAttr($value, $row)
{
if ($row['name']) {
$suffix = strtolower(pathinfo($row['name'], PATHINFO_EXTENSION));
return $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
}
return 'file';
}
public function getFullUrlAttr($value, $row)
{
2022-08-23 16:27:21 +00:00
return full_url($row['url']);
2022-03-13 13:23:43 +00:00
}
protected static function onBeforeInsert($model)
{
$repeat = $model->where([
['sha1', '=', $model->sha1],
['topic', '=', $model->topic],
['storage', '=', $model->storage],
])->find();
if ($repeat) {
$repeat->quote++;
$repeat->lastuploadtime = time();
$repeat->save();
return false;
}
2022-08-23 16:27:21 +00:00
return true;
}
2022-08-23 16:27:21 +00:00
protected static function onAfterInsert($model)
{
2022-08-23 16:27:21 +00:00
if (!$model->lastuploadtime) {
$model->quote = 1;
$model->lastuploadtime = time();
$model->save();
}
}
2022-03-14 13:46:51 +00:00
public function admin()
{
return $this->belongsTo(Admin::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}