app.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files;
  27. class App {
  28. /**
  29. * @var \OC_L10N
  30. */
  31. private $l10n;
  32. /**
  33. * @var \OCP\INavigationManager
  34. */
  35. private static $navigationManager;
  36. /**
  37. * @var \OC\Files\View
  38. */
  39. private $view;
  40. public function __construct($view, $l10n) {
  41. $this->view = $view;
  42. $this->l10n = $l10n;
  43. }
  44. /**
  45. * Returns the app's navigation manager
  46. *
  47. * @return \OCP\INavigationManager
  48. */
  49. public static function getNavigationManager() {
  50. if (self::$navigationManager === null) {
  51. self::$navigationManager = new \OC\NavigationManager();
  52. }
  53. return self::$navigationManager;
  54. }
  55. /**
  56. * rename a file
  57. *
  58. * @param string $dir
  59. * @param string $oldname
  60. * @param string $newname
  61. * @return array
  62. */
  63. public function rename($dir, $oldname, $newname) {
  64. $result = array(
  65. 'success' => false,
  66. 'data' => NULL
  67. );
  68. $normalizedOldPath = \OC\Files\Filesystem::normalizePath($dir . '/' . $oldname);
  69. $normalizedNewPath = \OC\Files\Filesystem::normalizePath($dir . '/' . $newname);
  70. // rename to non-existing folder is denied
  71. if (!$this->view->file_exists($normalizedOldPath)) {
  72. $result['data'] = array(
  73. 'message' => $this->l10n->t('%s could not be renamed as it has been deleted', array($oldname)),
  74. 'code' => 'sourcenotfound',
  75. 'oldname' => $oldname,
  76. 'newname' => $newname,
  77. );
  78. }else if (!$this->view->file_exists($dir)) {
  79. $result['data'] = array('message' => (string)$this->l10n->t(
  80. 'The target folder has been moved or deleted.',
  81. array($dir)),
  82. 'code' => 'targetnotfound'
  83. );
  84. // rename to existing file is denied
  85. } else if ($this->view->file_exists($normalizedNewPath)) {
  86. $result['data'] = array(
  87. 'message' => $this->l10n->t(
  88. "The name %s is already used in the folder %s. Please choose a different name.",
  89. array($newname, $dir))
  90. );
  91. } else if (
  92. // rename to "." is denied
  93. $newname !== '.' and
  94. // THEN try to rename
  95. $this->view->rename($normalizedOldPath, $normalizedNewPath)
  96. ) {
  97. // successful rename
  98. $meta = $this->view->getFileInfo($normalizedNewPath);
  99. $meta = \OCA\Files\Helper::populateTags(array($meta));
  100. $fileInfo = \OCA\Files\Helper::formatFileInfo(current($meta));
  101. $fileInfo['path'] = dirname($normalizedNewPath);
  102. $result['success'] = true;
  103. $result['data'] = $fileInfo;
  104. } else {
  105. // rename failed
  106. $result['data'] = array(
  107. 'message' => $this->l10n->t('%s could not be renamed', array($oldname))
  108. );
  109. }
  110. return $result;
  111. }
  112. }