php 自动同步 静态资源到码云 实现加速静态资源

 

新建 tbfile.php 把代码复制进入 修改参数

之后用计划任务 运行本文件就行了

<?php
declare(strict_types=1);
/*
 * @Author: xiflys
 * @Date: 2020-08-30 10:48:28
 * @LastEditTime: 2020-08-30 11:09:47
 * @LastEditors: xiflys
 * @Description: 
 */

class TbFile
{

    /**
     * Undocumented function
     *
     * @param [type] $dir 文件夹名
     * @param [type] $fname 文件数据记录文件名
     */
    public function __construct($dir, $fname)
    {
        $this->demofile($dir, $fname);
    }

    protected function demofile($dir, $fname)
    {

        if (!file_exists($fname)) {
            try {
                $fp = fopen($fname, "w+");
                fclose($fp);
                chmod($fname, 0777);
            } catch (\Throwable $th) {
                //throw $th;
                print_r($th->getMessage());
                die;
            }
        }
        $ars = explode(',', trim(file_get_contents($fname), ','));
        $handle = opendir($dir);
        try {
            while ($line = readdir($handle)) {
                if ($line == '.' || $line == '..') {
                    continue;
                }
                $ars = explode(',', trim(file_get_contents($fname), ','));
                if (file_exists($dir . '/' . $line) && !is_dir($dir . '/' . $line)) {
                    chmod($dir . '/' . $line, 0777); # 这里 一般静态文件777 也无所谓了
                    if (!in_array($dir . '/' . $line, $ars)) {

                        $dis = $dir . '/' . $line;
                        $ags = json_decode($this->giteeSuperFlexibleFileSynchronizerPro($dis,$this->base64jm($dis)),true);
                        
                         // dump($as);die;
                        # 文件 就是 $dir.'/'.$line
                        chmod($fname, 0777);
                        if(!isset($ags['message'])){
                            echo $dis.'同步成功'."<br>";
                        }
                        # 写入文件 数据
                        $fp = fopen($fname, "a+");
                        fwrite($fp, ',' . $dir . '/' . $line);
                        fclose($fp);

                        #file_put_contents($fname,file_get_contents($fname).','.$dir . '/' . $line);
                        
                    }
                }
                if (is_dir($dir . '/' . $line)) {
                    new self($dir . '/' . $line, $fname);
                }
            }
        } catch (\Throwable $th) {
            #print_r($th->getMessage());exit;
        }
    }

    protected function base64jm($filename){
        return base64_encode(file_get_contents($filename));
    }


    protected function giteeSuperFlexibleFileSynchronizerPro($filename,$content)
    {
        $arr = array(
            'user' => '', #用户名
            'repo' => '', #仓库名
            'token' => '', # token
            'branch' => 'master', # 分支
            'message'=> '测试',
        );

        $url = "https://gitee.com/api/v5/repos/".$arr['user']."/".$arr['repo']."/contents/".$filename;
        $ass = [
            "access_token" => $arr['token'],
            "message" => $arr['message'],
            "content" => $content,
            "owner" => $arr['user'],
            "repo" => $arr['repo'],
            "path" => $filename,
            "branch" => $arr['branch'],
        ];
        
        $ch = curl_init();
        $as = [
            CURLOPT_URL => $url,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $ass,
            CURLOPT_HTTPHEADER => [],
        ];
        curl_setopt_array($ch, $as);
        $reponse = curl_exec($ch);
        curl_close($ch);
        
        return $reponse;
    }
}

# 更改一下参数
$dir = 'assets'; #文件夹名
$FileRecord = 'flarum_cdn_FileRecord.txt'; #随便定义一个文件名 用来记录数据
# 下面无需更改
try {
    //code...
    $arr = new TbFile($dir, $FileRecord);
} catch (\Throwable $th) {
    //throw $th;
    print_r($th->getmessage());
    die;
}
缔梦资源网
缔梦资源网 » php 自动同步 静态资源到码云 实现加速静态资源

发表评论

提供最优质的资源集合

立即查看 了解详情