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 ;
@@ -43,17 +44,19 @@ public function __construct(
4344 * The cache is searched first and if nothing usable was found then a preview is
4445 * generated by one of the providers
4546 *
46- * @param File $file
47- * @param int $width
48- * @param int $height
49- * @param bool $crop
50- * @param string $mode
51- * @param string|null $mimeType
5247 * @return ISimpleFile
5348 * @throws NotFoundException
5449 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
5550 */
56- public function getPreview (File $ file , $ width = -1 , $ height = -1 , $ crop = false , $ mode = IPreview::MODE_FILL , $ mimeType = null ) {
51+ public function getPreview (
52+ File $ file ,
53+ int $ width = -1 ,
54+ int $ height = -1 ,
55+ bool $ crop = false ,
56+ string $ mode = IPreview::MODE_FILL ,
57+ ?string $ mimeType = null ,
58+ bool $ cacheResult = true ,
59+ ): ISimpleFile {
5760 $ specification = [
5861 'width ' => $ width ,
5962 'height ' => $ height ,
@@ -78,23 +81,19 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
7881 'mode ' => $ mode ,
7982 'mimeType ' => $ mimeType ,
8083 ]);
81-
84+
8285
8386 // since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
84- return $ this ->generatePreviews ($ file , [$ specification ], $ mimeType );
87+ return $ this ->generatePreviews ($ file , [$ specification ], $ mimeType, $ cacheResult );
8588 }
8689
8790 /**
8891 * Generates previews of a file
8992 *
90- * @param File $file
91- * @param non-empty-array $specifications
92- * @param string $mimeType
93- * @return ISimpleFile the last preview that was generated
9493 * @throws NotFoundException
9594 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
9695 */
97- public function generatePreviews (File $ file , array $ specifications , $ mimeType = null ) {
96+ public function generatePreviews (File $ file , array $ specifications , ? string $ mimeType = null , bool $ cacheResult = true ): ISimpleFile {
9897 //Make sure that we can read the file
9998 if (!$ file ->isReadable ()) {
10099 $ this ->logger ->warning ('Cannot read file: {path}, skipping preview generation. ' , ['path ' => $ file ->getPath ()]);
@@ -167,7 +166,7 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
167166 }
168167
169168 $ this ->logger ->warning ('Cached preview not found for file {path}, generating a new preview. ' , ['path ' => $ file ->getPath ()]);
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 }
@@ -351,11 +350,10 @@ private function generateProviderPreview(ISimpleFolder $previewFolder, File $fil
351350
352351 $ path = $ this ->generatePath ($ preview ->width (), $ preview ->height (), $ crop , $ max , $ preview ->dataMimeType (), $ prefix );
353352 try {
354- $ file = $ previewFolder ->newFile ($ path );
355353 if ($ preview instanceof IStreamImage) {
356- $ file -> putContent ( $ preview ->resource ());
354+ return $ previewFolder -> newFile ( $ path , $ preview ->resource ());
357355 } else {
358- $ file -> putContent ( $ preview ->data ());
356+ return $ previewFolder -> newFile ( $ path , $ preview ->data ());
359357 }
360358 } catch (NotPermittedException $ e ) {
361359 throw new NotFoundException ();
@@ -490,19 +488,20 @@ private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHei
490488 }
491489
492490 /**
493- * @param ISimpleFolder $previewFolder
494- * @param ISimpleFile $maxPreview
495- * @param int $width
496- * @param int $height
497- * @param bool $crop
498- * @param int $maxWidth
499- * @param int $maxHeight
500- * @param string $prefix
501- * @return ISimpleFile
502491 * @throws NotFoundException
503492 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
504493 */
505- private function generatePreview (ISimpleFolder $ previewFolder , IImage $ maxPreview , $ width , $ height , $ crop , $ maxWidth , $ maxHeight , $ prefix ) {
494+ private function generatePreview (
495+ ISimpleFolder $ previewFolder ,
496+ IImage $ maxPreview ,
497+ int $ width ,
498+ int $ height ,
499+ bool $ crop ,
500+ int $ maxWidth ,
501+ int $ maxHeight ,
502+ string $ prefix ,
503+ bool $ cacheResult ,
504+ ): ISimpleFile {
506505 $ preview = $ maxPreview ;
507506 if (!$ preview ->valid ()) {
508507 throw new \InvalidArgumentException ('Failed to generate preview, failed to load image ' );
@@ -539,12 +538,14 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
539538
540539 $ path = $ this ->generatePath ($ width , $ height , $ crop , false , $ preview ->dataMimeType (), $ prefix );
541540 try {
542- $ file = $ previewFolder ->newFile ($ path );
543- $ file ->putContent ($ preview ->data ());
541+ if ($ cacheResult ) {
542+ return $ previewFolder ->newFile ($ path , $ preview ->data ());
543+ } else {
544+ return new InMemoryFile ($ path , $ preview ->data ());
545+ }
544546 } catch (NotPermittedException $ e ) {
545547 throw new NotFoundException ();
546548 }
547-
548549 return $ file ;
549550 }
550551
0 commit comments