<?php

$getObjektImagesStatement = $RUNTIME['PDO']->prepare('SELECT DISTINCT objects.ObjectImage AS AssetID, grids.GridAssetServer AS AssetServerURL FROM objects INNER JOIN parcels ON objects.ObjectParcelUUID = parcels.ParcelUUID INNER JOIN regions ON parcels.ParcelRegionUUID = regions.RegionUUID INNER JOIN grids ON grids.GridName = regions.RegionGridName WHERE objects.ObjectImage != "00000000-0000-0000-0000-000000000000" ORDER BY RAND() LIMIT 50');
$getObjektImagesStatement->execute(array()); 

while($row = $getObjektImagesStatement->fetch())
{
    if(!file_exists("data/AssetImages/".$row['AssetID'].".png"))
    {
        $downloadURL = $row['AssetServerURL']."assets/".$row['AssetID']."/data";

        $DATA['WGETCMD']    =   '/usr/bin/wget '.$row['AssetServerURL'].'assets/'.$row['AssetID'].'/data -O ./data/AssetImages/'.$row['AssetID'].".jpg2000";
        $DATA['MPEGCMD']    =   '/usr/bin/ffmpeg -i ./data/AssetImages/'.$row['AssetID'].'.jpg2000 ./data/AssetImages/'.$row['AssetID'].'.png';

        if(file_exists("ffmpeg.exe") || file_exists("wget.exe"))
        {
            $DATA['WGETCMD']    =   'wget.exe '.$row['AssetServerURL'].'assets/'.$row['AssetID'].'/data -O ./data/AssetImages/'.$row['AssetID'].".jpg2000";
            $DATA['MPEGCMD']    =   'ffmpeg.exe -i ./data/AssetImages/'.$row['AssetID'].'.jpg2000 ./data/AssetImages/'.$row['AssetID'].'.png';
        }

        $DATA['WGET']   = shell_exec($DATA['WGETCMD']);
        $DATA['FFMPEG'] = shell_exec($DATA['MPEGCMD']);

        unlink('./data/AssetImages/'.$row['AssetID'].'.jpg2000');
    }
}

$getObjektTextureStatement = $RUNTIME['PDO']->prepare('SELECT DISTINCT textures.TextureUUID AS AssetID, grids.GridAssetServer AS AssetServerURL FROM textures INNER JOIN objects ON textures.TextureObjektUUID = objects.ObjectUUID INNER JOIN parcels ON objects.ObjectParcelUUID = parcels.ParcelUUID INNER JOIN regions ON parcels.ParcelRegionUUID = regions.RegionUUID INNER JOIN grids ON grids.GridName = regions.RegionGridName ORDER BY RAND() LIMIT 50');
$getObjektTextureStatement->execute(array()); 

while($row = $getObjektTextureStatement->fetch())
{
    if(!file_exists("data/AssetImages/".$row['AssetID'].".png"))
    {
        $downloadURL = $row['AssetServerURL']."assets/".$row['AssetID']."/data";

        $DATA['WGETCMD']    =   '/usr/bin/wget '.$row['AssetServerURL'].'assets/'.$row['AssetID'].'/data -O ./data/AssetImages/'.$row['AssetID'].".jpg2000";
        $DATA['MPEGCMD']    =   '/usr/bin/ffmpeg -i ./data/AssetImages/'.$row['AssetID'].'.jpg2000 ./data/AssetImages/'.$row['AssetID'].'.png';

        if(file_exists("ffmpeg.exe") || file_exists("wget.exe"))
        {
            $DATA['WGETCMD']    =   'wget.exe '.$row['AssetServerURL'].'assets/'.$row['AssetID'].'/data -O ./data/AssetImages/'.$row['AssetID'].".jpg2000";
            $DATA['MPEGCMD']    =   'ffmpeg.exe -i ./data/AssetImages/'.$row['AssetID'].'.jpg2000 ./data/AssetImages/'.$row['AssetID'].'.png';
        }

        $DATA['WGET']   = shell_exec($DATA['WGETCMD']);
        $DATA['FFMPEG'] = shell_exec($DATA['MPEGCMD']);

        unlink('./data/AssetImages/'.$row['AssetID'].'.jpg2000');
    }
}

$getItemImagesStatement = $RUNTIME['PDO']->prepare('SELECT DISTINCT items.ItemAssetUUID AS AssetID, grids.GridAssetServer AS AssetServerURL FROM items INNER JOIN objects ON items.ItemParent = objects.ObjectUUID INNER JOIN parcels ON objects.ObjectParcelUUID = parcels.ParcelUUID INNER JOIN regions ON parcels.ParcelRegionUUID = regions.RegionUUID INNER JOIN grids ON grids.GridName = regions.RegionGridName WHERE items.ItemType = "0" ORDER BY RAND() LIMIT 50');
$getItemImagesStatement->execute(array()); 

while($row = $getItemImagesStatement->fetch())
{
    if(!file_exists("data/AssetImages/".$row['AssetID'].".png"))
    {
        $downloadURL = $row['AssetServerURL']."assets/".$row['AssetID']."/data";

        $DATA['WGETCMD']    =   '/usr/bin/wget '.$row['AssetServerURL'].'assets/'.$row['AssetID'].'/data -O ./data/AssetImages/'.$row['AssetID'].".jpg2000";
        $DATA['MPEGCMD']    =   '/usr/bin/ffmpeg -i ./data/AssetImages/'.$row['AssetID'].'.jpg2000 -q:v 10 ./data/AssetImages/'.$row['AssetID'].'.png';

        if(file_exists("ffmpeg.exe") || file_exists("wget.exe"))
        {
            $DATA['WGETCMD']    =   'wget.exe '.$row['AssetServerURL'].'assets/'.$row['AssetID'].'/data -O ./data/AssetImages/'.$row['AssetID'].".jpg2000";
            $DATA['MPEGCMD']    =   'ffmpeg.exe -i ./data/AssetImages/'.$row['AssetID'].'.jpg2000 -q:v 10 ./data/AssetImages/'.$row['AssetID'].'.png';
        }

        $DATA['WGET']   = shell_exec($DATA['WGETCMD']);
        $DATA['FFMPEG'] = shell_exec($DATA['MPEGCMD']);

        unlink('./data/AssetImages/'.$row['AssetID'].'.jpg2000');
    }
}

?>