Browse Source

一時ファイルのライフサイクル処理を整備

develop
sosuke.iwabuchi 2 years ago
parent
commit
ac2ac611f7
3 changed files with 146 additions and 9 deletions
  1. +86
    -8
      app/Files/TmpFile.php
  2. +47
    -0
      app/Jobs/File/DeleteFile.php
  3. +13
    -1
      config/filesystems.php

+ 86
- 8
app/Files/TmpFile.php View File

@@ -2,32 +2,87 @@


namespace App\Files; 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\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;


class TmpFile 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; 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() 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() public function getPath()
{ {
return static::DIR . "/" . $this->uuid;
return implode(
"/",
[
self::BASE_DIR,
...static::DIR
]
) . "/" . $this->getFileName();
} }


public function getFullPath() public function getFullPath()
@@ -40,6 +95,11 @@ class TmpFile
Storage::put($this->getPath(), $content); Storage::put($this->getPath(), $content);
} }


public function get()
{
return Storage::get($this->getPath());
}

public function append(string $content) public function append(string $content)
{ {
Storage::append($this->getPath(), $content); Storage::append($this->getPath(), $content);
@@ -49,4 +109,22 @@ class TmpFile
{ {
return response()->download($this->getFullPath(), $name)->deleteFileAfterSend(); 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;
}
}
} }

+ 47
- 0
app/Jobs/File/DeleteFile.php View File

@@ -0,0 +1,47 @@
<?php

namespace App\Jobs\File;

use App\Codes\QueueName;
use App\Files\TmpFile;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;

class DeleteFile implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private string $fileId;
private string $storagePath;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(
private TmpFile $file,
) {
$this->onQueue(QueueName::JOB->value);
$this->fileId = $file->getId();
$this->storagePath = $file->getPath();
logger("FILE削除JOB登録:" . $this->storagePath);
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (Storage::exists($this->storagePath)) {
Storage::delete($this->storagePath);
info(sprintf("ファイル削除:%s ", $this->storagePath));
}
}
}

+ 13
- 1
config/filesystems.php View File

@@ -39,7 +39,7 @@ return [
'public' => [ 'public' => [
'driver' => 'local', 'driver' => 'local',
'root' => storage_path('app/public'), 'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'url' => env('APP_URL') . '/storage',
'visibility' => 'public', 'visibility' => 'public',
'throw' => false, 'throw' => false,
], ],
@@ -73,4 +73,16 @@ return [
public_path('storage') => storage_path('app/public'), public_path('storage') => storage_path('app/public'),
], ],


/*
|--------------------------------------------------------------------------
| 一時ファイル設定
|--------------------------------------------------------------------------
|
|
*/

'tmpFile' => [
'lifetime' => env('TMP_FILE_LIFETIME_MIN', 60), // 分
]

]; ];

Loading…
Cancel
Save