utils.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. require_once("mimetypes.php");
  3. define('BLKSIZE', 4*1024*1024);
  4. abstract class ActionType
  5. {
  6. const NONE = -1;
  7. const PUTFILE = 0;
  8. const POSTFILE = 1;
  9. const MINIT = 2;
  10. const MUPLOAD = 3;
  11. const MFINISH = 4;
  12. const MCANCEL = 5;
  13. const DELETE = 6;
  14. const UPLOADHIT = 7;
  15. const GETFILE = 8;
  16. const APPENDFILE = 9;
  17. const LISTOBJECTS = 10;
  18. const HEAD = 11;
  19. }
  20. class UCloud_Error
  21. {
  22. public $Code; // int
  23. public $ErrRet; // int
  24. public $ErrMsg; // string
  25. public $SessionId; // string
  26. public function __construct($code, $errRet, $errMsg)
  27. {
  28. $this->Code = $code;
  29. $this->ErrRet = $errRet;
  30. $this->ErrMsg = $errMsg;
  31. }
  32. }
  33. function UCloud_UrlSafe_Encode($data)
  34. {
  35. $find = array('+', '/');
  36. $replace = array('-', '_');
  37. return str_replace($find, $replace, $data);
  38. }
  39. function UCloud_UrlSafe_Decode($data)
  40. {
  41. $find = array('-', '_');
  42. $replace = array('+', '/');
  43. return str_replace($find, $replace, $data);
  44. }
  45. //@results: (hash, err)
  46. function UCloud_FileHash($file)
  47. {
  48. $f = fopen($file, "r");
  49. if (!$f) return array(null, new UCloud_Error(0, -1, "open $file error"));
  50. $fileSize = filesize($file);
  51. $buffer = '';
  52. $sha = '';
  53. $blkcnt = $fileSize/BLKSIZE;
  54. if ($fileSize % BLKSIZE) $blkcnt += 1;
  55. $buffer .= pack("L", $blkcnt);
  56. if ($fileSize <= BLKSIZE) {
  57. $content = fread($f, BLKSIZE);
  58. if (!$content) {
  59. fclose($f);
  60. return array("", new UCloud_Error(0, -1, "read file error"));
  61. }
  62. $sha .= sha1($content, TRUE);
  63. } else {
  64. for($i=0; $i<$blkcnt; $i+=1) {
  65. $content = fread($f, BLKSIZE);
  66. if (!$content) {
  67. if (feof($f)) break;
  68. fclose($f);
  69. return array("", new UCloud_Error(0, -1, "read file error"));
  70. }
  71. $sha .= sha1($content, TRUE);
  72. }
  73. $sha = sha1($sha, TRUE);
  74. }
  75. $buffer .= $sha;
  76. $hash = UCloud_UrlSafe_Encode(base64_encode($buffer));
  77. fclose($f);
  78. return array($hash, null);
  79. }
  80. //@results: (mime, err)
  81. function GetFileMimeType($filename)
  82. {
  83. $mimetype = "";
  84. $ext = "";
  85. $filename_component = explode(".", $filename);
  86. if (count($filename_component) >= 2) {
  87. $ext = "." . $filename_component[count($filename_component)-1];
  88. }
  89. global $mimetype_complete_map;
  90. if (array_key_exists($ext, $mimetype_complete_map)) {
  91. $mimetype = $mimetype_complete_map[$ext];
  92. } else if (function_exists('mime_content_type')) {
  93. $mimetype = mime_content_type($filename);
  94. } else if (function_exists('finfo_file')) {
  95. $finfo = finfo_open(FILEINFO_MIME_TYPE); // 返回 mime 类型
  96. $mimetype = finfo_file($finfo, $filename);
  97. finfo_close($finfo);
  98. } else {
  99. return array("application/octet-stream", null);
  100. }
  101. return array($mimetype, null);
  102. }
  103. function CheckConfig($action) {
  104. $UCLOUD_PUBLIC_KEY = '0cCMBBXqiuFdJrb3jy0csUwXHDEAzjPT4I8ZDvcnKlpd6RGdQ7vStHO0z1M=';
  105. $UCLOUD_PRIVATE_KEY = '8JYvmvzBIaM7W15bVqmdriR9pFohACpBMDz/w/c/XSqTjX+aL0YN8yFb9EQ2SUP1';
  106. $UCLOUD_PROXY_SUFFIX = '.infile.inspurcloud.cn';
  107. switch ($action) {
  108. case ActionType::PUTFILE:
  109. case ActionType::POSTFILE:
  110. case ActionType::MINIT:
  111. case ActionType::MUPLOAD:
  112. case ActionType::MCANCEL:
  113. case ActionType::MFINISH:
  114. case ActionType::DELETE:
  115. case ActionType::UPLOADHIT:
  116. case ActionType::LISTOBJECTS:
  117. case ActionType::HEAD:
  118. if ($UCLOUD_PROXY_SUFFIX == "") {
  119. return new UCloud_Error(400, -1, "no proxy suffix found in config");
  120. } else if ($UCLOUD_PUBLIC_KEY == "" || strstr($UCLOUD_PUBLIC_KEY, " ") != FALSE) {
  121. return new UCloud_Error(400, -1, "invalid public key found in config");
  122. } else if ($UCLOUD_PRIVATE_KEY == "" || strstr($UCLOUD_PRIVATE_KEY, " ") != FALSE) {
  123. return new UCloud_Error(400, -1, "invalid private key found in config");
  124. }
  125. break;
  126. case ActionType::GETFILE:
  127. if ($UCLOUD_PROXY_SUFFIX == "") {
  128. return new UCloud_Error(400, -1, "no proxy suffix found in config");
  129. }
  130. break;
  131. default:
  132. break;
  133. }
  134. return null;
  135. }