<%@ LANGUAGE = JavaScript %>
<% /*
     $Revision$
     $Id$

    File:               savepng.asp
    Description:        Opvangscript van uploadform.asp
    Parameters:
        extfilter       extensie filter
        pkey            ALG_RUIMTE_KEY of ...
        pModule         "ALG" of ...
        pKenmerk_key    ALG_KENMERK_KEY of ...
    Context:            Vanuit uploadform.asp
    Note:               Met TAMPER bescherming!
*/

JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./flexfiles.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/kenmerk_common.inc" -->
<%
protectQS.verify(); // tamper check

var pKey = getQParamInt("key", -1);
var pNiveau = getQParamSafe("niveau", "");
var pModule = getQParamSafe("module");
var pKenmerk_key = getQParamInt("kenmerk_key", -1);

var params = flexProps(pModule, pKey, String(pKenmerk_key), pNiveau);

  __Log("Opslaan onder: " + params.AttachPath);
  CreateFullPath(params.AttachPath);

  var fso = Server.CreateObject("Scripting.FileSystemObject")
  var f = fso.GetFolder(params.AttachPath);
  for (fc = new Enumerator(f.files); !fc.atEnd(); fc.moveNext())
  {
    var vFileName = fc.item().Name;
    __Log("Autodelete: " + params.AttachPath + vFileName)
    DeleteFile(params.AttachPath + vFileName);
  }

  var data = getFParam("data", "");
  var result = {};
  if (data)
  {
      var Attachment = data.split(",")[1]; // haal data:image/png;base64, er af
      var now = new Date();
      var SafeName = "signature_{0}-{1}-{2}_{3}-{4}-{5}.png".format(padout(now.getFullYear()),
                                                                    padout(now.getMonth()+1),
                                                                    padout(now.getDate()),
                                                                    padout(now.getHours()),
                                                                    padout(now.getMinutes()),
                                                                    padout(now.getSeconds()));

      try
      {
        encodedString2File(params.AttachPath + SafeName, Attachment, "bin.base64");
        checkWebconfig(params.AttachRootPath);
        var transitParam = buildTransitParam(["tmpfolder"]);

          result.safefilename = SafeName;
          result.fullPath = protectQS.create(OpenFlexFile(pModule, pNiveau, pKey, pKenmerk_key, SafeName) + transitParam);
      }
      catch (e)
      {
        result.message = L("lcl_shared_upload_error_start") + e.description + L("lcl_shared_upload_error_end");
      }
  }

  result.success = true;
  Response.Write(JSON.stringify(result));
  Response.End;
%>
