none
Compare MD5 hash value of new file with existing hash value of files ASP.Net RRS feed

  • Question

  • The program that I am writing now is able to generate the hash of the files and display it in a list when I run it. I would want to generate the hash value of the file when I upload and check it with the other hash value of the files before uploading. This is the code for my upload to Google Drive.

    public static void FileUpload(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                DriveService service = GetService();
    
                string path = Path.Combine(HttpContext.Current.Server.MapPath("~/GoogleDriveFiles"),Path.GetFileName(file.FileName));
                HashGenerator(path);
                compareHash(path);
               file.SaveAs(path);
    
                var FileMetaData = new Google.Apis.Drive.v3.Data.File();
                FileMetaData.Name = Path.GetFileName(file.FileName);
                FileMetaData.MimeType = MimeMapping.GetMimeMapping(path);
                FilesResource.CreateMediaUpload request; 
    
                    using (var stream = new FileStream(path, FileMode.Open))
                    {
                        request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
                        request.Fields = "id";
                        request.Upload();
                    }               
            }
        }

    These below are the codes for generating the hash of the file upon upload and comparing the hash of the files.

      public static string HashGenerator(string path)
        {
    
            using (var md5 = MD5.Create())
            {
                using (var stream = new FileStream(path, FileMode.Open))
                {
                    return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", null).ToLower();
                }
            }
        }
    
        public static void compareHash(string path)
        {
    
          DriveService service = GetService();
            FilesResource.ListRequest FileListRequest = service.Files.List();
            IList<Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files;
            List<GoogleDriveFiles> FileList = new List<GoogleDriveFiles>();
    
            foreach (var file in files)
            {
                if (file.Md5Checksum.Equals(path))
                {
                    throw new Exception("File already exists");
                }
    
            }
    
        }

    When i run the code, if it was a duplicated file, then the exception ("file already exists") will be thrown. Otherwise, if it is a new file, this error will be thrown. I am unsure how to be able to upload the file should the hash be a new hash.

    System.IO.FileNotFoundException: Could not find file 'C:\Users\jpvon\source\repos\GoogleDriveRestApi_v3\GoogleDriveRestApi_v3\GoogleDriveFiles\IC.pdf'.



    Jason

    Sunday, October 20, 2019 9:20 AM

All replies

  • Hi Jason Phang, 

    Thank you for posting here.

    According to your question, I have a question to confirm with you.

    Could you provide more details about your exception with which line of the code thrown the exception and some code about ‘GetService()’? It will help us to do the test.

    We are waiting for your udpdate.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 21, 2019 5:30 AM
  • Of course it does.  Look at the order of operations.  You try to generate the MD5 hash of the file before you have saved the file to disk.  That's never going to work.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, October 21, 2019 5:44 AM

  • string path = Path.Combine(HttpContext.Current.Server.MapPath("~/GoogleDriveFiles"),Path.GetFileName(file.FileName));
    HashGenerator(path);
    compareHash(path);
    file.SaveAs(path);

    From this code block, what I wanted to do is that, the code will get the Google Drive service first via the Google Drive API for

    DriveService service = GetService();

    Then, when the user wants to upload the file, the user would have to click the upload button which explains the second line of code. What i want to do next is that the code will generate the hash of the file and then compare the hash of the file. If there is a duplicated hash that exists in the Google Drive, then like an exception will be thrown like this. 

     if (file.Md5Checksum.Equals(path))
                {
                    throw new Exception("File already exists");
                }

    I would want to display a message box later but for now i purposely throw that so that i know that the code detected a duplicated file in the Google Drive. The problem that occur is that when its a new file that is supposed to be uploaded means that there was no prior of such files in the google drive, it throws this error. 

    System.IO.FileNotFoundException: Could not find file 'C:\Users\jpvon\source\repos\GoogleDriveRestApi_v3\GoogleDriveRestApi_v3\GoogleDriveFiles\IC.pdf'.
    But if it is a duplicated file, then the yellow page for the exception will be thrown.
    File already exists

    So, i am not sure whats wrong with my generate hash code. If you are still unclear what I meant, let me know. I will try to explain again.


    Jason


    • Edited by Jason Phang Monday, October 21, 2019 5:44 AM
    Monday, October 21, 2019 5:44 AM
  • Of course it does.  Look at the order of operations.  You try to generate the MD5 hash of the file before you have saved the file to disk.  That's never going to work.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Is there a way for me to generate the hash of the file when i click the upload button? I still do not quite get one thing which is if the system was able to check the hash of the duplicated file, then would not it able to check the hash of a new file that is not in the drive yet?

    Jason

    Monday, October 21, 2019 6:15 AM
  • Hi Jason Phang, 

    Since your problem occurs when uploading hash of the files to Google Drive and Google Drive is a third-party product for which we don't provide help. I suggest you post you question in Google Drive forums for more help.

    The C# forum is for C# related question, we discuss and ask the C# programming language, IDE, libraries, samples and tools here.

    Thank you for posting here.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 22, 2019 8:41 AM