upload.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. def post(self, *args, **kwargs):
  31. url = self.request.protocol + "://" + self.request.host
  32. file_metas = self.request.files["file"]
  33. # print(file_metas)
  34. day = str(date.today())
  35. day = day.split('-')
  36. for meta in file_metas:
  37. #meta['filename']
  38. file_name = str(uuid.uuid5(uuid.uuid1(), 'file'))
  39. file_path = day[0] + '/' + day[1] + '/' + day[2]
  40. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
  41. with open(file_path, 'wb') as up:
  42. up.write(meta['body'])
  43. self.out('yes', {'src':url + file_path.replace(Demeter.path + 'runtime', '')})