cos_params_check.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import re
  5. class ParamCheck(object):
  6. """BaseRequest基本类型的请求"""
  7. def __init__(self):
  8. self._err_tips = u''
  9. def get_err_tips(self):
  10. """获取错误信息
  11. :return:
  12. """
  13. return self._err_tips
  14. def check_param_unicode(self, param_name, param_value):
  15. """检查参数是否是unicode
  16. :param param_name: param_name 参数名
  17. :param param_value: param_value 参数值
  18. :return:
  19. """
  20. if param_value is None:
  21. self._err_tips = param_name + ' is None!'
  22. return False
  23. if not isinstance(param_value, unicode):
  24. self._err_tips = param_name + ' is not unicode!'
  25. return False
  26. return True
  27. def check_param_int(self, param_name, param_value):
  28. """检查参数是否是int
  29. :param param_name: param_name 参数名
  30. :param param_value: param_value 参数值
  31. :return:
  32. """
  33. if param_value is None:
  34. self._err_tips = param_name + ' is None!'
  35. return False
  36. if not isinstance(param_value, int):
  37. self._err_tips = param_name + ' is not int!'
  38. return False
  39. return True
  40. def check_cos_path_valid(self, cos_path, is_file_path):
  41. """检查cos_path是否合法
  42. 路径必须以/开始,文件路径则不能以/结束, 目录路径必须以/结束
  43. :param cos_path:
  44. :param is_file_path:
  45. :return: True for valid path, other False
  46. """
  47. if cos_path[0] != u'/':
  48. self._err_tips = 'cos path must start with /'
  49. return False
  50. last_letter = cos_path[len(cos_path) - 1]
  51. if is_file_path and last_letter == u'/':
  52. self._err_tips = 'for file operation, cos_path must not end with /'
  53. return False
  54. elif not is_file_path and last_letter != u'/':
  55. self._err_tips = 'for folder operation, cos_path must end with /'
  56. return False
  57. else:
  58. pass
  59. illegal_letters = ['?', '*', ':', '|', '\\', '<', '>', '"']
  60. for illegal_letter in illegal_letters:
  61. if cos_path.find(illegal_letter) != -1:
  62. self._err_tips = 'cos path contain illegal letter %s' % illegal_letter
  63. return False
  64. pattern = re.compile(r'/(\s*)/')
  65. if pattern.search(cos_path):
  66. self._err_tips = 'cos path contain illegal letter / /'
  67. return False
  68. return True
  69. def check_not_cos_root(self, cos_path):
  70. """检查不是cos的根路径
  71. 不能对根路径操作的有 1 update 2 create 3 delete
  72. :param cos_path:
  73. :return:
  74. """
  75. if cos_path == u'/':
  76. self._err_tips = 'bucket operation is not supported by sdk,'
  77. ' please use cos console: https://console.qcloud.com/cos'
  78. return False
  79. else:
  80. return True
  81. def check_local_file_valid(self, local_path):
  82. """检查本地文件有效(存在并且可读)
  83. :param local_path:
  84. :return:
  85. """
  86. if not os.path.exists(local_path):
  87. self._err_tips = 'local_file %s not exist!' % local_path
  88. return False
  89. if not os.path.isfile(local_path):
  90. self._err_tips = 'local_file %s is not regular file!' % local_path
  91. return False
  92. if not os.access(local_path, os.R_OK):
  93. self._err_tips = 'local_file %s is not readable!' % local_path
  94. return False
  95. return True
  96. def check_slice_size(self, slice_size):
  97. """检查分片大小有效
  98. :param slice_size:
  99. :return:
  100. """
  101. min_size = 64 * 1024 # 512KB
  102. max_size = 3 * 1024 * 1024 # 20MB
  103. if max_size >= slice_size >= min_size:
  104. return True
  105. else:
  106. self._err_tips = 'slice_size is invalid, only accept [%d, %d]' \
  107. % (min_size, max_size)
  108. return False
  109. def check_insert_only(self, insert_only):
  110. """检查文件上传的insert_only参数
  111. :param insert_only:
  112. :return:
  113. """
  114. if insert_only != 1 and insert_only != 0:
  115. self._err_tips = 'insert_only only support 0 and 1'
  116. return False
  117. else:
  118. return True
  119. def check_move_over_write(self, to_over_write):
  120. """检查move的over write标志
  121. :param to_over_write:
  122. :return:
  123. """
  124. if to_over_write != 1 and to_over_write != 0:
  125. self._err_tips = 'to_over_write only support 0 and 1'
  126. return False
  127. else:
  128. return True
  129. def check_file_authority(self, authority):
  130. """检查文件的authority属性
  131. 合法的取值只有eInvalid, eWRPrivate, eWPrivateRPublic和空值
  132. :param authority:
  133. :return:
  134. """
  135. if authority != u''and authority != u'eInvalid' and authority != u'eWRPrivate' and authority != u'eWPrivateRPublic':
  136. self._err_tips = 'file authority valid value is: eInvalid, eWRPrivate, eWPrivateRPublic'
  137. return False
  138. else:
  139. return True
  140. def check_x_cos_meta_dict(self, x_cos_meta_dict):
  141. """检查x_cos_meta_dict, key和value都必须是UTF8编码
  142. :param x_cos_meta_dict:
  143. :return:
  144. """
  145. prefix_len = len('x-cos-meta-')
  146. for key in x_cos_meta_dict.keys():
  147. if not self.check_param_unicode('x-cos-meta-key', key):
  148. return False
  149. if not self.check_param_unicode('x-cos-meta-value', x_cos_meta_dict[key]):
  150. return False
  151. if key[0:prefix_len] != u'x-cos-meta-':
  152. self._err_tips = 'x-cos-meta key must start with x-cos-meta-'
  153. return False
  154. if len(key) == prefix_len:
  155. self._err_tips = 'x-cos-meta key must not just be x-cos-meta-'
  156. return False
  157. if len(x_cos_meta_dict[key]) == 0:
  158. self._err_tips = 'x-cos-meta value must not be empty'
  159. return False
  160. return True
  161. def check_update_flag(self, flag):
  162. """检查更新文件的flag
  163. :param flag:
  164. :return:
  165. """
  166. if flag == 0:
  167. self._err_tips = 'no any attribute to be updated!'
  168. return False
  169. else:
  170. return True
  171. def check_list_order(self, list_order):
  172. """ 检查list folder的order
  173. :param list_order: 合法取值0(正序), 1(逆序)
  174. :return:
  175. """
  176. if list_order != 0 and list_order != 1:
  177. self._err_tips = 'list order is invalid, please use 0(positive) or 1(reverse)!'
  178. return False
  179. else:
  180. return True
  181. def check_list_pattern(self, list_pattern):
  182. """检查list folder的pattern
  183. :param list_pattern: 合法取值eListBoth, eListDirOnly, eListFileOnly
  184. :return:
  185. """
  186. if list_pattern != u'eListBoth' and list_pattern != u'eListDirOnly' and list_pattern != u'eListFileOnly':
  187. self._err_tips = 'list pattern is invalid, please use eListBoth or eListDirOnly or eListFileOnly'
  188. return False
  189. else:
  190. return True