Bladeren bron

ファイルクラス追加

develop
sosuke.iwabuchi 2 jaren geleden
bovenliggende
commit
065f10e86c
4 gewijzigde bestanden met toevoegingen van 310 en 0 verwijderingen
  1. +182
    -0
      app/Files/BaseFile.php
  2. +54
    -0
      app/Files/CsvFile.php
  3. +22
    -0
      app/Files/Image.php
  4. +52
    -0
      app/Files/TmpFile.php

+ 182
- 0
app/Files/BaseFile.php Bestand weergeven

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

namespace App\Files;

use App\Middlewares\Now;
use Exception;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Storage;

abstract class BaseFile
{

protected UploadedFile|null $file = null;

private bool $commit = false;

protected Carbon|null $updatedAt = null;

/**
* ディレクトリのパスを取得
*
* @return string
*/
abstract public function getDir(): string;

/**
* ファイル名の取得
*
* @return string
*/
abstract public function getFilename(): string;

/**
* MIMETYPEの取得
*
* @return string
*/
abstract public function getMimetype(): string;

/**
* DBの登録などを定義
*
* @return boolean
*/
abstract protected function onUpload(Carbon $timestamp): bool;


/**
* コミットする
*
* @param array<BaseFile>|Collection<BaseFile>|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();
}
}

+ 54
- 0
app/Files/CsvFile.php Bestand weergeven

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

namespace App\Files;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use LogicException;

class CsvFile extends TmpFile
{

const ENCODE_UTF8 = "UTF8";
const ENCODE_SJIS = "SJIS";


public function __construct(
protected array $headers = [],
protected string $encode = self::ENCODE_UTF8
) {
parent::__construct();

if (!in_array($encode, [static::ENCODE_UTF8, static::ENCODE_SJIS])) {
throw new LogicException("エンコード指定不正:" . $encode);
}

if (count($headers) !== 0) {
$this->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");
}
}

+ 22
- 0
app/Files/Image.php Bestand weergeven

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

namespace App\Files;

class Image
{

protected string $binary;

protected string $mimetype;

public function __construct(BaseFile $file)
{
$this->binary = $file->get();
$this->mimetype = $file->getMimetype();
}

public function __toString()
{
return sprintf("data:%s;base64,%s", $this->mimetype, base64_encode($this->binary));
}
}

+ 52
- 0
app/Files/TmpFile.php Bestand weergeven

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

namespace App\Files;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class TmpFile
{
protected const DIR = "/tmp";

protected string $uuid;


public function __construct()
{
$this->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();
}
}

Laden…
Annuleren
Opslaan