<?php

namespace app\event;

use app\model\saas\SaasUpload;

class ClearUpload
{

    /**
     * 清理上传文件
     * @param array $param
     * @return bool
     */
    public function clearData(array $param = []): bool
    {
        try {
            if (empty($param['key'])) return true;
            $uploadData = (new SaasUpload)->where("key",$param["key"])->select();
            if ($uploadData->isEmpty()) return true;
            foreach ($uploadData as $file) {
                if (file_exists(public_path().$file['path'])) {
                    unlink(public_path().$file['path']);
                }
            }
            $uploadData->delete(); // 清理数据库
            return true;
        } catch (\Throwable $th) {
            echo "清理上传文件报错\n";
            echo $th->getLine()."\n";
            echo $th->getFile()."\n";
            echo $th->getMessage()."\n";
            return false;
        }
    }

}