Example #1
0
$refresh_token = file_get_contents(CONFIG_DIR . '/refresh_token');
if ($refresh_token) {
    //若存在refresh token,则刷新它。
    $token_array = do_oauth_refresh(file_get_contents(CONFIG_DIR . '/appkey'), file_get_contents(CONFIG_DIR . '/appsec'), file_get_contents(CONFIG_DIR . '/refresh_token'));
    if ($token_array['access_token'] && $token_array['refresh_token']) {
        //防止获取不到token而自杀的行为
        $access_token = $token_array['access_token'];
        $refresh_token = $token_array['refresh_token'];
        file_put_contents(CONFIG_DIR . '/access_token', $access_token);
        file_put_contents(CONFIG_DIR . '/refresh_token', $refresh_token);
    }
}
switch ($argv[1]) {
    case 'quota':
        //quota - 获取空间
        $quota = get_quota($access_token);
        $u = $quota['used'] / 1024 / 1024 / 1024;
        $a = $quota['quota'] / 1024 / 1024 / 1024;
        echon(sprintf("Your Storage Status: %.2fG/%.2fG (%.2f%%)", $u, $a, $u / $a * 100));
        break;
    case 'upload':
        //upload - 上传文件
        if (count($argv) < 3) {
            echon("Missing parameters. Please check again.");
            die;
        }
        $res = upload_file($access_token, $argv[3], $argv[2], isset($argv[4]) ? $argv[4] : 'newcopy');
        echon(sprintf("File %s uploaded.\nSize:%.3fK MD5 Sum:%s", $res['path'], $res['size'] / 1024, $res['md5']));
        break;
    case 'download':
        //download - 下载文件
Example #2
0
function du_init($quickinit = false)
{
    define('BPCSU_KEY', 'uFBSHEwWE6DD94SQx9z77vgG');
    define('BPCSU_SEC', '7w6wdSFsTk6Vv586r1W1ozHLoDGhXogD');
    define('BPCSU_FNAME', 'bpcs_uploader');
    if ($quickinit) {
        //快速初始化
        $appkey = BPCSU_KEY;
        file_put_contents(CONFIG_DIR . '/appkey', $appkey);
        $appsec = BPCSU_SEC;
        file_put_contents(CONFIG_DIR . '/appsec', $appsec);
        $appname = BPCSU_FNAME;
        file_put_contents(CONFIG_DIR . '/appname', $appname);
    } else {
        //正常初始化
        echo <<<EOF
Please enter your PSC App API Key. You can get this key by visiting http://developer.baidu.com/dev#/create
If you have already created an app, you can visit http://developer.baidu.com/console#/app and get it in your app's info.
If you don't want to bother creating an app, you can press Enter to use the demo API Key.
Doing so (without your own API Key/Secret) will cause the access-token to expire every 30 days, and you'll have to
re-initialize when it expires.

EOF;
        echo 'App API KEY [' . BPCSU_KEY . '] :';
        $appkey = getline();
        $appkey = $appkey ? $appkey : BPCSU_KEY;
        file_put_contents(CONFIG_DIR . '/appkey', $appkey);
        echon('App API Key has been set to ' . $appkey . ' . ');
        $appsec = false;
        //作用域
        if ($appkey == BPCSU_KEY) {
            echon('Demo key detected. Using default API Secret.');
            $appsec = BPCSU_SEC;
        } else {
            echo <<<EOF
  Please enter your Baidu PSC App API Secret. If you have no idea what it is, keep it blank.

EOF;
            echo 'App API SECRET [' . BPCSU_SEC . '] :';
            $appsec = getline();
        }
        file_put_contents(CONFIG_DIR . '/appsec', $appsec);
        $prepathfile = CONFIG_DIR . '/appname';
        if ($appkey == BPCSU_KEY) {
            echon('Demo key detected. Using default app name.');
            $appname = 'bpcs_uploader';
        } else {
            echo <<<EOF
Please enter your app's folder name. You can choose to input this later in the file [ {$prepathfile} ].
** Why do I have to enter the app's folder name? Please check the FAQs. **
If your app's name has Chinese characters, please ensure that your client supports UTF-8 encoding.
Below are some Chinese characters for testing. Please make sure that you can read them before you enter Chinese here.
这里是一些中文字符。
If you can't read the characters above, please press Enter and change it manually within the file [ {$prepathfile} ].

EOF;
            echo 'App\'s Folder Name [] : ';
            $appname = getline();
        }
        file_put_contents(CONFIG_DIR . '/appname', $appname);
    }
    //end of 初始化配置
    if ($appsec) {
        $tokens = du_oauth_device($appkey, $appsec);
        $access_token = $tokens['access_token'];
        $refresh_token = $tokens['refresh_token'];
    } else {
        $access_token = do_oauth_token($appkey);
        $refresh_token = '';
    }
    file_put_contents(CONFIG_DIR . '/access_token', $access_token);
    file_put_contents(CONFIG_DIR . '/refresh_token', $refresh_token);
    $quota = get_quota($access_token);
    $u = $quota['used'] / 1024 / 1024 / 1024;
    $a = $quota['quota'] / 1024 / 1024 / 1024;
    echon(sprintf("Access Granted. Your Storage Status: %.2fG/%.2fG (%.2f%%)", $u, $a, $u / $a * 100));
    echon('Enjoy!');
}
Example #3
0
function display_quota($dir)
{
    $q = get_quota($dir);
    $used = $q['used'] / 1000000;
    // bytes to mega
    $total = $q['total'] / 1000000;
    // bytes to mega;
    $medium = $total * (70 / 100);
    // at 90% we start to get worried
    $high = $total * (90 / 100);
    if ($used <= $medium) {
        $bg = 'green';
    } else {
        if ($used > $medium && $used <= $high) {
            $bg = 'yellow';
        } else {
            if ($used > $high) {
                $bg = 'red';
            }
        }
    }
    $width = $used * 100 / $total;
    $width = number_format($width, 2);
    $ret = '<div class="quota" title="Quota: used ' . number_format($used, 2) . ' MB / ' . number_format($total) . ' MB">';
    $ret .= '   <div class="meter ' . $bg . '" style="width:' . $width . '%;"></div>';
    $ret .= '   <div class="meter-inner">' . number_format($used, 2) . 'MB /' . number_format($total) . 'MB</div>';
    $ret .= '</div>';
    return $ret;
}