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

Allow downloading multiple images into one directory #7030

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

major-mayer
Copy link

@major-mayer major-mayer commented Sep 26, 2024

First time contributor checklist:

Contributor checklist:

  • My contribution is not related to translations.
  • My commits are in nice logical chunks with good commit messages
  • My changes are rebased on the latest main branch
  • A npm run ready run passes successfully (more about tests here)
  • My changes are ready to be shipped to users

Description

Describe briefly what your pull request changes. Focus on the value provided to users.
Today I noticed, that right now it's not possible to download multiple images at once, which makes saving large galleries to the disk very time-consuming.
I was very surprised why this is the case, given that downloading a single image works without problems.

So I decided that I could maybe take a look into the code and try to fix things on my own, because the feature requests regarding this issue have not seen any attention for 3 years.
My experience with React is basically non-existent, so it was a lot of copy-pasting and adjusting the existing approach to download a single attachment.

It took a while to understand the data-flow between the different files, functions and actions, but I got it to work 🥳
I would like to get some feedback on my approach, then I can clean it up, maybe add some tests (although I would need some guidance on how to write these, maybe a similar example) and rebase it.

Does it address any outstanding issues in this project?

Please write a summary of your test approach:

  • What kind of manual testing did you do?
    • I only used the staging environment so far, and sent a few pictures and other attachments to myself. I checked if I could download them without any problems.
  • Did you write any new tests?
    • At least not yet. I checked briefly if I could find any test regarding the download of single attachments, but couldn't find one. Before I invest more time into this, I would like to get some feedback, if this has the chance to be accepted.
  • What operating systems did you test with? (please use specific versions: http://whatsmyos.com/)
    • Manjaro KDE Wayland
    • Windows 11 Home 23H2 22631.4037
  • What other devices did you test with? (other Desktop devices, Android, Android Simulator, iOS, iOS Simulator)
    • None, but I could test it on my laptop as well (right now just desktop PC).

Edit: In addition to my regular Manjaro Linux system, I successfully tested the changes with Windows 11 as well.

Copy link
Author

@major-mayer major-mayer left a comment

Choose a reason for hiding this comment

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

In general, I tried to re-use as much existing code as possible and not mutate existing functions.
That's why I added an additional saveAttachments() function that takes an array of attachments, opens a directory picker dialog once and uses this base directory for all files.

One could probably combine the functionality to save a single attachment (with a user-defined name) and multiple attachments into one common function, but I didn't want to risk breaking too much existing functionality.
If wanted, I can use saveAttachments() for single attachments as well, skip the showSaveMultiDialog() call if the array's length is one and set dirPath to undefined.

Comment on lines +256 to +265
// TODO This will only get executed on the first time the component is rendered
// How to check if attachment[x].pending changes?
const hasPendingAttachments = (): boolean => {
// Ensure that no attachment is still pending
if (attachments && attachments.length) {
return attachments.every(attachment => !attachment.pending);
}

return false;
}
Copy link
Author

Choose a reason for hiding this comment

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

I could need some help here.
I added this function to check if any of the attachments is in pending state, and not just the first one, as it was before (maybe that's not even necessary?).
Right now, I think this function is only run once, when the component is rendered, which mean that if the pending state changes later, the result will not change and thus the download button will not be rendered.

How can I make sure that this function gets re-run whenever the pending attribute of an attachment changes?
I think using useCallback() or useMemo() is not the right approach here, because it only notices when the reference to attachments changes, right?

Comment on lines +3909 to +3910
// TODO shouldn't we throw some kind of error here? Original code also doesn't do anything, but...
if (fullPath == null) return;
Copy link
Author

Choose a reason for hiding this comment

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

In saveAttachments() (so the function to download a single attachment), I guess this was a check to skip showing the toast, when the user cancels the download/ file picker dialog.
Since I'm doing this check a few lines above, we probably should either remove this check here completely or throw an error, because something went wrong in the Attachments.save() method?

@@ -3008,7 +3008,7 @@ ipc.handle('show-save-dialog', async (_event, { defaultPath }) => {
const { canceled, filePath: selectedFilePath } = await dialog.showSaveDialog(
mainWindow,
{
defaultPath,
defaultPath: join(app.getPath('downloads'), defaultPath),
Copy link
Author

Choose a reason for hiding this comment

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

Here I changed the original behavior of the save dialog for a single attachment, because I think it makes sense to use the users download directory as a base directory.
Otherwise, when I try to save a file, it uses ./ as the default directory and I have to navigate to downloads manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants