More work on Google Docs storage backend
This commit is contained in:
parent
2754cac21f
commit
02f15e9a03
|
@ -20,12 +20,15 @@
|
||||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class OC_Filestorage_Google {
|
require_once 'common.inc.php';
|
||||||
|
|
||||||
|
class OC_Filestorage_Google extends OC_Filestorage_Common {
|
||||||
|
|
||||||
private $datadir;
|
private $datadir;
|
||||||
private $consumer;
|
private $consumer;
|
||||||
private $oauth_token;
|
private $oauth_token;
|
||||||
private $sig_method;
|
private $sig_method;
|
||||||
|
private $entries;
|
||||||
|
|
||||||
public function __construct($arguments) {
|
public function __construct($arguments) {
|
||||||
$this->datadir = $arguments['datadir'];
|
$this->datadir = $arguments['datadir'];
|
||||||
|
@ -34,6 +37,7 @@ class OC_Filestorage_Google {
|
||||||
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
|
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
|
||||||
$this->oauth_token = new OAuthToken($arguments['token'], $arguments['token_secret']);
|
$this->oauth_token = new OAuthToken($arguments['token'], $arguments['token_secret']);
|
||||||
$this->sig_method = new OAuthSignatureMethod_HMAC_SHA1();
|
$this->sig_method = new OAuthSignatureMethod_HMAC_SHA1();
|
||||||
|
$this->entries = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function sendRequest($feedUri, $http_method, $postData = null) {
|
private function sendRequest($feedUri, $http_method, $postData = null) {
|
||||||
|
@ -66,32 +70,59 @@ class OC_Filestorage_Google {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getResource($path) {
|
private function getResource($path) {
|
||||||
// TODO Look up google docs query caching/only send back if changes occured
|
if (array_key_exists($path, $this->entries)) {
|
||||||
// TODO Look inside of collections for specific file
|
return $this->entries[$path];
|
||||||
// TODO Strip extension
|
} else {
|
||||||
$title = basename($path);
|
$title = basename($path);
|
||||||
return $this->sendRequest('https://docs.google.com/feeds/default/private/full?showfolders=true&title='.$title.'&title-exact=true', 'GET');
|
$dom = $this->sendRequest('https://docs.google.com/feeds/default/private/full?showfolders=true&title='.$title, 'GET');
|
||||||
|
// Check if request was successful and entry exists
|
||||||
|
if ($dom && $entry = $dom->getElementsByTagName('entry')->item(0)) {
|
||||||
|
$this->entries[$path] = $entry;
|
||||||
|
return $entry;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getExtension($entry) {
|
||||||
|
$mimetype = $this->getMimeType('', $entry);
|
||||||
|
switch($mimetype) {
|
||||||
|
case 'httpd/unix-directory':
|
||||||
|
return '';
|
||||||
|
case 'application/vnd.oasis.opendocument.text':
|
||||||
|
return 'odt';
|
||||||
|
case 'application/vnd.oasis.opendocument.spreadsheet':
|
||||||
|
return 'ods';
|
||||||
|
case 'application/vnd.oasis.opendocument.presentation':
|
||||||
|
return 'pptx';
|
||||||
|
case 'text/html':
|
||||||
|
return 'html';
|
||||||
|
default:
|
||||||
|
return 'html';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function mkdir($path) {
|
public function mkdir($path) {
|
||||||
$dir = dirname($path);
|
$dir = dirname($path);
|
||||||
// Check if path parent is root directory
|
// Check if path parent is root directory
|
||||||
if ($dir == '/' || $dir == '\.' || $dir == '.') {
|
if ($dir == '/' || $dir == '\.' || $dir == '.') {
|
||||||
$feedUri = 'https://docs.google.com/feeds/default/private/full';
|
$feedUri = 'https://docs.google.com/feeds/default/private/full';
|
||||||
// Get parent content link
|
// Get parent content link
|
||||||
} else {
|
} else if ($dom = $this->getResource(basename($dir))) {
|
||||||
$dom = $this->getResource(basename($dir));
|
|
||||||
$feedUri = $dom->getElementsByTagName('content')->item(0)->getAttribute('src');
|
$feedUri = $dom->getElementsByTagName('content')->item(0)->getAttribute('src');
|
||||||
}
|
}
|
||||||
$title = basename($path);
|
if (isset($feedUri)) {
|
||||||
// Construct post data
|
$title = basename($path);
|
||||||
$postData = '<?xml version="1.0" encoding="UTF-8"?>';
|
// Construct post data
|
||||||
$postData .= '<entry xmlns="http://www.w3.org/2005/Atom">';
|
$postData = '<?xml version="1.0" encoding="UTF-8"?>';
|
||||||
$postData .= '<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/docs/2007#folder"/>';
|
$postData .= '<entry xmlns="http://www.w3.org/2005/Atom">';
|
||||||
$postData .= '<title>'.$title.'</title>';
|
$postData .= '<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/docs/2007#folder"/>';
|
||||||
$postData .= '</entry>';
|
$postData .= '<title>'.$title.'</title>';
|
||||||
if ($dom = $this->sendRequest($feedUri, 'POST', $postData)) {
|
$postData .= '</entry>';
|
||||||
return true;
|
if ($dom = $this->sendRequest($feedUri, 'POST', $postData)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -102,106 +133,87 @@ class OC_Filestorage_Google {
|
||||||
|
|
||||||
public function opendir($path) {
|
public function opendir($path) {
|
||||||
if ($path == '' || $path == '/') {
|
if ($path == '' || $path == '/') {
|
||||||
$resource = 'https://docs.google.com/feeds/default/private/full/folder%3Aroot/contents';
|
$next = 'https://docs.google.com/feeds/default/private/full/folder%3Aroot/contents';
|
||||||
$dom = $this->sendRequest('https://docs.google.com/feeds/default/private/full/folder%3Aroot/contents', 'GET');
|
|
||||||
} else {
|
} else {
|
||||||
$dom = $this->getResource($path);
|
if ($entry = $this->getResource($path)) {
|
||||||
|
$collectionId = $entry->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'resourceId')->item(0)->nodeValue;
|
||||||
|
$next = 'https://docs.google.com/feeds/default/private/full/folder%3A'.$collectionId.'/contents';
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
global $FAKEDIRS;
|
|
||||||
$files = array();
|
$files = array();
|
||||||
// while ($next) {
|
while ($next) {
|
||||||
// // send request $next link
|
$dom = $this->sendRequest($next, 'GET');
|
||||||
// $links = $dom->getElementsByTagName('link');
|
$links = $dom->getElementsByTagName('link');
|
||||||
// foreach ($links as $link) {
|
foreach ($links as $link) {
|
||||||
// if ($link->getAttribute('rel') == 'next') {
|
if ($link->getAttribute('rel') == 'next') {
|
||||||
// $next = $link->getAttribute('src');
|
$next = $link->getAttribute('src');
|
||||||
// break;
|
break;
|
||||||
// } else {
|
} else {
|
||||||
// $next = false;
|
$next = false;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
$entries = $dom->getElementsByTagName('entry');
|
$entries = $dom->getElementsByTagName('entry');
|
||||||
foreach($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
$name = $entry->getElementsByTagName('title')->item(0)->nodeValue;
|
$name = $entry->getElementsByTagName('title')->item(0)->nodeValue;
|
||||||
// Native Google resources don't include extensions in title
|
// Google Docs resources don't always include extensions in title
|
||||||
if (!strpos($name, '.')) {
|
if (!strpos($name, '.')) {
|
||||||
if ($ext = $this->filetype('', $entry)) {
|
$name .= '.'.$this->getExtension($entry);
|
||||||
$name .= '.'.$ext;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$files[] = $name;
|
$files[] = $name;
|
||||||
|
// Cache entry for future use
|
||||||
|
$this->entries[$name] = $entry;
|
||||||
}
|
}
|
||||||
// }
|
}
|
||||||
$FAKEDIRS['google'] = $files;
|
OC_FakeDirStream::$dirs['google'] = $files;
|
||||||
return opendir('fakedir://google');
|
return opendir('fakedir://google');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function is_dir($path) {
|
public function stat($path) {
|
||||||
if ($entry = $this->getResource($path)) {
|
if ($path == '' || $path == '/') {
|
||||||
|
$stat['size'] = $this->free_space($path);
|
||||||
|
$stat['atime'] = time();
|
||||||
|
$stat['mtime'] = time();
|
||||||
|
$stat['ctime'] = time();
|
||||||
|
} else if ($entry = $this->getResource($path)) {
|
||||||
|
// NOTE: Native resources don't have a file size
|
||||||
|
$stat['size'] = $entry->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesUsed')->item(0)->nodeValue;
|
||||||
|
$stat['atime'] = strtotime($entry->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'lastViewed')->item(0)->nodeValue);
|
||||||
|
$stat['mtime'] = strtotime($entry->getElementsByTagName('updated')->item(0)->nodeValue);
|
||||||
|
$stat['ctime'] = strtotime($entry->getElementsByTagName('published')->item(0)->nodeValue);
|
||||||
|
}
|
||||||
|
return $stat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function filetype($path) {
|
||||||
|
if ($path == '' || $path == '/') {
|
||||||
|
return 'dir';
|
||||||
|
} else if ($entry = $this->getResource($path)) {
|
||||||
$categories = $entry->getElementsByTagName('category');
|
$categories = $entry->getElementsByTagName('category');
|
||||||
foreach ($categories as $category) {
|
foreach ($categories as $category) {
|
||||||
if ($category->getAttribute('scheme') == 'http://schemas.google.com/g/2005#kind') {
|
if ($category->getAttribute('scheme') == 'http://schemas.google.com/g/2005#kind') {
|
||||||
// Check if label is equal to folder
|
|
||||||
$type = $category->getAttribute('label');
|
$type = $category->getAttribute('label');
|
||||||
if (strlen(strstr($type, 'folder')) > 0) {
|
if (strlen(strstr($type, 'folder')) > 0) {
|
||||||
return true;
|
return 'dir';
|
||||||
|
} else {
|
||||||
|
return 'file';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function is_file($path) {
|
|
||||||
if ($this->getResource($path)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stat($path) {
|
|
||||||
if ($dom = $this->getResource($path)) {
|
|
||||||
// TODO Native resources don't have a file size
|
|
||||||
$stat['size'] = $dom->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesUsed')->item(0)->nodeValue;
|
|
||||||
$stat['mtime'] = strtotime($dom->getElementsByTagName('updated')->item(1)->nodeValue);
|
|
||||||
$stat['ctime'] = strtotime($dom->getElementsByTagName('published')->item(0)->nodeValue);
|
|
||||||
return $stat;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function filetype($path, $entry = null) {
|
|
||||||
if ($entry == null) {
|
|
||||||
$entry = $this->getResource($path);
|
|
||||||
}
|
|
||||||
$categories = $entry->getElementsByTagName('category');
|
|
||||||
foreach ($categories as $category) {
|
|
||||||
if ($category->getAttribute('scheme') == 'http://schemas.google.com/g/2005#kind') {
|
|
||||||
// Guess extension from label, default to ODF extensions
|
|
||||||
$type = $category->getAttribute('label');
|
|
||||||
if (strlen(strstr($type, 'folder')) > 0) {
|
|
||||||
return '';
|
|
||||||
} else if (strlen(strstr($type, 'document')) > 0) {
|
|
||||||
return 'odt';
|
|
||||||
} else if (strlen(strstr($type, 'presentation')) > 0) {
|
|
||||||
return 'odp';
|
|
||||||
} else if (strlen(strstr($type, 'spreadsheet')) > 0) {
|
|
||||||
return 'ods';
|
|
||||||
} else {
|
|
||||||
return $type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function is_readable($path) {
|
public function is_readable($path) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function is_writable($path) {
|
public function is_writable($path) {
|
||||||
// Check if edit or edit-media links exist
|
if ($path == '' || $path == '/') {
|
||||||
if ($entry = $this->getResource($path)) {
|
return true;
|
||||||
|
} else if ($entry = $this->getResource($path)) {
|
||||||
|
// Check if edit or edit-media links exist
|
||||||
$links = $entry->getElementsByTagName('link');
|
$links = $entry->getElementsByTagName('link');
|
||||||
foreach ($links as $link) {
|
foreach ($links as $link) {
|
||||||
if ($link->getAttribute('rel') == 'edit') {
|
if ($link->getAttribute('rel') == 'edit') {
|
||||||
|
@ -215,7 +227,9 @@ class OC_Filestorage_Google {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function file_exists($path) {
|
public function file_exists($path) {
|
||||||
if ($this->getResource($path)) {
|
if ($path == '' || $path == '/') {
|
||||||
|
return true;
|
||||||
|
} else if ($this->getResource($path)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -238,24 +252,82 @@ class OC_Filestorage_Google {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rename($path1,$path2) {
|
public function rename($path1, $path2) {
|
||||||
|
// TODO Add support for moving to different collections
|
||||||
|
// Get resource edit link to rename resource
|
||||||
|
if ($entry = $this->getResource($path1)) {
|
||||||
|
$etag = $entry->getElementsByTagName('entry')->item(0)->getAttribute('gd:etag');
|
||||||
|
$links = $entry->getElementsByTagName('link');
|
||||||
|
foreach ($links as $link) {
|
||||||
|
if ($link->getAttribute('rel') == 'edit') {
|
||||||
|
$feedUri = $link->getAttribute('href');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isset($etag) && isset($feedUri)) {
|
||||||
|
$title = basename($path2);
|
||||||
|
// Construct post data
|
||||||
|
$postData = '<?xml version="1.0" encoding="UTF-8"?>';
|
||||||
|
$postData .= '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007" xmlns:gd="http://schemas.google.com/g/2005" gd:etag='.$etag.'>';
|
||||||
|
$postData .= '<title>'.$title.'</title>';
|
||||||
|
$postData .= '</entry>';
|
||||||
|
$this->sendRequest($feedUri, 'PUT', $postData);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fopen($path,$mode){}
|
public function fopen($path, $mode) {
|
||||||
|
if ($entry = $this->getResource($path)) {
|
||||||
public function toTmpFile($path) {
|
$extension = $this->getExtension($path);
|
||||||
$dom = $this->getResource($path);
|
$downloadUri = $entry->getElementsByTagName('content')->item(0)->getAttribute('src');
|
||||||
$url = $dom->getElementsByTagName('content')->getAttribute('src');
|
// TODO Non-native documents don't need these additional parameters
|
||||||
|
$downloadUri .= '&exportFormat='.$extension.'&format='.$extension;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fromTmpFile($tmpPath,$path){}
|
public function getMimeType($path, $entry = null) {
|
||||||
public function fromUploadedFile($tmpPath,$path){}
|
if ($entry == null) {
|
||||||
public function getMimeType($path){}
|
if ($path == '' || $path == '/') {
|
||||||
public function hash($type,$path,$raw){}
|
return 'httpd/unix-directory';
|
||||||
|
} else {
|
||||||
|
$entry = $this->getResource($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($entry) {
|
||||||
|
$mimetype = $entry->getElementsByTagName('content')->item(0)->getAttribute('type');
|
||||||
|
// Native Google Docs resources often default to text/html, but it may be more useful to default to a corresponding ODF mimetype
|
||||||
|
// Collections get reported as application/atom+xml, make sure it actually is a folder and fix the mimetype
|
||||||
|
if ($mimetype == 'text/html' || $mimetype == 'application/atom+xml') {
|
||||||
|
$categories = $entry->getElementsByTagName('category');
|
||||||
|
foreach ($categories as $category) {
|
||||||
|
if ($category->getAttribute('scheme') == 'http://schemas.google.com/g/2005#kind') {
|
||||||
|
$type = $category->getAttribute('label');
|
||||||
|
if (strlen(strstr($type, 'folder')) > 0) {
|
||||||
|
return 'httpd/unix-directory';
|
||||||
|
} else if (strlen(strstr($type, 'document')) > 0) {
|
||||||
|
return 'application/vnd.oasis.opendocument.text';
|
||||||
|
} else if (strlen(strstr($type, 'spreadsheet')) > 0) {
|
||||||
|
return 'application/vnd.oasis.opendocument.spreadsheet';
|
||||||
|
} else if (strlen(strstr($type, 'presentation')) > 0) {
|
||||||
|
return 'application/vnd.oasis.opendocument.presentation';
|
||||||
|
} else if (strlen(strstr($type, 'drawing')) > 0) {
|
||||||
|
return 'application/vnd.oasis.opendocument.graphics';
|
||||||
|
} else {
|
||||||
|
// If nothing matches return text/html, all native Google Docs resources can be exported as text/html
|
||||||
|
return 'text/html';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $mimetype;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function free_space($path) {
|
public function free_space($path) {
|
||||||
if ($dom = $this->sendRequest('https://docs.google.com/feeds/metadata/default', 'GET')) {
|
if ($dom = $this->sendRequest('https://docs.google.com/feeds/metadata/default', 'GET')) {
|
||||||
|
// NOTE: Native Google Docs resources don't count towards quota
|
||||||
$total = $dom->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesTotal')->item(0)->nodeValue;
|
$total = $dom->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesTotal')->item(0)->nodeValue;
|
||||||
$used = $dom->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesUsed')->item(0)->nodeValue;
|
$used = $dom->getElementsByTagNameNS('http://schemas.google.com/g/2005', 'quotaBytesUsed')->item(0)->nodeValue;
|
||||||
return $total - $used;
|
return $total - $used;
|
||||||
|
@ -263,9 +335,8 @@ class OC_Filestorage_Google {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function search($query){}
|
public function search($query) {
|
||||||
|
|
||||||
public function getLocalFile($path) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue