<?php
namespace app\common\library\storage\engine;
use think\Request;
use think\Exception;
abstract class Server
{
protected $file;
protected $error;
protected $fileName;
protected $fileInfo;
protected $isInternal = false;
protected function __construct()
{
}
public function setUploadFile($name)
{
$this->file = Request::instance()->file($name);
if (empty($this->file)) {
throw new Exception('未找到上传文件的信息');
}
$this->fileInfo = $this->file->getInfo();
$this->fileName = $this->buildSaveName();
}
public function setUploadFileByReal($filePath)
{
$this->isInternal = true;
$this->fileInfo = [
'name' => basename($filePath),
'size' => filesize($filePath),
'tmp_name' => $filePath,
'error' => 0,
];
$this->fileName = $this->buildSaveName();
}
abstract protected function upload();
abstract protected function delete($fileName);
abstract public function getFileName();
public function getFileInfo()
{
return $this->fileInfo;
}
protected function getRealPath()
{
return $this->getFileInfo()['tmp_name'];
}
public function getError()
{
return $this->error;
}
private function buildSaveName()
{
$realPath = $this->getRealPath();
$ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
return date('YmdHis') . substr(md5($realPath), 0, 5)
. str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
}
}