file_operation.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # coding=utf-8
  2. import os
  3. import time
  4. import requests
  5. from django.conf import settings
  6. from django.utils import timezone
  7. from django.utils.deconstruct import deconstructible
  8. def UploadFile(file, upload_path):
  9. upload_path = PathAndRename(upload_path)
  10. filename = "%s%s.%s" % (
  11. upload_path.path,
  12. timezone.now().strftime('%Y%m%d%H%M%S%f'),
  13. file.name.split('.')[-1]
  14. )
  15. filename = filename.lower()
  16. full_filename = "%s%s" % (settings.MEDIA_ROOT, filename)
  17. with open(full_filename, 'wb+') as destination:
  18. for chunk in file.chunks():
  19. destination.write(chunk)
  20. return filename
  21. def DownloadFace(url, save_path, ext):
  22. upload_path = PathAndRename(save_path)
  23. filename = "%s%s.%s" % (
  24. upload_path.path,
  25. timezone.now().strftime('%Y%m%d%H%M%S%f'),
  26. ext
  27. )
  28. filename = filename.lower()
  29. full_filename = "%s/%s" % (settings.MEDIA_ROOT, filename)
  30. response = requests.get(url)
  31. img = response.content
  32. with open(full_filename, 'wb+') as destination:
  33. destination.write(img)
  34. return filename
  35. def DeleteFile(filename):
  36. img_path = '%s%s' % (settings.UIS_ROOT, filename)
  37. try:
  38. if os.path.exists(img_path):
  39. os.remove(img_path)
  40. except:
  41. import traceback
  42. traceback.print_exc()
  43. pass
  44. @deconstructible
  45. class PathAndRename(object):
  46. def __init__(self, sub_path):
  47. self.path = sub_path
  48. self.full_path = "%s/%s" % (settings.MEDIA_ROOT, sub_path)
  49. if not os.path.exists(self.full_path):
  50. os.makedirs(self.full_path)
  51. def __call__(self, instance, filename):
  52. ext = filename.split('.')[-1]
  53. t = timezone.now().strftime('%Y%m%d%H%M%S%f')
  54. if instance.pk:
  55. filename = '{}-{}.{}'.format(instance.pk, t, ext)
  56. else:
  57. filename = '{}.{}'.format(t, ext)
  58. return os.path.join(self.path, filename)
  59. @deconstructible
  60. class CertPath(object):
  61. def __init__(self, sub_path):
  62. self.path = sub_path
  63. self.full_path = "%s/%s" % (settings.MEDIA_ROOT, sub_path)
  64. if not os.path.exists(self.full_path):
  65. os.makedirs(self.full_path)
  66. def __call__(self, instance, filename):
  67. return os.path.join(self.path, filename)
  68. def attachment_save(export_data, name):
  69. now = int(time.time())
  70. # 删除过期文件
  71. # files = os.listdir(settings.EXPORT_ROOT)
  72. # for file in files:
  73. # filePath = os.path.join(settings.EXPORT_ROOT, file)
  74. #
  75. # print(11111111111, filePath)
  76. # if os.path.isfile(filePath):
  77. # last = int(os.stat(filePath).st_atime)
  78. # if now - last > 180:
  79. # os.remove(filePath)
  80. # print(filePath + " was removed!")
  81. filename = '{}.xlsx'.format(name)
  82. full_filename = os.path.join(settings.EXPORT_ROOT, filename)
  83. open(full_filename, 'wb').write(getattr(export_data, 'xlsx'))
  84. return settings.EXPORT_URL + filename