<% /*
    $Revision$
    $Id$

    File:           model_files.inc

    Description:    Files model.
    Parameters:
    Context:        Dit model staat toe diverse bestanden met codes van flexfiles.inc
                    via API2 op te vragen en eventueel toe te voegen

    Notes:
*/

function model_files(pmodule)
{
    this.module = pmodule;
    this.table = "files";
    this.records_name = "files";
    this.record_name = "file";
    this.autfunction = "WEB_FACTAB";

    this.fields =    { "name": { typ: "varchar", label: "Name" },
                       "date": { typ: "date",    label: "Module" },
                       "size": { typ: "number",  label: "Size" }
                     }

    this.REST_GET = function _GET(params)
    {
        // TODO: meer autorisatie?
        // TODO:
        var getProps = { api2name: "files", module: this.module, nodigest: !params.dataonly }; // alleen bij dataonly doen we iets met de digest
        if (params.filter.filename)
            getProps.getFile = params.filter.filename;
        else
        {
            getProps.getFiles = true;
        }
        if (params.filter.iecode)
            getProps.iecode = params.filter.iecode

        var fileprops = flexProps(this.module, null, null, null, getProps);
        user.checkAutorisation(fileprops.autfunctionapiread || this.autfunction);

        if (getProps.getFiles) // dan moet je wel browse rechten hebben
            user.checkAutorisation(fileprops.autfunctionapibrowse || this.autfunction);

        if (params.filter.filename)
        {
            if (!fileprops.files.length)
            {
                Response.Status = "404 Not Found";
                Response.End;
            }
            if (params.dataonly)
            {
                var file = fileprops.files[0];
                return [{ "name": file.name,
                          "date": file.date,
                          "size": file.size,
                          "content_url": file.deepurl,
                          "digest": file.digest // kan aanroeper controleren of het goed is overgekomen
                       }];
            }
            StreamFile(fileprops.AttachPath,
                       params.filter.filename,
                       { mime: fileprops.files[0].mimetype,
                        pcontentdp: "none"}); // geen content disposition
        }
        else
        {
            var result = [];
            for (var i = 0; i < fileprops.files.length; i++)
            {
                var file = fileprops.files[i];
                result.push({ "name": file.name,
                              "date": file.date,
                              "size": file.size,
                              "content_url": file.deepurl
                            });
            }
            return result;
        }
    }
    this.REST_PUT = this.REST_POST = function _PUTPOST(params, bodyStream)
    {
        var getProps = { api2name: "files", module: this.module };
        if (params.filter.iecode)
            getProps.iecode = params.filter.iecode
        var flexparams = flexProps(this.module, null, null, null, getProps);
        user.checkAutorisation(flexparams.autfunctionapiwrite || this.autfunction);

        var safefilename = safe.filename(params.filter.filename);
        if (!flexparams.isAllowedName(safefilename))
            api2.error(422, L("lcl_shared_file_ext_not_allowed"));

        __Log("Creating folder {0}".format(flexparams.AttachPath));
        CreateFullPath(flexparams.AttachPath);
        __Log("Saving datastream file to {0}".format(flexparams.AttachPath + safefilename));
        bodyStream.SaveToFile(safe.UNC(flexparams.AttachPath + safefilename), 2); // adSaveCreateOverWrite

        return { filename: safefilename };
    }

    this.disabled_REST_DELETE = function (params, the_key) /* remove file */
    {
        // TODO?
    }
}
%>