Skip to content

Commit f0109f1

Browse files
authored
Allow createDirectory to create nested directories
Fixes #2 and simplifies `ensureDirectoryExists`
1 parent 156ea0f commit f0109f1

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/Adapter.php

+6-13
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ protected function ensureValidPath(string $path)
9191

9292
public function write(string $path, string $contents, Config $config): void
9393
{
94+
$path = trim($path, '/');
95+
$this->ensureValidPath($path);
9496
//Files larger than 4MiB require an UploadSession
9597
if (strlen($contents) > 4194304) {
9698
$stream = fopen('php://temp', 'r+');
@@ -101,8 +103,6 @@ public function write(string $path, string $contents, Config $config): void
101103
return;
102104
}
103105

104-
$path = trim($path, '/');
105-
$this->ensureValidPath($path);
106106

107107
$file_name = basename($path);
108108
$parentItem = $this->getUrlToPath(dirname($path));
@@ -268,14 +268,12 @@ public function createDirectory(string $path, Config $config): void
268268
{
269269
$newDirPathArray = explode('/', $path);
270270
$newDirName = array_pop($newDirPathArray);
271-
$parentItem = count($newDirPathArray)
272-
? $this->getUrlToPath(implode('/', $newDirPathArray))
273-
: $this->getDriveRootUrl();
271+
$path = implode('/', $newDirPathArray);
274272

275273
$dirItem = $this->graph
276274
->createRequest(
277275
'POST',
278-
$this->getDriveItemUrl($parentItem) . '/children'
276+
$this->getUrlToPath($path) . ':/children'
279277
)
280278
->attachBody([
281279
'name' => $newDirName,
@@ -469,13 +467,8 @@ private function getFileAttributes(string $path): FileAttributes
469467

470468
protected function ensureDirectoryExists(string $path)
471469
{
472-
$directories = explode('/', $path);
473-
$current_path = '';
474-
foreach ($directories as $directory) {
475-
$current_path = trim($current_path .= '/' . $directory, '/');
476-
if (!$this->directoryExists($current_path)) {
477-
$this->createDirectory($current_path, new Config());
478-
}
470+
if (!$this->directoryExists($path)) {
471+
$this->createDirectory($path, new Config());
479472
}
480473
}
481474

0 commit comments

Comments
 (0)