2022-03-12 12:57:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\common\model;
|
|
|
|
|
|
|
|
use think\Model;
|
2022-03-14 13:46:51 +00:00
|
|
|
use app\admin\model\Admin;
|
2022-03-12 12:57:36 +00:00
|
|
|
|
2022-04-23 09:57:29 +00:00
|
|
|
/**
|
|
|
|
* Attachment模型
|
|
|
|
* @controllerUrl 'routineAttachment'
|
|
|
|
*/
|
2022-03-12 12:57:36 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-12 12:57:36 +00:00
|
|
|
protected static function onBeforeInsert($model)
|
|
|
|
{
|
|
|
|
$repeat = $model->where([
|
|
|
|
['sha1', '=', $model->sha1],
|
|
|
|
['topic', '=', $model->topic],
|
|
|
|
['storage', '=', $model->storage],
|
|
|
|
])->find();
|
|
|
|
if ($repeat) {
|
2022-09-14 07:38:07 +00:00
|
|
|
$storageFile = path_transform(public_path() . ltrim($repeat['url'], '/'));
|
|
|
|
if (!file_exists($storageFile)) {
|
2022-09-14 07:32:12 +00:00
|
|
|
$repeat->delete();
|
|
|
|
return true;
|
2022-09-14 07:38:07 +00:00
|
|
|
} else {
|
2022-09-14 07:32:12 +00:00
|
|
|
$repeat->quote++;
|
|
|
|
$repeat->lastuploadtime = time();
|
|
|
|
$repeat->save();
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-12 12:57:36 +00:00
|
|
|
}
|
2022-08-23 16:27:21 +00:00
|
|
|
return true;
|
2022-03-12 12:57:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 16:27:21 +00:00
|
|
|
protected static function onAfterInsert($model)
|
2022-03-12 12:57:36 +00:00
|
|
|
{
|
2022-08-23 16:27:21 +00:00
|
|
|
if (!$model->lastuploadtime) {
|
|
|
|
$model->quote = 1;
|
|
|
|
$model->lastuploadtime = time();
|
|
|
|
$model->save();
|
2022-03-12 12:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-14 13:46:51 +00:00
|
|
|
|
|
|
|
public function admin()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Admin::class);
|
|
|
|
}
|
2022-06-26 12:11:50 +00:00
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
2022-03-12 12:57:36 +00:00
|
|
|
}
|