2323use OCP \IStreamImage ;
2424use OCP \Preview \BeforePreviewFetchedEvent ;
2525use OCP \Preview \IVersionedPreviewFile ;
26+ use OCP \Snowflake \IGenerator ;
2627use Psr \Log \LoggerInterface ;
2728
2829class Generator {
@@ -37,6 +38,7 @@ public function __construct(
3738 private LoggerInterface $ logger ,
3839 private PreviewMapper $ previewMapper ,
3940 private StorageFactory $ storageFactory ,
41+ private IGenerator $ snowflakeGenerator ,
4042 ) {
4143 }
4244
@@ -348,6 +350,7 @@ private function generateProviderPreview(File $file, int $width, int $height, bo
348350
349351 try {
350352 $ previewEntry = new Preview ();
353+ $ previewEntry ->setId ($ this ->snowflakeGenerator ->nextId ());
351354 $ previewEntry ->setFileId ($ file ->getId ());
352355 $ previewEntry ->setStorageId ($ file ->getMountPoint ()->getNumericStorageId ());
353356 $ previewEntry ->setSourceMimeType ($ file ->getMimeType ());
@@ -360,7 +363,6 @@ private function generateProviderPreview(File $file, int $width, int $height, bo
360363 $ previewEntry ->setMimetype ($ preview ->dataMimeType ());
361364 $ previewEntry ->setEtag ($ file ->getEtag ());
362365 $ previewEntry ->setMtime ((new \DateTime ())->getTimestamp ());
363- $ previewEntry ->setSize (0 );
364366 return $ this ->savePreview ($ previewEntry , $ preview );
365367 } catch (NotPermittedException ) {
366368 throw new NotFoundException ();
@@ -502,6 +504,7 @@ private function generatePreview(
502504 }
503505
504506 $ previewEntry = new Preview ();
507+ $ previewEntry ->setId ($ this ->snowflakeGenerator ->nextId ());
505508 $ previewEntry ->setFileId ($ file ->getId ());
506509 $ previewEntry ->setStorageId ($ file ->getMountPoint ()->getNumericStorageId ());
507510 $ previewEntry ->setWidth ($ width );
@@ -514,7 +517,6 @@ private function generatePreview(
514517 $ previewEntry ->setMimeType ($ preview ->dataMimeType ());
515518 $ previewEntry ->setEtag ($ file ->getEtag ());
516519 $ previewEntry ->setMtime ((new \DateTime ())->getTimestamp ());
517- $ previewEntry ->setSize (0 );
518520 if ($ cacheResult ) {
519521 $ previewEntry = $ this ->savePreview ($ previewEntry , $ preview );
520522 return new PreviewFile ($ previewEntry , $ this ->storageFactory , $ this ->previewMapper );
@@ -530,26 +532,20 @@ private function generatePreview(
530532 * @throws \OCP\DB\Exception
531533 */
532534 public function savePreview (Preview $ previewEntry , IImage $ preview ): Preview {
533- $ previewEntry = $ this ->previewMapper ->insert ($ previewEntry );
534-
535535 // we need to save to DB first
536- try {
537- if ($ preview instanceof IStreamImage) {
538- $ size = $ this ->storageFactory ->writePreview ($ previewEntry , $ preview ->resource ());
539- } else {
540- $ stream = fopen ('php://temp ' , 'w+ ' );
541- fwrite ($ stream , $ preview ->data ());
542- rewind ($ stream );
543- $ size = $ this ->storageFactory ->writePreview ($ previewEntry , $ stream );
544- }
545- if (!$ size ) {
546- throw new \RuntimeException ('Unable to write preview file ' );
547- }
548- } catch (\Exception $ e ) {
549- $ this ->previewMapper ->delete ($ previewEntry );
550- throw $ e ;
536+ if ($ preview instanceof IStreamImage) {
537+ $ size = $ this ->storageFactory ->writePreview ($ previewEntry , $ preview ->resource ());
538+ } else {
539+ $ stream = fopen ('php://temp ' , 'w+ ' );
540+ fwrite ($ stream , $ preview ->data ());
541+ rewind ($ stream );
542+ $ size = $ this ->storageFactory ->writePreview ($ previewEntry , $ stream );
543+ }
544+ if (!$ size ) {
545+ throw new \RuntimeException ('Unable to write preview file ' );
551546 }
552547 $ previewEntry ->setSize ($ size );
553- return $ this ->previewMapper ->update ($ previewEntry );
548+
549+ return $ this ->previewMapper ->insert ($ previewEntry );
554550 }
555551}
0 commit comments