@@ -723,111 +723,124 @@ private Bitmap doThumbnailFromOCFileInBackground() {
723723
724724 boolean updateEnforced = (file instanceof OCFile && ((OCFile ) file ).isUpdateThumbnailNeeded ());
725725
726- if (updateEnforced ) {
727- thumbnail = null ;
728- } else {
729- // Check disk cache in background thread
726+ // Try to load thumbnail from disk cache
727+ if (!updateEnforced ) {
730728 thumbnail = getBitmapFromDiskCache (imageKey );
729+ if (thumbnail != null ) {
730+ Log_OC .d (TAG , "Thumbnail found in disk cache for file: " + file .getFileName ());
731+ return thumbnail ;
732+ } else {
733+ Log_OC .d (TAG , "Thumbnail not found in cache for file: " + file .getFileName ());
734+ }
735+ } else {
736+ Log_OC .d (TAG , "Thumbnail update enforced for file: " + file .getFileName ());
737+ thumbnail = null ;
731738 }
732739
733- // Not found in disk cache
734- if (thumbnail == null ) {
735- int pxW ;
736- int pxH ;
737- pxW = pxH = getThumbnailDimension ();
738-
739- if (file instanceof OCFile ocFile ) {
740- if (ocFile .isDown ()) {
741- Bitmap bitmap ;
742- if (MimeTypeUtil .isVideo (ocFile )) {
743- bitmap = ThumbnailUtils .createVideoThumbnail (ocFile .getStoragePath (),
744- MediaStore .Images .Thumbnails .MINI_KIND );
745- } else {
746- bitmap = BitmapUtils .decodeSampledBitmapFromFile (ocFile .getStoragePath (), pxW , pxH );
747- }
740+ int pxW ;
741+ int pxH ;
742+ pxW = pxH = getThumbnailDimension ();
748743
749- if (bitmap != null ) {
750- // Handle PNG
751- if (PNG_MIMETYPE .equalsIgnoreCase (ocFile .getMimeType ())) {
752- bitmap = handlePNG (bitmap , pxW , pxH );
753- }
744+ // Generate thumbnail from local file if available
745+ if (file instanceof OCFile ocFile && ocFile .isDown ()) {
746+ Log_OC .d (TAG , "Generating thumbnail from local file: " + ocFile .getFileName ());
754747
755- thumbnail = addThumbnailToCache (imageKey , bitmap , ocFile .getStoragePath (), pxW , pxH );
748+ Bitmap bitmap ;
749+ if (MimeTypeUtil .isVideo (ocFile )) {
750+ bitmap = ThumbnailUtils .createVideoThumbnail (ocFile .getStoragePath (),
751+ MediaStore .Images .Thumbnails .MINI_KIND );
752+ } else {
753+ bitmap = BitmapUtils .decodeSampledBitmapFromFile (ocFile .getStoragePath (), pxW , pxH );
754+ }
756755
757- ocFile . setUpdateThumbnailNeeded ( false );
758- mStorageManager . saveFile (ocFile );
759- }
756+ if ( bitmap != null ) {
757+ if ( PNG_MIMETYPE . equalsIgnoreCase (ocFile . getMimeType ())) {
758+ bitmap = handlePNG ( bitmap , pxW , pxH );
760759 }
760+
761+ thumbnail = addThumbnailToCache (imageKey , bitmap , ocFile .getStoragePath (), pxW , pxH );
762+ ocFile .setUpdateThumbnailNeeded (false );
763+ mStorageManager .saveFile (ocFile );
764+ }
765+ }
766+
767+ // Check resized version in disk cache if still null
768+ if (thumbnail == null ) {
769+ String resizedImageKey = PREFIX_RESIZED_IMAGE + file .getRemoteId ();
770+ Bitmap resizedImage = null ;
771+
772+ if (!updateEnforced ) {
773+ resizedImage = getBitmapFromDiskCache (resizedImageKey );
774+ }
775+
776+ if (resizedImage != null ) {
777+ thumbnail = ThumbnailUtils .extractThumbnail (resizedImage , pxW , pxH );
778+ Log_OC .d (TAG , "Thumbnail generated from resized image cache for file: " + file .getFileName ());
779+ } else {
780+ Log_OC .d (TAG , "No resized image cache available for file: " + file .getFileName ());
761781 }
782+ }
762783
763- if (thumbnail == null ) {
764- // check if resized version is available
765- String resizedImageKey = PREFIX_RESIZED_IMAGE + file .getRemoteId ();
784+ // Download thumbnail from server if still null
785+ if (thumbnail == null && mClient != null ) {
786+ Log_OC .d (TAG , "Attempting to download thumbnail from server for file: " + file .getFileName ());
787+ GetMethod getMethod = null ;
766788
767- Bitmap resizedImage ;
768- if (updateEnforced ) {
769- resizedImage = null ;
789+ try {
790+ String uri ;
791+ if (file instanceof OCFile ) {
792+ uri = mClient .getBaseUri () + "/index.php/core/preview?fileId="
793+ + file .getLocalId ()
794+ + "&x=" + pxW + "&y=" + pxH + "&a=1&mode=cover&forceIcon=0" ;
770795 } else {
771- resizedImage = getBitmapFromDiskCache (resizedImageKey );
796+ uri = mClient .getBaseUri () + "/index.php/apps/files_trashbin/preview?fileId="
797+ + file .getLocalId () + "&x=" + pxW + "&y=" + pxH ;
772798 }
773799
774- if (resizedImage != null ) {
775- thumbnail = ThumbnailUtils .extractThumbnail (resizedImage , pxW , pxH );
776- } else {
777- // Download thumbnail from server
778- if (mClient != null ) {
779- getMethod = null ;
780- try {
781- // thumbnail
782- String uri ;
783- if (file instanceof OCFile ) {
784- uri = mClient .getBaseUri () + "/index.php/core/preview?fileId="
785- + file .getLocalId ()
786- + "&x=" + pxW + "&y=" + pxH + "&a=1&mode=cover&forceIcon=0" ;
787- } else {
788- uri = mClient .getBaseUri () + "/index.php/apps/files_trashbin/preview?fileId=" +
789- file .getLocalId () + "&x=" + pxW + "&y=" + pxH ;
790- }
791-
792- Log_OC .d (TAG , "generate thumbnail: " + file .getFileName () + " URI: " + uri );
793- getMethod = new GetMethod (uri );
794- getMethod .setRequestHeader ("Cookie" ,
795- "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true" );
796-
797- getMethod .setRequestHeader (RemoteOperation .OCS_API_HEADER ,
798- RemoteOperation .OCS_API_HEADER_VALUE );
799-
800- int status = mClient .executeMethod (getMethod , READ_TIMEOUT , CONNECTION_TIMEOUT );
801- if (status == HttpStatus .SC_OK ) {
802- InputStream inputStream = getMethod .getResponseBodyAsStream ();
803- Bitmap bitmap = BitmapFactory .decodeStream (inputStream );
804- thumbnail = ThumbnailUtils .extractThumbnail (bitmap , pxW , pxH );
805- } else {
806- mClient .exhaustResponse (getMethod .getResponseBodyAsStream ());
807- }
808-
809- // Handle PNG
810- if (PNG_MIMETYPE .equalsIgnoreCase (file .getMimeType ())) {
811- thumbnail = handlePNG (thumbnail , pxW , pxH );
812- }
813- } catch (Exception e ) {
814- Log_OC .d (TAG , e .getMessage (), e );
815- } finally {
816- if (getMethod != null ) {
817- getMethod .releaseConnection ();
818- }
800+ Log_OC .d (TAG , "Downloading thumbnail URI: " + uri );
801+
802+ getMethod = new GetMethod (uri );
803+ getMethod .setRequestHeader ("Cookie" , "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true" );
804+ getMethod .setRequestHeader (RemoteOperation .OCS_API_HEADER , RemoteOperation .OCS_API_HEADER_VALUE );
805+
806+ int status = mClient .executeMethod (getMethod , READ_TIMEOUT , CONNECTION_TIMEOUT );
807+
808+ if (status == HttpStatus .SC_OK ) {
809+ try (InputStream inputStream = getMethod .getResponseBodyAsStream ()) {
810+ Bitmap bitmap = BitmapFactory .decodeStream (inputStream );
811+ if (bitmap != null ) {
812+ thumbnail = ThumbnailUtils .extractThumbnail (bitmap , pxW , pxH );
813+ Log_OC .d (TAG , "Thumbnail downloaded and extracted for file: " + file .getFileName ());
814+ } else {
815+ Log_OC .w (TAG , "Downloaded thumbnail bitmap is null for file: " + file .getFileName ());
819816 }
820817 }
818+ } else {
819+ mClient .exhaustResponse (getMethod .getResponseBodyAsStream ());
820+ Log_OC .w (TAG , "Failed to download thumbnail, HTTP status: " + status );
821821 }
822822
823- // Add thumbnail to cache
824- if (thumbnail != null ) {
825- Log_OC .d (TAG , "add thumbnail to cache: " + file .getFileName ());
826- addBitmapToCache (imageKey , thumbnail );
823+ if (thumbnail != null && PNG_MIMETYPE .equalsIgnoreCase (file .getMimeType ())) {
824+ thumbnail = handlePNG (thumbnail , pxW , pxH );
825+ Log_OC .d (TAG , "Handled PNG thumbnail for downloaded file: " + file .getFileName ());
826+ }
827+ } catch (Exception e ) {
828+ Log_OC .e (TAG , "Exception downloading thumbnail for file: " + file .getFileName (), e );
829+ } finally {
830+ if (getMethod != null ) {
831+ getMethod .releaseConnection ();
827832 }
828833 }
829834 }
830835
836+ // Add to disk cache if obtained
837+ if (thumbnail != null ) {
838+ Log_OC .d (TAG , "Adding final thumbnail to cache for file: " + file .getFileName ());
839+ addBitmapToCache (imageKey , thumbnail );
840+ } else {
841+ Log_OC .w (TAG , "Failed to obtain thumbnail for file: " + file .getFileName ());
842+ }
843+
831844 return thumbnail ;
832845 }
833846
0 commit comments