gooderp18绿色标准版
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.

105 line
2.9KB

  1. ##########################################################################
  2. #
  3. # pgAdmin 4 - PostgreSQL Tools
  4. #
  5. # Copyright (C) 2013 - 2020, The pgAdmin Development Team
  6. # This software is released under the PostgreSQL Licence
  7. #
  8. #########################################################################
  9. """This file contains functions fetching different utility paths."""
  10. import os
  11. from flask import current_app, url_for
  12. from flask_security import current_user, login_required
  13. @login_required
  14. def get_storage_directory():
  15. import config
  16. if config.SERVER_MODE is not True:
  17. return None
  18. storage_dir = getattr(
  19. config, 'STORAGE_DIR',
  20. os.path.join(
  21. os.path.realpath(
  22. os.path.expanduser('~/.pgadmin/')
  23. ), 'storage'
  24. )
  25. )
  26. if storage_dir is None:
  27. return None
  28. username = current_user.username.split('@')[0]
  29. if len(username) == 0 or username[0].isdigit():
  30. username = 'pga_user_' + username
  31. # Figure out the old-style storage directory name
  32. old_storage_dir = os.path.join(
  33. storage_dir.decode('utf-8') if hasattr(storage_dir, 'decode')
  34. else storage_dir,
  35. username
  36. )
  37. # Figure out the new style storage directory name
  38. storage_dir = os.path.join(
  39. storage_dir.decode('utf-8') if hasattr(storage_dir, 'decode')
  40. else storage_dir,
  41. current_user.username.replace('@', '_')
  42. )
  43. # Rename an old-style storage directory, if the new style doesn't exist
  44. if os.path.exists(old_storage_dir) and not os.path.exists(storage_dir):
  45. current_app.logger.warning(
  46. 'Renaming storage directory %s to %s.',
  47. old_storage_dir, storage_dir
  48. )
  49. os.rename(old_storage_dir, storage_dir)
  50. if not os.path.exists(storage_dir):
  51. os.makedirs(storage_dir, int('700', 8))
  52. return storage_dir
  53. def init_app(app):
  54. import config
  55. if config.SERVER_MODE is not True:
  56. return None
  57. storage_dir = getattr(
  58. config, 'STORAGE_DIR',
  59. os.path.join(
  60. os.path.realpath(
  61. os.path.expanduser('~/.pgadmin/')
  62. ), 'storage'
  63. )
  64. )
  65. if storage_dir and not os.path.isdir(storage_dir):
  66. if os.path.exists(storage_dir):
  67. raise Exception(
  68. 'The path specified for the storage directory is not a '
  69. 'directory.'
  70. )
  71. os.makedirs(storage_dir, int('700', 8))
  72. if storage_dir and not os.access(storage_dir, os.W_OK | os.R_OK):
  73. raise Exception(
  74. 'The user does not have permission to read and write to the '
  75. 'specified storage directory.'
  76. )
  77. def get_cookie_path():
  78. cookie_root_path = '/'
  79. pgadmin_root_path = url_for('browser.index')
  80. if pgadmin_root_path != '/browser/':
  81. cookie_root_path = pgadmin_root_path.replace(
  82. '/browser/', ''
  83. )
  84. return cookie_root_path
上海开阖软件有限公司 沪ICP备12045867号-1