Collections functions
General functions
Render 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 4297 to 4491

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,$unoconv_extensions;

    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=$path;}

        
# Store extension/data in the database
        
ps_query("UPDATE resource SET archive=0,file_path=?,file_extension=?,preview_extension=?,file_modified=NOW() WHERE ref=?",array("s",$file_path,"s",$extension,"s",$extension,"i",$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=copy($syncdir "/" $path,$destination); // Copy instead of rename so that permissions of filestore will be used
                
if ($result===false)
                    {
                    
# The copy failed. The file is possibly still being copied or uploaded and must be ignored on this pass.
                    # If the file is still being copied then $staticsync_file_minimum_age can be set to prevent this error from occurring
                    # Delete the resouce just created and return false.
                    
debug("ERROR: Staticsync failed to copy file from: " .  $syncdir "/" $path);
                    
delete_resource($r);
                    return 
false;
                    }
                
$use_error_exception_cache $GLOBALS["use_error_exception"] ?? false;
                
$GLOBALS["use_error_exception"] = true;
                try
                    {
                    
unlink($syncdir "/" $path);
                    try
                        {
                        
chmod($destination,0777);
                        }
                    catch (
Exception $e)
                        {
                        
// Not fatal, just log
                        
debug(" - ERROR: Staticsync failed to set permissions on ingested file: " .  $destination PHP_EOL " - Error message: " $e->getMessage() . PHP_EOL);
                        }                  
                    }
                catch (
Exception $e)
                    {
                    echo 
" - ERROR: failed to delete file from source. Please check correct permissions on: " .  $syncdir "/" $path PHP_EOL " - Error message: "  $e->getMessage() . PHP_EOL;
                    return 
false;
                    }
               
                
$GLOBALS["use_error_exception"] = $use_error_exception_cache;
                }
            }
        }

    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"]=ps_value("SELECT archive value from resource where ref=?", array("i",$r), "");
                
update_archive_status($r$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 2nd October 2022 17:35 Europe/London time based on the source file dated 30th September 2022 14:20 Europe/London time.