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

Development: Handle course image persistence directly #7912

Merged
merged 8 commits into from
Jan 21, 2024

Conversation

julian-christl
Copy link
Member

@julian-christl julian-christl commented Jan 18, 2024

Checklist

General

Server

  • Important: I implemented the changes with a very good performance and prevented too many (unnecessary) database calls.
  • I followed the coding and design guidelines.
  • I documented the Java code using JavaDoc style.

Motivation and Context

In #5733 I replaced the two-parted upload with a single multipart upload but forgot to remove the delayed persistence of the file path that was now obsolete.

Description

In this PR, I opened the API of the file service to allow services to save a file directly to a path instead of saving it to a temporary path as before. I removed the delayed persistence method from the Course object and let the course resources handle the file saving directly.
Creating a course requires an additional database call as the current implementation requires the ID as part of the course Icon public path. There are ways to prevent this, but it requires a completely different implementation or to keep replacing the ID in the path on load.

Steps for Testing

Prerequisites:

  • 1 Admin
  • 1 Instructor
  1. Create Course A with an Admin with a custom image
  2. Create Course B with an Admin without a custom image
  3. Update a course with an admin adapting the image (expect image updated)
  4. Update a course with an admin adapting the user groups (expect groups updated)
  5. Update a course with an instructor adapting the image (expect image updated)
  6. Delete the courses

Testserver States

Note

These badges show the state of the test servers.
Green = Currently available, Red = Currently locked







Review Progress

Performance Review

  • I (as a reviewer) confirm that the server changes (in particular related to database calls) are implemented with a very good performance

Code Review

  • Code Review 1
  • Code Review 2

Manual Tests

  • Test 1
  • Test 2

Test Coverage

Class/File Line Coverage Confirmation (assert/expect)
EntityFileService.java 97.0%
FileService.java 86.1%
LectureUnitProcessingService.java 91.45%
QuizExerciseService.java 97.6%
CourseResource.java 96.0%
AdminCourseResource.java 98.5%

Only changes that are expected to require no test adaptions. All changes in FileService (<90%) have enough coverage.

Summary by CodeRabbit

  • Refactor

    • Simplified file management in course-related operations.
    • Enhanced file path generation logic for better consistency.
    • Improved file saving process with clearer method responsibilities.
  • Bug Fixes

    • Ensured file names are safely generated and handled.
  • New Features

    • Introduced null safety checks for file operations.
  • Documentation

    • Updated method documentation to reflect changes in file handling logic.

@julian-christl julian-christl self-assigned this Jan 18, 2024
Copy link

coderabbitai bot commented Jan 18, 2024

Walkthrough

The software update streamlines file management within a Java application. Redundancies are eliminated by removing unused imports and file handling methods. Enhancements focus on file path generation and file saving processes, incorporating new methods for filename generation and ensuring null safety. Changes in file path handling affect several services and resources, indicating a shift towards a more unified and efficient file management system within the application.

Changes

Files Change Summary
Course.java Removed unused imports and file management methods.
.../service/EntityFileService.java, .../service/FileService.java, .../service/QuizExerciseService.java, .../service/LectureUnitProcessingService.java Updated file path generation logic and improved file saving functionality.
.../web/rest/CourseResource.java, .../web/rest/admin/AdminCourseResource.java Added imports and FilePathService, updated constructor and methods for file path handling.

🐰✍️
In the realm of code, where the files reside,
A rabbit hopped through, with a spring in its stride.
Paths now untangled, with a filename so clear,
"To cleaner code," it said, "we hop closer, my dear!" 🎉📂

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@github-actions github-actions bot added the server Pull requests that update Java code. (Added Automatically!) label Jan 18, 2024
@artemis-bot artemis-bot added this to In progress in Artemis Development Jan 18, 2024
@julian-christl julian-christl temporarily deployed to artemis-test1.artemis.cit.tum.de January 18, 2024 21:43 — with GitHub Actions Inactive
@julian-christl julian-christl marked this pull request as ready for review January 18, 2024 21:58
@julian-christl julian-christl requested a review from a team as a code owner January 18, 2024 21:58
@artemis-bot artemis-bot moved this from In progress to Ready for review in Artemis Development Jan 18, 2024
@julian-christl julian-christl requested a review from a team January 18, 2024 21:59
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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d4732f7 and 92cf859.
Files selected for processing (7)
  • src/main/java/de/tum/in/www1/artemis/domain/Course.java (3 hunks)
  • src/main/java/de/tum/in/www1/artemis/service/EntityFileService.java (1 hunks)
  • src/main/java/de/tum/in/www1/artemis/service/FileService.java (5 hunks)
  • src/main/java/de/tum/in/www1/artemis/service/LectureUnitProcessingService.java (1 hunks)
  • src/main/java/de/tum/in/www1/artemis/service/QuizExerciseService.java (1 hunks)
  • src/main/java/de/tum/in/www1/artemis/web/rest/CourseResource.java (5 hunks)
  • src/main/java/de/tum/in/www1/artemis/web/rest/admin/AdminCourseResource.java (5 hunks)
Files skipped from review due to trivial changes (1)
  • src/main/java/de/tum/in/www1/artemis/domain/Course.java
Additional comments: 21
src/main/java/de/tum/in/www1/artemis/service/EntityFileService.java (1)
  • 71-72: The new generateFilename method is used to generate the filename, which is then resolved to the target folder. This change affects the file path generation and assignment process within the moveFileBeforeEntityPersistenceWithIdIfIsTemp method. Ensure that the generateFilename method correctly handles edge cases and potential security issues related to file path manipulation.
src/main/java/de/tum/in/www1/artemis/web/rest/admin/AdminCourseResource.java (2)
  • 131-134: The createCourse method now handles file paths and saving directly. Ensure that the saveFileFoo method (which seems to be a placeholder name and should be verified) correctly saves the file and that the publicPathForActualPathOrThrow method properly converts the saved path to a public path. Also, verify that the file handling process does not introduce any security vulnerabilities, such as path traversal or improper access controls.
  • 160-162: The deleteCourse method now includes file path handling for course icons. Ensure that the schedulePathForDeletion method correctly schedules the deletion of the course icon file and that there are no race conditions or security issues with the deletion process.
src/main/java/de/tum/in/www1/artemis/service/LectureUnitProcessingService.java (1)
  • 214-214: The saveTempFileForProcessing method now uses a different approach for generating the file path and filename. Ensure that the generateFilename method is secure and correctly handles the file name generation to prevent any security issues such as path traversal or file name prediction attacks.
src/main/java/de/tum/in/www1/artemis/service/QuizExerciseService.java (1)
  • 383-383: The saveDragAndDropImage method has been modified to use the basePath.resolve method instead of fileService.generateFilePath to generate the savePath. Ensure that the new logic for constructing the savePath is secure and does not introduce any vulnerabilities, such as path traversal or improper file access.
src/main/java/de/tum/in/www1/artemis/service/FileService.java (10)
  • 166-166: The method handleSaveFile has been modified to include a call to validateExtension which checks the file extension against a set of allowed extensions. This is a security improvement to prevent the upload of potentially dangerous file types.
  • 177-178: The generateFilename method is used to create a filename when keepFilename is false. This method uses the current time and a random UUID to generate a unique filename, which is a good practice to avoid collisions and potential security issues with user-provided filenames.
  • 181-185: The copyFile method is called to handle the actual file copying. The method copyFile is private and encapsulates the file copying logic, which is a good example of the Single Responsibility Principle.
  • 204-209: The copyFile method is a new private method that encapsulates the logic for copying a file from an InputStream to a Path. This is a good example of refactoring for better readability and maintainability.
  • 221-222: The checkAndSanitizeFilename method has been made public and annotated with null safety annotations. This change increases the method's visibility for reuse and ensures that it handles null values correctly.
  • 237-242: The validateExtension method has been refactored to check the file extension against a set of allowed extensions. This is a security improvement to ensure that only files with allowed extensions can be uploaded.
  • 253-254: The generateFilename method has been refactored to generate a filename based on the current time and a random UUID. This is a good practice to avoid filename collisions and potential security issues.
  • 268-268: The copyExistingFileToTarget method has been modified to use the generateFilename method for generating the target filename. This ensures consistency in filename generation across the service.
  • 178-178: The bot has flagged potential security issues with uncontrolled data used in path expressions. The use of generateFilename mitigates this by generating a safe filename, but it's important to ensure that the base path (path) is also safe and not user-controlled.
  • 199-199: The bot has flagged potential security issues with uncontrolled data used in path expressions. The use of generateFilename mitigates this by generating a safe filename, but it's important to ensure that the base path (basePath) is also safe and not user-controlled.
src/main/java/de/tum/in/www1/artemis/web/rest/CourseResource.java (6)
  • 8-9: The addition of java.net.URI and java.net.URISyntaxException imports indicates new functionality related to URI handling has been introduced.
  • 116-116: A new field FilePathService filePathService has been added to the class. This is in line with the PR's objective to handle file paths more directly.
  • 124-124: The constructor now accepts FilePathService as a parameter, which is correctly assigned to the class field. This change is necessary to support the new file path handling logic.
  • 158-158: The updateCourse method now declares that it throws URISyntaxException. This change is consistent with the new file handling logic that can potentially throw this exception.
  • 231-237: The logic for handling file paths and icons has been updated. The FilePathService is used to determine the base path for course icons and to generate a public path for the saved file. This aligns with the PR's objective to streamline file handling.
  • 239-241: The code correctly handles the case where the course icon is removed by scheduling the old icon for deletion. This is a good use of the FilePathService to manage file system paths.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 92cf859 and 6e09f0c.
Files selected for processing (3)
  • src/main/java/de/tum/in/www1/artemis/service/FileService.java (5 hunks)
  • src/main/java/de/tum/in/www1/artemis/web/rest/CourseResource.java (5 hunks)
  • src/main/java/de/tum/in/www1/artemis/web/rest/admin/AdminCourseResource.java (5 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/main/java/de/tum/in/www1/artemis/service/FileService.java
  • src/main/java/de/tum/in/www1/artemis/web/rest/CourseResource.java
  • src/main/java/de/tum/in/www1/artemis/web/rest/admin/AdminCourseResource.java

Copy link
Member

@konrad2002 konrad2002 left a comment

Choose a reason for hiding this comment

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

manually tested on ts1, works as expected

Copy link
Contributor

@MaximilianJG MaximilianJG left a comment

Choose a reason for hiding this comment

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

Tested on Ts2. Works as expected

Copy link
Contributor

@kaancayli kaancayli left a comment

Choose a reason for hiding this comment

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

Tested on TS4. Works as expected. Code lgtm

Copy link
Contributor

@MichaelOwenDyer MichaelOwenDyer left a comment

Choose a reason for hiding this comment

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

Code looks clean and I don't see any issues 👍🏽

@krusche krusche merged commit df2a5a9 into develop Jan 21, 2024
29 of 33 checks passed
@krusche krusche deleted the chore/simplify-course-icon-save branch January 21, 2024 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to merge server Pull requests that update Java code. (Added Automatically!)
Projects
No open projects
Artemis Development
  
Ready for review
Development

Successfully merging this pull request may close these issues.

None yet

6 participants