SOLVED: Copy file using code

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

I am upgrading some v3 code.

Can someone please provide some advice on how to go about copying a file manually in the code. On publish of a dataobject I want to duplicate an existing file to a new filename and also link that new file to another Field in the same dataobject.

The code I have so far creates a new File object in the DB but it always comes up as ‘File cannot be found’ in the asset manager. The columns ‘FileHash’ and ‘FileFileName’ are always blank. The code successfully physically creates a copy of the file on the disk .

$loDraftImage = $this->DraftImage;
if($loDraftImage->ID > 0){
	$lsNewFileName= $loDraftImage->copyFile($loUserFolder->Filename . '1.jpg');
	$loFileLive = Image::find($lsNewFileName);				
	if(!$loFileLive){
		$loFileLive = new Image();
		$loFileLive->setFilename($lsNewFileName);
		$loFileLive->Title = 'Image '. $i;
		$loFileLive->ParentID = $loUserFolder->ID;
	}
	$loFileLive->write(); 
	$this->LiveImage = $loFileLive;
	$loFileLive->destroy();
}
$this->writeToStage('Live');

Can anyone please tell me what I am missing?

After a bit of digging I realised you need to use this function:

$loFileLive->setFromLocalFile($absoluteFilePath, $lsNewFileName);