Skip to content

Commit e439312

Browse files
committed
refactor(thumbnails): simplify doThumbnailFromOCFileInBackground logic
Signed-off-by: alperozturk <[email protected]>
1 parent 931fcda commit e439312

File tree

1 file changed

+97
-84
lines changed

1 file changed

+97
-84
lines changed

app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

Lines changed: 97 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -724,111 +724,124 @@ private Bitmap doThumbnailFromOCFileInBackground() {
724724

725725
boolean updateEnforced = (file instanceof OCFile && ((OCFile) file).isUpdateThumbnailNeeded());
726726

727-
if (updateEnforced) {
728-
thumbnail = null;
729-
} else {
730-
// Check disk cache in background thread
727+
// Try to load thumbnail from disk cache
728+
if (!updateEnforced) {
731729
thumbnail = getBitmapFromDiskCache(imageKey);
730+
if (thumbnail != null) {
731+
Log_OC.d(TAG, "Thumbnail found in disk cache for file: " + file.getFileName());
732+
return thumbnail;
733+
} else {
734+
Log_OC.d(TAG, "Thumbnail not found in cache for file: " + file.getFileName());
735+
}
736+
} else {
737+
Log_OC.d(TAG, "Thumbnail update enforced for file: " + file.getFileName());
738+
thumbnail = null;
732739
}
733740

734-
// Not found in disk cache
735-
if (thumbnail == null) {
736-
int pxW;
737-
int pxH;
738-
pxW = pxH = getThumbnailDimension();
739-
740-
if (file instanceof OCFile ocFile) {
741-
if (ocFile.isDown()) {
742-
Bitmap bitmap;
743-
if (MimeTypeUtil.isVideo(ocFile)) {
744-
bitmap = ThumbnailUtils.createVideoThumbnail(ocFile.getStoragePath(),
745-
MediaStore.Images.Thumbnails.MINI_KIND);
746-
} else {
747-
bitmap = BitmapUtils.decodeSampledBitmapFromFile(ocFile.getStoragePath(), pxW, pxH);
748-
}
741+
int pxW;
742+
int pxH;
743+
pxW = pxH = getThumbnailDimension();
749744

750-
if (bitmap != null) {
751-
// Handle PNG
752-
if (PNG_MIMETYPE.equalsIgnoreCase(ocFile.getMimeType())) {
753-
bitmap = handlePNG(bitmap, pxW, pxH);
754-
}
745+
// Generate thumbnail from local file if available
746+
if (file instanceof OCFile ocFile && ocFile.isDown()) {
747+
Log_OC.d(TAG, "Generating thumbnail from local file: " + ocFile.getFileName());
755748

756-
thumbnail = addThumbnailToCache(imageKey, bitmap, ocFile.getStoragePath(), pxW, pxH);
749+
Bitmap bitmap;
750+
if (MimeTypeUtil.isVideo(ocFile)) {
751+
bitmap = ThumbnailUtils.createVideoThumbnail(ocFile.getStoragePath(),
752+
MediaStore.Images.Thumbnails.MINI_KIND);
753+
} else {
754+
bitmap = BitmapUtils.decodeSampledBitmapFromFile(ocFile.getStoragePath(), pxW, pxH);
755+
}
757756

758-
ocFile.setUpdateThumbnailNeeded(false);
759-
mStorageManager.saveFile(ocFile);
760-
}
757+
if (bitmap != null) {
758+
if (PNG_MIMETYPE.equalsIgnoreCase(ocFile.getMimeType())) {
759+
bitmap = handlePNG(bitmap, pxW, pxH);
761760
}
761+
762+
thumbnail = addThumbnailToCache(imageKey, bitmap, ocFile.getStoragePath(), pxW, pxH);
763+
ocFile.setUpdateThumbnailNeeded(false);
764+
mStorageManager.saveFile(ocFile);
762765
}
766+
}
763767

764-
if (thumbnail == null) {
765-
// check if resized version is available
766-
String resizedImageKey = PREFIX_RESIZED_IMAGE + file.getRemoteId();
768+
// Check resized version in disk cache if still null
769+
if (thumbnail == null) {
770+
String resizedImageKey = PREFIX_RESIZED_IMAGE + file.getRemoteId();
771+
Bitmap resizedImage = null;
767772

768-
Bitmap resizedImage;
769-
if (updateEnforced) {
770-
resizedImage = null;
773+
if (!updateEnforced) {
774+
resizedImage = getBitmapFromDiskCache(resizedImageKey);
775+
}
776+
777+
if (resizedImage != null) {
778+
thumbnail = ThumbnailUtils.extractThumbnail(resizedImage, pxW, pxH);
779+
Log_OC.d(TAG, "Thumbnail generated from resized image cache for file: " + file.getFileName());
780+
} else {
781+
Log_OC.d(TAG, "No resized image cache available for file: " + file.getFileName());
782+
}
783+
}
784+
785+
// Download thumbnail from server if still null
786+
if (thumbnail == null && mClient != null) {
787+
Log_OC.d(TAG, "Attempting to download thumbnail from server for file: " + file.getFileName());
788+
GetMethod getMethod = null;
789+
790+
try {
791+
String uri;
792+
if (file instanceof OCFile) {
793+
uri = mClient.getBaseUri() + "/index.php/core/preview?fileId="
794+
+ file.getLocalId()
795+
+ "&x=" + pxW + "&y=" + pxH + "&a=1&mode=cover&forceIcon=0";
771796
} else {
772-
resizedImage = getBitmapFromDiskCache(resizedImageKey);
797+
uri = mClient.getBaseUri() + "/index.php/apps/files_trashbin/preview?fileId="
798+
+ file.getLocalId() + "&x=" + pxW + "&y=" + pxH;
773799
}
774800

775-
if (resizedImage != null) {
776-
thumbnail = ThumbnailUtils.extractThumbnail(resizedImage, pxW, pxH);
777-
} else {
778-
// Download thumbnail from server
779-
if (mClient != null) {
780-
getMethod = null;
781-
try {
782-
// thumbnail
783-
String uri;
784-
if (file instanceof OCFile) {
785-
uri = mClient.getBaseUri() + "/index.php/core/preview?fileId="
786-
+ file.getLocalId()
787-
+ "&x=" + pxW + "&y=" + pxH + "&a=1&mode=cover&forceIcon=0";
788-
} else {
789-
uri = mClient.getBaseUri() + "/index.php/apps/files_trashbin/preview?fileId=" +
790-
file.getLocalId() + "&x=" + pxW + "&y=" + pxH;
791-
}
792-
793-
Log_OC.d(TAG, "generate thumbnail: " + file.getFileName() + " URI: " + uri);
794-
getMethod = new GetMethod(uri);
795-
getMethod.setRequestHeader("Cookie",
796-
"nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true");
797-
798-
getMethod.setRequestHeader(RemoteOperation.OCS_API_HEADER,
799-
RemoteOperation.OCS_API_HEADER_VALUE);
800-
801-
int status = mClient.executeMethod(getMethod, READ_TIMEOUT, CONNECTION_TIMEOUT);
802-
if (status == HttpStatus.SC_OK) {
803-
InputStream inputStream = getMethod.getResponseBodyAsStream();
804-
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
805-
thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
806-
} else {
807-
mClient.exhaustResponse(getMethod.getResponseBodyAsStream());
808-
}
809-
810-
// Handle PNG
811-
if (PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) {
812-
thumbnail = handlePNG(thumbnail, pxW, pxH);
813-
}
814-
} catch (Exception e) {
815-
Log_OC.d(TAG, e.getMessage(), e);
816-
} finally {
817-
if (getMethod != null) {
818-
getMethod.releaseConnection();
819-
}
801+
Log_OC.d(TAG, "Downloading thumbnail URI: " + uri);
802+
803+
getMethod = new GetMethod(uri);
804+
getMethod.setRequestHeader("Cookie", "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true");
805+
getMethod.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE);
806+
807+
int status = mClient.executeMethod(getMethod, READ_TIMEOUT, CONNECTION_TIMEOUT);
808+
809+
if (status == HttpStatus.SC_OK) {
810+
try (InputStream inputStream = getMethod.getResponseBodyAsStream()) {
811+
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
812+
if (bitmap != null) {
813+
thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
814+
Log_OC.d(TAG, "Thumbnail downloaded and extracted for file: " + file.getFileName());
815+
} else {
816+
Log_OC.w(TAG, "Downloaded thumbnail bitmap is null for file: " + file.getFileName());
820817
}
821818
}
819+
} else {
820+
mClient.exhaustResponse(getMethod.getResponseBodyAsStream());
821+
Log_OC.w(TAG, "Failed to download thumbnail, HTTP status: " + status);
822822
}
823823

824-
// Add thumbnail to cache
825-
if (thumbnail != null) {
826-
Log_OC.d(TAG, "add thumbnail to cache: " + file.getFileName());
827-
addBitmapToCache(imageKey, thumbnail);
824+
if (thumbnail != null && PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) {
825+
thumbnail = handlePNG(thumbnail, pxW, pxH);
826+
Log_OC.d(TAG, "Handled PNG thumbnail for downloaded file: " + file.getFileName());
827+
}
828+
} catch (Exception e) {
829+
Log_OC.e(TAG, "Exception downloading thumbnail for file: " + file.getFileName(), e);
830+
} finally {
831+
if (getMethod != null) {
832+
getMethod.releaseConnection();
828833
}
829834
}
830835
}
831836

837+
// Add to disk cache if obtained
838+
if (thumbnail != null) {
839+
Log_OC.d(TAG, "Adding final thumbnail to cache for file: " + file.getFileName());
840+
addBitmapToCache(imageKey, thumbnail);
841+
} else {
842+
Log_OC.w(TAG, "Failed to obtain thumbnail for file: " + file.getFileName());
843+
}
844+
832845
return thumbnail;
833846
}
834847

0 commit comments

Comments
 (0)