game2004api/webapp/controller/VoiceController.class.php

60 lines
1.7 KiB
PHP

<?php
class VoiceController{
protected function getMysql($account_id)
{
$mysql_conf = getMysqlConfig(crc32($account_id));
$conn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => DBNAME_PREFIX . $mysql_conf['instance_id']
));
return $conn;
}
protected function getRedis($accountid)
{
$redis_conf = getRedisConfig(crc32($accountid));
$r = new phpcommon\Redis(array(
'host' => $redis_conf['host'],
'port' => $redis_conf['port'],
'passwd' => $redis_conf['passwd']
));
return $r;
}
public function upload()
{
$raw_data = file_get_contents('php://input');
$data = base64_encode($raw_data);
$md5 = md5($data);
$r = $this->getRedis($md5);
$r->set('game2004:voice:' . $md5, $data);
$r -> pexpire('game2004:voice:' . $md5, 1000 * 600);
$url = '';
if (SERVER_ENV == _ONLINE) {
$url = "https://game2004api.kingsome.cn/webapp/index.php?c=Voice&a=download&res_id=" . $md5;
} else {
$url = "https://game2004api-test.kingsome.cn/webapp/index.php?c=Voice&a=download&res_id=" . $md5;
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'download_url' => $url
));
}
public function download()
{
$r = $this->getRedis($_REQUEST['res_id']);
$data = $r->get('game2004:voice:' . $_REQUEST['res_id']);
echo base64_decode($data);
}
}