@@ -506,32 +506,27 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
506506 $ urn = $ this ->getURN ($ fileId );
507507 try {
508508 //upload to object storage
509- if ($ size === null ) {
510- $ countStream = CountWrapper::wrap ($ stream , function ($ writtenSize ) use ($ fileId , &$ size ) {
509+
510+ $ totalWritten = 0 ;
511+ $ countStream = CountWrapper::wrap ($ stream , function ($ writtenSize ) use ($ fileId , $ size , $ exists , &$ totalWritten ) {
512+ if (is_null ($ size ) && !$ exists ) {
511513 $ this ->getCache ()->update ($ fileId , [
512514 'size ' => $ writtenSize ,
513515 ]);
514- $ size = $ writtenSize ;
515- });
516- if ($ this ->objectStore instanceof IObjectStoreMetaData) {
517- $ this ->objectStore ->writeObjectWithMetaData ($ urn , $ countStream , $ metadata );
518- } else {
519- $ this ->objectStore ->writeObject ($ urn , $ countStream , $ metadata ['mimetype ' ]);
520516 }
521- if (is_resource ($ countStream )) {
522- fclose ($ countStream );
523- }
524- $ stat ['size ' ] = $ size ;
517+ $ totalWritten = $ writtenSize ;
518+ });
519+
520+ if ($ this ->objectStore instanceof IObjectStoreMetaData) {
521+ $ this ->objectStore ->writeObjectWithMetaData ($ urn , $ countStream , $ metadata );
525522 } else {
526- if ($ this ->objectStore instanceof IObjectStoreMetaData) {
527- $ this ->objectStore ->writeObjectWithMetaData ($ urn , $ stream , $ metadata );
528- } else {
529- $ this ->objectStore ->writeObject ($ urn , $ stream , $ metadata ['mimetype ' ]);
530- }
531- if (is_resource ($ stream )) {
532- fclose ($ stream );
533- }
523+ $ this ->objectStore ->writeObject ($ urn , $ countStream , $ metadata ['mimetype ' ]);
534524 }
525+ if (is_resource ($ countStream )) {
526+ fclose ($ countStream );
527+ }
528+
529+ $ stat ['size ' ] = $ totalWritten ;
535530 } catch (\Exception $ ex ) {
536531 if (!$ exists ) {
537532 /*
@@ -571,7 +566,7 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
571566 }
572567 }
573568
574- return $ size ;
569+ return $ totalWritten ;
575570 }
576571
577572 public function getObjectStore (): IObjectStore {
0 commit comments