Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

update_resource()

Parameters

ColumnTypeDefaultDescription
$r
$path
$type
$title
$ingest false
$createPreviews true
$extension ''
$after_upload_processing false

Location

include/resource_functions.php lines 3736 to 3906

Definition

 
function update_resource($r$path$type$title$ingest=false$createPreviews=true$extension='',$after_upload_processing=false)
    {
    
# Update the resource with the file at the given path
    # Note that the file will be used at it's present location and will not be copied.
    
global $syncdir$staticsync_prefer_embedded_title$view_title_field$filename_field$upload_then_process$offline_job_queue$lang,
        
$extracted_text_field$offline_job_queue$offline_job_in_progress$autorotate_ingest$enable_thumbnail_creation_on_upload,
        
$userref$lang$upload_then_process_holding_state;

    if(
$upload_then_process && !$offline_job_queue)
        {
        
$upload_then_process=false;
        }

    
# Work out extension based on path
    
if($extension=='')
        {
        
$extension=pathinfo($pathPATHINFO_EXTENSION);
        }

    if(
$extension!=='')
        {
        
$extension=trim(strtolower($extension));
        }

    if(!
$upload_then_process || !$after_upload_processing)
        {
        
update_resource_type($r$type);

        
# file_path should only really be set to indicate a staticsync location. Otherwise, it should just be left blank.
        
if ($ingest){$file_path="";} else {$file_path=escape_check($path);}

        
# Store extension/data in the database
        
sql_query("update resource set archive=0,file_path='".$file_path."',file_extension='$extension',preview_extension='$extension',file_modified=now() where ref='$r'");

        
# Store original filename in field, if set
        
if (!$ingest)
            {
            
# This file remains in situ; store the full path in file_path to indicate that the file is stored remotely.
            
if (isset($filename_field))
                {

                
$s=explode("/",$path);
                
$filename=end($s);

                
update_field($r,$filename_field,$filename);
                }
            }
        else
            {
            
# This file is being ingested. Store only the filename.
            
$s=explode("/",$path);
            
$filename=end($s);

            if (isset(
$filename_field))
                {
                
update_field($r,$filename_field,$filename);
                }

            
# Move the file
            
if(!hook('update_resource_replace_ingest','',array($r$path$extension)))
                {
                
$destination=get_resource_path($r,true,"",true,$extension);
                
$result=rename($syncdir "/" $path,$destination);
                if (
$result===false)
                    {
                    
# The rename failed. The file is possibly still being copied or uploaded and must be ignored on this pass.
                    # Delete the resouce just created and return false.
                    
delete_resource($r);
                    return 
false;
                    }
                
chmod($destination,0777);
                }
            }
        }

    if(!
$upload_then_process || $after_upload_processing)
        {
        
# generate title and extract embedded metadata
        # order depends on which title should be the default (embedded or generated)
        
if ($staticsync_prefer_embedded_title)
            {
            if (
$view_title_field!==$filename_field)
                {
                
update_field($r,$view_title_field,$title);
                }
            
extract_exif_comment($r,$extension);
            }
        else
            {
            
extract_exif_comment($r,$extension);
            if (
$view_title_field!==$filename_field)
                {
                
update_field($r,$view_title_field,$title);
                }
            }
        
        
# Extract text from documents (e.g. PDF, DOC)
        
if (isset($extracted_text_field) && !(isset($unoconv_path) && in_array($extension,$unoconv_extensions))) 
            {
            if(
$offline_job_queue && !$offline_job_in_progress)
                {
                
$extract_text_job_data = array(
                    
'ref'       => $r,
                    
'extension' => $extension,
                );

                
job_queue_add('extract_text'$extract_text_job_data);
                }
            else
                {
                
extract_text($r$extension);
                }
            }
        
        
# Ensure folder is created, then create previews.
        
get_resource_path($r,false,"pre",true,$extension);

        if (
$createPreviews)
            {
            
# Attempt autorotation
            
if($ingest && $autorotate_ingest){AutoRotateImage($destination);}
            
# Generate previews/thumbnails (if configured i.e if not completed by offline process 'create_previews.php')
            
if($enable_thumbnail_creation_on_upload)
                {
                
create_previews($rfalse$extensionfalsefalse, -1false$ingest);
                }
            else if(!
$enable_thumbnail_creation_on_upload && $offline_job_queue)
                {
                
$create_previews_job_data = array(
                    
'resource' => $r,
                    
'thumbonly' => false,
                    
'extension' => $extension,
                    
'previewonly' => false,
                    
'previewbased' => false,
                    
'alternative' => -1,
                    
'ignoremaxsize' => false,
                    
'ingested' => $ingest
                
);
                
$create_previews_job_success_text str_replace('%RESOURCE'$r$lang['jq_create_previews_success_text']);
                
$create_previews_job_failure_text str_replace('%RESOURCE'$r$lang['jq_create_previews_failure_text']);

                
job_queue_add('create_previews'$create_previews_job_data''''$create_previews_job_success_text$create_previews_job_failure_text);
                }
            }
        }
        
        if(
$upload_then_process && !$after_upload_processing)
            {
            
# Add this to the job queue for offline processing            
            
$job_data=array();
            
$job_data["r"]=$r;
            
$job_data["title"]=$title;
            
$job_data["ingest"]=$ingest;
            
$job_data["createPreviews"]=$createPreviews;
        
            if(isset(
$upload_then_process_holding_state))
                {
                
$job_data["archive"]=sql_value("SELECT archive value from resource where ref={$ref}""");
                
update_archive_status($ref$upload_then_process_holding_state);
                }
        
            
$job_code=$r md5($job_data["r"] . strtotime('now'));
            
$job_success_lang="update_resource success " str_replace(array('%ref''%title'), array($r$filename), $lang["ref-title"]);
            
$job_failure_lang="update_resource fail " ": " str_replace(array('%ref''%title'), array($r$filename), $lang["ref-title"]);
            
$jobadded=job_queue_add("update_resource"$job_data$userref''$job_success_lang$job_failure_lang$job_code);             
            }
        
    
hook('after_update_resource''', array("resourceId" => $r ));
    
# Pass back the newly created resource ID.
    
return $r;
    }

This article was last updated 10th July 2020 20:35 Europe/London time based on the source file dated 10th July 2020 16:06 Europe/London time.