Skip to content

fix: avoid NPE when previewing images with redis cache - #797

Merged
klboke merged 1 commit into
kekingcn:masterfrom
baiyu-jujuc:fix/redis-image-preview-npe
Oct 8, 2026
Merged

klboke merged 1 commit into
kekingcn:masterfrom
baiyu-jujuc:fix/redis-image-preview-npe

Conversation

@baiyu-jujuc

Copy link
Copy Markdown
Contributor

Problem

With cache.type=redis, previewing a single image always returns HTTP 500. PDF / Office / archive previews are unaffected.

java.lang.NullPointerException: map key can't be null
    at org.redisson.RedissonMap.checkKey(RedissonMap.java:448)
    at cn.keking.service.cache.impl.CacheServiceRedisImpl.getImgCache(CacheServiceRedisImpl.java:72)
    at cn.keking.service.FileHandlerService.getImgCache(FileHandlerService.java:119)
    at cn.keking.service.impl.PictureFilePreviewImpl.filePreviewHandle(PictureFilePreviewImpl.java:34)

Root cause

PictureFilePreviewImpl#filePreviewHandle passes fileAttribute.getCompressFileKey() into getImgCache(String). kkCompressfileKey is only present when previewing an image inside an archive, so for a plain image the key is null.

CacheServiceRedisImpl#getImgCache(String) forwards that key straight to Redisson, which rejects a null key with NullPointerException. CacheServiceJDKImpl#getImgCache(String) already guards against an empty key, so only the redis implementation is affected.
Fix

Return an empty list for empty keys, aligning the redis implementation with the JDK one (5 lines).

Verification

  • Added CacheServiceRedisImplTests (Mockito, no real Redis required):
    • null key → empty list, and verifyNoInteractions(redissonClient)
    • empty key → empty list
    • present key → still reads from Redis
  • Verified the tests fail before the fix (Tests run: 3, Errors: 2) and pass after (Tests run: 3, Failures: 0).
  • End-to-end: GET /onlinePreview?url=<base64 image url> with cache.type=redis returned 500 before, 200 after.

Fixes 793

CacheServiceRedisImpl#getImgCache(String) passed the key straight to Redisson, which throws NullPointerException (map key can't be null) for a null key. Every single-image preview hits this path because compressFileKey is only set for images inside an archive, so with cache.type=redis the preview endpoint always returned HTTP 500. Return an empty list for empty keys, matching CacheServiceJDKImpl.

Fixes kekingcn#793

@klboke klboke left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head c1b9daa and traced PictureFilePreviewImpl -> FileHandlerService -> CacheServiceRedisImpl. Returning an empty list for null/empty archive keys matches the existing JDK cache behavior and avoids calling Redisson for an ordinary image preview; a present key still reads the existing map.

Validation: all 3 CacheServiceRedisImplTests pass locally, including verifyNoInteractions for null/empty keys and a populated archive key. Linux, Windows, and macOS packaging plus PR E2E are all green on this head. No blocking findings.

@klboke
klboke merged commit 0938f36 into kekingcn:master Oct 8, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants