XF 2.2 Using files from abstracted path internal-data

Dannymh

Active member
I am building out reports in PDF that I then move to internal data using

\XF\Util\File::copyFileToAbstractedPath($theFile, 'internal-data://my/Reports/report' . $thread->thread_id . '.pdf');

The process starts by building a temp file and then copying it across with the above. This all works fine. However I am confused on how exactly I access this when I want to use that file as an attachment.

I have the following which works if I run ->send. However if I run ->queue it will fail

PHP:
          $tempReportFile = \XF\Util\File::copyAbstractedPathToTempFile('internal-data://my/Reports/report' . $thread->thread_id . '.pdf');
            $attachment = \Swift_Attachment::fromPath($tempReportFile);
            $attachment->setFilename('report' . $thread->thread_id . '.pdf');
            $mail->getMessageObject()->attach($attachment);

Is there a way to just use that abstracted path straight as

Code:
 $mail->getMessageObject()->attach('internal-data://my/Reports/report' . $thread->thread_id . '.pdf');

Rather than having to go through creating a temp file again just to attach it and so that I can use it in queue?
 
Nope, you basically have to do the same thing you do when writing the file in reverse.

Bear in mind that internal-data:// in theory could be pointing at anything. It could be pointing at remote object storage, it could be pointing at an NFS file share, it could even be pointing at FTP or Google Drive, and while it could also be pointing at a local file, technically that local file could be stored anywhere, rather than the default internal_data location.

The problem you're seeing is that, by default, temp files are cleaned up at the end of the request.

So if the mail is queued, the queue won't be processed until later, by which point the temp file has already been deleted.

To change this behaviour, you can pass a second argument into copyAbstractedPathToTempFile as false. This will disable the auto clean up of that temp file.

But, you will then need to handle cleaning up that temp file yourself otherwise they'll just sit in temp forever.
 
You could try
PHP:
$fileName = 'report' . $thread->thread_id . '.pdf';
$attachment = new Swift_Attachment(\XF::fs()->read('internal-data://my/Reports/' . $fileName), $fileName, 'application/pdf');
$mail->getMessageObject()->attach($attachment);

to avoid the temporary file.
 
But, you will then need to handle cleaning up that temp file yourself otherwise they'll just sit in temp forever.
Luckily, I think that this is wrong - the daily clean up cron takes care :)

PHP:
\XF\Util\File::cleanUpPersistentTempFiles();
 
Last edited:
Fun fact: this method, which apparently I forgot about, was committed exactly 5 years ago today.
While you are looking at this method, there is a small bug that could be fixed :)

 
Top Bottom