@@ -441,7 +441,13 @@ public function writeStream(string $path, $stream, int $size = null): int {
441441
442442 $ exists = $ this ->getCache ()->inCache ($ path );
443443 $ uploadPath = $ exists ? $ path : $ path . '.part ' ;
444- $ fileId = $ this ->getCache ()->put ($ uploadPath , $ stat );
444+
445+ if ($ exists ) {
446+ $ fileId = $ stat ['fileid ' ];
447+ } else {
448+ $ fileId = $ this ->getCache ()->put ($ uploadPath , $ stat );
449+ }
450+
445451 $ urn = $ this ->getURN ($ fileId );
446452 try {
447453 //upload to object storage
@@ -456,19 +462,33 @@ public function writeStream(string $path, $stream, int $size = null): int {
456462 if (is_resource ($ countStream )) {
457463 fclose ($ countStream );
458464 }
465+ $ stat ['size ' ] = $ size ;
459466 } else {
460467 $ this ->objectStore ->writeObject ($ urn , $ stream );
461468 }
462469 } catch (\Exception $ ex ) {
463- $ this ->getCache ()->remove ($ uploadPath );
464- $ this ->logger ->logException ($ ex , [
465- 'app ' => 'objectstore ' ,
466- 'message ' => 'Could not create object ' . $ urn . ' for ' . $ path ,
467- ]);
470+ if (!$ exists ) {
471+ /*
472+ * Only remove the entry if we are dealing with a new file.
473+ * Else people lose access to existing files
474+ */
475+ $ this ->getCache ()->remove ($ uploadPath );
476+ $ this ->logger ->logException ($ ex , [
477+ 'app ' => 'objectstore ' ,
478+ 'message ' => 'Could not create object ' . $ urn . ' for ' . $ path ,
479+ ]);
480+ } else {
481+ $ this ->logger ->logException ($ ex , [
482+ 'app ' => 'objectstore ' ,
483+ 'message ' => 'Could not update object ' . $ urn . ' for ' . $ path ,
484+ ]);
485+ }
468486 throw $ ex ; // make this bubble up
469487 }
470488
471- if (!$ exists ) {
489+ if ($ exists ) {
490+ $ this ->getCache ()->update ($ fileId , $ stat );
491+ } else {
472492 if ($ this ->objectStore ->objectExists ($ urn )) {
473493 $ this ->getCache ()->move ($ uploadPath , $ path );
474494 } else {
0 commit comments