Skip to content

Commit a1c56dd

Browse files
authored
Allow ignoring directory naming conflicts
Passing `'ignore'` as the value of `$options['directory_conflict_behavior']` will cause the adapter to silently ignore requests to create duplicate directories. This is in line with official adapters.
1 parent 5d2b404 commit a1c56dd

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/Adapter.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,30 @@ class Adapter implements FilesystemAdapter
2424
{
2525
protected $options = [];
2626

27+
protected const CONFLICT_BEHAVIOR_FAIL = 'fail';
28+
protected const CONFLICT_BEHAVIOR_IGNORE = 'ignore';
29+
protected const CONFLICT_BEHAVIOR_RENAME = 'rename';
30+
protected const CONFLICT_BEHAVIOR_REPLACE = 'replace';
31+
2732
public function __construct(public Graph $graph, protected string $drive_id, array $options = [])
2833
{
2934
$default_options = [
3035
'request_timeout' => 90, //Increase this for larger chunks / higher latency
3136
'chunk_size' => 320 * 1024 * 10, //Microsoft requires chunks to be multiples of 320KB
32-
'directory_conflict_behavior' => 'fail', //rename, replace, fail
37+
'directory_conflict_behavior' => static::CONFLICT_BEHAVIOR_IGNORE, //ignore, rename, replace, fail
3338
];
3439

3540
$this->options = array_merge($default_options, $options);
36-
41+
switch($this->options['directory_conflict_behavior']) {
42+
case static::CONFLICT_BEHAVIOR_FAIL:
43+
case static::CONFLICT_BEHAVIOR_IGNORE:
44+
case static::CONFLICT_BEHAVIOR_RENAME:
45+
case static::CONFLICT_BEHAVIOR_REPLACE:
46+
break;
47+
default:
48+
throw new Exception('Invalid directory_conflict_behavior');
49+
}
50+
3751
if ($this->options['chunk_size'] % (320 * 1024)) {
3852
throw new Exception('Chunk size must be a multiple of 320KB');
3953
}
@@ -276,6 +290,10 @@ public function getChildrenUrl(string $path): string
276290

277291
public function createDirectory(string $path, Config $config): void
278292
{
293+
if ($this->options['directory_conflict_behavior'] == static::CONFLICT_BEHAVIOR_IGNORE && $this->directoryExists($path)) {
294+
return;
295+
}
296+
279297
$newDirPathArray = explode('/', $path);
280298
$newDirName = array_pop($newDirPathArray);
281299
$path = implode('/', $newDirPathArray);

0 commit comments

Comments
 (0)