|
|
|
@@ -2,32 +2,87 @@ |
|
|
|
|
|
|
|
namespace App\Files; |
|
|
|
|
|
|
|
use App\Jobs\File\DeleteFile; |
|
|
|
use App\Util\DateUtil; |
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
|
|
|
use Illuminate\Support\Carbon; |
|
|
|
use Illuminate\Support\Facades\Storage; |
|
|
|
use Illuminate\Support\Str; |
|
|
|
|
|
|
|
class TmpFile |
|
|
|
{ |
|
|
|
protected const DIR = "/tmp"; |
|
|
|
final protected const BASE_DIR = "/tmp"; |
|
|
|
|
|
|
|
protected const DIR = []; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param string $id |
|
|
|
* @return static |
|
|
|
* @throws FileNotFoundException |
|
|
|
*/ |
|
|
|
static public function loadFile(string $id, ...$other): static |
|
|
|
{ |
|
|
|
$file = new static($id, $other); |
|
|
|
|
|
|
|
if (!$file->exists()) { |
|
|
|
throw new FileNotFoundException("ファイルが存在しません:" . $file->getFullPath()); |
|
|
|
} |
|
|
|
return $file; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected string $uuid; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct() |
|
|
|
public function __construct(?string $id = null) |
|
|
|
{ |
|
|
|
$this->uuid = Str::uuid(); |
|
|
|
if ($id === null) { |
|
|
|
$this->uuid = Str::uuid(); |
|
|
|
} else { |
|
|
|
$this->uuid = $id; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function __destruct() |
|
|
|
{ |
|
|
|
// ファイルダウンロード前に削除されては困るので処理をコメントアウトしている |
|
|
|
// if (Storage::exists($this->getPath())) { |
|
|
|
// Storage::delete($this->getPath()); |
|
|
|
// } |
|
|
|
// 消し忘れ防止のため、削除を予約しておく |
|
|
|
if ($this->exists()) { |
|
|
|
$lifeTimeMin = config("filesystems.tmpFile.lifetime", 60); |
|
|
|
$this->delete(DateUtil::now()->addMinutes($lifeTimeMin)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected function getFileTypeName() |
|
|
|
{ |
|
|
|
return "tmp"; |
|
|
|
} |
|
|
|
|
|
|
|
protected function getFileExtension(): string |
|
|
|
{ |
|
|
|
return "tmp"; |
|
|
|
} |
|
|
|
|
|
|
|
final protected function getFileName(): string |
|
|
|
{ |
|
|
|
return sprintf("%s_%s.%s", $this->getFileTypeName(), $this->uuid, $this->getFileExtension()); |
|
|
|
} |
|
|
|
|
|
|
|
public function getId(): string |
|
|
|
{ |
|
|
|
return $this->uuid; |
|
|
|
} |
|
|
|
|
|
|
|
public function getPath() |
|
|
|
{ |
|
|
|
return static::DIR . "/" . $this->uuid; |
|
|
|
return implode( |
|
|
|
"/", |
|
|
|
[ |
|
|
|
self::BASE_DIR, |
|
|
|
...static::DIR |
|
|
|
] |
|
|
|
) . "/" . $this->getFileName(); |
|
|
|
} |
|
|
|
|
|
|
|
public function getFullPath() |
|
|
|
@@ -40,6 +95,11 @@ class TmpFile |
|
|
|
Storage::put($this->getPath(), $content); |
|
|
|
} |
|
|
|
|
|
|
|
public function get() |
|
|
|
{ |
|
|
|
return Storage::get($this->getPath()); |
|
|
|
} |
|
|
|
|
|
|
|
public function append(string $content) |
|
|
|
{ |
|
|
|
Storage::append($this->getPath(), $content); |
|
|
|
@@ -49,4 +109,22 @@ class TmpFile |
|
|
|
{ |
|
|
|
return response()->download($this->getFullPath(), $name)->deleteFileAfterSend(); |
|
|
|
} |
|
|
|
|
|
|
|
public function exists() |
|
|
|
{ |
|
|
|
return Storage::exists($this->getPath()); |
|
|
|
} |
|
|
|
|
|
|
|
public function delete(?Carbon $delay = null): void |
|
|
|
{ |
|
|
|
if ($delay === null) { |
|
|
|
$ret = Storage::delete($this->getPath()); |
|
|
|
if ($ret) info(sprintf("ファイル削除:%s ", $this->getFullPath())); |
|
|
|
return; |
|
|
|
} else { |
|
|
|
DeleteFile::dispatch($this) |
|
|
|
->delay($delay); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |