upload.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web page
  5. name:upload.py
  6. author:rabin
  7. """
  8. from __load__ import *
  9. from datetime import *
  10. import os
  11. import uuid
  12. class index_path(Load):
  13. """
  14. @api {post} /upload 上传图片
  15. @apiVersion 1.0.0
  16. @apiName upload
  17. @apiGroup Common
  18. @apiParam {String} file 文件流名称
  19. @apiSuccess {Bool} status 状态
  20. @apiSuccess {String} msg 描述
  21. @apiSuccess {Number} code 状态码
  22. @apiSuccess {Object[]} data 返回数据
  23. @apiSuccess {String} data.src 图片地址
  24. @apiErrorExample 操作成功
  25. {"status": 1, "msg": "操作成功", "code": 0, "data": {"src":"url"}}
  26. @apiErrorExample 操作失败
  27. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  28. """
  29. @auth
  30. @Web.setting
  31. def post(self, *args, **kwargs):
  32. url = self.request.protocol + "://" + self.request.host
  33. file_metas = self.request.files["file"]
  34. # print(file_metas)
  35. day = str(date.today())
  36. day = day.split('-')
  37. for meta in file_metas:
  38. #meta['filename']
  39. file_name = str(uuid.uuid5(uuid.uuid1(), 'file'))
  40. file_path = day[0] + '/' + day[1] + '/' + day[2]
  41. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
  42. with open(file_path, 'wb') as up:
  43. up.write(meta['body'])
  44. self.out('yes', {'src':url + file_path.replace(Demeter.path + 'runtime', '')})