Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bug in TIdAttachment::FileName ?
#2
(08-27-2020, 05:06 PM)Asger-P Wrote: I have received a mail with an attachment that have a very stupid filename:
Fakturanr. 119180921 fra Lemvigh-Müller A/S.pdf

Indy only gives med the S.pdf part, the rest is discarded.

The parsed value of the TIdAttachment::FileName property comes from TIdMessageDecoderMIME::ReadHeader() (assuming a MIME-encoded email), which retrieves the filename from decoding the 'Content-Disposition' and 'Content-Type' headers (in that order).  Ultimately, the decoded data passes through TIdMessageDecoderMIME::RemoveInvalidCharsFromFilename() before being assigned to the FileName property, and that does indeed treat '/' as a path delimiter and will remove everything before and including the last '/'.  An attachment's filename is not supposed to contain path information.

(08-27-2020, 05:06 PM)Asger-P Wrote: Can I do anything to get the full filename ??

Without altering Indy's source code (I would be hesitant to do that without seeing an RFC spec that says this behavior is wrong), you could manually parse out the filename from the TIdAttachment::ContentDisposition and TIdAttachment::ContentType properties the same way Indy does, and just skip the final step that truncates the value, eg:

Code:
...
#include <IdGlobalProtocols.hpp>
#include <IdCoderHeader.hpp>

...

if( atcm /*&& attachmentsIsPdf( atcm->FileName )*/ )// checks if the extension is pdf
{
    String filename = ExtractHeaderSubItem(atcm->ContentDisposition, "filename", QuoteMIME);
    if( filename == "" )
        filename = ExtractHeaderSubItem(atcm->ContentType, "name", QuoteMIME);

    /* alternatively:
    String filename = atcm->Headers->Params["Content-Disposition"]["filename"];
    if( filename == "" )
        filename = atcm->Headers->Params["Content-Type"]["name"];
    */

    if( filename != "" )
    {
        filename = DecodeHeader(filename);
        //filename = RemoveInvalidCharsFromFilename(filename); // <-- skip this! Or mutate the filename however you want...
    }

    if( attachmentsIsPdf( filename ) ) // checks if the extension is pdf
        MailContent->Attachments.Add( atcm );
}

Reply


Messages In This Thread
Bug in TIdAttachment::FileName ? - by Asger-P - 08-27-2020, 05:06 PM
RE: Bug in TIdAttachment::FileName ? - by rlebeau - 08-28-2020, 12:47 AM
RE: Bug in TIdAttachment::FileName ? - by Asger-P - 08-28-2020, 10:44 AM
RE: Bug in TIdAttachment::FileName ? - by rlebeau - 08-28-2020, 10:27 PM
RE: Bug in TIdAttachment::FileName ? - by Asger-P - 08-29-2020, 05:46 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)