QiniuUpload.class.php 4.32 KB
<?php
/**
 * 七牛上传
 * 
 */
class Lib_Utils_QiniuUpload implements Util_Upload_QiniuInterface
{
	/**
	 * 文件上传
	 * 
	 * param array array('file'=>'文件数组','prefix'=>'前缀’,'filePath'=>'文件存储路径')
	 * @return array(relaPath=>'')
	 */
	public static function fileUpload($params)
	{
		$ret = array();
		if(isset($params['filePath']) && isset($params['file']) && isset($params['prefix']))
		{
			$filePath = $params['filePath'];
			$file = $params['file'];
			$prefix = $params['prefix'];
			$bucket = self::getBucket($filePath, $prefix);
			$project = self::getProject($filePath);
			$key = self::getFileKey($file, $prefix);
			$uploadKey = $key;
			if(empty($project))//项目为空,去掉前面的/
			{
				$uploadKey = substr($uploadKey, 1);
			}
			$fileName = $file['tmp_name'];
			Util_Qiniu_Upload::initKey(QINIU_UPLOAD_ACCESSKEY, QINIU_UPLOAD_SECRETKEY);
			if(Util_Qiniu_Upload::upload($bucket, $project.$uploadKey, $fileName))
			{
				$ret = array('relaPath'=> $key);
			}
		}
		return $ret;
	}
	
	/**
	 * 文件批量上传
	 * 
	 * @param array ('file'=>'多个文件数组','prefix'=>'前缀’,'filePath'=>'文件存储路径')
	 * @return array(array(relaPath=>''))
	 */
	public static function fileBatchUpload($params)
	{
		$ret = array();
		if(isset($params['filePath']) && isset($params['file']) && isset($params['prefix']))
		{
			$filePath = $params['filePath'];
			$prefix = $params['prefix'];
			Util_Qiniu_Upload::initKey(QINIU_UPLOAD_ACCESSKEY, QINIU_UPLOAD_SECRETKEY);
			foreach($params['file'] as $fileKey => $file)
			{
				$bucket = self::getBucket($filePath, $prefix);
				$key = self::getFileKey($file, $prefix);
				$fileName = $file['tmp_name'];
				$project = self::getProject($filePath);
				$uploadKey = $key;
				if(empty($project))//项目为空,去掉前面的/
				{
					$uploadKey = substr($uploadKey, 1);
				}
				if(Util_Qiniu_Upload::upload($bucket, $project.$uploadKey, $fileName))
				{
					$ret[$fileKey]['relaPath'] = $key;
				}
			}
		}
		return $ret;
	}
	
	/**
	 * 文件流上传
	 *
	 * @param string ('file'=>'文件流','prefix'=>'前缀’,'filePath'=>'文件存储路径')
	 * @return string
	 */
	public static function streamUpload($params)
	{
		$ret = '';
		if(isset($params['filePath']) && isset($params['file']) && isset($params['prefix']))
		{
			$filePath = $params['filePath'];
			$prefix = $params['prefix'];
			$file = $params['file'];
			Util_Qiniu_Upload::initKey(QINIU_UPLOAD_ACCESSKEY, QINIU_UPLOAD_SECRETKEY);
			$bucket = self::getBucket($filePath, $prefix);
			$key = self::getStreamKey($prefix);
			$project = self::getProject($filePath);
			$uploadKey = $key;
			if(empty($project))//项目为空,去掉前面的/
			{
				$uploadKey = substr($uploadKey, 1);
			}
			if(Util_Qiniu_Upload::uploadStream($bucket, $project.$uploadKey, $file))
			{
				$ret = $key;
			}
		}
		return $ret;
	}
	
	/**
	 * 获取空间
	 * 
	 * @param string $filePath
	 * @param string $prefix
	 * @return string
	 */
	private static function getBucket($filePath, $prefix = 0)
	{
		//video暂时存储到show的视频上面
		$buckets = array('video'=> array('showvideo'),'voice'=> array('wwwyohocnvoice'), 'img'=> array( 1 => 'wwwyohocnimg01', 2 => 'wwwyohocnimg02'));
		foreach($buckets as $key => $bucket)
		{
			$pos = strrpos($filePath, $key);
			$length = strlen($filePath);
			if($pos !== false && $pos >= ($length - strlen($key) - 1))
			{
				return $bucket[intval($prefix)];
			}
		}
	}
	
	/**
	 * 获取模块
	 * 
	 * @param string $filePath
	 * @return string
	 */
	private static function getProject($filePath)
	{
		if(strrpos($filePath, 'video') !== false || strrpos($filePath, 'voice') !== false)
		{
			return '';
		}
		else
		{
			return basename($filePath);
		}
	}
	
	/**
	 * 获取文件方式key
	 * 
	 * @param string $file
	 * @param string $prefix
	 * @return string
	 */
	private static function getFileKey($file, $prefix)
	{
		$fileInfo = pathinfo($file['name']);
		$datePath = date('/Y/m/d/H/');
		return $datePath.$prefix.md5(date('H') . $file['name'] . $file['type'] . $file['size'] ) . '.' . strtolower($fileInfo['extension']);
	}
	
	/**
	 * 获取文件流方式key
	 * 
	 * @param string $prefix
	 * @return string
	 */
	private static function getStreamKey($prefix)
	{
		$datePath = date('/Y/m/d/H/');
		return  $datePath.$prefix.md5(microtime()) . '.jpg'; // 文件名以时间md5值命名
	}
}