領収証発行サービス
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.

183 lines
3.7KB

  1. <?php
  2. namespace App\Files;
  3. use App\Middlewares\Now;
  4. use Exception;
  5. use Illuminate\Http\UploadedFile;
  6. use Illuminate\Support\Carbon;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Crypt;
  9. use Illuminate\Support\Facades\Storage;
  10. abstract class BaseFile
  11. {
  12. protected UploadedFile|null $file = null;
  13. private bool $commit = false;
  14. protected Carbon|null $updatedAt = null;
  15. /**
  16. * ディレクトリのパスを取得
  17. *
  18. * @return string
  19. */
  20. abstract public function getDir(): string;
  21. /**
  22. * ファイル名の取得
  23. *
  24. * @return string
  25. */
  26. abstract public function getFilename(): string;
  27. /**
  28. * MIMETYPEの取得
  29. *
  30. * @return string
  31. */
  32. abstract public function getMimetype(): string;
  33. /**
  34. * DBの登録などを定義
  35. *
  36. * @return boolean
  37. */
  38. abstract protected function onUpload(Carbon $timestamp): bool;
  39. /**
  40. * コミットする
  41. *
  42. * @param array<BaseFile>|Collection<BaseFile>|BaseFile $files
  43. * @return void
  44. */
  45. public static function commitAll(array|Collection|BaseFile $files)
  46. {
  47. if (is_array($files) || $files instanceof Collection) {
  48. foreach ($files as $file) {
  49. $file->commit();
  50. }
  51. } else {
  52. $files->commit();
  53. }
  54. }
  55. public function __construct(UploadedFile $file = null)
  56. {
  57. $this->file = $file;
  58. }
  59. /**
  60. * 変更後、コミットしていない場合は削除する
  61. */
  62. public function __destruct()
  63. {
  64. if (!$this->commit && $this->updatedAt !== null) {
  65. $this->delete();
  66. }
  67. }
  68. /**
  69. * コミット
  70. *
  71. * @param boolean $commit
  72. * @return void
  73. */
  74. public function commit($commit = true)
  75. {
  76. $this->commit = $commit;
  77. }
  78. /**
  79. * ファイルパスを取得する disk.rootからの相対パス
  80. *
  81. * @return string
  82. */
  83. public function getFilepath(): string
  84. {
  85. return $this->getDir() . "/" . $this->getFilename();
  86. }
  87. /**
  88. * ファイル取得
  89. *
  90. * @return string|bool
  91. */
  92. public function get(): string|bool
  93. {
  94. if ($this->exists()) {
  95. return Crypt::decryptString(Storage::get($this->getFilepath()));
  96. }
  97. return false;
  98. }
  99. /**
  100. * ファイルの存在確認
  101. *
  102. * @return boolean
  103. */
  104. public function exists(): bool
  105. {
  106. return Storage::exists($this->getFilepath());
  107. }
  108. /**
  109. * ファイル削除
  110. *
  111. * @return boolean 成功可否
  112. */
  113. public function delete(): bool
  114. {
  115. if ($this->exists()) {
  116. return Storage::delete($this->getFilepath());
  117. }
  118. return true;
  119. }
  120. /**
  121. * アップロードファイルの保存
  122. *
  123. * @param UploadedFile $file
  124. * @param Carbon|null|null $updatedAt
  125. * @return boolean
  126. */
  127. public function store(Carbon|null $timestamp = null): bool
  128. {
  129. if ($this->file === null) return false;
  130. $this->updatedAt = $timestamp ?? Now::get();
  131. $contents = Crypt::encryptString($this->file->get());
  132. $ret = Storage::put($this->getDir() . DIRECTORY_SEPARATOR . $this->getFilename(), $contents);
  133. if ($ret === false) {
  134. return false;
  135. }
  136. //DBへの登録
  137. try {
  138. $ret = $this->onUpload($timestamp ?? Now::get());
  139. if (!$ret) {
  140. $this->delete();
  141. }
  142. } catch (Exception $e) {
  143. $this->delete();
  144. throw $e;
  145. }
  146. return $ret;
  147. }
  148. public function toImageStr()
  149. {
  150. return (new Image($this))->__toString();
  151. }
  152. }