I wanted to make a migration and the hook_file_presave modified the type of the migrating file. We should modify the type only when it is empty.

Files: 
CommentFileSizeAuthor
#1 1677010-Fixing_type_regeneration-2.patch417 bytespoedan
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1677010-Fixing_type_regeneration-2.patch. Unable to apply patch. See the log in the details link for more information. View

Comments

poedan’s picture

FileSize
417 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1677010-Fixing_type_regeneration-2.patch. Unable to apply patch. See the log in the details link for more information. View

I check if the file type is already set.

Dave Reid’s picture

We should probably also check if (!empty($file->original) && $file->original->filemime != $file->filemime)) in case the file mime type has changed.

Dave Reid’s picture

We should probably also check if (!empty($file->original) && $file->original->filemime != $file->filemime)) in case the file mime type has changed.

Devin Carlson’s picture

Version: 7.x-2.0-unstable6 » 7.x-2.x-dev
Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 1677010-Fixing_type_regeneration-2.patch, failed testing.

Heine’s picture

Issue summary: View changes
Status: Needs work » Closed (fixed)

The suggestions appear to have been implemented elsewhere, at least in 7.x-2.0-beta2:

function file_entity_file_presave($file) {
  // Always ensure the filemime property is current.
  if (!empty($file->original) || empty($file->filemime)) {
    $file->filemime = file_get_mimetype($file->uri);
  }

  // The file type is used as a bundle key, and therefore, must not be NULL.
  // It defaults to FILE_TYPE_NONE when loaded via file_load(), but in case
  // file_save() is called on a new file object, default it here too.
  if (!isset($file->type)) {
    $file->type = FILE_TYPE_NONE;
  }

  // If the file isn't already assigned a real type, determine what type should
  // be assigned to it.
  if ($file->type === FILE_TYPE_NONE) {
    $type = file_get_type($file);
    if (isset($type)) {
      $file->type = $type;
    }
  }

  field_attach_presave('file', $file);

  // Fetch image dimensions.
  file_entity_metadata_fetch_image_dimensions($file);
}

Please reopen if this is still an issue.