diff --git a/file_entity.file_api.inc b/file_entity.file_api.inc index 2f92256..6494215 100644 --- a/file_entity.file_api.inc +++ b/file_entity.file_api.inc @@ -450,15 +450,6 @@ function file_type_load_all($reset = FALSE) { } /** - * CTools exportables 'subrecords callback' to load {file_type} subrecords. - */ -function file_type_load_subrecords($types) { - foreach ($types as $type) { - $type->mimetypes = db_query('SELECT mimetype FROM {file_type_mimetypes} WHERE type = :type', array(':type' => $type->type))->fetchCol(); - } -} - -/** * Returns an array of enabled file types. */ function file_type_get_enabled_types() { @@ -514,17 +505,9 @@ function file_type_save($type) { 'type' => $type->type, 'label' => $type->label, 'description' => $type->description, + 'mimetypes' => serialize($type->mimetypes), ); - // Prepare the mimetype multiple insert query, but don't execute it until - // later in this function. - if (!empty($type->mimetypes)) { - $mimetype_insert = db_insert('file_type_mimetypes')->fields(array('type', 'mimetype')); - foreach ($type->mimetypes as $mimetype) { - $mimetype_insert->values(array('type' => $type->type, 'mimetype' => $mimetype)); - } - } - // Update an existing type object, whether with a modified 'type' property or // not. if ($old_type) { @@ -533,18 +516,12 @@ function file_type_save($type) { ->fields($fields) ->condition('type', $old_type->type) ->execute(); - db_delete('file_type_mimetypes') - ->condition('type', $old_type->type) - ->execute(); } else { db_insert('file_type') ->fields($fields) ->execute(); } - if (isset($mimetype_insert)) { - $mimetype_insert->execute(); - } if ($rename_bundle) { field_attach_rename_bundle('file', $old_type->type, $type->type); } @@ -556,9 +533,6 @@ function file_type_save($type) { db_insert('file_type') ->fields($fields) ->execute(); - if (isset($mimetype_insert)) { - $mimetype_insert->execute(); - } field_attach_create_bundle('file', $type->type); module_invoke_all('file_type_insert', $type); $status = SAVED_NEW; @@ -592,9 +566,6 @@ function file_type_delete($type) { db_delete('file_type') ->condition('type', $type->type) ->execute(); - db_delete('file_type_mimetypes') - ->condition('type', $type->type) - ->execute(); // Remove this type from CToolS status variable. $status = variable_get('default_file_type', array()); diff --git a/file_entity.install b/file_entity.install index a1f86c2..da5d288 100644 --- a/file_entity.install +++ b/file_entity.install @@ -34,6 +34,13 @@ function file_entity_schema() { 'size' => 'medium', 'translatable' => TRUE, ), + 'mimetypes' => array( + 'description' => 'Mimetypes mapped to this file type.', + 'type' => 'blob', + 'size' => 'big', + 'not null' => FALSE, + 'serialize' => TRUE, + ), ), 'primary key' => array('type'), 'export' => array( @@ -43,7 +50,6 @@ function file_entity_schema() { 'default hook' => 'file_default_types', 'identifier' => 'file_type', 'export type string' => 'ctools_type', - 'subrecords callback' => 'file_type_load_subrecords', 'save callback' => 'file_type_save', 'delete callback' => 'file_type_delete', 'api' => array( @@ -54,29 +60,6 @@ function file_entity_schema() { ), ), ); - $schema['file_type_mimetypes'] = array( - 'description' => 'Maps mimetypes to file types.', - 'fields' => array( - 'type' => array( - 'description' => 'The machine name of the file type.', - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - ), - 'mimetype' => array( - 'description' => 'Mimetypes mapped to this file type.', - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - ), - ), - 'indexes' => array( - 'file_type' => array('type'), - 'file_type_mimetype' => array('mimetype'), - ), - ); $schema['file_display'] = array( 'description' => 'Stores configuration options for file displays.', 'fields' => array( @@ -859,3 +842,42 @@ function file_entity_update_7209() { db_drop_table('file_type_streams'); } } + +/** + * Merge MIME types into the {file_type} table. + */ +function file_entity_update_7210() { + // Add the new mimetypes field if it doesn't already exist. + if (!db_field_exists('file_type', 'mimetypes')) { + $field = array( + 'description' => 'Mimetypes mapped to this file type.', + 'type' => 'blob', + 'size' => 'big', + 'not null' => FALSE, + 'serialize' => TRUE, + ); + + db_add_field('file_type', 'mimetypes', $field); + } + + // Migrate any existing MIME type information into {file_type}. + if (db_table_exists('file_type_mimetypes')) { + foreach (file_type_load_all(TRUE) as $type) { + $mimetypes = array(); + $result = db_select('file_type_mimetypes', 'ftm') + ->fields('ftm', array('mimetype')) + ->condition('type', $type->type, '=') + ->execute(); + + foreach ($result as $record) { + $mimetypes[] = $record->mimetype; + } + + $type->mimetypes = $mimetypes; + file_type_save($type); + } + + // Remove {file_type_mimetypes} after the information is migrated. + db_drop_table('file_type_mimetypes'); + } +}