upload.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #file = url + file_path.replace(Demeter.path + 'runtime', '')
  37. #self.write({'state':True,'file_path':file, 'src':file})
  38. def cropImage(file):
  39. img = Image.open(file)
  40. width = img.size[0]
  41. height = img.size[1]
  42. size = 0
  43. x = 0
  44. y = 0
  45. if width == height:
  46. return file
  47. elif width < height:
  48. size = width
  49. x = 0
  50. y = (height-width)/2
  51. else:
  52. size = height
  53. x = (width-height)/2
  54. y = 0
  55. #img.thumbnail((500, 500))
  56. crop = img.crop((x, y, size+x, size+y))
  57. file = file + '_crop_center.jpg'
  58. crop.save(file)
  59. return file