OTAEquipment.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. import base64
  2. from Object.base64Object import base64Object
  3. from zlib import crc32
  4. from django.core import serializers
  5. from wsgiref.util import FileWrapper
  6. import traceback, hashlib
  7. from Ansjer.config import SERVER_DOMAIN
  8. from django.views.decorators.csrf import csrf_exempt
  9. from django.views.generic import TemplateView
  10. from django.utils.decorators import method_decorator
  11. from Model.models import Equipment_Version
  12. from Model.models import Device_User
  13. from Service.ModelService import ModelService
  14. from Service.CommonService import CommonService
  15. import time, os, simplejson as json
  16. from Object.ResponseObject import ResponseObject
  17. from Object.TokenObject import TokenObject
  18. from Object.UrlTokenObject import UrlTokenObject
  19. from django.http import HttpResponse
  20. from Ansjer.config import BASE_DIR
  21. def downloadUrl(fileType, fileCode, fileVersion, fileName):
  22. fullPath = os.path.join(BASE_DIR, "static/Upgrade").replace('\\', '/')
  23. if fileType == 'IPC':
  24. Path = '/'.join((fullPath, 'IPC', fileCode, fileVersion, fileName)).replace('\\', '/')
  25. elif fileType == 'DVR':
  26. Path = '/'.join((fullPath, 'DVR', fileCode, fileVersion, fileName)).replace('\\', '/')
  27. elif fileType == 'NVR':
  28. Path = '/'.join((fullPath, 'NVR', fileCode, fileVersion, fileName)).replace('\\', '/')
  29. elif fileType == 'XVR':
  30. Path = '/'.join((fullPath, 'IPC', fileCode, fileVersion, fileName)).replace('\\', '/')
  31. else:
  32. if fileType == 'CHM':
  33. Path = fileName
  34. else:
  35. Path = '/'.join((fullPath, 'Other', fileName)).replace('\\', '/')
  36. if os.path.isfile(Path):
  37. try:
  38. JSON = json.dumps(
  39. {
  40. "result_code": 0,
  41. "reason": 'Success',
  42. "result": {},
  43. "error_code": 0,
  44. }, ensure_ascii=False
  45. )
  46. wrapper = FileWrapper(open(Path, 'rb'))
  47. response = HttpResponse(wrapper, content_type="application/octet-stream")
  48. response['Content-Length'] = os.path.getsize(Path)
  49. response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(Path)
  50. response['Content-MD5'] = getMD5orSHA265(Path)
  51. response['Content-SHA265'] = getMD5orSHA265(Path, 'SHA265')
  52. response['Content-CRC32'] = getMD5orSHA265(Path, 'CRC32')
  53. response['Content-Error'] = JSON
  54. return response
  55. except Exception as e:
  56. res = ResponseObject()
  57. errorJSON = res.formal(906, {'details': repr(e)})
  58. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  59. response['Content-Error'] = errorJSON
  60. return response
  61. else:
  62. res = ResponseObject()
  63. errorJSON = res.formal(907)
  64. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  65. response['Content-Error'] = errorJSON
  66. return response
  67. def getMD5orSHA265(fileName, encryptionType='MD5'):
  68. """
  69. :param filePath:
  70. :param encryptionType:
  71. :return:
  72. """
  73. if not os.path.isfile(fileName):
  74. return ''
  75. else:
  76. if encryptionType == 'MD5':
  77. encryption = hashlib.md5()
  78. elif encryptionType == 'SHA265':
  79. encryption = hashlib.sha256()
  80. elif encryptionType == 'CRC32':
  81. f = open(fileName, 'rb')
  82. chunk = f.read()
  83. return crc32(chunk)
  84. f = open(fileName, 'rb')
  85. block_size = 8192 # why is 8192 | 8192 is fast than 2048
  86. while True:
  87. chunk = f.read(block_size)
  88. if not chunk:
  89. break
  90. encryption.update(chunk)
  91. f.close()
  92. return encryption.hexdigest()
  93. @csrf_exempt
  94. def downloadUpdataFileUrl(request):
  95. response = ResponseObject()
  96. if request.method == 'GET':
  97. request_dict = request.GET
  98. elif request.method == 'POST':
  99. request_dict = request.POST
  100. else:
  101. errorJSON = response.formal(444)
  102. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  103. response['Content-Error'] = errorJSON
  104. return response
  105. fileType = request_dict.get('fileType', None)
  106. fileCode = request_dict.get('fileCode', None)
  107. fileVersion = request_dict.get('fileVersion', None)
  108. fileName = request_dict.get('fileName', None)
  109. if fileType != None and fileCode != None and fileVersion != \
  110. None and fileName != None:
  111. return downloadUrl(fileType, fileCode, fileVersion, fileName, response)
  112. else:
  113. errorJSON = response.formal(444)
  114. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  115. response['Content-Error'] = errorJSON
  116. return response
  117. @csrf_exempt
  118. def getEquipmentVersionInterface(request):
  119. response = ResponseObject()
  120. if request.method == "POST":
  121. request.encoding = 'utf-8'
  122. request_dict = request.POST
  123. elif request.method == "GET":
  124. request.encoding = 'gb2312'
  125. request_dict = request.GET
  126. else:
  127. return response.json(444)
  128. code = request_dict.get('code', None)
  129. ov = request_dict.get('ov', None)
  130. if not code:
  131. return response.json(444, 'code')
  132. eqs = Equipment_Version.objects.filter(code=code, status=1, lang='en').order_by('-data_joined')
  133. if not eqs.exists():
  134. return response.json(900)
  135. if ov is not None:
  136. # 判断大小
  137. nv = eqs[0].softwareVersion
  138. if ov > nv:
  139. return response.json(0, {'softwareVersion': ov})
  140. equipment = eqs[0]
  141. return response.json(0, {'softwareVersion': equipment.softwareVersion})
  142. @csrf_exempt
  143. def getUpdataFileUrlInterface(request):
  144. response = ResponseObject()
  145. request.encoding = 'utf-8'
  146. if request.method == "POST":
  147. code = request.POST.get('code', None)
  148. elif request.method == "GET":
  149. code = request.GET.get('code', None)
  150. else:
  151. return response.json(444)
  152. if not code:
  153. return response.json(444, 'code')
  154. eq = Equipment_Version.objects.filter(code=code, status=1, lang='en').order_by('-data_joined')
  155. # 判断是否有版本存在
  156. if not eq.exists():
  157. return response.json(902)
  158. file_path = eq[0].filePath
  159. urls = []
  160. if file_path:
  161. if file_path.find('static/Upgrade/') != -1:
  162. path = file_path.replace('static/Upgrade/', '').replace('\\', '/')
  163. url = SERVER_DOMAIN + 'OTA/downloads/' + path + '?time=' + str(time.time())
  164. urls.append(url)
  165. if file_path.find('static/otapack') != -1:
  166. url = SERVER_DOMAIN + 'OTA/downloadsPack/' + file_path + '?time=' + str(time.time())
  167. urls.append(url)
  168. if len(urls) > 0:
  169. res = {
  170. "urlCount": len(urls),
  171. "url": urls,
  172. "fileSize": eq[0].fileSize,
  173. "Description": eq[0].Description,
  174. }
  175. return response.json(0, res)
  176. else:
  177. return response.json(901)
  178. @csrf_exempt
  179. def downloadUpdataFileUrlInterface(request, fileType, fileName,
  180. *callback_args, **callback_kwargs):
  181. res = ResponseObject()
  182. if fileType is not None and fileName is not None:
  183. fullPath = os.path.join(BASE_DIR, "static/Upgrade/").replace('\\', '/')
  184. if fileType == 'IPC':
  185. fullPath += 'IPC/' + fileName
  186. elif fileType == 'DVR':
  187. fullPath += 'DVR/' + fileName
  188. elif fileType == 'NVR':
  189. fullPath += 'NVR/' + fileName
  190. elif fileType == 'XVR':
  191. fullPath += 'XVR/' + fileName
  192. elif fileType == 'User':
  193. fullPath = os.path.join(BASE_DIR, "static/").replace('\\', '/')
  194. fullPath += 'User/' + fileName
  195. elif fileType == 'ADCloud':
  196. fullPath = os.path.join(BASE_DIR, "static/APK/").replace('\\', '/')
  197. fullPath += 'ADCloud/' + fileName
  198. elif fileType == 'ACCloud':
  199. fullPath = os.path.join(BASE_DIR, "static/APK/").replace('\\', '/')
  200. fullPath += 'ACCloud/' + fileName
  201. else:
  202. fullPath += 'Other/' + fileName
  203. print(fullPath)
  204. if os.path.isfile(fullPath):
  205. try:
  206. JSON = res.formal(0)
  207. if fileType != 'User':
  208. wrapper = FileWrapper(open(fullPath, 'rb'))
  209. response = HttpResponse(wrapper, content_type="application/octet-stream")
  210. response['Content-Length'] = os.path.getsize(fullPath)
  211. response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(fullPath)
  212. response['Content-MD5'] = getMD5orSHA265(fullPath)
  213. # 校验文件md5值
  214. response['Content-SHA265'] = getMD5orSHA265(fullPath, 'SHA265')
  215. response['Content-CRC32'] = getMD5orSHA265(fullPath, 'CRC32')
  216. response['Content-Error'] = JSON
  217. return response
  218. else:
  219. Imagedata = open(fullPath, 'rb').read()
  220. response = HttpResponse(Imagedata, content_type="image/jpeg")
  221. return response
  222. except Exception as e:
  223. errorJSON = res.formal(906)
  224. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  225. response['Content-Error'] = errorJSON
  226. return response
  227. else:
  228. errorJSON = res.formal(907)
  229. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  230. response['Content-Error'] = errorJSON
  231. return response
  232. else:
  233. errorJSON = res.formal(444)
  234. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  235. response['Content-Error'] = errorJSON
  236. return response
  237. class getUploadFiletoDirView(TemplateView):
  238. @method_decorator(csrf_exempt)
  239. def dispatch(self, *args, **kwargs):
  240. return super(getUploadFiletoDirView, self).dispatch(*args, **kwargs)
  241. def post(self, request, *args, **kwargs):
  242. request.encoding = 'utf-8'
  243. request_dict = request.POST
  244. fileName = request.FILES.get('fileName', None)
  245. return self.ValidationError(request_dict, fileName)
  246. def get(self, request, *args, **kwargs):
  247. request.encoding = 'gb2312'
  248. request_dict = request.GET
  249. fileName = request.FILES.get('fileName', None)
  250. return self.ValidationError(request_dict, fileName)
  251. def ValidationError(self, request_dict, fileName):
  252. response = ResponseObject()
  253. token = request_dict.get('token', None)
  254. fileType = request_dict.get('fileType', None)
  255. fileCode = request_dict.get('fileCode', None)
  256. fileVersion = request_dict.get('fileVersion', None)
  257. if not fileName or not fileType:
  258. return response.json(444, 'fileName,fileType')
  259. tko = TokenObject(token)
  260. response.lang = tko.lang
  261. if tko.code != 0:
  262. return response.json(tko.code)
  263. userID = tko.userID
  264. if not userID:
  265. return response.json(104)
  266. own_perm = ModelService.check_perm(userID=userID, permID=210)
  267. if not own_perm:
  268. return response.json(404)
  269. if fileVersion != None and fileCode != None:
  270. return self.getUploadFiletoDir(userID, fileType, fileName, response, fileCode,
  271. fileVersion=fileVersion)
  272. else:
  273. return self.getUploadFiletoDir(userID, fileType, fileName, response)
  274. def getUploadFiletoDir(self, userID, fileType, fileName, response, *args, **kwargs):
  275. """
  276. 将上传的文件写入服务器对应目录下
  277. :param Type: equipment type
  278. :param fileName: File name of upgrade file.
  279. :return: filePath
  280. """
  281. User = Device_User.objects.filter(userID=userID)
  282. if not User:
  283. return response.json(104)
  284. own_perm = ModelService.check_perm(userID, permID=210)
  285. if own_perm is not True:
  286. return response.json(404)
  287. updataFile = fileName.name
  288. updataFile = updataFile.replace(' ', '+')
  289. versionIndex = updataFile.find('.', updataFile.find('.', updataFile.find('.') + 1) + 1)
  290. codeIndex = versionIndex + 12
  291. if codeIndex != -1 and versionIndex != -1:
  292. fileVersion = len(updataFile[1: versionIndex]) > 0 and updataFile[1: versionIndex] or None
  293. fileCode = len(updataFile[versionIndex + 1: codeIndex]) > 0 and \
  294. updataFile[versionIndex + 1: codeIndex] or None
  295. if fileCode is not None and fileVersion is not None:
  296. return self.getDir(fileType, fileName, fileCode, fileVersion, response)
  297. else:
  298. fileCode = kwargs.get('fileCode', None)
  299. fileVersion = kwargs.get('fileVersion', None)
  300. print(fileCode, fileVersion)
  301. if fileCode != None and fileVersion != None:
  302. return self.getDir(fileType, fileName, fileCode, fileVersion)
  303. else:
  304. return response.json(903)
  305. else:
  306. fileCode = kwargs.get('fileCode', None)
  307. fileVersion = kwargs.get('fileVersion', None)
  308. if fileCode != None and fileVersion != None:
  309. return self.getDir(fileType, fileName, fileCode, fileVersion, response)
  310. else:
  311. return response.json(903)
  312. def getDir(self, fileType, fileName, fileCode, fileVersion, response):
  313. try:
  314. if fileCode != None and fileVersion != None:
  315. path = '/'.join((BASE_DIR, 'static/Upgrade', fileType, fileCode, fileVersion)).replace('\\', '/') + '/'
  316. else:
  317. if fileType != 'IPC' and fileType != 'DVR' and fileType != 'NVR' and fileType != 'XVR':
  318. path = '/'.join((BASE_DIR, "static/Upgrade", 'Other')).replace('\\', '/') + '/'
  319. if not os.path.exists(path):
  320. os.makedirs(path)
  321. file_name = path + str(fileName)
  322. if os.path.exists(file_name):
  323. os.remove(file_name)
  324. destination = open(file_name, 'wb+')
  325. for chunk in fileName.chunks():
  326. destination.write(chunk)
  327. destination.close()
  328. else:
  329. file_name = path + str(fileName)
  330. if os.path.exists(file_name):
  331. os.remove(file_name)
  332. destination = open(file_name, 'wb+')
  333. for chunk in fileName.chunks():
  334. destination.write(chunk)
  335. destination.close()
  336. except Exception as e:
  337. errorInfo = traceback.format_exc()
  338. print('上传文件错误: %s' % errorInfo)
  339. return response.json(700, {'details': repr(e)})
  340. else:
  341. index = file_name.find('static/')
  342. filePath = file_name[index:]
  343. return response.json(0, {'filePath': filePath})
  344. @csrf_exempt
  345. def addNewEquipmentVersionInterface(request):
  346. request.encoding = 'utf-8'
  347. response = ResponseObject()
  348. if request.method == "POST":
  349. request_dict = request.POST
  350. elif request.method == "GET":
  351. request_dict = request.GET
  352. else:
  353. return response.json(444)
  354. deviceContent = request_dict.get('content', None).encode('utf-8')
  355. token = request_dict.get('token', None)
  356. deviceContent = str(deviceContent, encoding='utf-8')
  357. deviceContent = deviceContent.replace(' ', ' ').replace('\'', '\"')
  358. if deviceContent is None:
  359. return response.json(444, 'content')
  360. tko = TokenObject(token)
  361. response.lang = tko.lang
  362. if tko.code != 0:
  363. return response.json(tko.code)
  364. userID = tko.userID
  365. own_perm = ModelService.check_perm(userID=userID, permID=220)
  366. if own_perm is False:
  367. return response.json(404)
  368. try:
  369. deviceData = json.loads(deviceContent)
  370. version = deviceData.get('version', None)
  371. filePath = deviceData.get('filePath', None)
  372. if version is None or filePath is None:
  373. return response.json(444, 'content')
  374. deviceData['filePath'] = ','.join(filePath)
  375. ev_qs = Equipment_Version(
  376. eid=CommonService.getUserID(getUser=False, setOTAID=True),
  377. **deviceData)
  378. ev_qs.save()
  379. except Exception as e:
  380. return response.json(444, repr(e))
  381. else:
  382. res = CommonService.qs_to_dict([ev_qs])
  383. return response.json(0, res)
  384. def showAllEquipmentVersion(userID, response):
  385. userValid = Device_User.objects.filter(userID=userID).order_by('-data_joined')
  386. if not userValid.exists():
  387. return response.json(104)
  388. own_perm = ModelService.check_perm(userID=userID, permID=240)
  389. if not own_perm:
  390. return response.json(404)
  391. qs = Equipment_Version.objects.all()
  392. res = CommonService.qs_to_dict(qs)
  393. return response.json(0, res)
  394. # 检测ota更新包
  395. @csrf_exempt
  396. def getNewVerInterface(request):
  397. response = ResponseObject()
  398. if request.method == "POST":
  399. request_dict = request.POST
  400. elif request.method == "GET":
  401. request_dict = request.GET
  402. else:
  403. return response.json(404)
  404. code = request_dict.get('code', None)
  405. token = request_dict.get('token', None)
  406. lang = request_dict.get('lang', None)
  407. if not code:
  408. return response.json(444, 'code')
  409. tko = TokenObject(token)
  410. response.lang = tko.lang
  411. if tko.code != 0:
  412. return response.json(tko.code)
  413. if lang == 'zh-Hans':
  414. equipmentValid = Equipment_Version.objects.filter(code=code, status=1, lang=lang).order_by(
  415. '-data_joined')
  416. else:
  417. equipmentValid = Equipment_Version.objects.filter(code=code, status=1, lang='en').order_by(
  418. '-data_joined')
  419. if equipmentValid.exists():
  420. ver = equipmentValid[0].softwareVersion
  421. equipment = equipmentValid[0]
  422. file_path = equipment.filePath
  423. # 检测ip进行替换
  424. url = SERVER_DOMAIN + 'OTA/downloadsPack/' + file_path + '?time=' + str(time.time())
  425. return response.json(0, {
  426. 'ver': ver,
  427. 'url': url,
  428. "Description": equipment.Description,
  429. })
  430. else:
  431. return response.json(902)
  432. # ota包上传
  433. class uploadOTAInterfaceView(TemplateView):
  434. @method_decorator(csrf_exempt)
  435. def dispatch(self, *args, **kwargs):
  436. return super(uploadOTAInterfaceView, self).dispatch(*args, **kwargs)
  437. def post(self, request, *args, **kwargs):
  438. request.encoding = 'utf-8'
  439. token = request.POST.get('token', None)
  440. fileName = request.FILES.get('fileName', None)
  441. fileType = request.POST.get('fileType', None)
  442. language = request.POST.get('language', None)
  443. return self.validate(token, fileName, fileType, language)
  444. def get(self, request, *args, **kwargs):
  445. request.encoding = 'gb2312'
  446. token = request.GET.get('token', None)
  447. fileName = request.FILES.get('fileName', None)
  448. fileType = request.GET.get('fileType', None)
  449. language = request.GET.get('language', None)
  450. return self.validate(token, fileName, fileType, language)
  451. def validate(self, token, fileName, fileType, language):
  452. response = ResponseObject()
  453. if not fileName or not fileType:
  454. return response.json(444, 'fileName,fileType')
  455. tko = TokenObject(token)
  456. response.lang = tko.lang
  457. if tko.code != 0:
  458. return response.json(tko.code)
  459. userID = tko.userID
  460. if not userID:
  461. return response.json(104)
  462. own_permission = ModelService.check_perm(userID=userID, permID=210)
  463. if own_permission is not True:
  464. return response.json(404)
  465. return self.upload_ota_file(fileName, response, fileType, language)
  466. def upload_ota_file(self, fileName, response, fileType, language):
  467. try:
  468. if not language:
  469. language = 0
  470. path = '/'.join((BASE_DIR, 'static/otapack', fileType, str(language))).replace('\\', '/') + '/'
  471. if not os.path.exists(path):
  472. os.makedirs(path)
  473. file_name = path + str(fileName)
  474. if os.path.exists(file_name):
  475. os.remove(file_name)
  476. destination = open(file_name, 'wb+')
  477. for chunk in fileName.chunks():
  478. destination.write(chunk)
  479. destination.close()
  480. else:
  481. file_name = path + str(fileName)
  482. if os.path.exists(file_name):
  483. os.remove(file_name)
  484. destination = open(file_name, 'wb+')
  485. for chunk in fileName.chunks():
  486. destination.write(chunk)
  487. destination.close()
  488. except Exception as e:
  489. errorInfo = traceback.format_exc()
  490. print('上传文件错误: %s' % errorInfo)
  491. return response.json(700, {'details': repr(e)})
  492. else:
  493. index = file_name.find('static/')
  494. filePath = file_name[index:]
  495. return response.json(0, {'filePath': filePath})
  496. # ota包下载
  497. @csrf_exempt
  498. def downloadOTAInterface(request, fullPath, *callback_args, **callback_kwargs):
  499. res = ResponseObject()
  500. print('fullPath:')
  501. print(fullPath)
  502. if fullPath:
  503. if os.path.isfile(fullPath):
  504. try:
  505. wrapper = FileWrapper(open(fullPath, 'rb'))
  506. response = HttpResponse(wrapper, content_type="application/octet-stream")
  507. response['Content-Length'] = os.path.getsize(fullPath)
  508. response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(fullPath)
  509. response['Content-MD5'] = getMD5orSHA265(fullPath)
  510. # 校验文件md5值
  511. response['Content-SHA265'] = getMD5orSHA265(fullPath, 'SHA265')
  512. response['Content-CRC32'] = getMD5orSHA265(fullPath, 'CRC32')
  513. response['Content-Error'] = res.formal(0)
  514. return response
  515. except Exception as e:
  516. return res.json(906, repr(e))
  517. else:
  518. return res.json(907)
  519. else:
  520. return res.json(444, 'fullPath')
  521. # ota包下载
  522. @csrf_exempt
  523. def downloadOTAInterfaceV2(request, fullPath, *callback_args, **callback_kwargs):
  524. res = ResponseObject()
  525. print('fullPath:')
  526. print(fullPath)
  527. # 解密base64的url的值
  528. # try:
  529. # fp = base64Object.get_certify_token(token=fullPath+'==')
  530. # print("解密结果:", fp)
  531. # user_de = base64Object.certify_token(key=fp, token=fullPath+'==')
  532. # if user_de:
  533. # print("验证结果:", user_de)
  534. # else:
  535. # return res.json(907)
  536. # except Exception as e:
  537. # return res.json(906, repr(e))
  538. # 解密url的token
  539. url_token = UrlTokenObject(fullPath)
  540. if ( '' == url_token.Url):
  541. print('过期了')
  542. return res.json(907)
  543. else:
  544. print (url_token.Url)
  545. fp = url_token.Url
  546. fullPath = fp[14:]
  547. if fullPath:
  548. if os.path.isfile(fullPath):
  549. try:
  550. wrapper = FileWrapper(open(fullPath, 'rb'))
  551. response = HttpResponse(wrapper, content_type="application/octet-stream")
  552. response['Content-Length'] = os.path.getsize(fullPath)
  553. response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(fullPath)
  554. response['Content-MD5'] = getMD5orSHA265(fullPath)
  555. # 校验文件md5值
  556. response['Content-SHA265'] = getMD5orSHA265(fullPath, 'SHA265')
  557. response['Content-CRC32'] = getMD5orSHA265(fullPath, 'CRC32')
  558. response['Content-Error'] = res.formal(0)
  559. return response
  560. except Exception as e:
  561. return res.json(906, repr(e))
  562. else:
  563. return res.json(907)
  564. else:
  565. return res.json(444, 'fullPath')