Skip to content

Issues #242146 Feat: Download provision for uploaded file#351

Open
vaibhavsTekdi wants to merge 1 commit intotekdi:aspire-leadersfrom
vaibhavsTekdi:vs-file-field
Open

Issues #242146 Feat: Download provision for uploaded file#351
vaibhavsTekdi wants to merge 1 commit intotekdi:aspire-leadersfrom
vaibhavsTekdi:vs-file-field

Conversation

@vaibhavsTekdi
Copy link
Collaborator

@vaibhavsTekdi vaibhavsTekdi commented Jun 25, 2025

Summary by CodeRabbit

  • New Features

    • Added the ability for users to securely download files associated with specific fields, with access restricted to file owners and admins.
    • Admin users can download any file for a given field, while non-admins can only access their own files.
  • Bug Fixes

    • Improved role detection and authorization checks for file-related operations.
  • Other Improvements

    • Enhanced error handling and response headers for file downloads.
    • Improved type safety and data handling for field and field value retrieval.

@coderabbitai
Copy link

coderabbitai bot commented Jun 25, 2025

Walkthrough

A secure file download feature was introduced, allowing users to download files associated with fields, with access restricted based on user roles (admin or owner). This required new endpoint/controller logic, service methods for authorization and file retrieval, interface extensions, and storage provider support for downloading files from both local and S3 backends.

Changes

File(s) Change Summary
src/common/utils/api-id.config.ts Added FIELDVALUES_DOWNLOAD constant to the APIID export object.
src/fields/fields.controller.ts Added a new authenticated GET endpoint for downloading files by field ID, with error handling and response headers.
src/fields/fields.service.ts Updated getField and getFieldValue to return interface types with null handling; added getFieldValuesByFieldId method.
src/storage/file-upload.service.ts Added user role resolution, unified admin checks, and implemented a new downloadFile method with role-based authorization.
src/storage/interfaces/field-operations.interface.ts Added getFieldValuesByFieldId method to the IFieldOperations interface.
src/storage/interfaces/storage.provider.ts Extended StorageProvider interface with a download method for file downloads.
src/storage/providers/local-storage.provider.ts Implemented the download method to retrieve files from local storage with error handling and metadata extraction.
src/storage/providers/s3-storage.provider.ts Implemented the download method for S3, including metadata retrieval, error handling, and stream-to-buffer conversion.
src/storage/storage.module.ts Imported UserRoleMapping and Role entities for use with TypeORM in the storage module.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant FieldsController
    participant FileUploadService
    participant FieldsService
    participant StorageProvider
    participant UserRoleRepo

    Client->>FieldsController: GET /download-file/:fieldId (with JWT)
    FieldsController->>FileUploadService: downloadFile(fieldId, userId, userRole)
    FileUploadService->>FieldsService: getField(fieldId)
    FieldsService-->>FileUploadService: Field or null
    alt userRole not provided
        FileUploadService->>UserRoleRepo: getUserRole(userId)
        UserRoleRepo-->>FileUploadService: userRole or null
    end
    alt user is admin
        FileUploadService->>FieldsService: getFieldValuesByFieldId(fieldId)
        FieldsService-->>FileUploadService: [FieldValue]
        FileUploadService->>StorageProvider: download(filePath)
    else user is owner
        FileUploadService->>FieldsService: getFieldValue(fieldId, userId)
        FieldsService-->>FileUploadService: FieldValue or null
        FileUploadService->>StorageProvider: download(filePath)
    end
    StorageProvider-->>FileUploadService: {buffer, contentType, originalName, size}
    FileUploadService-->>FieldsController: {buffer, contentType, originalName, size}
    FieldsController-->>Client: File download response with headers
Loading

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.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-06-25T12_41_36_743Z-debug-0.log

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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 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.

@sonarqubecloud
Copy link

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

🧹 Nitpick comments (4)
src/storage/providers/local-storage.provider.ts (3)

117-119: Improve consistency by using async file operations.

Mixed synchronous and asynchronous file operations can impact performance. Consider using fs.promises.access() instead of fs.existsSync() for consistency.

-      if (!fs.existsSync(filePath)) {
-        throw new Error('File not found in local storage');
-      }
+      try {
+        await fs.promises.access(filePath, fs.constants.F_OK);
+      } catch {
+        throw new Error('File not found in local storage');
+      }

132-146: Consider using a more robust content type detection.

The hardcoded content type mapping is functional but limited. Consider using a library like mime-types for more comprehensive and maintainable content type detection.

+import { lookup as getMimeType } from 'mime-types';
+
-      // Simple content type mapping
-      const contentTypeMap: { [key: string]: string } = {
-        '.pdf': 'application/pdf',
-        '.jpg': 'image/jpeg',
-        '.jpeg': 'image/jpeg',
-        '.png': 'image/png',
-        '.gif': 'image/gif',
-        '.txt': 'text/plain',
-        '.doc': 'application/msword',
-        '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
-        '.xls': 'application/vnd.ms-excel',
-        '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
-        '.csv': 'text/csv',
-        '.zip': 'application/zip',
-        '.rar': 'application/x-rar-compressed'
-      };
-      
-      if (contentTypeMap[ext]) {
-        contentType = contentTypeMap[ext];
-      }
+      contentType = getMimeType(filePath) || 'application/octet-stream';

152-153: Consider preserving actual original filename.

Extracting the filename from the file path may not preserve the original filename uploaded by the user. Consider storing and retrieving the original filename from file metadata or database.

The current implementation uses path.basename(filePath) which returns the generated filename. If you need to preserve the original filename uploaded by users, consider storing it in the database and retrieving it along with file metadata.

src/storage/file-upload.service.ts (1)

589-589: Fix grammatical errors in error messages

The error messages have grammatical errors - "does not found" should be "not found".

Apply these fixes:

-          throw new FileValidationException('Field values does not found for this field');
+          throw new FileValidationException('Field values not found for this field');
-          throw new FileValidationException('Field values does not found for this field and user');
+          throw new FileValidationException('Field values not found for this field and user');

Also applies to: 597-597

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 7e69cf9 and fb01a4f.

📒 Files selected for processing (9)
  • src/common/utils/api-id.config.ts (1 hunks)
  • src/fields/fields.controller.ts (2 hunks)
  • src/fields/fields.service.ts (2 hunks)
  • src/storage/file-upload.service.ts (5 hunks)
  • src/storage/interfaces/field-operations.interface.ts (1 hunks)
  • src/storage/interfaces/storage.provider.ts (2 hunks)
  • src/storage/providers/local-storage.provider.ts (1 hunks)
  • src/storage/providers/s3-storage.provider.ts (2 hunks)
  • src/storage/storage.module.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.ts`: "Review the JavaScript code for conformity with the Google JavaScript...

**/*.ts: "Review the JavaScript code for conformity with the Google JavaScript style guide, highlighting any deviations. Ensure that:

  • The code adheres to best practices associated with nodejs.
  • The code adheres to best practices associated with nestjs framework.
  • The code adheres to best practices recommended for performance.
  • The code adheres to similar naming conventions for controllers, models, services, methods, variables."

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • src/common/utils/api-id.config.ts
  • src/storage/storage.module.ts
  • src/storage/interfaces/field-operations.interface.ts
  • src/storage/interfaces/storage.provider.ts
  • src/storage/providers/s3-storage.provider.ts
  • src/fields/fields.controller.ts
  • src/fields/fields.service.ts
  • src/storage/providers/local-storage.provider.ts
  • src/storage/file-upload.service.ts
🧬 Code Graph Analysis (3)
src/fields/fields.controller.ts (3)
src/common/utils/api-id.config.ts (1)
  • APIID (1-68)
src/storage/exceptions/file-validation.exception.ts (1)
  • FileValidationException (14-30)
src/common/utils/response.messages.ts (1)
  • API_RESPONSES (1-225)
src/fields/fields.service.ts (1)
src/storage/interfaces/field-operations.interface.ts (2)
  • Field (39-50)
  • FieldValue (16-33)
src/storage/file-upload.service.ts (2)
src/storage/interfaces/field-operations.interface.ts (1)
  • IFieldOperations (52-94)
src/storage/exceptions/file-validation.exception.ts (1)
  • FileValidationException (14-30)
🔇 Additional comments (15)
src/common/utils/api-id.config.ts (1)

40-40: LGTM! Consistent API identifier addition.

The new FIELDVALUES_DOWNLOAD API identifier follows the established naming convention and value pattern correctly.

src/storage/storage.module.ts (2)

9-11: LGTM! Proper RBAC entity imports.

The TypeOrmModule and RBAC entity imports are correctly added to support role-based authorization for file operations.


16-17: LGTM! Correct TypeOrmModule configuration.

The TypeOrmModule.forFeature configuration properly enables dependency injection of UserRoleMapping and Role repositories for the storage services.

src/storage/interfaces/storage.provider.ts (2)

7-7: LGTM! Documentation properly updated.

The interface documentation correctly includes file download operations in the contract description.


29-34: LGTM! Well-designed download method interface.

The download method signature is properly typed and documented, providing all necessary file metadata for download operations.

src/storage/interfaces/field-operations.interface.ts (1)

80-85: LGTM! Well-designed admin access method.

The getFieldValuesByFieldId method signature is properly typed and documented for admin-level access to field values by field ID.

src/storage/providers/local-storage.provider.ts (1)

114-167: LGTM! Solid download implementation with good error handling.

The download method correctly implements the interface contract with comprehensive error handling and proper file operations.

src/fields/fields.controller.ts (1)

832-857: LGTM! Error handling is consistent

The error handling follows the established pattern in this controller, properly distinguishing between FileValidationException and other errors.

src/storage/providers/s3-storage.provider.ts (1)

318-372: LGTM! Well-implemented download method

The download method is properly implemented with:

  • Good error handling for missing files
  • Correct stream-to-buffer conversion using async iteration
  • Proper metadata extraction with fallbacks
  • Appropriate content type defaulting
src/fields/fields.service.ts (3)

446-466: LGTM! Improved type safety with explicit interface mapping

The method now properly converts the ORM entity to the interface type with null handling, improving abstraction and type safety.


468-496: LGTM! Consistent interface mapping for field values

The method follows the same pattern as getField, properly converting the entity to the interface type with null safety.


498-522: LGTM! Well-implemented method for admin access patterns

The new method properly supports retrieving all field values for a given field ID, which aligns with the admin access requirements in the file download feature.

src/storage/file-upload.service.ts (3)

51-77: LGTM! Well-implemented role resolution from database

The getUserRole method properly queries the database for user roles with good error handling, returning null on any failure which allows for graceful fallback.


465-501: LGTM! Improved role checking with database lookup

The enhanced role resolution and case-insensitive admin role checking improves the robustness of the authorization logic.


577-614: LGTM! Well-designed authorization logic for file downloads

The authorization logic properly handles both admin and non-admin users:

  • Admins can download any file associated with the field
  • Non-admin users can only download their own files
  • Clear separation of access patterns based on user role

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