1212use OCP \Files \InvalidPathException ;
1313use OCP \Files \NotFoundException ;
1414use OCP \Files \NotPermittedException ;
15+ use OCP \Files \SimpleFS \InMemoryFile ;
1516use OCP \Files \SimpleFS \ISimpleFile ;
1617use OCP \Files \SimpleFS \ISimpleFolder ;
1718use OCP \IConfig ;
@@ -57,17 +58,19 @@ public function __construct(
5758 * The cache is searched first and if nothing usable was found then a preview is
5859 * generated by one of the providers
5960 *
60- * @param File $file
61- * @param int $width
62- * @param int $height
63- * @param bool $crop
64- * @param string $mode
65- * @param string|null $mimeType
6661 * @return ISimpleFile
6762 * @throws NotFoundException
6863 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
6964 */
70- public function getPreview (File $ file , $ width = -1 , $ height = -1 , $ crop = false , $ mode = IPreview::MODE_FILL , $ mimeType = null ) {
65+ public function getPreview (
66+ File $ file ,
67+ int $ width = -1 ,
68+ int $ height = -1 ,
69+ bool $ crop = false ,
70+ string $ mode = IPreview::MODE_FILL ,
71+ ?string $ mimeType = null ,
72+ bool $ cacheResult = true ,
73+ ): ISimpleFile {
7174 $ specification = [
7275 'width ' => $ width ,
7376 'height ' => $ height ,
@@ -84,20 +87,16 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
8487 ));
8588
8689 // since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
87- return $ this ->generatePreviews ($ file , [$ specification ], $ mimeType );
90+ return $ this ->generatePreviews ($ file , [$ specification ], $ mimeType, $ cacheResult );
8891 }
8992
9093 /**
9194 * Generates previews of a file
9295 *
93- * @param File $file
94- * @param non-empty-array $specifications
95- * @param string $mimeType
96- * @return ISimpleFile the last preview that was generated
9796 * @throws NotFoundException
9897 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
9998 */
100- public function generatePreviews (File $ file , array $ specifications , $ mimeType = null ) {
99+ public function generatePreviews (File $ file , array $ specifications , ? string $ mimeType = null , bool $ cacheResult = true ): ISimpleFile {
101100 //Make sure that we can read the file
102101 if (!$ file ->isReadable ()) {
103102 throw new NotFoundException ('Cannot read file ' );
@@ -167,7 +166,7 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
167166 $ maxPreviewImage = $ this ->helper ->getImage ($ maxPreview );
168167 }
169168
170- $ preview = $ this ->generatePreview ($ previewFolder , $ maxPreviewImage , $ width , $ height , $ crop , $ maxWidth , $ maxHeight , $ previewVersion );
169+ $ preview = $ this ->generatePreview ($ previewFolder , $ maxPreviewImage , $ width , $ height , $ crop , $ maxWidth , $ maxHeight , $ previewVersion, $ cacheResult );
171170 // New file, augment our array
172171 $ previewFiles [] = $ preview ;
173172 }
@@ -346,11 +345,10 @@ private function generateProviderPreview(ISimpleFolder $previewFolder, File $fil
346345
347346 $ path = $ this ->generatePath ($ preview ->width (), $ preview ->height (), $ crop , $ max , $ preview ->dataMimeType (), $ prefix );
348347 try {
349- $ file = $ previewFolder ->newFile ($ path );
350348 if ($ preview instanceof IStreamImage) {
351- $ file -> putContent ( $ preview ->resource ());
349+ return $ previewFolder -> newFile ( $ path , $ preview ->resource ());
352350 } else {
353- $ file -> putContent ( $ preview ->data ());
351+ return $ previewFolder -> newFile ( $ path , $ preview ->data ());
354352 }
355353 } catch (NotPermittedException $ e ) {
356354 throw new NotFoundException ();
@@ -485,19 +483,20 @@ private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHei
485483 }
486484
487485 /**
488- * @param ISimpleFolder $previewFolder
489- * @param ISimpleFile $maxPreview
490- * @param int $width
491- * @param int $height
492- * @param bool $crop
493- * @param int $maxWidth
494- * @param int $maxHeight
495- * @param string $prefix
496- * @return ISimpleFile
497486 * @throws NotFoundException
498487 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
499488 */
500- private function generatePreview (ISimpleFolder $ previewFolder , IImage $ maxPreview , $ width , $ height , $ crop , $ maxWidth , $ maxHeight , $ prefix ) {
489+ private function generatePreview (
490+ ISimpleFolder $ previewFolder ,
491+ IImage $ maxPreview ,
492+ int $ width ,
493+ int $ height ,
494+ bool $ crop ,
495+ int $ maxWidth ,
496+ int $ maxHeight ,
497+ string $ prefix ,
498+ bool $ cacheResult ,
499+ ): ISimpleFile {
501500 $ preview = $ maxPreview ;
502501 if (!$ preview ->valid ()) {
503502 throw new \InvalidArgumentException ('Failed to generate preview, failed to load image ' );
@@ -534,12 +533,14 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
534533
535534 $ path = $ this ->generatePath ($ width , $ height , $ crop , false , $ preview ->dataMimeType (), $ prefix );
536535 try {
537- $ file = $ previewFolder ->newFile ($ path );
538- $ file ->putContent ($ preview ->data ());
536+ if ($ cacheResult ) {
537+ return $ previewFolder ->newFile ($ path , $ preview ->data ());
538+ } else {
539+ return new InMemoryFile ($ path , $ preview ->data ());
540+ }
539541 } catch (NotPermittedException $ e ) {
540542 throw new NotFoundException ();
541543 }
542-
543544 return $ file ;
544545 }
545546
0 commit comments