You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
namespace App\Service;
use App\Constants\ErrorCode;use League\Flysystem\FilesystemNotFoundException;
class AttachmentService implements AttachmentServiceInterface{
/** * @inheritDoc */ public function formUpload($file, $path, $filesystem, $attachmenttype = 'image') {
$fileRealPath = $file->getRealPath(); $fileHash = md5_file($fileRealPath);
$path = $this->getBasePath($path, $attachmenttype); $fileName = $path . '/' . $fileHash . '.' . $file->getExtension();
if ($filesystem->has($fileName)) { return $fileName; }
$stream = fopen($fileRealPath, 'r+'); $filesystem->writeStream($fileName, $stream); fclose($stream);
return $fileName; }
/** * @inheritDoc */ public function base64Upload($contents, $path, $filesystem) {
preg_match('/^(data:\s*image\/(\w+);base64,)/', $contents, $result); if (empty($result)) { throw new FilesystemNotFoundException(ErrorCode::getMessage(ErrorCode::UPLOAD_INVALID),ErrorCode::UPLOAD_INVALID); }
$contents = base64_decode(str_replace($result[1], '', $contents));
$fileHash = md5($contents); $path = $this->getBasePath($path); $fileName = $path . '/' . $fileHash . '.' . $result[2];
if ($filesystem->has($fileName)) { return $fileName; }
$filesystem->write($fileName, $contents);
return $fileName; }
protected function getBasePath($path, $attachmenttype = 'image') { switch ($attachmenttype) { case 'image': $baseDir = env('IMAGE_BASE', '/attachment/images'); break;
case 'file': $baseDir = env('FILES_BASE', '/attachment/files'); break;
default: $baseDir = env('FILES_BASE', '/attachment'); break; }
$path = $path ? '/'.$path : ''; $path .= '/'.date('Y').'/'.date('m').'/'.date('d'); return $baseDir.$path; }}
|