Skip to content

feature(web): Added option to download file and copy stream url#209

Merged
PartyDonut merged 2 commits intodevelopfrom
feature/Download-on-web
Feb 1, 2025
Merged

feature(web): Added option to download file and copy stream url#209
PartyDonut merged 2 commits intodevelopfrom
feature/Download-on-web

Conversation

@PartyDonut
Copy link
Copy Markdown
Collaborator

Pull Request Description

Adds two buttons on web only

  • Download file
  • Copy stream-link

Issue Being Fixed

Resolves #193

@PartyDonut PartyDonut added the feature New feature or request label Feb 1, 2025
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Feb 1, 2025

Reviewer's Guide by Sourcery

This pull request adds the ability to download files and copy stream URLs on the web platform. It introduces two new buttons within the item action menu when the app is running on the web. These buttons allow users to download the file directly or copy the stream URL to their clipboard.

Sequence diagram for file download and stream URL copy process

sequenceDiagram
    actor User
    participant UI
    participant AccountModel
    participant UserProvider
    participant Browser

    User->>UI: Clicks download file button
    UI->>AccountModel: Check canDownload
    AccountModel-->>UI: Return download permission
    UI->>UserProvider: createDownloadUrl(item)
    UserProvider-->>UI: Return download URL
    UI->>Browser: Trigger file download

    User->>UI: Clicks copy stream URL button
    UI->>UserProvider: createDownloadUrl(item)
    UserProvider-->>UI: Return stream URL
    UI->>Browser: Copy URL to clipboard
    UI->>User: Show success notification
Loading

Class diagram showing modified components

classDiagram
    class AccountModel {
      +String server
      +bool canDownload
      +sameIdentity(AccountModel other) bool
    }

    class User {
      +createDownloadUrl(ItemBaseModel item) String?
    }

    class ItemBaseModelExtensions {
      +buildItemActions()
    }

    class FileDownloader {
      +downloadFile(String url) Future~void~
    }

    ItemBaseModelExtensions ..> User: uses
    ItemBaseModelExtensions ..> FileDownloader: uses
    ItemBaseModelExtensions ..> AccountModel: uses
Loading

File-Level Changes

Change Details Files
Added download and copy stream URL buttons for web platform.
  • Added conditional rendering for download and copy stream URL buttons, only showing them on web.
  • Added a download button that triggers a file download using the provided URL.
  • Added a copy stream URL button that copies the download URL to the clipboard.
lib/util/item_base_model/item_base_model_extensions.dart
Modified AccountModel to remove web platform check for download capability.
  • Removed the kIsWeb check from the canDownload getter, allowing downloads on web.
lib/models/account_model.dart
Added a method to create a download URL.
  • Added createDownloadUrl method to the User provider to generate a download URL for a given item.
lib/providers/user_provider.dart
Added a file downloader utility.
  • Created a file_downloader.dart file with a downloadFile function that uses the universal_html package to download files on the web.
lib/util/file_downloader.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @PartyDonut - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Avoid hardcoding API keys directly in the URL. (link)

Overall Comments:

  • Consider implementing proper error handling with user feedback in downloadFile() instead of just printing to console. Also, the download filename should be more user-friendly than the raw URL.
  • Exposing API keys in copyable stream URLs is a security risk. Consider implementing temporary download tokens or another secure method for sharing URLs.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🔴 Security: 1 blocking issue, 1 other issue
  • 🟢 Review instructions: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Future<void> downloadFile(String url) async {
try {
html.AnchorElement anchorElement = html.AnchorElement(href: url);
anchorElement.download = url;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: The download attribute should be a filename, not the full URL

Consider extracting a meaningful filename from the URL or passing it as a parameter


void deleteAllFilters() => state = state?.copyWith(savedFilters: []);

String? createDownloadUrl(ItemBaseModel item) =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 issue (security): URL parameters should be properly encoded

Use Uri.encodeComponent() for the api_key and other parameters to ensure proper URL encoding

action: () => showSyncItemDetails(context, syncedItem, ref),
label: Text(context.localized.syncDetails),
icon: const Icon(IconsaxOutline.document_download),
action: () => downloadFile(ref.read(userProvider.notifier).createDownloadUrl(this) ?? "Null"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: Handle null download URL case more gracefully

Instead of passing "Null" as the URL, consider showing an error message to the user

void deleteAllFilters() => state = state?.copyWith(savedFilters: []);

String? createDownloadUrl(ItemBaseModel item) =>
"${state?.server}/Items/${item.id}/Download?api_key=${state?.credentials.token}";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 issue (security): Avoid hardcoding API keys directly in the URL.

Exposing API keys like this can lead to unauthorized access. Consider using a more secure approach, such as storing the API key in secure storage and adding it to the request headers.

@PartyDonut PartyDonut merged commit bd8faf2 into develop Feb 1, 2025
1 check passed
@PartyDonut PartyDonut deleted the feature/Download-on-web branch February 1, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant