Exemplo n.º 1
0
 /**
  * rename a file
  *
  * @param string $dir
  * @param string $oldname
  * @param string $newname
  * @return array
  */
 public function rename($dir, $oldname, $newname)
 {
     $result = array('success' => false, 'data' => NULL);
     $normalizedOldPath = \OC\Files\Filesystem::normalizePath($dir . '/' . $oldname);
     $normalizedNewPath = \OC\Files\Filesystem::normalizePath($dir . '/' . $newname);
     // rename to non-existing folder is denied
     if (!$this->view->file_exists($normalizedOldPath)) {
         $result['data'] = array('message' => $this->l10n->t('%s could not be renamed as it has been deleted', array($oldname)), 'code' => 'sourcenotfound', 'oldname' => $oldname, 'newname' => $newname);
     } else {
         if (!$this->view->file_exists($dir)) {
             $result['data'] = array('message' => (string) $this->l10n->t('The target folder has been moved or deleted.', array($dir)), 'code' => 'targetnotfound');
             // rename to existing file is denied
         } else {
             if ($this->view->file_exists($normalizedNewPath)) {
                 $result['data'] = array('message' => $this->l10n->t("The name %s is already used in the folder %s. Please choose a different name.", array($newname, $dir)));
             } else {
                 if ($newname !== '.' and $this->view->rename($normalizedOldPath, $normalizedNewPath)) {
                     // successful rename
                     $meta = $this->view->getFileInfo($normalizedNewPath);
                     $meta = \OCA\Files\Helper::populateTags(array($meta));
                     $fileInfo = \OCA\Files\Helper::formatFileInfo(current($meta));
                     $fileInfo['path'] = dirname($normalizedNewPath);
                     $result['success'] = true;
                     $result['data'] = $fileInfo;
                 } else {
                     // rename failed
                     $result['data'] = array('message' => $this->l10n->t('%s could not be renamed', array($oldname)));
                 }
             }
         }
     }
     return $result;
 }
 /**
  * @NoAdminRequired
  * @NoCSRFRequired
  * @SSOCORS
  */
 public function getFileList($dir = null, $sortby = 'name', $sort = false)
 {
     \OCP\JSON::checkLoggedIn();
     \OC::$server->getSession()->close();
     // Load the files
     $dir = $dir ? (string) $dir : '';
     $dir = \OC\Files\Filesystem::normalizePath($dir);
     try {
         $dirInfo = \OC\Files\Filesystem::getFileInfo($dir);
         if (!$dirInfo || !$dirInfo->getType() === 'dir') {
             header('HTTP/1.0 404 Not Found');
             exit;
         }
         $data = array();
         $baseUrl = \OCP\Util::linkTo('files', 'index.php') . '?dir=';
         $permissions = $dirInfo->getPermissions();
         $sortDirection = $sort === 'desc';
         $mimetypeFilters = '';
         $files = [];
         if (is_array($mimetypeFilters) && count($mimetypeFilters)) {
             $mimetypeFilters = array_unique($mimetypeFilters);
             if (!in_array('httpd/unix-directory', $mimetypeFilters)) {
                 $mimetypeFilters[] = 'httpd/unix-directory';
             }
             foreach ($mimetypeFilters as $mimetypeFilter) {
                 $files = array_merge($files, \OCA\Files\Helper::getFiles($dir, $sortby, $sortDirection, $mimetypeFilter));
             }
             $files = \OCA\Files\Helper::sortFiles($files, $sortby, $sortDirection);
         } else {
             $files = \OCA\Files\Helper::getFiles($dir, $sortby, $sortDirection);
         }
         $files = \OCA\Files\Helper::populateTags($files);
         $data['directory'] = $dir;
         $data['files'] = \OCA\Files\Helper::formatFileInfos($files);
         $data['permissions'] = $permissions;
         return new DataResponse(array('data' => $data, 'status' => 'success'));
     } catch (\OCP\Files\StorageNotAvailableException $e) {
         \OCP\Util::logException('files', $e);
         return new DataResponse(array('data' => array('exception' => '\\OCP\\Files\\StorageNotAvailableException', 'message' => 'Storage not available'), 'status' => 'error'));
     } catch (\OCP\Files\StorageInvalidException $e) {
         \OCP\Util::logException('files', $e);
         return new DataResponse(array('data' => array('exception' => '\\OCP\\Files\\StorageInvalidException', 'message' => 'Storage invalid'), 'status' => 'error'));
     } catch (\Exception $e) {
         \OCP\Util::logException('files', $e);
         return new DataResponse(array('data' => array('exception' => '\\Exception', 'message' => 'Unknown error'), 'status' => 'error'));
     }
 }
Exemplo n.º 3
0
        if (!in_array('httpd/unix-directory', $mimetypeFilters)) {
            // append folder filter to be able to browse folders
            $mimetypeFilters[] = 'httpd/unix-directory';
        }
        // create filelist with mimetype filter - as getFiles only supports on
        // mimetype filter at once we will filter this folder for each
        // mimetypeFilter
        foreach ($mimetypeFilters as $mimetypeFilter) {
            $files = array_merge($files, \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection, $mimetypeFilter));
        }
        // sort the files accordingly
        $files = \OCA\Files\Helper::sortFiles($files, $sortAttribute, $sortDirection);
    } else {
        // create file list without mimetype filter
        $files = \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection);
    }
    $files = \OCA\Files\Helper::populateTags($files);
    $data['directory'] = $dir;
    $data['files'] = \OCA\Files\Helper::formatFileInfos($files);
    $data['permissions'] = $permissions;
    OCP\JSON::success(array('data' => $data));
} catch (\OCP\Files\StorageNotAvailableException $e) {
    \OCP\Util::logException('files', $e);
    OCP\JSON::error(array('data' => array('exception' => '\\OCP\\Files\\StorageNotAvailableException', 'message' => $l->t('Storage not available'))));
} catch (\OCP\Files\StorageInvalidException $e) {
    \OCP\Util::logException('files', $e);
    OCP\JSON::error(array('data' => array('exception' => '\\OCP\\Files\\StorageInvalidException', 'message' => $l->t('Storage invalid'))));
} catch (\Exception $e) {
    \OCP\Util::logException('files', $e);
    OCP\JSON::error(array('data' => array('exception' => '\\Exception', 'message' => $l->t('Unknown error'))));
}