upload.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #from PIL import Image
  13. class upload_path(Load):
  14. @Web.auth
  15. @Web.setting
  16. def post(self, *args, **kwargs):
  17. url = self.request.protocol + "://" + self.request.host
  18. url = 'http://admin.nongxiaohe.com'
  19. file_metas = self.request.files["file"]
  20. #crop = self.input('crop', 1)
  21. crop = False
  22. # print(file_metas)
  23. day = str(date.today())
  24. day = day.split('-')
  25. for meta in file_metas:
  26. #meta['filename']
  27. file_name = str(uuid.uuid5(uuid.uuid1(), 'file'))
  28. file_path = day[0] + '/' + day[1] + '/' + day[2]
  29. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
  30. with open(file_path, 'wb') as up:
  31. up.write(meta['body'])
  32. if crop:
  33. #裁图类型,暂时居中裁图
  34. file_path = cropImage(file_path)
  35. #self.out('yes', {'src':url + file_path.replace(Demeter.path + 'runtime', '')})
  36. self.write({'state':True,'file_path':url + file_path.replace(Demeter.path + 'runtime', '')})
  37. def cropImage(file):
  38. img = Image.open(file)
  39. width = img.size[0]
  40. height = img.size[1]
  41. size = 0
  42. x = 0
  43. y = 0
  44. if width == height:
  45. return file
  46. elif width < height:
  47. size = width
  48. x = 0
  49. y = (height-width)/2
  50. else:
  51. size = height
  52. x = (width-height)/2
  53. y = 0
  54. #img.thumbnail((500, 500))
  55. crop = img.crop((x, y, size+x, size+y))
  56. file = file + '_crop_center.jpg'
  57. crop.save(file)
  58. return file