Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

update_resource()

Description

Updates a resource with a new file and its associated metadata.

Parameters

ColumnTypeDefaultDescription
$r int The resource ID to update.
$path string The file path of the new resource.
$type string The type of the resource.
$title string The title for the resource.
$ingest bool false Indicates if the resource is being ingested.
$createPreviews bool true Flag to create previews.
$extension string '' The file extension of the new resource.
$after_upload_processing bool false Indicates if the update is after upload processing.

Return

int|bool The resource ID if successful, false otherwise.

Location

include/resource_functions.php lines 4826 to 5021

Definition

 
function update_resource($r$path$type$title$ingest=false$createPreviews=true$extension='',$after_upload_processing=false)
    {
    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;
        }

    
# FStemplate support - do not allow samples from the template to be replaced
    
if (resource_file_readonly($r)) {
        return 
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(),no_file=0 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);
                }
            elseif(!
$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_failure_text str_replace('%RESOURCE'$r$lang['jq_create_previews_failure_text']);

                
job_queue_add('create_previews'$create_previews_job_data''''''$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_failure_lang="update_resource fail " ": " str_replace(array('%ref''%title'), array($r$filename), $lang["ref-title"]);
            
job_queue_add("update_resource"$job_data$userref''''$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 4th December 2024 09:05 Europe/London time based on the source file dated 28th November 2024 12:10 Europe/London time.