Skip to content

Conversation

@lu-yg
Copy link
Collaborator

@lu-yg lu-yg commented Sep 17, 2025

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Resource download now uses simple, shareable URLs with name and an option to fetch the original or thumbnail.
    • Automatic unique naming for uploaded resources to avoid collisions.
  • Bug Fixes

    • Clear error returned when a requested resource is not found.
  • Documentation

    • Updated API docs to reflect new request parameters (name, isResource) for resource and thumbnail retrieval.
  • Chores

    • Simplified upload and lookup logic to improve reliability and consistency.

@coderabbitai
Copy link

coderabbitai bot commented Sep 17, 2025

Warning

Rate limit exceeded

@lu-yg has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 44 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 1014efc and 64a7397.

📒 Files selected for processing (2)
  • base/src/main/java/com/tinyengine/it/controller/ResourceController.java (2 hunks)
  • base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java (3 hunks)

Walkthrough

Refactors resource retrieval from base64 data to name-based lookup with an isResource flag. Updates controller endpoint signature, service interface, and implementation accordingly. Removes category from ResourceRequestDto. Upload logic now assigns unique names and builds download/thumbnail URLs; duplicate handling via ServiceException.

Changes

Cohort / File(s) Summary of Changes
Controller: name-based download
base/src/main/java/com/tinyengine/it/controller/ResourceController.java
Endpoint now accepts name and isResource; decodes name, queries by name, selects resource/thumbnail data, sets filename accordingly; throws ServiceException (CM009) if not found; imports adjusted.
DTO simplification
base/src/main/java/com/tinyengine/it/model/dto/ResourceRequestDto.java
Removed category field; DTO now only includes name and isResource.
Service API: query by name
base/src/main/java/com/tinyengine/it/service/material/ResourceService.java
Renamed and changed method signature: queryResourceByData(ResourceRequestDto)queryResourceByName(String); removed DTO dependency; Javadoc updated.
Service implementation and upload flow
base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java
Implements queryResourceByName(String) with name-only lookup; upload now generates unique names, constructs resource/thumbnail URLs using query params, removes base64/data-URL handling; duplicate checks via ServiceException; removed checked exception from resourceUpload; imports updated (URLEncoder, StandardCharsets, Instant).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant Controller as ResourceController
  participant Service as ResourceService
  participant Repo as ResourceRepository

  rect rgba(230,245,255,0.5)
  note over Client,Controller: Download by name
  Client->>Controller: GET /resource?name=...&isResource=bool
  Controller->>Controller: URL decode(name)
  Controller->>Service: queryResourceByName(name)
  Service->>Repo: findByName(name)
  Repo-->>Service: Resource or null
  alt Resource found
    Service-->>Controller: Resource
    Controller->>Controller: Select data (resource vs thumbnail)
    Controller-->>Client: 200 OK + bytes (Content-Disposition)
  else Not found
    Controller-->>Client: ServiceException (CM009)
  end
  end
Loading
sequenceDiagram
  autonumber
  actor Client
  participant Service as ResourceServiceImpl
  participant Repo as ResourceRepository

  rect rgba(235,255,235,0.5)
  note over Client,Service: Upload flow (name/URL-based)
  Client->>Service: resourceUpload(Resource)
  Service->>Service: Generate unique name (Instant-based)
  Service->>Service: Build URLs with ?name=...&isResource=true/false
  Service->>Repo: Check duplicate by name
  alt Duplicate
    Service-->>Client: throw ServiceException
  else New
    Service->>Repo: save(Resource)
    Repo-->>Service: Persisted Resource
    Service-->>Client: Persisted Resource with URLs
  end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I hop through names, not data streams,
Thumbnails twinkle, resource gleams.
A fresh URL, a tidy trail,
No base64 to tip the scale.
With ears up high, I stamp “OK!”—
New flows by name now lead the way. 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "fix: modify resource upload" correctly signals that the PR changes the resource upload flow and related resource handling (controller and service API changes, DTO removal), so it is related to the main changes in the diff and is not misleading, though it is somewhat high-level.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 7

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java (1)

189-196: Also enforce app/tenant in duplicate check.

Currently checking only name and category can cross apps.

Apply:

     QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
     queryWrapper.eq("name", resource.getName());
     queryWrapper.eq("category", resource.getCategory());
+    queryWrapper.eq("app_id", loginUserContext.getAppId());
🧹 Nitpick comments (4)
base/src/main/java/com/tinyengine/it/service/material/ResourceService.java (1)

47-52: Drop unnecessary checked exception from the new API.

queryResourceByName(String name) doesn’t throw checked exceptions in the impl; keep the interface clean by removing throws Exception for this method.

Apply:

-    Resource queryResourceByName(String name) throws Exception;
+    Resource queryResourceByName(String name);
base/src/main/java/com/tinyengine/it/controller/ResourceController.java (1)

312-312: Narrow the throws clause.

getOutputStream() only throws IOException. Prefer throws IOException (or let your global exception handler translate it).

base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java (2)

175-178: Reduce collision risk in generated names.

Concatenating epoch millis with the original name can collide under concurrent uploads. Add a delimiter and a UUID.

Apply:

-    String imageName = Instant.now().toEpochMilli()+resource.getName();
+    String imageName = Instant.now().toEpochMilli() + "-" + java.util.UUID.randomUUID() + "-" + resource.getName();

181-188: Guard against empty resource data.

Persisting without resourceUrl/thumbnailUrl if data is empty leads to unusable records.

Apply:

-    if (!StringUtils.isEmpty(resourceData)) {
+    if (!StringUtils.isEmpty(resourceData)) {
         ...
-    }
+    } else {
+        throw new ServiceException(ExceptionEnum.CM002.getResultCode(), "Resource data is empty");
+    }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ff2f353 and 1014efc.

📒 Files selected for processing (4)
  • base/src/main/java/com/tinyengine/it/controller/ResourceController.java (3 hunks)
  • base/src/main/java/com/tinyengine/it/model/dto/ResourceRequestDto.java (0 hunks)
  • base/src/main/java/com/tinyengine/it/service/material/ResourceService.java (1 hunks)
  • base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java (3 hunks)
💤 Files with no reviewable changes (1)
  • base/src/main/java/com/tinyengine/it/model/dto/ResourceRequestDto.java
🧰 Additional context used
🧬 Code graph analysis (1)
base/src/main/java/com/tinyengine/it/controller/ResourceController.java (1)
base/src/main/java/com/tinyengine/it/common/utils/ImageThumbnailGenerator.java (1)
  • ImageThumbnailGenerator (42-515)
🔇 Additional comments (1)
base/src/main/java/com/tinyengine/it/controller/ResourceController.java (1)

324-324: LGTM: filename logic.

thumbnail_ prefix selection for derived files is correct and user-friendly.

lichunn
lichunn previously approved these changes Sep 17, 2025
hexqi
hexqi previously approved these changes Sep 17, 2025
@lu-yg lu-yg merged commit 15011b4 into opentiny:develop Sep 17, 2025
1 check passed
lu-yg added a commit to lu-yg/tiny-engine-backend-java that referenced this pull request Oct 23, 2025
* fix: modify resource upload
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.

3 participants