4

I have an application that allows a user to upload a file, the file is scanned with Symantec protection engine before it is saved. The problem I'm encountering is that after the files are scanned with protection engine they have 0 bytes. I'm trying to come up with a solution to this problem.

I've tried the cloning solution mentioned here: Deep cloning objects, but my uploaded files are not all Serializable. I've also tried resetting the stream to 0 in the scan engine class before passing it back to be saved.

I have been in contact with Symantec and they said the custom connection class that was written for this application looks correct, and protection engine is not throwing errors.

I'm open to any solution to this problem.

Here is the code where the file is uploaded:

private void UploadFiles()
{
    System.Web.HttpPostedFile objFile;
    string strFilename = string.Empty;
    if (FileUpload1.HasFile)
    {

        objFile = FileUpload1.PostedFile;
        strFilename = FileUpload1.FileName;


        if (GetUploadedFilesCount() < 8)
        {
            if (IsDuplicateFileName(Path.GetFileName(objFile.FileName)) == false)
            {
                if (ValidateUploadedFiles(FileUpload1.PostedFile) == true)
                {
                    //stores full path of folder
                    string strFileLocation = CreateFolder();

                    //Just to know the uploading folder
                    mTransactionInfo.FileLocation = strFileLocation.Split('\\').Last();
                    if (ScanUploadedFile(objFile) == true)
                    {
                            SaveFile(objFile, strFileLocation);
                    }
                    else
                    {
                        lblErrorMessage.Visible = true;
                        if (mFileStatus != null)
                        { lblErrorMessage.Text = mFileStatus.ToString(); }

I can provide the connection class code if anyone needs that, but it is quite large.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
rogerdeuce
  • 1,471
  • 6
  • 31
  • 48

1 Answers1

8

You could take a copy of the filestream before you pass it into the scanning engine.

byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
    fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}

// pass the scanning engine
StreamScanRequest scan = requestManagerObj.CreateStreamScanRequest(Policy.DEFAULT);
//...

Update To copy the stream you can do this:

MemoryStream ms = new MemoryStream();
file.InputStream.CopyTo(ms);
file.InputStream.Position = ms.Position = 0;
Rebecca
  • 13,914
  • 10
  • 95
  • 136
  • For this solution, I would then save fileData when the protection engine determines the file is clean? or are you saying to pass the engine the fileData array? – rogerdeuce Jan 22 '15 at 16:43
  • As far as I am aware, you can pass a `Stream` to the engine using `CreateStreamScanRequest()`. The result from that scan.Finish() gives you a ScanResult I believe, not a file, so you would then still have a copy of the file bytes to save to disk or a cloud API (i.e. Amazon S3 or Rackspace Cloud files, etc). The point is that you want to take a copy of the `HttpPostedFile.InputStream`. Once you have copied that stream, you need to reset the position to zero. – Rebecca Jan 23 '15 at 10:01
  • I've been trying to implement your solution. The copy does work, but it more or less leaves me with the same problem, even when I do use .position=0 or a seekorigin.begin on the original httpPostedFile, when I pass it to scan engine it errors out. However the copy does save to file properly. Can you elaborate on how to reset the original HttpPostedFile position? @Junto – rogerdeuce Jan 26 '15 at 17:45
  • Working Fine. Thanks :) – Thulasiram Dec 13 '19 at 12:56