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

Lectures: Fix file names for downloads with chromium browsers #9899

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

SimonEntholzer
Copy link
Contributor

@SimonEntholzer SimonEntholzer commented Nov 28, 2024

Checklist

General

Server

  • Important: I implemented the changes with a very good performance and prevented too many (unnecessary) and too complex database calls.
  • I strictly followed the principle of data economy for all database calls.
  • I strictly followed the server coding and design guidelines.
  • I documented the Java code using JavaDoc style.

Client

  • Important: I implemented the changes with a very good performance, prevented too many (unnecessary) REST calls and made sure the UI is responsive, even with large data (e.g. using paging).
  • I strictly followed the principle of data economy for all client-server REST calls.
  • I strictly followed the client coding and design guidelines.
  • Following the theming guidelines, I specified colors only in the theming variable files and checked that the changes look consistent in both the light and the dark theme.
  • I added multiple integration tests (Jest) related to the features (with a high test coverage), while following the test guidelines.
  • I documented the TypeScript code using JSDoc style.
  • I added multiple screenshots/screencasts of my UI changes.

Motivation and Context

There are still issues with the attachment downloads for lectures and lecture units:

  1. The file name in the URL is still the original file name, and not the attachment name
  2. Saving with chromium based browsers does not include the extension : in firefox it does have an extension
    Cause: We send the file without extension to the client, firefox manages to automatically deduct the file type + extension from the content, and appends the extension, and chrome simply doesn't.

Description

  1. Makes sure the user defined attachment name is used as the new file name, which users get when they download the attachment.
  2. We explicitly send the file with extension

Steps for Testing

  1. In lecture units and lectures upload attachments, and set names.
  2. Download the files again from the instructor view
  3. Download the files again from the student view

Testserver States

Note

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







Review Progress

Code Review

  • Code Review 1
  • Code Review 2

Manual Tests

  • Test 1
  • Test 2

Test Coverage

Client

Class/File Line Coverage Confirmation (assert/expect)
lecture-attachments.component.ts 96.58% ✅ ❌
attachment-unit.component.ts 94.11% ✅ ❌
course-lecture-details.component.ts 98.61% ✅ ❌
file.service.ts 44.18% -> 100%

Server

Screenshots

image
image

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced file download functionality by allowing downloads using both attachment name and URL.
    • Introduced a new method to download files by attachment name in the file service.
  • Improvements

    • Updated method signatures across components to include attachment names for improved clarity and functionality.
    • Improved error handling and logging related to file downloads.
  • Bug Fixes

    • Adjusted parameter handling in download methods to ensure correct attachment retrieval and processing.

@SimonEntholzer SimonEntholzer requested a review from a team as a code owner November 28, 2024 21:28
@github-actions github-actions bot added server Pull requests that update Java code. (Added Automatically!) client Pull requests that update TypeScript code. (Added Automatically!) core Pull requests that affect the corresponding module labels Nov 28, 2024
@SimonEntholzer SimonEntholzer added lecture Pull requests that affect the corresponding module ready for review labels Nov 28, 2024
@github-actions github-actions bot removed the lecture Pull requests that affect the corresponding module label Nov 28, 2024
Copy link

coderabbitai bot commented Nov 28, 2024

Walkthrough

The pull request introduces several modifications across multiple files, primarily focusing on enhancing the handling of attachment names and download processes. Key changes include updating method signatures to accept both attachment names and URLs, modifying the logic for file downloads, and introducing a new method in the FileService for downloading files by attachment name. The changes aim to improve clarity in variable naming and streamline the attachment retrieval and download functionalities while maintaining existing error handling and control flows.

Changes

File Path Change Summary
src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java Added static imports for getBaseName and getExtension. Renamed filename to attachmentName in getLectureAttachment. Updated logic for attachment filtering. Modified getAttachmentUnitAttachment to append file extension.
src/main/webapp/app/lecture/lecture-attachments.component.html Updated downloadAttachment method call to pass both attachment.name and attachment.link.
src/main/webapp/app/lecture/lecture-attachments.component.ts Changed downloadAttachment method to accept downloadName and downloadUrl. Updated implementation to call downloadFileByAttachmentName.
src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts Modified handleDownload to call downloadFileByAttachmentName with attachment name and link.
src/main/webapp/app/overview/course-lectures/course-lecture-details.component.html Updated downloadAttachment method call to include attachment name.
src/main/webapp/app/overview/course-lectures/course-lecture-details.component.ts Updated downloadAttachment method to include downloadName. Adjusted logic to check for downloadName before proceeding.
src/main/webapp/app/shared/http/file.service.ts Added downloadFileByAttachmentName method for downloading files with attachment name. Removed replaceLectureAttachmentPrefixAndUnderscores method.
src/test/javascript/spec/component/lecture/lecture-attachments.component.spec.ts Updated tests to reflect the new downloadAttachment method signature.
src/test/javascript/spec/component/overview/course-lectures/course-lecture-details.component.spec.ts Updated tests to use downloadFileByAttachmentName and reflect changes in downloadAttachment method signature.
src/test/javascript/spec/helpers/mocks/service/mock-file.service.ts Added downloadFileByAttachmentName method to MockFileService.
src/test/java/de/tum/cit/aet/artemis/lecture/AttachmentResourceIntegrationTest.java Updated logic in createAttachment test method to use getExtension for file name handling.
src/test/javascript/spec/component/lecture-unit/attachment-unit/attachment-unit.component.spec.ts Updated tests to spy on downloadFileByAttachmentName instead of downloadFile.
src/test/javascript/spec/component/shared/http/file.service.spec.ts Expanded test cases for FileService, including new methods and ensuring coverage for downloadFileByAttachmentName.

Possibly related PRs

Suggested labels

bugfix, ready to merge

Suggested reviewers

  • BBesrour
  • HawKhiem
  • az108
  • Feras797
  • krusche
  • HanyangXu0508

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 pmd (7.7.0)
src/test/java/de/tum/cit/aet/artemis/lecture/AttachmentResourceIntegrationTest.java

The following rules are missing or misspelled in your ruleset file category/vm/bestpractices.xml: BooleanInstantiation, DontImportJavaLang, DuplicateImports, EmptyFinallyBlock, EmptyIfStmt, EmptyInitializer, EmptyStatementBlock, EmptyStatementNotInLoop, EmptySwitchStatements, EmptySynchronizedBlock, EmptyTryBlock, EmptyWhileStmt, ExcessiveClassLength, ExcessiveMethodLength, ImportFromSamePackage, MissingBreakInSwitch, SimplifyBooleanAssertion. Please check your ruleset configuration.


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>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 6

🧹 Outside diff range and nitpick comments (5)
src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts (1)

Line range hint 33-41: Update getFileName to align with new naming strategy

The getFileName method still uses URL-based naming while the PR objective is to use user-defined attachment names.

Consider updating the method to use the attachment name first, falling back to URL-based naming:

 getFileName(): string {
-    if (this.lectureUnit().attachment?.link) {
-        const link = this.lectureUnit().attachment!.link!;
-        const filename = link.substring(link.lastIndexOf('/') + 1);
-        return this.fileService.replaceAttachmentPrefixAndUnderscores(filename);
+    const attachment = this.lectureUnit().attachment;
+    if (attachment?.name) {
+        return attachment.name;
+    } else if (attachment?.link) {
+        const link = attachment.link;
+        const filename = link.substring(link.lastIndexOf('/') + 1);
+        return this.fileService.replaceAttachmentPrefixAndUnderscores(filename);
     }
     return '';
 }
🧰 Tools
🪛 GitHub Check: client-compilation

[failure] 54-54:
Property 'downloadFileWithName' does not exist on type 'FileService'.

🪛 GitHub Check: client-tests-selected

[failure] 54-54:
Property 'downloadFileWithName' does not exist on type 'FileService'.

🪛 GitHub Check: client-tests

[failure] 54-54:
Property 'downloadFileWithName' does not exist on type 'FileService'.

src/main/webapp/app/shared/http/file.service.ts (2)

91-100: Consider refactoring to reduce code duplication

The downloadFileByAttachmentName method shares similar logic with downloadFile. Consider extracting common functionality.

+private openDownloadWindow(url: string): Window | null {
+    const normalizedUrl = encodeURIComponent(url);
+    const newWindow = window.open('about:blank');
+    if (!newWindow) {
+        window.location.href = normalizedUrl;
+        return null;
+    }
+    newWindow.location.href = normalizedUrl;
+    return newWindow;
+}

 downloadFile(downloadUrl: string) {
     const downloadUrlComponents = downloadUrl.split('/');
     const fileName = downloadUrlComponents.pop()!;
     const restOfUrl = downloadUrlComponents.join('/');
-    const normalizedDownloadUrl = restOfUrl + '/' + encodeURIComponent(fileName);
-    const newWindow = window.open('about:blank');
-    newWindow!.location.href = normalizedDownloadUrl;
-    return newWindow;
+    return this.openDownloadWindow(`${restOfUrl}/${fileName}`);
 }

 downloadFileByAttachmentName(downloadUrl: string, downloadName: string) {
     // ... extension extraction logic ...
-    const normalizedDownloadUrl = restOfUrl + '/' + encodeURIComponent(downloadName + '.' + extension);
-    const newWindow = window.open('about:blank');
-    newWindow!.location.href = normalizedDownloadUrl;
-    return newWindow;
+    return this.openDownloadWindow(`${restOfUrl}/${downloadName}.${extension}`);
 }

85-90: Documentation needs improvement

The JSDoc comment should include:

  • Return type description
  • Possible errors that might be thrown
  • Example usage
 /**
  * Downloads the file from the provided downloadUrl and the attachment name
  *
  * @param downloadUrl url that is stored in the attachment model
  * @param downloadName the name given to the attachment
+ * @returns {Window | null} The window object for the download, or null if popup was blocked
+ * @throws {Error} When download URL or name is invalid
+ * @example
+ * // Download file with custom name
+ * fileService.downloadFileByAttachmentName('http://example.com/file.pdf', 'custom-name');
  */
src/main/webapp/app/overview/course-lectures/course-lecture-details.component.ts (1)

131-136: Consider adding download progress indicator

To improve user experience, consider showing a progress indicator during downloads.

 downloadAttachment(downloadUrl: string, downloadName: string): void {
     if (!this.isDownloadingLink) {
         this.isDownloadingLink = downloadUrl;
+        // Show loading indicator in template when isDownloadingLink matches the current attachment
+        this.alertService.info('artemisApp.attachment.download.started');
         this.fileService.downloadFileByAttachmentName(downloadUrl, downloadName).pipe(
             finalize(() => {
                 this.isDownloadingLink = undefined;
             })
         ).subscribe({
+            next: () => {
+                this.alertService.success('artemisApp.attachment.download.success');
+            },
             error: (error: HttpErrorResponse) => {
                 this.alertService.error('artemisApp.attachment.download.error');
             }
         });
     }
src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java (1)

424-425: Consider improving error message clarity

The error message uses attachmentName directly, which includes the extension. Consider using getBaseName(attachmentName) in the error message for consistency with the filtering logic.

-                .orElseThrow(() -> new EntityNotFoundException("Attachment", attachmentName));
+                .orElseThrow(() -> new EntityNotFoundException("Attachment", getBaseName(attachmentName)));
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5f2e030 and d3b0932.

📒 Files selected for processing (7)
  • src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java (4 hunks)
  • src/main/webapp/app/lecture/lecture-attachments.component.html (1 hunks)
  • src/main/webapp/app/lecture/lecture-attachments.component.ts (1 hunks)
  • src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts (1 hunks)
  • src/main/webapp/app/overview/course-lectures/course-lecture-details.component.html (1 hunks)
  • src/main/webapp/app/overview/course-lectures/course-lecture-details.component.ts (1 hunks)
  • src/main/webapp/app/shared/http/file.service.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java (1)

Pattern src/main/java/**/*.java: naming:CamelCase; principles:{single_responsibility,small_methods,no_duplication}; db:{perf_queries,datetime_not_timestamp}; rest:{stateless,singleton,delegate_logic,http_only,minimal_dtos}; dtos:{java_records,no_entities,min_data,single_resp}; di:constructor_injection; kiss:simple_code; file_handling:os_indep_paths; practices:{least_access,avoid_transactions,code_reuse,static_member_ref,prefer_primitives}; sql:{param_annotation,uppercase,avoid_subqueries};java:avoid_star_imports

src/main/webapp/app/lecture/lecture-attachments.component.html (1)

Pattern src/main/webapp/**/*.html: @if and @for are new and valid Angular syntax replacing *ngIf and *ngFor. They should always be used over the old style.

src/main/webapp/app/lecture/lecture-attachments.component.ts (1)

Pattern src/main/webapp/**/*.ts: angular_style:https://angular.io/guide/styleguide;methods_in_html:false;lazy_loading:true;code_reuse:true;tests:meaningful;types:PascalCase;enums:PascalCase;funcs:camelCase;props:camelCase;no_priv_prefix:true;strings:single_quotes;localize:true;btns:functionality;links:navigation;icons_text:newline;labels:associate;code_style:arrow_funcs,curly_braces,open_braces_same_line,indent_4;memory_leak_prevention:true;routes:naming_schema;chart_framework:ngx-charts;responsive_layout:true

src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts (1)

Pattern src/main/webapp/**/*.ts: angular_style:https://angular.io/guide/styleguide;methods_in_html:false;lazy_loading:true;code_reuse:true;tests:meaningful;types:PascalCase;enums:PascalCase;funcs:camelCase;props:camelCase;no_priv_prefix:true;strings:single_quotes;localize:true;btns:functionality;links:navigation;icons_text:newline;labels:associate;code_style:arrow_funcs,curly_braces,open_braces_same_line,indent_4;memory_leak_prevention:true;routes:naming_schema;chart_framework:ngx-charts;responsive_layout:true

src/main/webapp/app/overview/course-lectures/course-lecture-details.component.html (1)

Pattern src/main/webapp/**/*.html: @if and @for are new and valid Angular syntax replacing *ngIf and *ngFor. They should always be used over the old style.

src/main/webapp/app/overview/course-lectures/course-lecture-details.component.ts (1)

Pattern src/main/webapp/**/*.ts: angular_style:https://angular.io/guide/styleguide;methods_in_html:false;lazy_loading:true;code_reuse:true;tests:meaningful;types:PascalCase;enums:PascalCase;funcs:camelCase;props:camelCase;no_priv_prefix:true;strings:single_quotes;localize:true;btns:functionality;links:navigation;icons_text:newline;labels:associate;code_style:arrow_funcs,curly_braces,open_braces_same_line,indent_4;memory_leak_prevention:true;routes:naming_schema;chart_framework:ngx-charts;responsive_layout:true

src/main/webapp/app/shared/http/file.service.ts (1)

Pattern src/main/webapp/**/*.ts: angular_style:https://angular.io/guide/styleguide;methods_in_html:false;lazy_loading:true;code_reuse:true;tests:meaningful;types:PascalCase;enums:PascalCase;funcs:camelCase;props:camelCase;no_priv_prefix:true;strings:single_quotes;localize:true;btns:functionality;links:navigation;icons_text:newline;labels:associate;code_style:arrow_funcs,curly_braces,open_braces_same_line,indent_4;memory_leak_prevention:true;routes:naming_schema;chart_framework:ngx-charts;responsive_layout:true

🪛 GitHub Check: client-compilation
src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts

[failure] 54-54:
Property 'downloadFileWithName' does not exist on type 'FileService'.

🪛 GitHub Check: client-tests-selected
src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts

[failure] 54-54:
Property 'downloadFileWithName' does not exist on type 'FileService'.

🪛 GitHub Check: client-tests
src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts

[failure] 54-54:
Property 'downloadFileWithName' does not exist on type 'FileService'.

🔇 Additional comments (9)
src/main/webapp/app/shared/http/file.service.ts (1)

91-100: Verify browser compatibility

Since this PR specifically addresses Chromium browser issues, we should verify the implementation across different browsers.

src/main/webapp/app/overview/course-lectures/course-lecture-details.component.ts (1)

131-136: Verify the impact on template bindings

The method signature change requires updates to the template bindings.

✅ Verification successful

Template binding is correctly aligned with method signature

The template binding in course-lecture-details.component.html correctly provides both required parameters:

  • attachment.link maps to downloadUrl
  • attachment.name maps to donwloadName
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check template usage of downloadAttachment method
# Expected: All calls should provide both URL and name parameters

# Search for downloadAttachment usage in templates
rg -t html 'downloadAttachment\(' src/main/webapp/app/overview/course-lectures/

Length of output: 300

src/main/webapp/app/overview/course-lectures/course-lecture-details.component.html (1)

Line range hint 38-44: Verify merged PDF download functionality.

Since this PR fixes file naming issues, ensure that the merged PDF download also uses proper file names.

Let's check the implementation:

✅ Verification successful

Let me gather more information about the file service implementation to verify the merged PDF download functionality.


Merged PDF download functionality is properly implemented

The implementation in course-lecture-details.component.ts shows that the merged PDF download uses the lecture title as the filename:

downloadStream(blob.body, 'application/pdf', this.lecture?.title ?? 'Lecture');

This is consistent with the file naming improvements in this PR, as it:

  • Uses a meaningful name (lecture title) for the downloaded file
  • Has a fallback to 'Lecture' if title is not available
  • Includes the correct file extension (.pdf)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the merged PDF download implementation

# Check the downloadMergedFiles method implementation
ast-grep --pattern 'downloadMergedFiles() {
  $$$
}'

# Look for any file service usage in the merged files download
rg -A 5 'downloadMergedFiles|FileService.*download.*PDF'

Length of output: 2604


Script:

#!/bin/bash
# Check the file service implementation for merged file download
ast-grep --pattern 'downloadMergedFile($_) {
  $$$
}'

# Look for any blob handling or file naming in the file service
rg -A 5 'downloadMergedFile|createBlobUrl|getFilename'

Length of output: 10958

src/main/webapp/app/lecture/lecture-attachments.component.ts (1)

203-208: Implementation aligns with PR objectives.

The changes correctly implement the file name handling requirements for Chromium browsers by passing both the attachment name and URL to the download service.

Add JSDoc documentation for better maintainability.

Consider adding JSDoc documentation to describe the parameters and purpose of the method.

+    /**
+     * Downloads an attachment using both the attachment name and URL.
+     * @param downloadName The name to be used for the downloaded file
+     * @param downloadUrl The URL from which to download the file
+     */
     downloadAttachment(downloadName: string, downloadUrl: string): void {

Verify template updates.

Ensure that all template files calling this method have been updated to provide both parameters.

✅ Verification successful

Template update is correctly implemented

The template file lecture-attachments.component.html correctly calls downloadAttachment with both required parameters: attachment.name and attachment.link, with null coalescing operators for safety.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all template files that might call downloadAttachment
# and verify they provide both parameters

# Search for downloadAttachment calls in template files
rg -t html 'downloadAttachment\s*\(' src/main/webapp/app/lecture/

Length of output: 286

src/main/webapp/app/lecture/lecture-attachments.component.html (1)

64-64: LGTM! The change properly addresses the file naming issues.

The updated click handler now correctly passes both the attachment name and link, which will help fix the file naming issues in Chromium browsers. The null coalescing operator is properly used for both parameters to prevent undefined/null errors.

Let's verify that the component's TypeScript file has been updated to match this new method signature:

✅ Verification successful

The component's TypeScript implementation correctly matches the template usage

The TypeScript implementation of downloadAttachment already accepts both downloadName and downloadUrl parameters, perfectly matching the template's usage. The method signature and implementation are properly aligned with the changes made to fix the file naming issues in Chromium browsers.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the downloadAttachment method signature in the component file
# Expected: Method should accept both name and link parameters

ast-grep --pattern 'downloadAttachment(name: string, link: string)' src/main/webapp/app/lecture/lecture-attachments.component.ts

Length of output: 130


Script:

#!/bin/bash
# Let's try a broader pattern to find the downloadAttachment method implementation
ast-grep --pattern 'downloadAttachment($_) {
  $$$
}'

Length of output: 55


Script:

#!/bin/bash
# As a backup, let's also search using ripgrep
rg "downloadAttachment" -A 5 src/main/webapp/app/lecture/lecture-attachments.component.ts

Length of output: 426


Script:

#!/bin/bash
# Let's also check if the file exists
fd "lecture-attachments.component.ts" src/

Length of output: 103

src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java (4)

4-5: LGTM: Appropriate use of FilenameUtils

Using Apache Commons IO's FilenameUtils is a good choice for handling file names and extensions in a platform-independent way.


414-415: LGTM: Improved parameter naming and documentation

The parameter rename from filename to attachmentName better reflects its purpose and usage.

Also applies to: 418-421


490-490: LGTM: Proper file extension handling

The change correctly combines the attachment name with its extension, ensuring proper file downloads across different browsers.


Line range hint 414-434: Verify similar download endpoints

Let's check if other file download endpoints in the codebase might need similar fixes for consistent behavior across browsers.

Also applies to: 490-490

✅ Verification successful

Let me gather more specific information about file download endpoints.


Based on the comprehensive search results, I can now provide the final response:

All file download endpoints follow consistent patterns and security checks

The codebase shows consistent implementation across file download endpoints:

  • All endpoints use buildFileResponse with proper path handling
  • Each endpoint implements appropriate authorization checks
  • File name handling is consistent across different file types (attachments, submissions, etc.)
  • The reviewed endpoint follows the same pattern as other similar endpoints in the codebase
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find similar patterns in other endpoints that might need the same fix

# Look for ResponseEntity<byte[]> endpoints that might handle file downloads
rg -l "ResponseEntity<byte\[\]>" --type java

# Look for specific file handling patterns that might need fixing
ast-grep --pattern 'ResponseEntity<byte[]> $method($_) {
  $$$
  return buildFileResponse($_, $_, $_);
}'

Length of output: 1481


Script:

#!/bin/bash
# Search for file download related endpoints in FileResource.java
rg -A 5 "@GetMapping.*files.*" src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java

# Search for buildFileResponse usage to find all file download endpoints
rg -B 5 "buildFileResponse" src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java

# Look for similar patterns in other resources
rg -A 5 "@GetMapping.*download.*" --type java

Length of output: 16564

Copy link

⚠️ Unable to deploy to test servers ⚠️

The docker build needs to run through before deploying.

@github-actions github-actions bot added the deployment-error Added by deployment workflows if an error occured label Nov 28, 2024
@HawKhiem HawKhiem added deploy:artemis-test3 and removed deployment-error Added by deployment workflows if an error occured labels Nov 28, 2024
Copy link

⚠️ Unable to deploy to test servers ⚠️

The docker build needs to run through before deploying.

@github-actions github-actions bot added the deployment-error Added by deployment workflows if an error occured label Nov 28, 2024
@HawKhiem HawKhiem added deploy:artemis-test3 and removed deployment-error Added by deployment workflows if an error occured labels Nov 28, 2024
Copy link

⚠️ Unable to deploy to test servers ⚠️

The docker build needs to run through before deploying.

@github-actions github-actions bot added the deployment-error Added by deployment workflows if an error occured label Nov 28, 2024
@HawKhiem HawKhiem removed the deployment-error Added by deployment workflows if an error occured label Nov 28, 2024
@github-actions github-actions bot added the tests label Nov 29, 2024
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: 1

🧹 Outside diff range and nitpick comments (3)
src/test/javascript/spec/helpers/mocks/service/mock-file.service.ts (1)

Line range hint 1-24: Consider standardizing mock implementations across the service.

The service uses two different approaches for mocking:

  1. RxJS of() operator (e.g., downloadMergedFile, getTemplateFile)
  2. Manual subscription objects (e.g., downloadFile, downloadFileByAttachmentName)

This inconsistency could make the mock service harder to maintain and understand.

Consider standardizing all methods to use RxJS operators for consistency:

import { Observable, of, throwError } from 'rxjs';

export class MockFileService {
-    downloadMergedFile = () => {
-        return of({ body: null });
-    };
+    downloadMergedFile = (): Observable<{ body: null }> => {
+        return of({ body: null });
+    };

-    downloadFile = () => {
-        return { subscribe: (fn: (value: any) => void) => fn({ body: new Window() }) };
-    };
+    downloadFile = (): Observable<{ body: Window }> => {
+        return of({ body: new Window() });
+    };

-    downloadFileByAttachmentName = () => {
-        return { subscribe: (fn: (value: any) => void) => fn({ body: new Window() }) };
-    };
+    downloadFileByAttachmentName = (
+        url: string,
+        attachmentName: string
+    ): Observable<{ body: Window }> => {
+        return url && attachmentName
+            ? of({ body: new Window() })
+            : throwError(() => new Error('Invalid URL or attachment name'));
+    };

     getTemplateFile = () => {
         return of();
     };

     replaceLectureAttachmentPrefixAndUnderscores = (link: string) => link;
     replaceAttachmentPrefixAndUnderscores = (link: string) => link;
}

This approach:

  • Provides consistent usage of RxJS throughout the service
  • Improves type safety with explicit return types
  • Makes error handling more idiomatic using throwError
  • Aligns better with Angular's reactive programming patterns
src/test/javascript/spec/component/overview/course-lectures/course-lecture-details.component.spec.ts (1)

288-289: Consider adding test cases for error scenarios

While the current expectation correctly verifies the happy path using the recommended toHaveBeenCalledWith matcher, consider adding test cases for:

  1. Error handling when the download fails
  2. State management verification (isDownloadingLink) during the download process
  3. Edge cases with missing or invalid attachment names

Example test case to add:

it('should handle download errors gracefully', fakeAsync(() => {
    const fileService = TestBed.inject(FileService);
    const downloadFileSpy = jest.spyOn(fileService, 'downloadFileByAttachmentName')
        .mockRejectedValue(new Error('Download failed'));
    const attachment = getAttachmentUnit(lecture, 1, dayjs()).attachment!;

    courseLecturesDetailsComponent.downloadAttachment(attachment.link, attachment.name);
    tick();

    expect(downloadFileSpy).toHaveBeenCalledOnce();
    expect(courseLecturesDetailsComponent.isDownloadingLink).toBeFalsy();
}));
src/test/javascript/spec/component/lecture/lecture-attachments.component.spec.ts (1)

364-364: Enhance test coverage for downloadAttachment.

While the test verifies the basic functionality, consider adding these test cases to ensure robust error handling and browser compatibility:

  1. Test error scenarios when download fails
  2. Verify filename handling across different browsers (especially Chromium vs Firefox)
  3. Test with various file types and extensions
  4. Verify the correct content-disposition header is set

Example test cases to add:

it('should handle download errors', fakeAsync(() => {
    fixture.detectChanges();
    const fileService = TestBed.inject(FileService);
    jest.spyOn(fileService, 'downloadFileByAttachmentName').mockReturnValue(throwError(() => new Error('Download failed')));
    
    comp.downloadAttachment('https://example.com/file', 'test.pdf');
    tick();
    
    expect(comp.isDownloadingAttachmentLink).toBeUndefined();
    // Verify error handling
}));

it('should set correct filename for different browsers', fakeAsync(() => {
    fixture.detectChanges();
    const fileService = TestBed.inject(FileService);
    const downloadSpy = jest.spyOn(fileService, 'downloadFileByAttachmentName');
    
    comp.downloadAttachment('https://example.com/file', 'test.pdf');
    tick();
    
    expect(downloadSpy).toHaveBeenCalledWith('https://example.com/file', 'test.pdf');
}));
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d3b0932 and d355802.

📒 Files selected for processing (4)
  • src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts (1 hunks)
  • src/test/javascript/spec/component/lecture/lecture-attachments.component.spec.ts (1 hunks)
  • src/test/javascript/spec/component/overview/course-lectures/course-lecture-details.component.spec.ts (1 hunks)
  • src/test/javascript/spec/helpers/mocks/service/mock-file.service.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts
🧰 Additional context used
📓 Path-based instructions (3)
src/test/javascript/spec/component/lecture/lecture-attachments.component.spec.ts (1)

Pattern src/test/javascript/spec/**/*.ts: jest: true; mock: NgMocks; bad_practices: avoid_full_module_import; perf_improvements: mock_irrelevant_deps; service_testing: mock_http_for_logic; no_schema: avoid_NO_ERRORS_SCHEMA; expectation_specificity: true; solutions: {boolean: toBeTrue/False, reference: toBe, existence: toBeNull/NotNull, undefined: toBeUndefined, class_obj: toContainEntries/toEqual, spy_calls: {not_called: not.toHaveBeenCalled, once: toHaveBeenCalledOnce, with_value: toHaveBeenCalledWith|toHaveBeenCalledExactlyOnceWith}}

src/test/javascript/spec/component/overview/course-lectures/course-lecture-details.component.spec.ts (1)

Pattern src/test/javascript/spec/**/*.ts: jest: true; mock: NgMocks; bad_practices: avoid_full_module_import; perf_improvements: mock_irrelevant_deps; service_testing: mock_http_for_logic; no_schema: avoid_NO_ERRORS_SCHEMA; expectation_specificity: true; solutions: {boolean: toBeTrue/False, reference: toBe, existence: toBeNull/NotNull, undefined: toBeUndefined, class_obj: toContainEntries/toEqual, spy_calls: {not_called: not.toHaveBeenCalled, once: toHaveBeenCalledOnce, with_value: toHaveBeenCalledWith|toHaveBeenCalledExactlyOnceWith}}

src/test/javascript/spec/helpers/mocks/service/mock-file.service.ts (1)

Pattern src/test/javascript/spec/**/*.ts: jest: true; mock: NgMocks; bad_practices: avoid_full_module_import; perf_improvements: mock_irrelevant_deps; service_testing: mock_http_for_logic; no_schema: avoid_NO_ERRORS_SCHEMA; expectation_specificity: true; solutions: {boolean: toBeTrue/False, reference: toBe, existence: toBeNull/NotNull, undefined: toBeUndefined, class_obj: toContainEntries/toEqual, spy_calls: {not_called: not.toHaveBeenCalled, once: toHaveBeenCalledOnce, with_value: toHaveBeenCalledWith|toHaveBeenCalledExactlyOnceWith}}

🔇 Additional comments (2)
src/test/javascript/spec/component/overview/course-lectures/course-lecture-details.component.spec.ts (2)

282-282: LGTM: Spy correctly set up for the new download method

The spy is properly configured to track calls to the new downloadFileByAttachmentName method, which aligns with the PR's objective to improve file naming during downloads.


285-285: LGTM: Method call correctly updated with new signature

The test properly validates the new behavior by passing both the attachment link and name to the downloadAttachment method.

@github-actions github-actions bot added the lecture Pull requests that affect the corresponding module label Dec 1, 2024
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: 6

🧹 Outside diff range and nitpick comments (6)
src/test/javascript/spec/component/lecture-unit/attachment-unit/attachment-unit.component.spec.ts (3)

Line range hint 85-92: Enhance test coverage for downloadFileByAttachmentName parameters

While the test verifies the method call and completion emission, it should also verify that the correct attachment name and URL are passed to the new method.

Consider updating the test like this:

 const downloadFileSpy = jest.spyOn(fileService, 'downloadFileByAttachmentName');
 const onCompletionEmitSpy = jest.spyOn(component.onCompletion, 'emit');

 fixture.detectChanges();
 component.handleDownload();

 expect(downloadFileSpy).toHaveBeenCalledOnce();
+expect(downloadFileSpy).toHaveBeenCalledExactlyOnceWith('test', '/path/to/file/test.pdf');
 expect(onCompletionEmitSpy).toHaveBeenCalledOnce();

Line range hint 116-124: Verify parameters in click handler test

Similar to the previous test, this one should verify that clicking the button triggers the download with correct parameters.

Update the test to verify parameter passing:

 const downloadFileSpy = jest.spyOn(fileService, 'downloadFileByAttachmentName');

 fixture.detectChanges();

 const viewIsolatedButton = fixture.debugElement.query(By.css('#view-isolated-button'));
 viewIsolatedButton.nativeElement.click();

 fixture.detectChanges();
 expect(downloadFileSpy).toHaveBeenCalledOnce();
+expect(downloadFileSpy).toHaveBeenCalledExactlyOnceWith('test', '/path/to/file/test.pdf');

Line range hint 94-114: Add test cases for different file extensions

While the current test suite covers icon selection for different file types, consider adding test cases that verify the download behavior with different file extensions to ensure the PR's objective of fixing file naming issues is thoroughly tested.

Consider adding a test like this:

it.each([
    ['pdf', 'document.pdf'],
    ['doc', 'document.doc'],
    ['txt', 'document.txt']
])('should download file with correct extension: %s', (extension: string, filename: string) => {
    const downloadFileSpy = jest.spyOn(fileService, 'downloadFileByAttachmentName');
    component.lectureUnit().attachment!.name = filename;
    component.lectureUnit().attachment!.link = `/path/to/file/${filename}`;
    
    fixture.detectChanges();
    component.handleDownload();
    
    expect(downloadFileSpy).toHaveBeenCalledExactlyOnceWith(filename, `/path/to/file/${filename}`);
});
src/test/java/de/tum/cit/aet/artemis/lecture/AttachmentResourceIntegrationTest.java (1)

73-77: Improve test readability and coverage.

While the changes correctly verify the file name fix:

  1. Consider extracting the file name construction logic to a helper method for better readability:
-        String linkWithCorrectFileName = actualLink.substring(0, actualLink.lastIndexOf('/') + 1) + attachment.getName() + "." + getExtension(actualAttachment.getLink());
+        String linkWithCorrectFileName = constructAttachmentLink(actualLink, attachment.getName());
  1. Add test cases using @ParameterizedTest to verify handling of different file extensions:
@ParameterizedTest
@ValueSource(strings = {"test.pdf", "test.txt", "test.doc"})
void createAttachmentWithDifferentExtensions(String fileName) {
    // Test implementation
}
src/test/javascript/spec/component/shared/http/file.service.spec.ts (2)

82-117: Consider adding error case tests for getTemplateFile.

While the happy path cases are well covered, consider adding tests for:

  • Invalid programming language
  • Invalid project type
  • Server error responses

139-176: Add boundary tests for getAeolusTemplateFile.

Consider adding tests for:

  • Invalid combinations of parameters
  • Edge cases where some parameters are undefined/null
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a241971 and c3f3601.

📒 Files selected for processing (3)
  • src/test/java/de/tum/cit/aet/artemis/lecture/AttachmentResourceIntegrationTest.java (2 hunks)
  • src/test/javascript/spec/component/lecture-unit/attachment-unit/attachment-unit.component.spec.ts (2 hunks)
  • src/test/javascript/spec/component/shared/http/file.service.spec.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
src/test/javascript/spec/component/lecture-unit/attachment-unit/attachment-unit.component.spec.ts (1)

Pattern src/test/javascript/spec/**/*.ts: jest: true; mock: NgMocks; bad_practices: avoid_full_module_import; perf_improvements: mock_irrelevant_deps; service_testing: mock_http_for_logic; no_schema: avoid_NO_ERRORS_SCHEMA; expectation_specificity: true; solutions: {boolean: toBeTrue/False, reference: toBe, existence: toBeNull/NotNull, undefined: toBeUndefined, class_obj: toContainEntries/toEqual, spy_calls: {not_called: not.toHaveBeenCalled, once: toHaveBeenCalledOnce, with_value: toHaveBeenCalledWith|toHaveBeenCalledExactlyOnceWith}}

src/test/java/de/tum/cit/aet/artemis/lecture/AttachmentResourceIntegrationTest.java (1)

Pattern src/test/java/**/*.java: test_naming: descriptive; test_size: small_specific; fixed_data: true; junit5_features: true; assert_use: assertThat; assert_specificity: true; archunit_use: enforce_package_rules; db_query_count_tests: track_performance; util_service_factory_pattern: true; avoid_db_access: true; mock_strategy: static_mocks; context_restart_minimize: true

src/test/javascript/spec/component/shared/http/file.service.spec.ts (1)

Pattern src/test/javascript/spec/**/*.ts: jest: true; mock: NgMocks; bad_practices: avoid_full_module_import; perf_improvements: mock_irrelevant_deps; service_testing: mock_http_for_logic; no_schema: avoid_NO_ERRORS_SCHEMA; expectation_specificity: true; solutions: {boolean: toBeTrue/False, reference: toBe, existence: toBeNull/NotNull, undefined: toBeUndefined, class_obj: toContainEntries/toEqual, spy_calls: {not_called: not.toHaveBeenCalled, once: toHaveBeenCalledOnce, with_value: toHaveBeenCalledWith|toHaveBeenCalledExactlyOnceWith}}

Copy link
Contributor

@coolchock coolchock left a comment

Choose a reason for hiding this comment

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

re-approve after fixing tests

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.

reapprove

Copy link

@kevinfischer4 kevinfischer4 left a comment

Choose a reason for hiding this comment

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

Reapprove

Copy link
Contributor

@florian-glombik florian-glombik left a comment

Choose a reason for hiding this comment

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

Re-approve after merge and adding more test cases

Copy link

@Malekos74 Malekos74 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 Server 02:

Works as intended

Copy link

@sachmii sachmii left a comment

Choose a reason for hiding this comment

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

re-approve

Copy link

@sawys777 sawys777 left a comment

Choose a reason for hiding this comment

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

Reapprove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Pull requests that update TypeScript code. (Added Automatically!) core Pull requests that affect the corresponding module lecture Pull requests that affect the corresponding module ready for review ready to merge server Pull requests that update Java code. (Added Automatically!) tests
Projects
Status: Ready For Review
Development

Successfully merging this pull request may close these issues.

9 participants