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

Fix an attachment duplication bug and move to top level for saving #18

Merged
merged 2 commits into from
Feb 12, 2024

Conversation

dcaliste
Copy link
Contributor

@pvuorela, I tried to fix an issue where attachments become duplicated on disk when opened from the email application. It's a bit of a corner case, but anyway :

  • receive an email with an attachment, for instance a PNG image, properly identified by its MIME type image/png, but without a proper extension (like no extension at all for instance).
  • open the email in the application and tap on the image, it is opened in the gallery as expected.
  • leave the mail, and reopen it, retap on the image, it is still opened in the gallery, but with a duplicated file. Now you have it twice in the gallery since the email application saved it again with another name.

This is due to the fact that the EmailAgent is looking for a file without the extension, don't find it and ask QMF to save it again. While QMF is adding the extension, and thus finds the file and save it again with a different name.

To fix this, instead of trying to get the filename out of QMF, I prefered to go along the way you proposed once : associate the saved filename to the mail and reuse this information to know if the file has been saved already or not. QMailMessageMetaData::customField() was a good candidate to save such data. This is the purpose of the first commit.

It's quite clean in my opinion, expect when saving the custom field. Doing so make any QMailStore client to react on message updated signal. And the attachment list is thus resetting itself. Not a big deal, except that it is happening during a JS call within a delegate and the current roles are then unavailable in the JS function since the model reset itself. That's why, I delay the store update with a QTimer::singleShot() call.

If you agree with this first commit, then it becomes possible not to save anymore in this complicated tree structure based on account ids, attachment QMF locations… One can easily save in proper XDG directories, in first levels of such directories. QMF will take care of deduplicating, and the custom field information will give the possibility to have the information back when knowing an attachment location. This is the purpose of the second commit.

QMF is not exposing which filename it
is using for a saved attachment since
this name does not depend only on the
part information.

To know if an attachment has been saved,
don't try to guess the QMF filename,
but save it as a customField.
src/emailagent.cpp Outdated Show resolved Hide resolved
@dcaliste dcaliste changed the title Fix an attachment duplication bug and move to XDG directories for saving Fix an attachment duplication bug and move to top level for saving Jan 26, 2024
@dcaliste
Copy link
Contributor Author

I've changed the last commit to save directly in ~/Downloads. I chose not to keep the mail_attachments subdirectory because this is not a plain XDG location. This subdir is not translated (not that Downloads is actually translated in Sailfish OS, but it could be) for instance. And it's currently cluterred with numerical subdirs related to accounts and the legacy naming scheme. But if you disagree, I can put it back, of course.

@pvuorela
Copy link
Contributor

One more thing I started pondering is what should happen if user downloads a file, then modifies it, and then opens it again from the email. Should it open the modified file or the content that was gotten with the email? I'm thinking latter could make more sense. Storing and comparing the assumed file size could be a cheap check, md5 checksum more complicated but robust.

Though the current way of saving to deeper directory hierarchy doesn't either ensure opening the original content so this detail shouldn't block merging. Can be left as future work, possibly now a TODO comment or something.

@dcaliste
Copy link
Contributor Author

It's interesting that you raised this concern. I actually first implemented the MR with a check on the md5 sum for the reasons you provided. I imagine a case where you receive a ticket.pdf file by email. Then, you rename it to GreatTripTicket.pdf. Also browse the Internet and download a file called ticket.pdf. If you open again the attachment of the email, you end up with displaying the download from the Internet :/ But then, I was thinking that it's a far fetch case and I over engineered the problem with an md5sum (or even size check). So I finally removed it for the current PR.

I'll add it again and check that it's working. Should be ready tomorrow or on Tuesday.

Since there is a possibility to uniquely
link attachments and file locations, use
now the top level location to save attachments.
@dcaliste
Copy link
Contributor Author

I've amended the second commit to add the md5 check, ensuring that we actually open the file attached to the email and not a file with the same name.

Copy link
Contributor

@pvuorela pvuorela left a comment

Choose a reason for hiding this comment

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

Seems to work but now testing same file name from different emails the qmf mechanism of forming name as "." looks strange. And didn't even have to create test emails, when I already had two boardingPass.pdf from trip to fosdem, latter one now as PzJ27.boardingPass.pdf on my download folder. Yugh.

Could be nicer having some more conventional file(1).pdf type of naming, or even just the random chars after the base name, but guess this is already less bad than hiding the attachments under the tree. Different names would need adjustments to messagingframework. Though unfortunate that the output format is defined by the documentation.

Could be also an option to consider should we give user the possibility to override the name when saving, but that again would require UI changes.

All in all this PR should be good enough to go in.

@pvuorela pvuorela merged commit dff7649 into sailfishos:master Feb 12, 2024
@dcaliste
Copy link
Contributor Author

Thank you for merging this. Yes, the naming convention to avoid file clash in QMF is a bit "unusual".

Though unfortunate that the output format is defined by the documentation.

Do you mean, the doc string of QMailMessagePart::writeBodyTo() mentioning "a new unique name of the format . is saved" ? Since Sailfish OS may be the only downstream user of QMF, is it in practice a real issue ? Can we just break the behaviour here and update the doc string ?

@pvuorela
Copy link
Contributor

Do you mean, the doc string of QMailMessagePart::writeBodyTo() mentioning "a new unique name of the format . is saved" ? Since Sailfish OS may be the only downstream user of QMF, is it in practice a real issue ? Can we just break the behaviour here and update the doc string ?

Yes. Indeed behavior could be changed. Maybe even leaving the exact format out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants