Skip to content

Commit

Permalink
Fix GDrive upload file which name might match the one of a folder
Browse files Browse the repository at this point in the history
Whenever a file is uploaded to GDrive, there is a check for that file
with and without extension, due to Google Docs files having no
extension. This logic now only kicks in whenever the detected
extensionless file is really a Google Doc file, not a folder.

This makes it possible again to upload a file "test.txt" in a folder
that also has a folder called "test"
  • Loading branch information
Vincent Petry committed Jun 7, 2016
1 parent 46fe2dd commit 15fffb2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion apps/files_external/lib/Lib/Storage/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public function getId() {
private function getDriveFile($path) {
// Remove leading and trailing slashes
$path = trim($path, '/');
if ($path === '.') {
$path = '';
}
if (isset($this->driveFiles[$path])) {
return $this->driveFiles[$path];
} else if ($path === '') {
Expand Down Expand Up @@ -138,7 +141,7 @@ private function getDriveFile($path) {
if ($pos !== false) {
$pathWithoutExt = substr($path, 0, $pos);
$file = $this->getDriveFile($pathWithoutExt);
if ($file) {
if ($file && $this->isGoogleDocFile($file)) {
// Switch cached Google_Service_Drive_DriveFile to the correct index
unset($this->driveFiles[$pathWithoutExt]);
$this->driveFiles[$path] = $file;
Expand Down Expand Up @@ -208,6 +211,17 @@ private function getGoogleDocExtension($mimetype) {
}
}

/**
* Returns whether the given drive file is a Google Doc file
*
* @param \Google_Service_Drive_DriveFile
*
* @return true if the file is a Google Doc file, false otherwise
*/
private function isGoogleDocFile($file) {
return $this->getGoogleDocExtension($file->getMimeType()) !== '';
}

public function mkdir($path) {
if (!$this->is_dir($path)) {
$parentFolder = $this->getDriveFile(dirname($path));
Expand Down
9 changes: 9 additions & 0 deletions apps/files_external/tests/Storage/GoogleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ protected function tearDown() {

parent::tearDown();
}

public function testSameNameAsFolderWithExtension() {
$this->assertTrue($this->instance->mkdir('testsamename'));
$this->assertEquals(13, $this->instance->file_put_contents('testsamename.txt', 'some contents'));
$this->assertEquals('some contents', $this->instance->file_get_contents('testsamename.txt'));
$this->assertTrue($this->instance->is_dir('testsamename'));
$this->assertTrue($this->instance->unlink('testsamename.txt'));
$this->assertTrue($this->instance->rmdir('testsamename'));
}
}

0 comments on commit 15fffb2

Please sign in to comment.