<%@ Page Title="Virus Scan Integration" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" %>
<%@ Register Assembly="AjaxUploader" Namespace="AjaxUploader.Controls" TagPrefix="au" %>

<script runat="server">
    private readonly System.Collections.Generic.List<string> _cleanFiles = new System.Collections.Generic.List<string>();
    private readonly System.Collections.Generic.List<string> _rejectedFiles = new System.Collections.Generic.List<string>();

    protected void Uploader1_FileUploaded(object sender, AjaxUploader.Events.FileUploadedEventArgs e)
    {
        var fileName = e.FileName ?? "(unnamed)";
        var uploadService = new AjaxUploader.Services.UploadService();

        if (fileName.IndexOf("virus", StringComparison.OrdinalIgnoreCase) >= 0
            || fileName.IndexOf("eicar", StringComparison.OrdinalIgnoreCase) >= 0)
        {
            uploadService.DeleteFile(e.FileGuid);
            _rejectedFiles.Add(fileName + " - simulated malware signature match");
            return;
        }

        var cleanFolder = Server.MapPath("~/App_Data/ScannedClean");
        System.IO.Directory.CreateDirectory(cleanFolder);
        uploadService.CopyFile(e.FileGuid, System.IO.Path.Combine(cleanFolder, System.IO.Path.GetFileName(fileName)));
        _cleanFiles.Add(fileName);
    }

    protected void BtnScan_Click(object sender, EventArgs e)
    {
        if (_cleanFiles.Count == 0 && _rejectedFiles.Count == 0)
        {
            lblStatus.CssClass = "alert alert-warning mt-3";
            lblStatus.Text = "Upload one or more files first, then click Run Post-Upload Scan.";
        }
        else
        {
            lblStatus.CssClass = "alert alert-info mt-3";
            lblStatus.Text = "Scan finished: " + _cleanFiles.Count + " clean, " + _rejectedFiles.Count + " rejected.";
        }

        lblStatus.Visible = true;
        litClean.Text = RenderItems(_cleanFiles);
        litRejected.Text = RenderItems(_rejectedFiles);
    }

    private string RenderItems(System.Collections.Generic.IEnumerable<string> items)
    {
        var sb = new System.Text.StringBuilder();
        foreach (var item in items)
        {
            sb.Append("<div>");
            sb.Append(Server.HtmlEncode(item));
            sb.Append("</div>");
        }

        return sb.ToString();
    }
</script>

<asp:Content ID="TitleContent" ContentPlaceHolderID="TitleContent" runat="server">Virus Scan Integration - AjaxUploader v5.0 Demos</asp:Content>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderTitle" runat="server">Virus Scan Integration</asp:Content>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<div class="demo-card">
    <div class="demo-card-header">Virus Scan Integration</div>
    <div class="demo-card-body">
        <p class="demo-description">
            Hook into a virus scanning step after upload on postback. This sample simulates a scan
            and rejects filenames containing <code>virus</code> or <code>eicar</code>, while copying
            clean files into <code>App_Data/ScannedClean</code>.
        </p>

        <au:AjaxFileUpload ID="Uploader1" runat="server"
            AutoUpload="true"
            ShowProgress="true"
            ButtonText="Upload &amp; Scan"
            OnFileUploaded="Uploader1_FileUploaded" />

        <asp:Button ID="BtnScan" runat="server" Text="Run Post-Upload Scan"
            CssClass="btn btn-primary btn-sm mt-3" OnClick="BtnScan_Click" />
        <asp:Label ID="lblStatus" runat="server" Visible="false" />
        <div class="row mt-3">
            <div class="col-md-6">
                <div class="fw-semibold mb-2">Clean</div>
                <asp:Literal ID="litClean" runat="server" />
            </div>
            <div class="col-md-6">
                <div class="fw-semibold mb-2">Rejected</div>
                <asp:Literal ID="litRejected" runat="server" />
            </div>
        </div>

        <div class="code-panel">
            <pre><code>&lt;%-- ASPX markup --%&gt;
&lt;au:AjaxFileUpload ID="Uploader1" runat="server"
    AutoUpload="true"
    ShowProgress="true"
    OnFileUploaded="Uploader1_FileUploaded" /&gt;

&lt;%-- Code-behind: virus scanning in FileUploaded event --%&gt;
protected void Uploader1_FileUploaded(object sender,
    AjaxFileUploadEventArgs e)
{
    if (e.FileName.Contains("virus"))
    {
        new UploadService().DeleteFile(e.FileGuid);
        return;
    }

    new UploadService().CopyFile(
        e.FileGuid,
        Server.MapPath("~/App_Data/ScannedClean/" + e.FileName));
}
</code></pre>
        </div>

        <p class="mt-3 text-muted">
            Tip: rename a test file to include <code>virus</code> or <code>eicar</code> to see the
            rejection branch in action.
        </p>
    </div>
</div>

</asp:Content>
