Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

get_resource_type_from_extension()

Description

If required, truncate to 255 characters - {@see https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits}

if (mb_strlen($filename, 'UTF-8') > 255)
{
$filename = mb_strcut($filename, 0, 255, 'UTF-8');
}

// Extra check that the generated filename extension matches the requested extension e.g. if using $filename_field value previews it may have the extension of the original resource file, rather than jpg or the field data was too long and the extension has been removed by truncation.
$filename_parts = pathinfo($filename);
if(!isset($filename_parts["extension"]) || strtolower($filename_parts["extension"]) != strtolower($ext))
{
$filename = mb_strcut($filename_parts["basename"], 0, (254-strlen($ext)), 'UTF-8') . "." . $ext;
}

if ($filename !== '')
{
return $filename;
}

debug('[get_download_filename] Invalid download filename, fall back.');
return (string) preg_replace('/(:|\r\n|\r|\n)/', '_', strip_tags(nl2br($fallback_filename)));
}



Get resource type ID based on extension
$mappings = array(resource_type_id => array(allowed_extensions));

Example of mapping array:
$mappings = array(2 => array('pdf', 'doc', 'docx', 'epub', 'ppt', 'pptx', 'odt', 'ods', 'tpl'));

Parameters

ColumnTypeDefaultDescription
$extension string Extension we search by (ie. "mp4")
$resource_type_extension_mapping array Maps between resource types and extensions
$default integer The default value to use in case we can't find it the mappings

Return

integer Resource type ID

Location

include/resource_functions.php lines 8437 to 8460

Definition

 
function get_resource_type_from_extension($extension, array $resource_type_extension_mapping$default)
    {
    
$resource_types ps_array("SELECT ref AS value FROM resource_type",array());
    foreach(
$resource_type_extension_mapping as $resource_type_id => $allowed_extensions)
        {
        if (
            !
checkperm('T' $resource_type_id)
            && 
in_array(strtolower($extension), $allowed_extensions)
            && 
in_array($resource_type_id$resource_types)
            ) {
                return 
$resource_type_id;
            }
        }
    if(
in_array($default$resource_types))
        {
        return 
$default;
        }
    else
        {
        
// default resource type does not exist so use the first available type
        
sort($resource_types,SORT_NUMERIC);
        return 
$resource_types[0];
        }
    }

This article was last updated 19th March 2024 04:35 Europe/London time based on the source file dated 11th March 2024 14:25 Europe/London time.