<% /*
    $Revision$
    $Id$

    File:           upgrade20242.inc
    Description:    2024.2 file conversie
    Parameters:
    Context:        (alleen) vanuit facilitor.inc
    Notes:          - insdeel thumb plaatjes zijn te klein. Maak deze opnieuw aan als 192x108 (bxh).
*/

%>
<!-- #include file="../shared/FlexFiles.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<%

function upgrade20242()
{
    var marker = "upgrade20242.inc";
    var sql = "SELECT adm_tracking_revision, adm_tracking_date"
            + "  FROM adm_tracking"
            + " WHERE adm_tracking_name = " + safe.quoted_sql(marker);
    var oRs = Oracle.Execute(sql);
    if (!oRs.Eof)
    {
        __Log("2024.2 JSON upgrade rev {1} is al uitgevoerd op {0}".format(toDateTimeString(oRs("adm_tracking_date").Value), oRs("adm_tracking_revision").Value));
        oRs.Close();
        return;
    }
    oRs.Close();

    /* START INS_DEEL_IMAGE THUMBNAIL CONVERSIE */
    var ins_PhotoPath = Server.MapPath(S("ins_image_path") + "\deel");
    convertThumb(ins_PhotoPath);
    var res_PhotoPath = Server.MapPath(S("res_image_path") + "\deel");
    convertThumb(res_PhotoPath);
    
    function convertThumb(oldPhotoPath)
    {
        __Log("Converting " + oldPhotoPath);
        var fso = Server.CreateObject("Scripting.FileSystemObject");
        if (fso.FolderExists(oldPhotoPath)) 
        {
            if (!fso.FolderExists(oldPhotoPath + "/thumb/")) 
            {
                fso.CreateFolder(oldPhotoPath + "/thumb/");
            }
            var oIMG = Server.CreateObject("SLNKDWF.ImageConvert");
            var maxThumbW = 192;
            var maxThumbH = 144;
            var insPhotoFolder = fso.GetFolder(oldPhotoPath);
            var today = new Date().midnight();
            var f_org = new Enumerator(insPhotoFolder.files);
            for (; !f_org.atEnd(); f_org.moveNext()) 
            {
                try 
                {
                    var photo = f_org.item();
                    var orgPhotoName = photo.Name;
                    var thumbPhotoPath = oldPhotoPath + "/thumb/"+ orgPhotoName;
                    if (fso.fileExists(thumbPhotoPath))
                    {   // Bestaande thumbnail verwijderen.
                        f_thumb = fso.getFile(thumbPhotoPath);
                        var dt = new Date(f_thumb.DateLastModified);
                        if (dt > today) // vandaag al een keer gewijzigd. Afblijven.
                            continue; 
                        f_thumb.Delete();
                    }
                    // Nieuw formaat thumbnail weer toevoegen.
                    oIMG.Open(oldPhotoPath + "/" + orgPhotoName);
                    if (oIMG.Width / oIMG.Height > maxThumbW / maxThumbH) 
                    {
                        oIMG.Height = oIMG.Height / oIMG.Width * maxThumbW;
                        oIMG.Width = maxThumbW;
                    } 
                    else 
                    {
                        oIMG.Width = oIMG.Width / oIMG.Height * maxThumbH;
                        oIMG.Height = maxThumbH;
                    }
                    oIMG.SaveAs(thumbPhotoPath);
                } 
                catch (e) 
                {
                    __DoLog("ERROR resizing thumbnails " + oldPhotoPath + "/" + orgPhotoName + " -> " + thumbPhotoPath, "#FF0000");
                    __DoLog(e.description);
                }
            }
        }
    }
    
    /* EINDE INS_DEEL_IMAGE THUMBNAIL CONVERSIE */

    // Track dat we ge-upgrade hebben, geen risico voor multi-user omgevingen
    sql = "INSERT INTO adm_tracking"
        + "   (adm_tracking_name, adm_tracking_revision)"
        + " VALUES ({0}, {1})".format(safe.quoted_sql(marker), safe.quoted_sql("$Revision$".substr(11, 5)));
    Oracle.Execute(sql);
}
%>

