You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.7 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import os
  20. import re
  21. import io
  22. import sys
  23. import base64
  24. import shutil
  25. import urllib
  26. import logging
  27. import hashlib
  28. import binascii
  29. import tempfile
  30. import mimetypes
  31. import unicodedata
  32. from odoo.tools import human_size
  33. from odoo.tools.mimetypes import guess_mimetype
  34. _logger = logging.getLogger(__name__)
  35. #----------------------------------------------------------
  36. # File Helper
  37. #----------------------------------------------------------
  38. def slugify(value):
  39. value = str(unicodedata.normalize('NFKD', value))
  40. value = str(value.encode('ascii', 'ignore'))
  41. value = str(re.sub('[^\w\s-]', '', value))
  42. value = str(re.sub('[-\s]+', '-', value))
  43. value = value.strip().lower()
  44. return value
  45. def check_name(name):
  46. tmp_dir = tempfile.mkdtemp()
  47. try:
  48. open(os.path.join(tmp_dir, name), 'a').close()
  49. except IOError:
  50. return False
  51. finally:
  52. shutil.rmtree(tmp_dir)
  53. return True
  54. def compute_name(name, suffix, escape_suffix):
  55. if escape_suffix:
  56. name, extension = os.path.splitext(name)
  57. return "%s(%s)%s" % (name, suffix, extension)
  58. else:
  59. return "%s(%s)" % (name, suffix)
  60. def unique_name(name, names, escape_suffix=False):
  61. if not name in names:
  62. return name
  63. else:
  64. suffix = 1
  65. name = compute_name(name, suffix, escape_suffix)
  66. while name in names:
  67. suffix += 1
  68. name = compute_name(name, suffix, escape_suffix)
  69. return name
  70. def unique_files(files):
  71. ufiles = []
  72. unames = []
  73. for file in files:
  74. uname = unique_name(file[0], unames, escape_suffix=True)
  75. ufiles.append((uname, file[1]))
  76. unames.append(uname)
  77. return ufiles
  78. def guess_extension(filename=None, mimetype=None, binary=None):
  79. extension = filename and os.path.splitext(filename)[1][1:].strip().lower()
  80. if not extension and mimetype:
  81. extension = mimetypes.guess_extension(mimetype)[1:].strip().lower()
  82. if not extension and binary:
  83. mimetype = guess_mimetype(binary, default="")
  84. extension = mimetypes.guess_extension(mimetype)[1:].strip().lower()
  85. return extension
  86. #----------------------------------------------------------
  87. # System Helper
  88. #----------------------------------------------------------
  89. def ensure_path_directories(path):
  90. directory_path = os.path.dirname(path)
  91. if not os.path.exists(directory_path):
  92. os.makedirs(directory_path)
  93. def remove_empty_directories(path):
  94. if not os.path.isdir(path):
  95. return
  96. entries = os.listdir(path)
  97. if len(entries) > 0:
  98. for entry in entries:
  99. subpath = os.path.join(path, entry)
  100. if os.path.isdir(subpath):
  101. self._remove_empty_directories(subpath)
  102. else:
  103. os.rmdir(path)