Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix - Use eTag for Thumbnail Generation #14513

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ class FileUploadWorker(

val task = ThumbnailsCacheManager.ThumbnailGenerationTask(storageManager, user)
val file = File(uploadFileOperation.originalStoragePath)
val remoteId: String? = uploadFileOperation.file.remoteId
task.execute(ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, remoteId))
val eTag: String? = uploadFileOperation.file.etag
task.execute(ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, eTag))
} catch (e: Exception) {
Log_OC.e(TAG, "Error uploading", e)
result = RemoteOperationResult<Any?>(e)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/nextcloud/ui/ImageDetailFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class ImageDetailFragment : Fragment(), Injectable {
val drawable = ContextCompat.getDrawable(context, R.drawable.photo_pin) as LayerDrawable

val bitmap =
ThumbnailsCacheManager.getBitmapFromDiskCache(ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.remoteId)
ThumbnailsCacheManager.getBitmapFromDiskCache(ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.etag)
BitmapUtils.bitmapToCircularBitmapDrawable(resources, bitmap)?.let {
drawable.setDrawable(1, it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ public GalleryImageGenerationTask(
FileDataStorageManager storageManager,
List<GalleryImageGenerationTask> asyncTasks,
String imageKey,
int backgroundColor
) {
int backgroundColor) {
this.user = user;
this.storageManager = storageManager;
imageViewReference = new WeakReference<>(imageView);
Expand All @@ -277,10 +276,6 @@ public void setListener(GalleryImageGenerationTask.GalleryListener listener) {
this.listener = listener;
}

public String getImageKey() {
return imageKey;
}

@Override
protected Bitmap doInBackground(Object... params) {
Bitmap thumbnail;
Expand All @@ -292,10 +287,10 @@ protected Bitmap doInBackground(Object... params) {

file = (OCFile) params[0];

if (file.getRemoteId() != null || file.isPreviewAvailable()) {
if (file.getEtag() != null || file.isPreviewAvailable()) {
// Thumbnail in cache?
thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.getRemoteId());
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.getEtag());

if (thumbnail != null && !file.isUpdateThumbnailNeeded())
return getThumbnailFromCache(thumbnail);
Expand Down Expand Up @@ -654,6 +649,9 @@ private Bitmap doThumbnailFromOCFileInBackground() {
Bitmap thumbnail;
ServerFileInterface file = (ServerFileInterface) mFile;
String imageKey = PREFIX_THUMBNAIL + file.getRemoteId();
if (file instanceof OCFile ocFile) {
imageKey = PREFIX_THUMBNAIL + ocFile.getEtag();
}

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

Expand All @@ -670,8 +668,7 @@ private Bitmap doThumbnailFromOCFileInBackground() {
int pxH;
pxW = pxH = getThumbnailDimension();

if (file instanceof OCFile) {
OCFile ocFile = (OCFile) file;
if (file instanceof OCFile ocFile) {
if (ocFile.isDown()) {
Bitmap bitmap;
if (MimeTypeUtil.isVideo(ocFile)) {
Expand All @@ -698,6 +695,9 @@ private Bitmap doThumbnailFromOCFileInBackground() {
if (thumbnail == null) {
// check if resized version is available
String resizedImageKey = PREFIX_RESIZED_IMAGE + file.getRemoteId();
if (file instanceof OCFile ocFile) {
resizedImageKey = PREFIX_RESIZED_IMAGE + ocFile.getEtag();
}

Bitmap resizedImage;
if (updateEnforced) {
Expand Down Expand Up @@ -1295,7 +1295,7 @@ public static void generateResizedImage(OCFile file) {
Point p = getScreenDimension();
int pxW = p.x;
int pxH = p.y;
String imageKey = PREFIX_RESIZED_IMAGE + file.getRemoteId();
String imageKey = PREFIX_RESIZED_IMAGE + file.getEtag();

Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(file.getStoragePath(), pxW, pxH);

Expand All @@ -1313,7 +1313,7 @@ public static void generateThumbnailFromOCFile(OCFile file, User user, Context c
int pxW;
int pxH;
pxW = pxH = getThumbnailDimension();
String imageKey = PREFIX_THUMBNAIL + file.getRemoteId();
String imageKey = PREFIX_THUMBNAIL + file.getEtag();

GetMethod getMethod = null;

Expand Down Expand Up @@ -1373,7 +1373,7 @@ public static void clearCache() {
private static Bitmap doResizedImageInBackground(OCFile file, FileDataStorageManager storageManager) {
Bitmap thumbnail;

String imageKey = PREFIX_RESIZED_IMAGE + file.getRemoteId();
String imageKey = PREFIX_RESIZED_IMAGE + file.getEtag();

// Check disk cache in background thread
thumbnail = getBitmapFromDiskCache(imageKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ private void saveUploadedFile(OwnCloudClient client) {
// generate new Thumbnail
final ThumbnailsCacheManager.ThumbnailGenerationTask task =
new ThumbnailsCacheManager.ThumbnailGenerationTask(getStorageManager(), user);
task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, file.getRemoteId()));
task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, file.getEtag()));
}

private void updateOCFile(OCFile file, RemoteFile remoteFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) thr

public static ParcelFileDescriptor getParcelFileDescriptorForOCFile(OCFile ocFile) throws FileNotFoundException {
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + ocFile.getRemoteId());
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + ocFile.getEtag());

// fallback to thumbnail
if (thumbnail == null) {
thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_THUMBNAIL + ocFile.getRemoteId());
ThumbnailsCacheManager.PREFIX_THUMBNAIL + ocFile.getEtag());
}

// fallback to default image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public AssetFileDescriptor openDocumentThumbnail(String documentId,
OCFile file = document.getFile();

boolean exists = ThumbnailsCacheManager.containsBitmap(ThumbnailsCacheManager.PREFIX_THUMBNAIL
+ file.getRemoteId());
+ file.getEtag());
if (!exists) {
ThumbnailsCacheManager.generateThumbnailFromOCFile(file, document.getUser(), getContext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ protected void setThumbnailView(final User user) {
LayerDrawable drawable = MimeTypeUtil.getFolderIcon(preferences.isDarkModeEnabled(), overlayIconId, this, viewThemeUtils);
binding.thumbnail.setImageDrawable(drawable);
} else {
if ((MimeTypeUtil.isImage(file) || MimeTypeUtil.isVideo(file)) && file.getRemoteId() != null) {
if ((MimeTypeUtil.isImage(file) || MimeTypeUtil.isVideo(file)) && file.getEtag() != null) {
// Thumbnail in cache?
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getRemoteId());
ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getEtag());

if (thumbnail != null && !file.isUpdateThumbnailNeeded()) {
if (MimeTypeUtil.isVideo(file)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ protected void onCreate(Bundle savedInstanceState) {
this,
viewThemeUtils));
if (MimeTypeUtil.isImage(file)) {
String remoteId = String.valueOf(file.getRemoteId());
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(file.getEtag());
if (thumbnail != null) {
binding.shareFileIcon.setImageBitmap(thumbnail);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class OCFileListDelegate(
// cancel previous generation, if view is re-used
if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, thumbnailView)) {
for (task in asyncTasks) {
if (file.remoteId != null && task.imageKey != null && file.remoteId == task.imageKey) {
if (file.etag != null && task.imageKey != null && file.etag == task.imageKey) {
return
}
}
Expand All @@ -138,7 +138,7 @@ class OCFileListDelegate(
user,
storageManager,
asyncGalleryTasks,
file.remoteId,
file.etag,
ContextCompat.getColor(context, R.color.bg_default)
)
var drawable = MimeTypeUtil.getFileTypeIcon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ReceiveExternalFilesAdapter(

if (file.isFolder) {
setupThumbnailForFolder(thumbnailImageView, file)
} else if (MimeTypeUtil.isImage(file) && file.remoteId != null) {
} else if (MimeTypeUtil.isImage(file) && file.etag != null) {
setupThumbnailForImage(thumbnailImageView, file)
} else {
setupDefaultThumbnail(thumbnailImageView, file)
Expand All @@ -121,7 +121,7 @@ class ReceiveExternalFilesAdapter(

@Suppress("NestedBlockDepth")
private fun setupThumbnailForImage(thumbnailImageView: ImageView, file: OCFile) {
var thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(file.remoteId.toString())
var thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(file.etag)
if (thumbnail != null && !file.isUpdateThumbnailNeeded) {
thumbnailImageView.setImageBitmap(thumbnail)
} else {
Expand All @@ -143,7 +143,7 @@ class ReceiveExternalFilesAdapter(
thumbnailImageView.setImageDrawable(asyncDrawable)

@Suppress("DEPRECATION")
task.execute(ThumbnailGenerationTaskObject(file, file.remoteId))
task.execute(ThumbnailGenerationTaskObject(file, file.etag))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,10 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati

// TODO this code is duplicated; refactor to a common place
if (MimeTypeUtil.isImage(fakeFileToCheatThumbnailsCacheManagerInterface)
&& fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId() != null &&
&& fakeFileToCheatThumbnailsCacheManagerInterface.getEtag() != null &&
item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED) {
// Thumbnail in Cache?
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId()));
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getEtag()));

if (thumbnail != null && !fakeFileToCheatThumbnailsCacheManagerInterface.isUpdateThumbnailNeeded()) {
itemViewHolder.binding.thumbnail.setImageBitmap(thumbnail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ private void setFilePreview(OCFile file) {
Bitmap resizedImage;

if (toolbarActivity != null && MimeTypeUtil.isImage(file)) {
String tagId = ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + getFile().getRemoteId();
String tagId = ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + getFile().getEtag();
resizedImage = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);

if (resizedImage != null && !file.isUpdateThumbnailNeeded()) {
Expand All @@ -674,7 +674,7 @@ private void setFilePreview(OCFile file) {
} else {
// show thumbnail while loading resized image
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_THUMBNAIL + getFile().getRemoteId());
ThumbnailsCacheManager.PREFIX_THUMBNAIL + getFile().getEtag());

if (thumbnail != null) {
toolbarActivity.setPreviewImageBitmap(thumbnail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class PreviewImageFragment : FileFragment(), Injectable {
while (i < 3 && cachedImage == null) {
try {
cachedImage = ThumbnailsCacheManager.getScaledBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.remoteId,
ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + file.etag,
scaledWidth,
scaledHeight
)
Expand All @@ -328,7 +328,7 @@ class PreviewImageFragment : FileFragment(), Injectable {
}

private fun getThumbnailBitmap(file: OCFile): Bitmap? {
return ThumbnailsCacheManager.getBitmapFromDiskCache(ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.remoteId)
return ThumbnailsCacheManager.getBitmapFromDiskCache(ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.etag)
}

override fun onStop() {
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/com/owncloud/android/utils/DisplayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -871,10 +871,10 @@ public static void setThumbnail(OCFile file,
LayerDrawable fileIcon = MimeTypeUtil.getFolderIcon(isDarkModeActive, overlayIconId, context, viewThemeUtils);
thumbnailView.setImageDrawable(fileIcon);
} else {
if (file.getRemoteId() != null && file.isPreviewAvailable()) {
if (file.getEtag() != null && file.isPreviewAvailable()) {
// Thumbnail in cache?
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getRemoteId());
ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getEtag());

if (thumbnail != null && !file.isUpdateThumbnailNeeded()) {
stopShimmer(shimmerThumbnail, thumbnailView);
Expand All @@ -897,7 +897,7 @@ public static void setThumbnail(OCFile file,
thumbnailView.setBackgroundColor(context.getResources().getColor(R.color.bg_default));
}
} else {
if (file.getRemoteId() != null) {
if (file.getEtag() != null) {
generateNewThumbnail(file, thumbnailView, user, storageManager, asyncTasks, gridView, context, shimmerThumbnail, preferences, viewThemeUtils);
} else {
stopShimmer(shimmerThumbnail, thumbnailView);
Expand Down Expand Up @@ -925,7 +925,7 @@ private static void generateNewThumbnail(OCFile file,
}

Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getRemoteId());
ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getEtag());

if (thumbnail != null) {
// If thumbnail is already in cache, display it immediately
Expand All @@ -935,8 +935,8 @@ private static void generateNewThumbnail(OCFile file,
}

for (ThumbnailsCacheManager.ThumbnailGenerationTask task : asyncTasks) {
if (file.getRemoteId() != null && task.getImageKey() != null &&
file.getRemoteId().equals(task.getImageKey())) {
if (file.getEtag() != null && task.getImageKey() != null &&
file.getEtag().equals(task.getImageKey())) {
return;
}
}
Expand All @@ -948,7 +948,7 @@ private static void generateNewThumbnail(OCFile file,
user,
asyncTasks,
gridView,
file.getRemoteId());
file.getEtag());
Drawable drawable = MimeTypeUtil.getFileTypeIcon(file.getMimeType(),
file.getFileName(),
context,
Expand Down Expand Up @@ -995,7 +995,7 @@ public void onError() {
asyncTasks.add(task);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file,
file.getRemoteId()));
file.getEtag()));
} catch (IllegalArgumentException e) {
Log_OC.d(TAG, "ThumbnailGenerationTask : " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static Drawable getFileTypeIcon(String mimetype,
return null;
}

if (R.drawable.file_zip == iconId) {
if (R.drawable.file_zip == iconId && viewThemeUtils != null) {
viewThemeUtils.platform.tintDrawable(context, icon, ColorRole.PRIMARY);
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public static Drawable getOCFileIcon(OCFile file, Context context, ViewThemeUtil
return MimeTypeUtil.getFileTypeIcon(file.getMimeType(), file.getFileName(), context, viewThemeUtils);
}

Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getRemoteId());
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getEtag());
if (thumbnail == null || file.isUpdateThumbnailNeeded()) {
return MimeTypeUtil.getFileTypeIcon(file.getMimeType(), file.getFileName(), context, viewThemeUtils);
}
Expand Down
Loading