Analyzing URL – ShiVa Engine

Analyzing URL

It is often interesting when you have to download an image to put into cache to get the name and the extension of the file in the URL.
For this tutorial, you need to declare a table in your AIModel variables, I named it tTable. Create a function named getFile and add the variable sFileURL as parameter.

--start the script by clearing the table data
table.empty ( this.tTable ( ) )
--explode string with “/” as delimiter character
string.explode ( sFileURL..”/”, this.tTable ( ), “/” )
--the last item of the table is the filename with its extension
local sFilenameAndExtension = table.getLast ( this.tTable ( ) )
--logically, the path of the file is the URL without the file and the extension
local sPath = string.getSubString ( sFileURL, 0, string.getLength ( sFileURL ) – string.getLength ( sFilenameAndExtension ) )
--to separate the filename from its extension, you must find the “.” symbol
local nDotIndex = string.findFirst ( sFilenameAndExtension, “.”, 0 )
--the extension is the part at the right of the string...
local sExtension = string.getSubString ( sFilenameAndExtension, nDotIndex, string.getLength ( sFilenameAndExtension ) – nDotIndex )
--...and the filename is the other part
local sFilename = string.getSubString ( sFilenameAndExtension, 0, string.getLength ( sFilenameAndExtension ) – string.getLength ( sExtension ) )
--return the results
return sPath, sFilename, sExtension



Need more answers?

  • slackBanner