diff --git a/app/Files/BaseFile.php b/app/Files/BaseFile.php new file mode 100644 index 0000000..2e771d0 --- /dev/null +++ b/app/Files/BaseFile.php @@ -0,0 +1,182 @@ +|Collection|BaseFile $files + * @return void + */ + public static function commitAll(array|Collection|BaseFile $files) + { + if (is_array($files) || $files instanceof Collection) { + foreach ($files as $file) { + $file->commit(); + } + } else { + $files->commit(); + } + } + + + public function __construct(UploadedFile $file = null) + { + $this->file = $file; + } + + /** + * 変更後、コミットしていない場合は削除する + */ + public function __destruct() + { + if (!$this->commit && $this->updatedAt !== null) { + $this->delete(); + } + } + + /** + * コミット + * + * @param boolean $commit + * @return void + */ + public function commit($commit = true) + { + $this->commit = $commit; + } + + /** + * ファイルパスを取得する disk.rootからの相対パス + * + * @return string + */ + public function getFilepath(): string + { + return $this->getDir() . "/" . $this->getFilename(); + } + + /** + * ファイル取得 + * + * @return string|bool + */ + public function get(): string|bool + { + if ($this->exists()) { + return Crypt::decryptString(Storage::get($this->getFilepath())); + } + return false; + } + + /** + * ファイルの存在確認 + * + * @return boolean + */ + public function exists(): bool + { + return Storage::exists($this->getFilepath()); + } + + /** + * ファイル削除 + * + * @return boolean 成功可否 + */ + public function delete(): bool + { + if ($this->exists()) { + return Storage::delete($this->getFilepath()); + } + return true; + } + + /** + * アップロードファイルの保存 + * + * @param UploadedFile $file + * @param Carbon|null|null $updatedAt + * @return boolean + */ + public function store(Carbon|null $timestamp = null): bool + { + + if ($this->file === null) return false; + + $this->updatedAt = $timestamp ?? Now::get(); + $contents = Crypt::encryptString($this->file->get()); + + $ret = Storage::put($this->getDir() . DIRECTORY_SEPARATOR . $this->getFilename(), $contents); + + if ($ret === false) { + return false; + } + + + //DBへの登録 + try { + $ret = $this->onUpload($timestamp ?? Now::get()); + if (!$ret) { + $this->delete(); + } + } catch (Exception $e) { + $this->delete(); + throw $e; + } + + + return $ret; + } + + public function toImageStr() + { + return (new Image($this))->__toString(); + } +} diff --git a/app/Files/CsvFile.php b/app/Files/CsvFile.php new file mode 100644 index 0000000..7e50e48 --- /dev/null +++ b/app/Files/CsvFile.php @@ -0,0 +1,54 @@ +addLine($headers); + } + } + + public function addLine(array|Collection $row) + { + $str = ""; + foreach ($row as $col => $val) { + if ($str !== "") { + $str .= ","; + } + + $str .= $val; + } + + if ($this->encode === static::ENCODE_SJIS) { + $str = $this->toSjis($str); + } + + $this->append($str); + } + + private function toSjis(string $source): string + { + return mb_convert_encoding($source, "SJIS", "UTF8"); + } +} diff --git a/app/Files/Image.php b/app/Files/Image.php new file mode 100644 index 0000000..125fad6 --- /dev/null +++ b/app/Files/Image.php @@ -0,0 +1,22 @@ +binary = $file->get(); + $this->mimetype = $file->getMimetype(); + } + + public function __toString() + { + return sprintf("data:%s;base64,%s", $this->mimetype, base64_encode($this->binary)); + } +} diff --git a/app/Files/TmpFile.php b/app/Files/TmpFile.php new file mode 100644 index 0000000..09dd5f1 --- /dev/null +++ b/app/Files/TmpFile.php @@ -0,0 +1,52 @@ +uuid = Str::uuid(); + } + + public function __destruct() + { + // ファイルダウンロード前に削除されては困るので処理をコメントアウトしている + // if (Storage::exists($this->getPath())) { + // Storage::delete($this->getPath()); + // } + } + + public function getPath() + { + return static::DIR . "/" . $this->uuid; + } + + public function getFullPath() + { + return Storage::path($this->getPath()); + } + + public function put(string $content) + { + Storage::put($this->getPath(), $content); + } + + public function append(string $content) + { + Storage::append($this->getPath(), $content); + } + + public function download(string $name = "download") + { + return response()->download($this->getFullPath(), $name)->deleteFileAfterSend(); + } +}