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);
     // rename to "/Shared" is denied
     if ($dir === '/' and $newname === 'Shared') {
         $result['data'] = array('message' => $this->l10n->t("Invalid folder name. Usage of 'Shared' is reserved."));
         // rename to existing file is denied
     } else {
         if ($this->view->file_exists($dir . '/' . $newname)) {
             $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 !($dir === '/' and $oldname === 'Shared') and $this->view->rename($dir . '/' . $oldname, $dir . '/' . $newname)) {
                 // successful rename
                 $meta = $this->view->getFileInfo($dir . '/' . $newname);
                 if ($meta['mimetype'] === 'httpd/unix-directory') {
                     $meta['type'] = 'dir';
                 } else {
                     $meta['type'] = 'file';
                 }
                 $fileinfo = array('id' => $meta['fileid'], 'mime' => $meta['mimetype'], 'size' => $meta['size'], 'etag' => $meta['etag'], 'directory' => $dir, 'name' => $newname, 'isPreviewAvailable' => \OC::$server->getPreviewManager()->isMimeSupported($meta['mimetype']), 'icon' => \OCA\Files\Helper::determineIcon($meta));
                 $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;
 }
Exemplo n.º 2
0
 public function testGetFilesByTagMultiple()
 {
     $tagName = 'MyTagName';
     $fileInfo1 = new FileInfo('/root.txt', $this->getMockBuilder('\\OC\\Files\\Storage\\Storage')->disableOriginalConstructor()->getMock(), '/var/www/root.txt', ['mtime' => 55, 'mimetype' => 'application/pdf', 'size' => 1234, 'etag' => 'MyEtag'], $this->getMockBuilder('\\OCP\\Files\\Mount\\IMountPoint')->disableOriginalConstructor()->getMock());
     $fileInfo2 = new FileInfo('/root.txt', $this->getMockBuilder('\\OC\\Files\\Storage\\Storage')->disableOriginalConstructor()->getMock(), '/var/www/some/sub.txt', ['mtime' => 999, 'mimetype' => 'application/binary', 'size' => 9876, 'etag' => 'SubEtag'], $this->getMockBuilder('\\OCP\\Files\\Mount\\IMountPoint')->disableOriginalConstructor()->getMock());
     $this->tagService->expects($this->once())->method('getFilesByTag')->with($this->equalTo([$tagName]))->will($this->returnValue([$fileInfo1, $fileInfo2]));
     $expected = new DataResponse(['files' => [['id' => null, 'parentId' => null, 'date' => \OCP\Util::formatDate(55), 'mtime' => 55000, 'icon' => \OCA\Files\Helper::determineIcon($fileInfo1), 'name' => 'root.txt', 'permissions' => null, 'mimetype' => 'application/pdf', 'size' => 1234, 'type' => 'file', 'etag' => 'MyEtag', 'path' => '/', 'tags' => [['MyTagName']]], ['id' => null, 'parentId' => null, 'date' => \OCP\Util::formatDate(999), 'mtime' => 999000, 'icon' => \OCA\Files\Helper::determineIcon($fileInfo2), 'name' => 'root.txt', 'permissions' => null, 'mimetype' => 'application/binary', 'size' => 9876, 'type' => 'file', 'etag' => 'SubEtag', 'path' => '/', 'tags' => [['MyTagName']]]]]);
     $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName]));
 }
Exemplo n.º 3
0
 /**
  * Retrieves the contents of a trash bin directory.
  * @param string $dir path to the directory inside the trashbin
  * or empty to retrieve the root of the trashbin
  * @return array of files
  */
 public static function getTrashFiles($dir)
 {
     $result = array();
     $user = \OCP\User::getUser();
     if ($dir && $dir !== '/') {
         $view = new \OC_Filesystemview('/' . $user . '/files_trashbin/files');
         $dirContent = $view->opendir($dir);
         if ($dirContent === false) {
             return null;
         }
         if (is_resource($dirContent)) {
             while (($entryName = readdir($dirContent)) !== false) {
                 if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
                     $pos = strpos($dir . '/', '/', 1);
                     $tmp = substr($dir, 0, $pos);
                     $pos = strrpos($tmp, '.d');
                     $timestamp = substr($tmp, $pos + 2);
                     $result[] = array('id' => $entryName, 'timestamp' => $timestamp, 'mime' => $view->getMimeType($dir . '/' . $entryName), 'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file', 'location' => $dir);
                 }
             }
             closedir($dirContent);
         }
     } else {
         $query = \OC_DB::prepare('SELECT `id`,`location`,`timestamp`,`type`,`mime` FROM `*PREFIX*files_trash` WHERE `user` = ?');
         $result = $query->execute(array($user))->fetchAll();
     }
     $files = array();
     $id = 0;
     foreach ($result as $r) {
         $i = array();
         $i['id'] = $id++;
         $i['name'] = $r['id'];
         $i['date'] = \OCP\Util::formatDate($r['timestamp']);
         $i['timestamp'] = $r['timestamp'];
         $i['etag'] = $i['timestamp'];
         // dummy etag
         $i['mimetype'] = $r['mime'];
         $i['type'] = $r['type'];
         if ($i['type'] === 'file') {
             $fileinfo = pathinfo($r['id']);
             $i['basename'] = $fileinfo['filename'];
             $i['extension'] = isset($fileinfo['extension']) ? '.' . $fileinfo['extension'] : '';
         }
         $i['directory'] = $r['location'];
         if ($i['directory'] === '/') {
             $i['directory'] = '';
         }
         $i['permissions'] = \OCP\PERMISSION_READ;
         $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($r['mime']);
         $i['icon'] = \OCA\Files\Helper::determineIcon($i);
         $files[] = $i;
     }
     usort($files, array('\\OCA\\Files\\Helper', 'fileCmp'));
     return $files;
 }
Exemplo n.º 4
0
function determineIcon($file, $sharingRoot, $sharingToken)
{
    // for folders we simply reuse the files logic
    if ($file['type'] == 'dir') {
        return \OCA\Files\Helper::determineIcon($file);
    }
    $relativePath = substr($file['path'], 6);
    $relativePath = substr($relativePath, strlen($sharingRoot));
    if ($file['isPreviewAvailable']) {
        return OCP\publicPreview_icon($relativePath, $sharingToken) . '&c=' . $file['etag'];
    }
    return OCP\mimetype_icon($file['mimetype']);
}
Exemplo n.º 5
0
 /**
  * Retrieves the contents of the given directory and
  * returns it as a sorted array.
  * @param string $dir path to the directory
  * @return array of files
  */
 public static function getFiles($dir)
 {
     $content = \OC\Files\Filesystem::getDirectoryContent($dir);
     $files = array();
     foreach ($content as $i) {
         $i['date'] = \OCP\Util::formatDate($i['mtime']);
         if ($i['type'] === 'file') {
             $fileinfo = pathinfo($i['name']);
             $i['basename'] = $fileinfo['filename'];
             if (!empty($fileinfo['extension'])) {
                 $i['extension'] = '.' . $fileinfo['extension'];
             } else {
                 $i['extension'] = '';
             }
         }
         $i['directory'] = $dir;
         $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($i['mimetype']);
         $i['icon'] = \OCA\Files\Helper::determineIcon($i);
         $files[] = $i;
     }
     usort($files, array('\\OCA\\Files\\Helper', 'fileCmp'));
     return $files;
 }
Exemplo n.º 6
0
 /**
  * Formats the file info to be returned as JSON to the client.
  *
  * @param \OCP\Files\FileInfo $i
  * @return array formatted file info
  */
 public static function formatFileInfo(FileInfo $i)
 {
     $entry = array();
     $entry['id'] = $i['fileid'];
     $entry['parentId'] = $i['parent'];
     $entry['date'] = \OCP\Util::formatDate($i['mtime']);
     $entry['mtime'] = $i['mtime'] * 1000;
     // only pick out the needed attributes
     $entry['icon'] = \OCA\Files\Helper::determineIcon($i);
     if (\OC::$server->getPreviewManager()->isAvailable($i)) {
         $entry['isPreviewAvailable'] = true;
     }
     $entry['name'] = $i->getName();
     $entry['permissions'] = $i['permissions'];
     $entry['mimetype'] = $i['mimetype'];
     $entry['size'] = $i['size'];
     $entry['type'] = $i['type'];
     $entry['etag'] = $i['etag'];
     if (isset($i['tags'])) {
         $entry['tags'] = $i['tags'];
     }
     if (isset($i['displayname_owner'])) {
         $entry['shareOwner'] = $i['displayname_owner'];
     }
     if (isset($i['is_share_mount_point'])) {
         $entry['isShareMountPoint'] = $i['is_share_mount_point'];
     }
     $mountType = null;
     if ($i->isShared()) {
         $mountType = 'shared';
     } else {
         if ($i->isMounted()) {
             $mountType = 'external';
         }
     }
     if ($mountType !== null) {
         if ($i->getInternalPath() === '') {
             $mountType .= '-root';
         }
         $entry['mountType'] = $mountType;
     }
     if (isset($i['extraData'])) {
         $entry['extraData'] = $i['extraData'];
     }
     return $entry;
 }
Exemplo n.º 7
0
 /**
  * Formats the file info to be returned as JSON to the client.
  *
  * @param \OCP\Files\FileInfo $i
  * @return array formatted file info
  */
 public static function formatFileInfo(FileInfo $i)
 {
     $entry = array();
     $entry['id'] = $i['fileid'];
     $entry['parentId'] = $i['parent'];
     $entry['date'] = \OCP\Util::formatDate($i['mtime']);
     $entry['mtime'] = $i['mtime'] * 1000;
     // only pick out the needed attributes
     $entry['icon'] = \OCA\Files\Helper::determineIcon($i);
     if (\OC::$server->getPreviewManager()->isAvailable($i)) {
         $entry['isPreviewAvailable'] = true;
     }
     $entry['name'] = $i->getName();
     $entry['permissions'] = $i['permissions'];
     $entry['mimetype'] = $i['mimetype'];
     $entry['size'] = $i['size'];
     $entry['type'] = $i['type'];
     $entry['etag'] = $i['etag'];
     //$entry['mount'] = $i->getStorage()->getId();
     $query = \OC_DB::prepare('SELECT `uid_owner`
                                     FROM
                                     `*PREFIX*share`
                                     WHERE
                                     `item_source` = ?');
     $result = $query->execute(array($entry['id']));
     if (\OCP\DB::isError($result)) {
         \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage(), \OC_Log::ERROR);
     } else {
         while ($row = $result->fetchRow()) {
             $entry['owner'] = $row['uid_owner'];
         }
     }
     if (isset($i['tags'])) {
         $entry['tags'] = $i['tags'];
     }
     if (isset($i['displayname_owner'])) {
         $entry['shareOwner'] = $i['displayname_owner'];
         $query = \OC_DB::prepare('SELECT `directory_uuid`
                                     FROM
                                     `*PREFIX*ldap_user_mapping`
                                     WHERE
                                     `ldap_dn` LIKE LOWER("cn="?"%")');
         $result = $query->execute(array($entry['shareOwner']));
         if (\OCP\DB::isError($result)) {
             \OCP\Util::writeLog('OCP\\Share', \OC_DB::getErrorMessage(), \OC_Log::ERROR);
             if ($entry['shareOwner'] == 'admin') {
                 $entry['storage'] = 'admin';
             }
         } else {
             while ($row = $result->fetchRow()) {
                 $entry['storage'] = $row['directory_uuid'];
             }
             if ($entry['shareOwner'] == 'admin') {
                 $entry['storage'] = 'admin';
             }
         }
     }
     if (isset($i['is_share_mount_point'])) {
         $entry['isShareMountPoint'] = $i['is_share_mount_point'];
     }
     $mountType = null;
     if ($i->isShared()) {
         $mountType = 'shared';
     } else {
         if ($i->isMounted()) {
             $mountType = 'external';
         }
     }
     if ($mountType !== null) {
         if ($i->getInternalPath() === '') {
             $mountType .= '-root';
         }
         $entry['mountType'] = $mountType;
     }
     if (isset($i['extraData'])) {
         $entry['extraData'] = $i['extraData'];
     }
     return $entry;
 }
Exemplo n.º 8
0
        $file['mimetype_icon'] = \OCA\Files\Helper::determineIcon($file);
        $files[] = $file;
    }
}
if (is_array($mimetypes) && count($mimetypes)) {
    foreach ($mimetypes as $mimetype) {
        foreach (\OC\Files\Filesystem::getDirectoryContent($dir, $mimetype) as $file) {
            $file['directory'] = $dir;
            $file['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($file['mimetype']);
            $file["date"] = OCP\Util::formatDate($file["mtime"]);
            $file['mimetype_icon'] = \OCA\Files\Helper::determineIcon($file);
            $files[] = $file;
        }
    }
} else {
    foreach (\OC\Files\Filesystem::getDirectoryContent($dir) as $file) {
        $file['directory'] = $dir;
        $file['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($file['mimetype']);
        $file["date"] = OCP\Util::formatDate($file["mtime"]);
        $file['mimetype_icon'] = \OCA\Files\Helper::determineIcon($file);
        $files[] = $file;
    }
}
// Sort by name
usort($files, function ($a, $b) {
    if ($a['name'] === $b['name']) {
        return 0;
    }
    return $a['name'] < $b['name'] ? -1 : 1;
});
OC_JSON::success(array('data' => $files));