AmazonS3.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib\Storage;
  8. use Aws\S3\Exception\S3Exception;
  9. use Icewind\Streams\CallbackWrapper;
  10. use Icewind\Streams\CountWrapper;
  11. use Icewind\Streams\IteratorDirectory;
  12. use OC\Files\Cache\CacheEntry;
  13. use OC\Files\ObjectStore\S3ConnectionTrait;
  14. use OC\Files\ObjectStore\S3ObjectTrait;
  15. use OC\Files\Storage\Common;
  16. use OCP\Cache\CappedMemoryCache;
  17. use OCP\Constants;
  18. use OCP\Files\FileInfo;
  19. use OCP\Files\IMimeTypeDetector;
  20. use OCP\ICache;
  21. use OCP\ICacheFactory;
  22. use OCP\Server;
  23. use Psr\Log\LoggerInterface;
  24. class AmazonS3 extends Common {
  25. use S3ConnectionTrait;
  26. use S3ObjectTrait;
  27. private LoggerInterface $logger;
  28. public function needsPartFile(): bool {
  29. return false;
  30. }
  31. /** @var CappedMemoryCache<array|false> */
  32. private CappedMemoryCache $objectCache;
  33. /** @var CappedMemoryCache<bool> */
  34. private CappedMemoryCache $directoryCache;
  35. /** @var CappedMemoryCache<array> */
  36. private CappedMemoryCache $filesCache;
  37. private IMimeTypeDetector $mimeDetector;
  38. private ?bool $versioningEnabled = null;
  39. private ICache $memCache;
  40. public function __construct(array $parameters) {
  41. parent::__construct($parameters);
  42. $this->parseParams($parameters);
  43. $this->id = 'amazon::external::' . md5($this->params['hostname'] . ':' . $this->params['bucket'] . ':' . $this->params['key']);
  44. $this->objectCache = new CappedMemoryCache();
  45. $this->directoryCache = new CappedMemoryCache();
  46. $this->filesCache = new CappedMemoryCache();
  47. $this->mimeDetector = Server::get(IMimeTypeDetector::class);
  48. /** @var ICacheFactory $cacheFactory */
  49. $cacheFactory = Server::get(ICacheFactory::class);
  50. $this->memCache = $cacheFactory->createLocal('s3-external');
  51. $this->logger = Server::get(LoggerInterface::class);
  52. }
  53. private function normalizePath(string $path): string {
  54. $path = trim($path, '/');
  55. if (!$path) {
  56. $path = '.';
  57. }
  58. return $path;
  59. }
  60. private function isRoot(string $path): bool {
  61. return $path === '.';
  62. }
  63. private function cleanKey(string $path): string {
  64. if ($this->isRoot($path)) {
  65. return '/';
  66. }
  67. return $path;
  68. }
  69. private function clearCache(): void {
  70. $this->objectCache = new CappedMemoryCache();
  71. $this->directoryCache = new CappedMemoryCache();
  72. $this->filesCache = new CappedMemoryCache();
  73. }
  74. private function invalidateCache(string $key): void {
  75. unset($this->objectCache[$key]);
  76. $keys = array_keys($this->objectCache->getData());
  77. $keyLength = strlen($key);
  78. foreach ($keys as $existingKey) {
  79. if (substr($existingKey, 0, $keyLength) === $key) {
  80. unset($this->objectCache[$existingKey]);
  81. }
  82. }
  83. unset($this->filesCache[$key]);
  84. $keys = array_keys($this->directoryCache->getData());
  85. $keyLength = strlen($key);
  86. foreach ($keys as $existingKey) {
  87. if (substr($existingKey, 0, $keyLength) === $key) {
  88. unset($this->directoryCache[$existingKey]);
  89. }
  90. }
  91. unset($this->directoryCache[$key]);
  92. }
  93. private function headObject(string $key): array|false {
  94. if (!isset($this->objectCache[$key])) {
  95. try {
  96. $this->objectCache[$key] = $this->getConnection()->headObject([
  97. 'Bucket' => $this->bucket,
  98. 'Key' => $key
  99. ])->toArray();
  100. } catch (S3Exception $e) {
  101. if ($e->getStatusCode() >= 500) {
  102. throw $e;
  103. }
  104. $this->objectCache[$key] = false;
  105. }
  106. }
  107. if (is_array($this->objectCache[$key]) && !isset($this->objectCache[$key]['Key'])) {
  108. /** @psalm-suppress InvalidArgument Psalm doesn't understand nested arrays well */
  109. $this->objectCache[$key]['Key'] = $key;
  110. }
  111. return $this->objectCache[$key];
  112. }
  113. /**
  114. * Return true if directory exists
  115. *
  116. * There are no folders in s3. A folder like structure could be archived
  117. * by prefixing files with the folder name.
  118. *
  119. * Implementation from flysystem-aws-s3-v3:
  120. * https://github.com/thephpleague/flysystem-aws-s3-v3/blob/8241e9cc5b28f981e0d24cdaf9867f14c7498ae4/src/AwsS3Adapter.php#L670-L694
  121. *
  122. * @throws \Exception
  123. */
  124. private function doesDirectoryExist(string $path): bool {
  125. if ($path === '.' || $path === '') {
  126. return true;
  127. }
  128. $path = rtrim($path, '/') . '/';
  129. if (isset($this->directoryCache[$path])) {
  130. return $this->directoryCache[$path];
  131. }
  132. try {
  133. // Maybe this isn't an actual key, but a prefix.
  134. // Do a prefix listing of objects to determine.
  135. $result = $this->getConnection()->listObjectsV2([
  136. 'Bucket' => $this->bucket,
  137. 'Prefix' => $path,
  138. 'MaxKeys' => 1,
  139. ]);
  140. if (isset($result['Contents'])) {
  141. $this->directoryCache[$path] = true;
  142. return true;
  143. }
  144. // empty directories have their own object
  145. $object = $this->headObject($path);
  146. if ($object) {
  147. $this->directoryCache[$path] = true;
  148. return true;
  149. }
  150. } catch (S3Exception $e) {
  151. if ($e->getStatusCode() >= 400 && $e->getStatusCode() < 500) {
  152. $this->directoryCache[$path] = false;
  153. }
  154. throw $e;
  155. }
  156. $this->directoryCache[$path] = false;
  157. return false;
  158. }
  159. protected function remove(string $path): bool {
  160. // remember fileType to reduce http calls
  161. $fileType = $this->filetype($path);
  162. if ($fileType === 'dir') {
  163. return $this->rmdir($path);
  164. } elseif ($fileType === 'file') {
  165. return $this->unlink($path);
  166. } else {
  167. return false;
  168. }
  169. }
  170. public function mkdir(string $path): bool {
  171. $path = $this->normalizePath($path);
  172. if ($this->is_dir($path)) {
  173. return false;
  174. }
  175. try {
  176. $this->getConnection()->putObject([
  177. 'Bucket' => $this->bucket,
  178. 'Key' => $path . '/',
  179. 'Body' => '',
  180. 'ContentType' => FileInfo::MIMETYPE_FOLDER
  181. ]);
  182. $this->testTimeout();
  183. } catch (S3Exception $e) {
  184. $this->logger->error($e->getMessage(), [
  185. 'app' => 'files_external',
  186. 'exception' => $e,
  187. ]);
  188. return false;
  189. }
  190. $this->invalidateCache($path);
  191. return true;
  192. }
  193. public function file_exists(string $path): bool {
  194. return $this->filetype($path) !== false;
  195. }
  196. public function rmdir(string $path): bool {
  197. $path = $this->normalizePath($path);
  198. if ($this->isRoot($path)) {
  199. return $this->clearBucket();
  200. }
  201. if (!$this->file_exists($path)) {
  202. return false;
  203. }
  204. $this->invalidateCache($path);
  205. return $this->batchDelete($path);
  206. }
  207. protected function clearBucket(): bool {
  208. $this->clearCache();
  209. return $this->batchDelete();
  210. }
  211. private function batchDelete(?string $path = null): bool {
  212. // TODO explore using https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.BatchDelete.html
  213. $params = [
  214. 'Bucket' => $this->bucket
  215. ];
  216. if ($path !== null) {
  217. $params['Prefix'] = $path . '/';
  218. }
  219. try {
  220. $connection = $this->getConnection();
  221. // Since there are no real directories on S3, we need
  222. // to delete all objects prefixed with the path.
  223. do {
  224. // instead of the iterator, manually loop over the list ...
  225. $objects = $connection->listObjects($params);
  226. // ... so we can delete the files in batches
  227. if (isset($objects['Contents'])) {
  228. $connection->deleteObjects([
  229. 'Bucket' => $this->bucket,
  230. 'Delete' => [
  231. 'Objects' => $objects['Contents']
  232. ]
  233. ]);
  234. $this->testTimeout();
  235. }
  236. // we reached the end when the list is no longer truncated
  237. } while ($objects['IsTruncated']);
  238. if ($path !== '' && $path !== null) {
  239. $this->deleteObject($path);
  240. }
  241. } catch (S3Exception $e) {
  242. $this->logger->error($e->getMessage(), [
  243. 'app' => 'files_external',
  244. 'exception' => $e,
  245. ]);
  246. return false;
  247. }
  248. return true;
  249. }
  250. public function opendir(string $path) {
  251. try {
  252. $content = iterator_to_array($this->getDirectoryContent($path));
  253. return IteratorDirectory::wrap(array_map(function (array $item) {
  254. return $item['name'];
  255. }, $content));
  256. } catch (S3Exception $e) {
  257. return false;
  258. }
  259. }
  260. public function stat(string $path): array|false {
  261. $path = $this->normalizePath($path);
  262. if ($this->is_dir($path)) {
  263. $stat = $this->getDirectoryMetaData($path);
  264. } else {
  265. $object = $this->headObject($path);
  266. if ($object === false) {
  267. return false;
  268. }
  269. $stat = $this->objectToMetaData($object);
  270. }
  271. $stat['atime'] = time();
  272. return $stat;
  273. }
  274. /**
  275. * Return content length for object
  276. *
  277. * When the information is already present (e.g. opendir has been called before)
  278. * this value is return. Otherwise a headObject is emitted.
  279. */
  280. private function getContentLength(string $path): int {
  281. if (isset($this->filesCache[$path])) {
  282. return (int)$this->filesCache[$path]['ContentLength'];
  283. }
  284. $result = $this->headObject($path);
  285. if (isset($result['ContentLength'])) {
  286. return (int)$result['ContentLength'];
  287. }
  288. return 0;
  289. }
  290. /**
  291. * Return last modified for object
  292. *
  293. * When the information is already present (e.g. opendir has been called before)
  294. * this value is return. Otherwise a headObject is emitted.
  295. */
  296. private function getLastModified(string $path): string {
  297. if (isset($this->filesCache[$path])) {
  298. return $this->filesCache[$path]['LastModified'];
  299. }
  300. $result = $this->headObject($path);
  301. if (isset($result['LastModified'])) {
  302. return $result['LastModified'];
  303. }
  304. return 'now';
  305. }
  306. public function is_dir(string $path): bool {
  307. $path = $this->normalizePath($path);
  308. if (isset($this->filesCache[$path])) {
  309. return false;
  310. }
  311. try {
  312. return $this->doesDirectoryExist($path);
  313. } catch (S3Exception $e) {
  314. $this->logger->error($e->getMessage(), [
  315. 'app' => 'files_external',
  316. 'exception' => $e,
  317. ]);
  318. return false;
  319. }
  320. }
  321. public function filetype(string $path): string|false {
  322. $path = $this->normalizePath($path);
  323. if ($this->isRoot($path)) {
  324. return 'dir';
  325. }
  326. try {
  327. if (isset($this->directoryCache[$path]) && $this->directoryCache[$path]) {
  328. return 'dir';
  329. }
  330. if (isset($this->filesCache[$path]) || $this->headObject($path)) {
  331. return 'file';
  332. }
  333. if ($this->doesDirectoryExist($path)) {
  334. return 'dir';
  335. }
  336. } catch (S3Exception $e) {
  337. $this->logger->error($e->getMessage(), [
  338. 'app' => 'files_external',
  339. 'exception' => $e,
  340. ]);
  341. return false;
  342. }
  343. return false;
  344. }
  345. public function getPermissions(string $path): int {
  346. $type = $this->filetype($path);
  347. if (!$type) {
  348. return 0;
  349. }
  350. return $type === 'dir' ? Constants::PERMISSION_ALL : Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
  351. }
  352. public function unlink(string $path): bool {
  353. $path = $this->normalizePath($path);
  354. if ($this->is_dir($path)) {
  355. return $this->rmdir($path);
  356. }
  357. try {
  358. $this->deleteObject($path);
  359. $this->invalidateCache($path);
  360. } catch (S3Exception $e) {
  361. $this->logger->error($e->getMessage(), [
  362. 'app' => 'files_external',
  363. 'exception' => $e,
  364. ]);
  365. return false;
  366. }
  367. return true;
  368. }
  369. public function fopen(string $path, string $mode) {
  370. $path = $this->normalizePath($path);
  371. switch ($mode) {
  372. case 'r':
  373. case 'rb':
  374. // Don't try to fetch empty files
  375. $stat = $this->stat($path);
  376. if (is_array($stat) && isset($stat['size']) && $stat['size'] === 0) {
  377. return fopen('php://memory', $mode);
  378. }
  379. try {
  380. return $this->readObject($path);
  381. } catch (\Exception $e) {
  382. $this->logger->error($e->getMessage(), [
  383. 'app' => 'files_external',
  384. 'exception' => $e,
  385. ]);
  386. return false;
  387. }
  388. case 'w':
  389. case 'wb':
  390. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
  391. $handle = fopen($tmpFile, 'w');
  392. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile): void {
  393. $this->writeBack($tmpFile, $path);
  394. });
  395. case 'a':
  396. case 'ab':
  397. case 'r+':
  398. case 'w+':
  399. case 'wb+':
  400. case 'a+':
  401. case 'x':
  402. case 'x+':
  403. case 'c':
  404. case 'c+':
  405. if (strrpos($path, '.') !== false) {
  406. $ext = substr($path, strrpos($path, '.'));
  407. } else {
  408. $ext = '';
  409. }
  410. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  411. if ($this->file_exists($path)) {
  412. $source = $this->readObject($path);
  413. file_put_contents($tmpFile, $source);
  414. }
  415. $handle = fopen($tmpFile, $mode);
  416. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile): void {
  417. $this->writeBack($tmpFile, $path);
  418. });
  419. }
  420. return false;
  421. }
  422. public function touch(string $path, ?int $mtime = null): bool {
  423. if (is_null($mtime)) {
  424. $mtime = time();
  425. }
  426. $metadata = [
  427. 'lastmodified' => gmdate(\DateTime::RFC1123, $mtime)
  428. ];
  429. try {
  430. if ($this->file_exists($path)) {
  431. return false;
  432. }
  433. $mimeType = $this->mimeDetector->detectPath($path);
  434. $this->getConnection()->putObject([
  435. 'Bucket' => $this->bucket,
  436. 'Key' => $this->cleanKey($path),
  437. 'Metadata' => $metadata,
  438. 'Body' => '',
  439. 'ContentType' => $mimeType,
  440. 'MetadataDirective' => 'REPLACE',
  441. ]);
  442. $this->testTimeout();
  443. } catch (S3Exception $e) {
  444. $this->logger->error($e->getMessage(), [
  445. 'app' => 'files_external',
  446. 'exception' => $e,
  447. ]);
  448. return false;
  449. }
  450. $this->invalidateCache($path);
  451. return true;
  452. }
  453. public function copy(string $source, string $target, ?bool $isFile = null): bool {
  454. $source = $this->normalizePath($source);
  455. $target = $this->normalizePath($target);
  456. if ($isFile === true || $this->is_file($source)) {
  457. try {
  458. $this->copyObject($source, $target, [
  459. 'StorageClass' => $this->storageClass,
  460. ]);
  461. $this->testTimeout();
  462. } catch (S3Exception $e) {
  463. $this->logger->error($e->getMessage(), [
  464. 'app' => 'files_external',
  465. 'exception' => $e,
  466. ]);
  467. return false;
  468. }
  469. } else {
  470. $this->remove($target);
  471. try {
  472. $this->mkdir($target);
  473. $this->testTimeout();
  474. } catch (S3Exception $e) {
  475. $this->logger->error($e->getMessage(), [
  476. 'app' => 'files_external',
  477. 'exception' => $e,
  478. ]);
  479. return false;
  480. }
  481. foreach ($this->getDirectoryContent($source) as $item) {
  482. $childSource = $source . '/' . $item['name'];
  483. $childTarget = $target . '/' . $item['name'];
  484. $this->copy($childSource, $childTarget, $item['mimetype'] !== FileInfo::MIMETYPE_FOLDER);
  485. }
  486. }
  487. $this->invalidateCache($target);
  488. return true;
  489. }
  490. public function rename(string $source, string $target): bool {
  491. $source = $this->normalizePath($source);
  492. $target = $this->normalizePath($target);
  493. if ($this->is_file($source)) {
  494. if ($this->copy($source, $target) === false) {
  495. return false;
  496. }
  497. if ($this->unlink($source) === false) {
  498. $this->unlink($target);
  499. return false;
  500. }
  501. } else {
  502. if ($this->copy($source, $target) === false) {
  503. return false;
  504. }
  505. if ($this->rmdir($source) === false) {
  506. $this->rmdir($target);
  507. return false;
  508. }
  509. }
  510. return true;
  511. }
  512. public function test(): bool {
  513. $this->getConnection()->headBucket([
  514. 'Bucket' => $this->bucket
  515. ]);
  516. return true;
  517. }
  518. public function getId(): string {
  519. return $this->id;
  520. }
  521. public function writeBack(string $tmpFile, string $path): bool {
  522. try {
  523. $source = fopen($tmpFile, 'r');
  524. $this->writeObject($path, $source, $this->mimeDetector->detectPath($path));
  525. $this->invalidateCache($path);
  526. unlink($tmpFile);
  527. return true;
  528. } catch (S3Exception $e) {
  529. $this->logger->error($e->getMessage(), [
  530. 'app' => 'files_external',
  531. 'exception' => $e,
  532. ]);
  533. return false;
  534. }
  535. }
  536. /**
  537. * check if curl is installed
  538. */
  539. public static function checkDependencies(): bool {
  540. return true;
  541. }
  542. public function getDirectoryContent(string $directory): \Traversable {
  543. $path = $this->normalizePath($directory);
  544. if ($this->isRoot($path)) {
  545. $path = '';
  546. } else {
  547. $path .= '/';
  548. }
  549. $results = $this->getConnection()->getPaginator('ListObjectsV2', [
  550. 'Bucket' => $this->bucket,
  551. 'Delimiter' => '/',
  552. 'Prefix' => $path,
  553. ]);
  554. foreach ($results as $result) {
  555. // sub folders
  556. if (is_array($result['CommonPrefixes'])) {
  557. foreach ($result['CommonPrefixes'] as $prefix) {
  558. $dir = $this->getDirectoryMetaData($prefix['Prefix']);
  559. if ($dir) {
  560. yield $dir;
  561. }
  562. }
  563. }
  564. if (is_array($result['Contents'])) {
  565. foreach ($result['Contents'] as $object) {
  566. $this->objectCache[$object['Key']] = $object;
  567. if ($object['Key'] !== $path) {
  568. yield $this->objectToMetaData($object);
  569. }
  570. }
  571. }
  572. }
  573. }
  574. private function objectToMetaData(array $object): array {
  575. return [
  576. 'name' => basename($object['Key']),
  577. 'mimetype' => $this->mimeDetector->detectPath($object['Key']),
  578. 'mtime' => strtotime($object['LastModified']),
  579. 'storage_mtime' => strtotime($object['LastModified']),
  580. 'etag' => trim($object['ETag'], '"'),
  581. 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE,
  582. 'size' => (int)($object['Size'] ?? $object['ContentLength']),
  583. ];
  584. }
  585. private function getDirectoryMetaData(string $path): ?array {
  586. $path = trim($path, '/');
  587. // when versioning is enabled, delete markers are returned as part of CommonPrefixes
  588. // resulting in "ghost" folders, verify that each folder actually exists
  589. if ($this->versioningEnabled() && !$this->doesDirectoryExist($path)) {
  590. return null;
  591. }
  592. $cacheEntry = $this->getCache()->get($path);
  593. if ($cacheEntry instanceof CacheEntry) {
  594. return $cacheEntry->getData();
  595. } else {
  596. return [
  597. 'name' => basename($path),
  598. 'mimetype' => FileInfo::MIMETYPE_FOLDER,
  599. 'mtime' => time(),
  600. 'storage_mtime' => time(),
  601. 'etag' => uniqid(),
  602. 'permissions' => Constants::PERMISSION_ALL,
  603. 'size' => -1,
  604. ];
  605. }
  606. }
  607. public function versioningEnabled(): bool {
  608. if ($this->versioningEnabled === null) {
  609. $cached = $this->memCache->get('versioning-enabled::' . $this->getBucket());
  610. if ($cached === null) {
  611. $this->versioningEnabled = $this->getVersioningStatusFromBucket();
  612. $this->memCache->set('versioning-enabled::' . $this->getBucket(), $this->versioningEnabled, 60);
  613. } else {
  614. $this->versioningEnabled = $cached;
  615. }
  616. }
  617. return $this->versioningEnabled;
  618. }
  619. protected function getVersioningStatusFromBucket(): bool {
  620. try {
  621. $result = $this->getConnection()->getBucketVersioning(['Bucket' => $this->getBucket()]);
  622. return $result->get('Status') === 'Enabled';
  623. } catch (S3Exception $s3Exception) {
  624. // This is needed for compatibility with Storj gateway which does not support versioning yet
  625. if ($s3Exception->getAwsErrorCode() === 'NotImplemented' || $s3Exception->getAwsErrorCode() === 'AccessDenied') {
  626. return false;
  627. }
  628. throw $s3Exception;
  629. }
  630. }
  631. public function hasUpdated(string $path, int $time): bool {
  632. // for files we can get the proper mtime
  633. if ($path !== '' && $object = $this->headObject($path)) {
  634. $stat = $this->objectToMetaData($object);
  635. return $stat['mtime'] > $time;
  636. } else {
  637. // for directories, the only real option we have is to do a prefix listing and iterate over all objects
  638. // however, since this is just as expensive as just re-scanning the directory, we can simply return true
  639. // and have the scanner figure out if anything has actually changed
  640. return true;
  641. }
  642. }
  643. public function writeStream(string $path, $stream, ?int $size = null): int {
  644. if ($size === null) {
  645. $size = 0;
  646. // track the number of bytes read from the input stream to return as the number of written bytes.
  647. $stream = CountWrapper::wrap($stream, function (int $writtenSize) use (&$size): void {
  648. $size = $writtenSize;
  649. });
  650. }
  651. if (!is_resource($stream)) {
  652. throw new \InvalidArgumentException('Invalid stream provided');
  653. }
  654. $path = $this->normalizePath($path);
  655. $this->writeObject($path, $stream, $this->mimeDetector->detectPath($path));
  656. $this->invalidateCache($path);
  657. return $size;
  658. }
  659. }