Skip to content

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

Open
baiyu-jujuc wants to merge 1 commit into
kekingcn:masterfrom
baiyu-jujuc:fix/redis-image-preview-npe
Open

baiyu-jujuc wants to merge 1 commit into
kekingcn:masterfrom
baiyu-jujuc:fix/redis-image-preview-npe

Conversation

@baiyu-jujuc

Copy link
Copy Markdown

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
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.

1 participant