Fixed "Unassociated attachments" and hourly cleanup

Kruzya

Well-known member
Affected version
2.2.4
I started translating a series of articles one week ago on Russian language. In first day i translated fully a three articles and started translation fourth article. I haven't finished translating it, and just saved draft.
On my forum, drafts are kept one month. You can say "oh my god, it is too long!", but primarily drafts used only by me. Users writes a small messages (posts in threads) and posts instantly, I can formulate and write sometimes for a week. But i've never used attachments before for long writing messages.

So, back to the thread.
I started a translation of article and attached four files: three images and one video, and saved a draft. Returned to him after 4 days and was horrified: all attachments deleted from server. "Hourly clean up" job does not take into account the possible presence of drafts with which the attachment may actually be associated, and drops all these attachment, if is uploaded more than a day ago.

This behavior can be changed? This causes wild discomfort that attachments do not survive for more than a day after upload, if you're doing something long.
 
The option "Store drafts for X hour" can be set to more than 24 hours, but this doesn't affect the hard-coded 1 day timeout in deleteUnassociatedAttachments
 
The problem with simply tying this to draft saving lifetime - which is not unreasonable - is that if you're working on a draft post over the course of a few days, the last_update time of the draft gets updated but the attach_date stays the same.

So even if you have drafts as the default 24 hours, the attachments will still be deleted 24 hours later, even if the draft is still active and being modified constantly over the last 24 hours.

Only solution is to bump the attach_date but this is difficult to do in a generic way because certain content types may support multiple attachment types per content (resources support version_attachment_hash and attachment_hash).

I've settled on an opt-in approach. It amounts to a few extra lines of code if your particular content type might care about premature deletion of unassociated attachments. For example, here's the relevant code now in XF\Pub\Controller\Forum::actionThread:

PHP:
/** @var \XF\ControllerPlugin\Draft $draftPlugin */
$draftPlugin = $this->plugin('XF:Draft');
$draftReply = $draftPlugin->actionDraftMessage($forum->draft_thread, $extraData, 'message', $draftAction);

if ($draftAction == 'save')
{
   $draftPlugin->refreshTempAttachments($extraData['attachment_hash']);
}

return $draftReply;

refreshTempAttachments also supports an array of attachment hashes (for the RM style case). Empty values are removed.

In addition to this if draft saving is enabled, unassociated attachment deletion cut-off will be based on the draft lifetime, but still a minimum 24 hours.

The combination of these things mean unassociated attachments should not be deleted while there is still a valid draft record.
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.7).

Change log:
Allow unassociated attachment deletion cutoff to be extended inline with draft save lifetime, plus refresh temporary attachments when saving a draft.
There may be a delay before changes are rolled out to the XenForo Community.
 
Back
Top Bottom