@@ -50,17 +50,34 @@ public function getMimeType(): string {
5050 public function getThumbnail (File $ file , int $ maxX , int $ maxY ): ?IImage {
5151 // TODO: use proc_open() and stream the source file ?
5252
53- $ absPath = $ this ->getLocalFile ($ file , 5242880 ); // only use the first 5MB
53+ $ result = null ;
54+ if ($ this ->useTempFile ($ file )) {
55+ // try downloading 5 MB first as it's likely that the first frames are present there
56+ // in some cases this doesn't work for example when the moov atom is at the
57+ // end of the file, so if it fails we fall back to getting the full file
58+ $ sizeAttempts = [5242880 , null ];
59+ } else {
60+ // size is irrelevant, only attempt once
61+ $ sizeAttempts = [null ];
62+ }
5463
55- $ result = $ this ->generateThumbNail ($ maxX , $ maxY , $ absPath , 5 );
56- if ($ result === null ) {
57- $ result = $ this ->generateThumbNail ($ maxX , $ maxY , $ absPath , 1 );
64+ foreach ($ sizeAttempts as $ size ) {
65+ $ absPath = $ this ->getLocalFile ($ file , $ size );
66+
67+ $ result = $ this ->generateThumbNail ($ maxX , $ maxY , $ absPath , 5 );
5868 if ($ result === null ) {
59- $ result = $ this ->generateThumbNail ($ maxX , $ maxY , $ absPath , 0 );
69+ $ result = $ this ->generateThumbNail ($ maxX , $ maxY , $ absPath , 1 );
70+ if ($ result === null ) {
71+ $ result = $ this ->generateThumbNail ($ maxX , $ maxY , $ absPath , 0 );
72+ }
6073 }
61- }
6274
63- $ this ->cleanTmpFiles ();
75+ $ this ->cleanTmpFiles ();
76+
77+ if ($ result !== null ) {
78+ break ;
79+ }
80+ }
6481
6582 return $ result ;
6683 }
0 commit comments