<?php namespace Phpcmf\ThirdParty\Storage;
class Local {
protected $data;
protected $filename;
protected $filepath;
protected $attachment;
protected $watermark;
protected $fullpath;
protected $fullname;
public function init($attachment, $filename) {
if ($attachment['value']['path'] == 'null') {
$attachment['value']['path'] = '';
$this->filename = $filename;
$this->filepath = dirname($filename);
} else {
$this->filename = trim($filename, DIRECTORY_SEPARATOR);
$this->filepath = dirname($filename);
$this->filepath == '.' && $this->filepath = '';
$attachment['value']['path'] = rtrim($attachment['value']['path'], DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}
$this->attachment = $attachment;
$this->fullpath = $this->attachment['value']['path'].$this->filepath;
$this->fullname = $this->attachment['value']['path'].$this->filename;
return $this;
}
public function upload($type = 0, $data, $watermark) {
$this->data = $data;
$this->watermark = $watermark;
!is_dir($this->fullpath) && dr_mkdirs($this->fullpath);
if (!is_dir($this->fullpath)) {
log_message('error', '目录创建失败:'.$this->fullpath);
return dr_return_data(0, dr_lang('创建目录%s失败', IS_ADMIN ? $this->fullpath : ''));
}
if ($type) {
if (!(move_uploaded_file($this->data, $this->fullname) || !is_file($this->fullname))) {
return dr_return_data(0, dr_lang('文件移动失败'));
}
} else {
$filesize = file_put_contents($this->fullname, $this->data);
if (!$filesize || !is_file($this->fullname)) {
log_message('error', '文件创建失败:'.$this->fullname);
return dr_return_data(0, dr_lang('文件创建失败'));
}
}
if (dr_is_image($this->fullname) && $this->attachment['image_reduce']) {
\Phpcmf\Service::L('image')->reduce($this->fullname, $this->attachment['image_reduce']);
}
if ($this->watermark && ($config = \Phpcmf\Service::C()->get_cache('site', SITE_ID, 'watermark'))) {
$config['source_image'] = $this->fullname;
$config['dynamic_output'] = false;
\Phpcmf\Service::L('image')->watermark($config);
}
return dr_return_data(1, 'ok', [
'url' => $this->attachment['url'].$this->filename,
'md5' => md5_file($this->fullname),
'size' => $filesize,
]);
}
public function delete() {
@unlink($this->fullname);
}
}