- <?php
- * @file
- * File hooks implemented by the File module.
- */
-
- * Implements hook_file_presave().
- */
- function file_file_presave($file) {
-
- if (!empty($file->original) || empty($file->filemime)) {
- $file->filemime = file_get_mimetype($file->uri);
- }
-
- field_attach_presave('file', $file);
-
-
- file_metadata_fetch_image_dimensions($file);
- }
-
- * Implements hook_file_type().
- */
- function file_file_type($file) {
- $types = array();
- foreach (file_type_get_types() as $type) {
- if (file_match_mimetypes($type->mimetypes, $file->filemime)) {
- $types[] = $type->type;
- }
- }
-
- return $types;
- }
-
- * Implements hook_file_insert().
- */
- function file_file_insert($file) {
-
- if (!empty($file->metadata)) {
- $query = db_insert('file_metadata')->fields(array('fid', 'name', 'value'));
- foreach ($file->metadata as $name => $value) {
- $query->values(array(
- 'fid' => $file->fid,
- 'name' => $name,
- 'value' => serialize($value),
- ));
- }
- $query->execute();
- }
-
-
- file_invalidate_field_caches($file);
- }
-
- * Implements hook_file_update().
- */
- function file_file_update($file) {
-
- field_attach_update('file', $file);
-
-
- db_delete('file_metadata')->condition('fid', $file->fid)->execute();
- if (!empty($file->metadata)) {
- $query = db_insert('file_metadata')->fields(array('fid', 'name', 'value'));
- foreach ($file->metadata as $name => $value) {
- $query->values(array(
- 'fid' => $file->fid,
- 'name' => $name,
- 'value' => serialize($value),
- ));
- }
- $query->execute();
- }
-
- if (module_exists('image') && file_get_mimetype_type($file) == 'image' && $file->filesize && isset($file->original)) {
- if (!isset($file->metadata)) {
- $file->metadata = array();
- }
-
- if (!isset($file->original->metadata)) {
- if (!is_object($file->original)) {
- $file->original = new stdClass();
- }
- $file->original->metadata = array();
- }
-
-
-
-
- $file->metadata += array('width' => NULL, 'height' => NULL);
- $file->original->metadata += array('width' => NULL, 'height' => NULL);
- if ($file->filesize != $file->original->filesize || $file->metadata['width'] != $file->original->metadata['width'] || $file->metadata['height'] != $file->original->metadata['height']) {
- _file_update_image_field_dimensions($file);
- }
-
-
- image_path_flush($file->uri);
- }
-
-
- file_invalidate_field_caches($file);
- }
-
- * Implements hook_file_delete().
- */
- function file_file_delete($file) {
- field_attach_delete('file', $file);
-
-
-
- file_invalidate_field_caches($file);
-
-
- db_delete('file_metadata')->condition('fid', $file->fid)->execute();
-
-
-
-
-
- if (module_exists('search')) {
- search_reindex($file->fid, 'file');
- }
- }
-
- * Implements hook_file_mimetype_mapping_alter().
- */
- function file_file_mimetype_mapping_alter(&$mapping) {
-
-
- $new_mappings['mka'] = 'audio/x-matroska';
- $new_mappings['mkv'] = 'video/x-matroska';
-
-
-
- $new_mappings['weba'] = 'audio/webm';
- $new_mappings['webm'] = 'video/webm';
- $new_mappings['webp'] = 'image/webp';
-
- foreach ($new_mappings as $extension => $mime_type) {
- if (!in_array($mime_type, $mapping['mimetypes'])) {
-
- $mapping['mimetypes'][] = $mime_type;
- }
-
-
- $index = array_search($mime_type, $mapping['mimetypes']);
- $mapping['extensions'][$extension] = $index;
- }
- }
-
- * Implements hook_file_load().
- */
- function file_file_load($files) {
-
- $config = config('file.settings');
-
- $replace_options = array(
- 'clear' => TRUE,
- 'sanitize' => FALSE,
- );
-
-
- $results = db_query("SELECT * FROM {file_metadata} WHERE fid IN (:fids)", array(':fids' => array_keys($files)));
-
- foreach ($results as $result) {
- $name = $result->name;
-
-
- if ($name == 'height' || $name == 'width') {
- $files[$result->fid]->$name = unserialize($result->value);
- }
-
- $files[$result->fid]->metadata[$name] = unserialize($result->value);
- }
- }
-
- * Fetch the dimensions of an image and store them in the file metadata array.
- */
- function file_metadata_fetch_image_dimensions($file) {
-
-
- if (!$file->filesize) {
- return;
- }
-
-
- if (file_get_mimetype_type($file) != 'image') {
- return;
- }
-
-
- $image_info = image_get_info($file->uri);
- if ($image_info) {
- $file->metadata['width'] = $image_info['width'];
- $file->metadata['height'] = $image_info['height'];
- }
- else {
-
- $file->metadata['width'] = NULL;
- $file->metadata['height'] = NULL;
- }
- }
-
- * Update the image dimensions stored in any image fields for a file.
- *
- * @param object $file
- * A file object that is an image.
- *
- * @see http://drupal.org/node/1448124
- */
- function _file_update_image_field_dimensions($file) {
-
-
- if (!$file->filesize) {
- return;
- }
-
-
- if (file_get_mimetype_type($file) != 'image') {
- return;
- }
-
-
- $image_fields = _file_get_fields_by_type('image');
-
- foreach ($image_fields as $image_field) {
- $query = new EntityFieldQuery();
- $query->fieldCondition($image_field, 'fid', $file->fid);
- $results = $query->execute();
-
- foreach ($results as $entity_type => $entities) {
- $entities = entity_load($entity_type, array_keys($entities));
- foreach ($entities as $entity) {
- foreach ($entity->{$image_field} as $langcode => $items) {
- foreach ($items as $delta => $item) {
- if ($item['fid'] == $file->fid) {
- $entity->{$image_field}[$langcode][$delta]['width'] = $file->metadata['width'];
- $entity->{$image_field}[$langcode][$delta]['height'] = $file->metadata['height'];
- }
- }
- }
-
-
- _file_fields_update($entity_type, $entity);
- }
- }
- }
- }
-
- * Update an entity's field values without changing anything on the entity.
- */
- function _file_fields_update($entity_type, $entity) {
- $id = $entity->id();
- if (empty($id)) {
- throw new Exception(t('Cannot call _file_fields_update() on a new entity.'));
- }
-
-
- if (!isset($entity->original)) {
- $entity->original = $entity;
- }
-
-
- unset($entity->revision);
-
-
- field_attach_presave($entity_type, $entity);
- field_attach_update($entity_type, $entity);
-
-
- entity_get_controller($entity_type)->resetCache(array($id));
- }
-
- * Implements hook_file_metadata_info().
- */
- function file_file_metadata_info() {
- $info['width'] = array('label' => t('Width'), 'type' => 'integer');
- $info['height'] = array('label' => t('Height'), 'type' => 'integer');
- return $info;
- }