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

    File:           exp_export.asp
    Description:    Export, resultaat direct downloaden
    Parameters:
        export_key  Export key uit fac_export_app
        compress    Optional 0 of 1, overrules flag in fac_export_app
    Context:
    Note:           De download limiet ligt meestal rond de 5MB
                    Daar kun je een beetje omheen werken door &compress=1 mee te geven

    TODO: Er is nog geen interface om aanroep van dit bestand te regelen
*/ %>
<%
Server.ScriptTimeout = 6000; // Extra tijd
DOCTYPE_Disable = 1;
%>
<!-- #include file="../Shared/common.inc" -->
<script language="javascript" src="exp_shared.js" runat="server"></script>

<%
    protectRequest.validateToken();

    var export_key = getQParamInt("export_key"); // 24=HV_DUMP,25=CPUA

    var params        = getSettings(export_key);
    user.checkAutorisation(params.functie_key);

    params.custid     = customerId;
    params.compress   = getFParamInt("compress", params.compress?1:0) == 1; // Je mag in de url overrulen
    params.preview    = getFParamInt("preview", 0) == 1; // dan geen _EXPORT_ achteraan aanroepen?
    params.tempFolder = shared.tempFolder() + "/";
    params.applrun    = new Date();
    params.fac_home   = Server.MapPath(rooturl + "/") + "/";
    params.customerId = customerId;


    try
    {
        var streamObj = expCreateStream(export_key, params);
    }
    catch (e)
    {
        var msg = "Export key {0} failed: {1}".format(export_key, e.description);
        __DoLog(e.description + "<br>" + msg);
        Response.clear;
        Response.contenttype = "text/html";
        // De content-disposition krijgen we niet echt weg
        Response.Write(msg);
        Response.End;
    }
    if (!streamObj.streamData)
        shared.simpel_page(L("lcl_export_nodata"));

    Response.clear;
    streamObj.streamData.Type = 1; // verder als adTypeBinary
    if (params.compress)
    {
        Response.contenttype = 'application/zip';
    }
    else
    {
        if (params.charset.toUpperCase() == 'UTF-8')
        {
            Session.Codepage = 65001;
            Response.Charset = 'utf-8'; // Alleen voor de header var, we doen zo een binary write
        }
        Response.contenttype = "application/octet-stream";
    }
    Response.AddHeader("content-disposition", "attachment; filename= \"" + streamObj.exportFile + "\"");
    try
    {
        Response.BinaryWrite(streamObj.streamData.Read());
    }
    catch (e)
    {   // Meestal te groot
        var msg = L("lcl_export_oversized").format(params.code, export_key, streamObj.streamData.Size); // S("maxdownloadsize")
        __DoLog(e.description + "<br>" + msg);
        Response.clear;
        Response.contenttype = "text/html";
        // De content-disposition krijgen we niet echt weg
        Response.Write(msg);
    }

// Komen uit wsf_shared.js
function Hexify(p_path)
{
    var oStream = new ActiveXObject("ADODB.Stream");
    oStream.charset = "Windows-1252";
    oStream.Open();
    oStream.Type = 2; // adTypeText

    oZIP = new ActiveXObject("SLNKDWF.Zip");
    oZIP.FileToHexStream(p_path, oStream);

    oStream.Position = 0;
    ReadBytes = 131072; // optimaal volgens http://support.microsoft.com/kb/280067
    var result = "";
    while (!oStream.EOS)
    {
        result = result + oStream.ReadText(ReadBytes);
    }

    return result;
}

function Base64fy(p_filename)
{
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var base64="";
    if (fso.FileExists(p_filename))
    {
        var bytes = ReadBinaryFile(p_filename);
        var base64 = encodeBase64(bytes);
    }
    return base64;
}

function ReadBinaryFile (p_filename)
{
    var oStream = new ActiveXObject("ADODB.Stream");
    oStream.Open();
    oStream.Type = 1; // adTypeBinary
    oStream.LoadFromFile(p_filename);
    return oStream.Read;
}

function encodeBase64(p_bytes){
  var l_xml=new ActiveXObject("Msxml2.DOMDocument.6.0");
  // Create temporary node with Base64 data type
  var xml_element = l_xml.createElement("tmp");
  xml_element.dataType = "bin.base64";
  // Set bytes, get encoded String
  xml_element.nodeTypedValue = p_bytes
  var encodeBase64 = xml_element.text
  return encodeBase64;
}

%>
<% ASPPAGE_END(); %>
