Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

153 linhas
3.3KB

  1. <?php
  2. namespace App\Files;
  3. use App\Jobs\File\DeleteFile;
  4. use App\Util\DateUtil;
  5. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  6. use Illuminate\Support\Carbon;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Str;
  9. class TmpFile
  10. {
  11. final protected const BASE_DIR = "/tmp";
  12. protected const DIR = [];
  13. /**
  14. * @param string $id
  15. * @return static
  16. * @throws FileNotFoundException
  17. */
  18. static public function loadFile(string $id, ...$other): static
  19. {
  20. $file = new static($id, $other);
  21. if (!$file->exists()) {
  22. throw new FileNotFoundException("ファイルが存在しません:" . $file->getFullPath());
  23. }
  24. return $file;
  25. }
  26. protected string $uuid;
  27. protected string $content;
  28. public function __construct(?string $id = null)
  29. {
  30. if ($id === null) {
  31. $this->uuid = Str::uuid();
  32. } else {
  33. $this->uuid = $id;
  34. }
  35. }
  36. public function __destruct()
  37. {
  38. // 消し忘れ防止のため、削除を予約しておく
  39. if ($this->exists()) {
  40. $lifeTimeMin = config("filesystems.tmpFile.lifetime", 60);
  41. $this->delete(DateUtil::now()->addMinutes($lifeTimeMin));
  42. }
  43. }
  44. protected function getFileTypeName()
  45. {
  46. return "tmp";
  47. }
  48. public function getFileExtension(): string
  49. {
  50. return "tmp";
  51. }
  52. public function getMimeType(): string
  53. {
  54. return "txt/plain";
  55. }
  56. final public function getFileName(): string
  57. {
  58. return sprintf("%s_%s.%s", $this->getFileTypeName(), $this->uuid, $this->getFileExtension());
  59. }
  60. public function getAppFileName()
  61. {
  62. return $this->getFileName();
  63. }
  64. public function getId(): string
  65. {
  66. return $this->uuid;
  67. }
  68. public function getPath()
  69. {
  70. return implode(
  71. "/",
  72. [
  73. self::BASE_DIR,
  74. ...static::DIR
  75. ]
  76. ) . "/" . $this->getFileName();
  77. }
  78. public function getFullPath()
  79. {
  80. return Storage::path($this->getPath());
  81. }
  82. public function put(string $content)
  83. {
  84. Storage::put($this->getPath(), $content);
  85. $this->content = $content;
  86. return $this;
  87. }
  88. public function load()
  89. {
  90. $this->content = Storage::get($this->getPath());
  91. return $this;
  92. }
  93. public function append(string $content)
  94. {
  95. Storage::append($this->getPath(), $content);
  96. $this->content .= $content;
  97. return $this;
  98. }
  99. public function get(): string
  100. {
  101. return $this->content;
  102. }
  103. public function download(string $name = "download")
  104. {
  105. return response()->download($this->getFullPath(), $name)->deleteFileAfterSend();
  106. }
  107. public function exists()
  108. {
  109. return Storage::exists($this->getPath());
  110. }
  111. public function delete(?Carbon $delay = null): void
  112. {
  113. if ($delay === null) {
  114. $ret = Storage::delete($this->getPath());
  115. if ($ret) info(sprintf("ファイル削除:%s ", $this->getFullPath()));
  116. return;
  117. } else {
  118. $this->content = "";
  119. DeleteFile::dispatch($this)
  120. ->delay($delay);
  121. return;
  122. }
  123. }
  124. }