Skip to content
Snippets Groups Projects
Commit 00e2b460 authored by Michael Gapczynski's avatar Michael Gapczynski
Browse files

Fix 'most' Google Drive tests

parent 24b4806a
No related branches found
No related tags found
No related merge requests found
...@@ -96,7 +96,7 @@ class Google extends \OC\Files\Storage\Common { ...@@ -96,7 +96,7 @@ class Google extends \OC\Files\Storage\Common {
if (isset($this->driveFiles[$path])) { if (isset($this->driveFiles[$path])) {
$parentId = $this->driveFiles[$path]->getId(); $parentId = $this->driveFiles[$path]->getId();
} else { } else {
$q = "title='".$name."' and '".$parentId."' in parents"; $q = "title='".$name."' and '".$parentId."' in parents and trashed = false";
$result = $this->service->files->listFiles(array('q' => $q))->getItems(); $result = $this->service->files->listFiles(array('q' => $q))->getItems();
if (!empty($result)) { if (!empty($result)) {
// Google Drive allows files with the same name, ownCloud doesn't // Google Drive allows files with the same name, ownCloud doesn't
...@@ -132,6 +132,25 @@ class Google extends \OC\Files\Storage\Common { ...@@ -132,6 +132,25 @@ class Google extends \OC\Files\Storage\Common {
} }
} }
/**
* Set the Google_DriveFile object in the cache
* @param string $path
* @param Google_DriveFile|false $file
*/
private function setDriveFile($path, $file) {
$path = trim($this->root.$path, '/');
$this->driveFiles[$path] = $file;
if ($file === false) {
// Set all child paths as false
$len = strlen($path);
foreach ($this->driveFiles as $key => $file) {
if (substr($key, 0, $len) === $path) {
$this->driveFiles[$key] = false;
}
}
}
}
/** /**
* Write a log message to inform about duplicate file names * Write a log message to inform about duplicate file names
* @param string $path * @param string $path
...@@ -141,7 +160,8 @@ class Google extends \OC\Files\Storage\Common { ...@@ -141,7 +160,8 @@ class Google extends \OC\Files\Storage\Common {
$user = $about->getName(); $user = $about->getName();
\OCP\Util::writeLog('files_external', \OCP\Util::writeLog('files_external',
'Ignoring duplicate file name: '.$path.' on Google Drive for Google user: '.$user, 'Ignoring duplicate file name: '.$path.' on Google Drive for Google user: '.$user,
\OCP\Util::INFO); \OCP\Util::INFO
);
} }
/** /**
...@@ -165,6 +185,7 @@ class Google extends \OC\Files\Storage\Common { ...@@ -165,6 +185,7 @@ class Google extends \OC\Files\Storage\Common {
} }
public function mkdir($path) { public function mkdir($path) {
if (!$this->is_dir($path)) {
$parentFolder = $this->getDriveFile(dirname($path)); $parentFolder = $this->getDriveFile(dirname($path));
if ($parentFolder) { if ($parentFolder) {
$folder = new \Google_DriveFile(); $folder = new \Google_DriveFile();
...@@ -173,15 +194,33 @@ class Google extends \OC\Files\Storage\Common { ...@@ -173,15 +194,33 @@ class Google extends \OC\Files\Storage\Common {
$parent = new \Google_ParentReference(); $parent = new \Google_ParentReference();
$parent->setId($parentFolder->getId()); $parent->setId($parentFolder->getId());
$folder->setParents(array($parent)); $folder->setParents(array($parent));
return (bool)$this->service->files->insert($folder); $result = $this->service->files->insert($folder);
} else { if ($result) {
return false; $this->setDriveFile($path, $result);
} }
return (bool)$result;
}
}
return false;
} }
public function rmdir($path) { public function rmdir($path) {
if (trim($this->root.$path, '/') === '') {
$dir = $this->opendir($path);
while ($file = readdir($dir)) {
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
if (!$this->unlink($path.'/'.$file)) {
return false;
}
}
}
closedir($dir);
$this->driveFiles = array();
return true;
} else {
return $this->unlink($path); return $this->unlink($path);
} }
}
public function opendir($path) { public function opendir($path) {
// Remove leading and trailing slashes // Remove leading and trailing slashes
...@@ -196,12 +235,12 @@ class Google extends \OC\Files\Storage\Common { ...@@ -196,12 +235,12 @@ class Google extends \OC\Files\Storage\Common {
if ($pageToken !== true) { if ($pageToken !== true) {
$params['pageToken'] = $pageToken; $params['pageToken'] = $pageToken;
} }
$params['q'] = "'".$folder->getId()."' in parents"; $params['q'] = "'".$folder->getId()."' in parents and trashed = false";
$children = $this->service->files->listFiles($params); $children = $this->service->files->listFiles($params);
foreach ($children->getItems() as $child) { foreach ($children->getItems() as $child) {
$name = $child->getTitle(); $name = $child->getTitle();
// Check if this is a Google Doc i.e. no extension in name // Check if this is a Google Doc i.e. no extension in name
if ($child->getFileExtension() == '' if ($child->getFileExtension() === ''
&& $child->getMimeType() !== self::FOLDER && $child->getMimeType() !== self::FOLDER
) { ) {
$name .= '.'.$this->getGoogleDocExtension($child->getMimeType()); $name .= '.'.$this->getGoogleDocExtension($child->getMimeType());
...@@ -213,29 +252,22 @@ class Google extends \OC\Files\Storage\Common { ...@@ -213,29 +252,22 @@ class Google extends \OC\Files\Storage\Common {
} }
// Google Drive allows files with the same name, ownCloud doesn't // Google Drive allows files with the same name, ownCloud doesn't
// Prevent opendir() from returning any duplicate files // Prevent opendir() from returning any duplicate files
if (isset($this->driveFiles[$filepath]) && !isset($duplicates[$filepath])) { $key = array_search($name, $files);
// Save this key to unset later in case there are more than 2 duplicates if ($key !== false || isset($duplicates[$filepath])) {
$duplicates[$filepath] = $name; if (!isset($duplicates[$filepath])) {
$duplicates[$filepath] = true;
$this->setDriveFile($filepath, false);
unset($files[$key]);
$this->onDuplicateFileDetected($filepath);
}
} else { } else {
// Cache the Google_DriveFile for future use // Cache the Google_DriveFile for future use
$this->driveFiles[$filepath] = $child; $this->setDriveFile($filepath, $child);
$files[] = $name; $files[] = $name;
} }
} }
$pageToken = $children->getNextPageToken(); $pageToken = $children->getNextPageToken();
} }
// Remove all duplicate files
foreach ($duplicates as $filepath => $name) {
unset($this->driveFiles[$filepath]);
$key = array_search($name, $files);
unset($files[$key]);
$this->onDuplicateFileDetected($filepath);
}
// Reindex $files array if duplicates were removed
// This is necessary for \OC\Files\Stream\Dir
if (!empty($duplicates)) {
$files = array_values($files);
}
\OC\Files\Stream\Dir::register('google'.$path, $files); \OC\Files\Stream\Dir::register('google'.$path, $files);
return opendir('fakedir://google'.$path); return opendir('fakedir://google'.$path);
} else { } else {
...@@ -285,7 +317,7 @@ class Google extends \OC\Files\Storage\Common { ...@@ -285,7 +317,7 @@ class Google extends \OC\Files\Storage\Common {
} }
public function isReadable($path) { public function isReadable($path) {
return true; return $this->file_exists($path);
} }
public function isUpdatable($path) { public function isUpdatable($path) {
...@@ -304,7 +336,11 @@ class Google extends \OC\Files\Storage\Common { ...@@ -304,7 +336,11 @@ class Google extends \OC\Files\Storage\Common {
public function unlink($path) { public function unlink($path) {
$file = $this->getDriveFile($path); $file = $this->getDriveFile($path);
if ($file) { if ($file) {
return (bool)$this->service->files->trash($file->getId()); $result = $this->service->files->trash($file->getId());
if ($result) {
$this->setDriveFile($path, false);
}
return (bool)$result;
} else { } else {
return false; return false;
} }
...@@ -322,9 +358,16 @@ class Google extends \OC\Files\Storage\Common { ...@@ -322,9 +358,16 @@ class Google extends \OC\Files\Storage\Common {
$parent = new \Google_ParentReference(); $parent = new \Google_ParentReference();
$parent->setId($parentFolder2->getId()); $parent->setId($parentFolder2->getId());
$file->setParents(array($parent)); $file->setParents(array($parent));
} else {
return false;
}
} }
$result = $this->service->files->patch($file->getId(), $file);
if ($result) {
$this->setDriveFile($path1, false);
$this->setDriveFile($path2, $result);
} }
return (bool)$this->service->files->patch($file->getId(), $file); return (bool)$result;
} else { } else {
return false; return false;
} }
...@@ -361,7 +404,7 @@ class Google extends \OC\Files\Storage\Common { ...@@ -361,7 +404,7 @@ class Google extends \OC\Files\Storage\Common {
} }
} }
} }
return null; return false;
case 'w': case 'w':
case 'wb': case 'wb':
case 'a': case 'a':
...@@ -390,23 +433,28 @@ class Google extends \OC\Files\Storage\Common { ...@@ -390,23 +433,28 @@ class Google extends \OC\Files\Storage\Common {
$path = self::$tempFiles[$tmpFile]; $path = self::$tempFiles[$tmpFile];
$parentFolder = $this->getDriveFile(dirname($path)); $parentFolder = $this->getDriveFile(dirname($path));
if ($parentFolder) { if ($parentFolder) {
$file = new \Google_DriveFile();
$file->setTitle(basename($path));
$mimetype = \OC_Helper::getMimeType($tmpFile);
$file->setMimeType($mimetype);
$parent = new \Google_ParentReference();
$parent->setId($parentFolder->getId());
$file->setParents(array($parent));
// TODO Research resumable upload // TODO Research resumable upload
$mimetype = \OC_Helper::getMimeType($tmpFile);
$data = file_get_contents($tmpFile); $data = file_get_contents($tmpFile);
$params = array( $params = array(
'data' => $data, 'data' => $data,
'mimeType' => $mimetype, 'mimeType' => $mimetype,
); );
$result = false;
if ($this->file_exists($path)) { if ($this->file_exists($path)) {
$this->service->files->update($file->getId(), $file, $params); $file = $this->getDriveFile($path);
$result = $this->service->files->update($file->getId(), $file, $params);
} else { } else {
$this->service->files->insert($file, $params); $file = new \Google_DriveFile();
$file->setTitle(basename($path));
$file->setMimeType($mimetype);
$parent = new \Google_ParentReference();
$parent->setId($parentFolder->getId());
$file->setParents(array($parent));
$result = $this->service->files->insert($file, $params);
}
if ($result) {
$this->setDriveFile($path, $result);
} }
} }
unlink($tmpFile); unlink($tmpFile);
...@@ -444,19 +492,32 @@ class Google extends \OC\Files\Storage\Common { ...@@ -444,19 +492,32 @@ class Google extends \OC\Files\Storage\Common {
public function touch($path, $mtime = null) { public function touch($path, $mtime = null) {
$file = $this->getDriveFile($path); $file = $this->getDriveFile($path);
$result = false;
if ($file) { if ($file) {
if (isset($mtime)) { if (isset($mtime)) {
$file->setModifiedDate($mtime); $file->setModifiedDate($mtime);
$this->service->files->patch($file->getId(), $file, array( $result = $this->service->files->patch($file->getId(), $file, array(
'setModifiedDate' => true, 'setModifiedDate' => true,
)); ));
} else { } else {
return (bool)$this->service->files->touch($file->getId()); $result = $this->service->files->touch($file->getId());
} }
} else { } else {
return false; $parentFolder = $this->getDriveFile(dirname($path));
if ($parentFolder) {
$file = new \Google_DriveFile();
$file->setTitle(basename($path));
$parent = new \Google_ParentReference();
$parent->setId($parentFolder->getId());
$file->setParents(array($parent));
$result = $this->service->files->insert($file);
} }
} }
if ($result) {
$this->setDriveFile($path, $result);
}
return (bool)$result;
}
public function test() { public function test() {
if ($this->free_space('')) { if ($this->free_space('')) {
...@@ -500,7 +561,9 @@ class Google extends \OC\Files\Storage\Common { ...@@ -500,7 +561,9 @@ class Google extends \OC\Files\Storage\Common {
// Check if a file in this folder has been updated // Check if a file in this folder has been updated
// There is no way to filter by folder at the API level... // There is no way to filter by folder at the API level...
foreach ($changes->getItems() as $change) { foreach ($changes->getItems() as $change) {
foreach ($change->getFile()->getParents() as $parent) { $file = $change->getFile();
if ($file) {
foreach ($file->getParents() as $parent) {
if ($parent->getId() === $folderId) { if ($parent->getId() === $folderId) {
$result = true; $result = true;
// Check if there are changes in different folders // Check if there are changes in different folders
...@@ -511,6 +574,7 @@ class Google extends \OC\Files\Storage\Common { ...@@ -511,6 +574,7 @@ class Google extends \OC\Files\Storage\Common {
} }
} }
} }
}
$pageToken = $changes->getNextPageToken(); $pageToken = $changes->getNextPageToken();
} else { } else {
// Assuming the initial scan just occurred and changes are negligible // Assuming the initial scan just occurred and changes are negligible
......
<?php <?php
/** /**
* ownCloud * ownCloud
* *
...@@ -22,20 +21,23 @@ ...@@ -22,20 +21,23 @@
namespace Test\Files\Storage; namespace Test\Files\Storage;
require_once 'files_external/lib/google.php';
class Google extends Storage { class Google extends Storage {
private $config; private $config;
public function setUp() { protected function setUp() {
$id = uniqid();
$this->config = include('files_external/tests/config.php'); $this->config = include('files_external/tests/config.php');
if ( ! is_array($this->config) or ! isset($this->config['google']) or ! $this->config['google']['run']) { if (!is_array($this->config) || !isset($this->config['google'])
$this->markTestSkipped('Google backend not configured'); || !$this->config['google']['run']
) {
$this->markTestSkipped('Google Drive backend not configured');
} }
$this->config['google']['root'] .= '/' . $id; //make sure we have an new empty folder to work in
$this->instance = new \OC\Files\Storage\Google($this->config['google']); $this->instance = new \OC\Files\Storage\Google($this->config['google']);
} }
public function tearDown() { protected function tearDown() {
if ($this->instance) { if ($this->instance) {
$this->instance->rmdir('/'); $this->instance->rmdir('/');
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment