diff --git a/feeds.module b/feeds.module index cba702d..557d6e8 100644 --- a/feeds.module +++ b/feeds.module @@ -60,8 +60,33 @@ function feeds_cron() { // very long. $sources = db_query("SELECT feed_nid, id FROM {feeds_source} WHERE id IN (:ids)", array(':ids' => $importers)); + $valid_fids = array(); foreach ($sources as $source) { - feeds_source($source->id, $source->feed_nid)->schedule(); + // Get the FeedsSource object + $source_object = feeds_source($source->id, $source->feed_nid); + // Schedule all periodic tasks for this source. + $source_object->schedule(); + // Find out the File ID of the temporary file for this source. + if (!empty($source_object->fetcher_result->fid)) { + $valid_fids[] = $source_object->fetcher_result->fid; + } + } + + // Get current temporary feed file File IDs. + $result = db_select('file_managed') + ->fields('file_managed', array('fid')) + ->condition('uri', db_like('public://feeds/Feeds') . '%' . db_like('Fetcher') . '%', 'LIKE') + // We don't want to accidently delete a recently created temporary file, + // hence limit to files older than 1 minute. + ->condition('timestamp', REQUEST_TIME - 60, '<') + ->execute() + ->fetchAll(); + + // Delete any temporary feed files without a source. + foreach ($result as $record) { + if (!in_array($record->fid, $valid_fids)) { + file_delete($record->fid); + } } feeds_reschedule(FALSE); diff --git a/feeds.pages.inc b/feeds.pages.inc index 9bed7b7..1bc29b2 100644 --- a/feeds.pages.inc +++ b/feeds.pages.inc @@ -258,6 +258,12 @@ function feeds_unlock_tab_form_submit($form, &$form_state) { $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid']; $importer_id = $form['#importer_id']; + // Delete temporary files if any. + $source = feeds_source($importer_id, $feed_id); + if (!empty($source->fetcher_result)) { + $source->fetcher_result->deleteFile(); + } + //Is there a more API-friendly way to set the state? db_update('feeds_source') ->condition('id', $importer_id) diff --git a/includes/FeedsSource.inc b/includes/FeedsSource.inc index 2fe2249..8e68450 100644 --- a/includes/FeedsSource.inc +++ b/includes/FeedsSource.inc @@ -405,6 +405,7 @@ class FeedsSource extends FeedsConfigurable { $this->imported = time(); $this->log('import', 'Imported in !s s', array('!s' => $this->imported - $this->state[FEEDS_START]), WATCHDOG_INFO); module_invoke_all('feeds_after_import', $this); + $this->fetcher_result->deleteFile(); unset($this->fetcher_result, $this->state); } $this->save(); diff --git a/plugins/FeedsFetcher.inc b/plugins/FeedsFetcher.inc index 33457de..fd03cc9 100644 --- a/plugins/FeedsFetcher.inc +++ b/plugins/FeedsFetcher.inc @@ -11,6 +11,7 @@ class FeedsFetcherResult extends FeedsResult { protected $raw; protected $file_path; + protected $fid; /** * Constructor. @@ -50,9 +51,10 @@ class FeedsFetcherResult extends FeedsResult { } $this->file_path = FALSE; if ($file = file_save_data($this->getRaw(), $destination . '/' . get_class($this) . REQUEST_TIME)) { - $file->status = 0; + $file->status = FILE_STATUS_PERMANENT; file_save($file); $this->file_path = $file->uri; + $this->fid = $file->fid; } else { throw new Exception(t('Cannot write content to %dest', array('%dest' => $destination))); @@ -62,6 +64,17 @@ class FeedsFetcherResult extends FeedsResult { } /** + * Deletes the file containing the resource provided by the fetcher. + */ + public function deleteFile() { + if (isset($this->fid) && $file = file_load($this->fid)) { + file_delete($file); + unset($this->fid); + unset($this->file_path); + } + } + + /** * Sanitize the raw content string. Currently supported sanitizations: * * - Remove BOM header from UTF-8 files.