Jason Phang
Well-known member
- Joined
- Aug 13, 2019
- Messages
- 46
- Programming Experience
- Beginner
I am currently doing a data deduplication program in C# and so far, I am able to upload, download, select and delete files. However, i would want to generate the hash value of the files in the table of my application. However, there is no hash that is displayed, hence I am unsure if I am doing the hashing code right. I just want to show like the MD5 hash of the files in the MD5 column.
C#:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Download;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Web;
using System.Security.Cryptography;
namespace GoogleDriveRestApi_v3.Models
{
public class GoogleDriveFilesRepository
{
public static string[] Scopes = { DriveService.Scope.Drive };
//create Drive API service.
public static DriveService GetService()
{
//get Credentials from client_secret.json file
UserCredential credential;
using (var stream = new FileStream(@"D:\client_secret.json", FileMode.Open, FileAccess.Read))
{
String FolderPath = @"D:\";
String FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(FilePath, true)).Result;
}
//create Drive API service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveRestAPI-v3",
});
return service;
}
//get all files from Google Drive.
public static List<GoogleDriveFiles> GetDriveFiles()
{
DriveService service = GetService();
// define parameters of request.
FilesResource.ListRequest FileListRequest = service.Files.List();
//listRequest.PageSize = 10;
//listRequest.PageToken = 10;
FileListRequest.Fields = "nextPageToken, files(id, name, size, version, createdTime)";
//get file list.
IList<Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files;
List<GoogleDriveFiles> FileList = new List<GoogleDriveFiles>();
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
GoogleDriveFiles File = new GoogleDriveFiles
{
Id = file.Id,
Name = file.Name,
Size = file.Size,
Version = file.Version,
CreatedTime = file.CreatedTime,
HashFiles = file.Md5Checksum,
// HashFile = file.Md5Checksum,
};
FileList.Add(File);
}
}
return FileList;
}
//Get Hash Value
public static void GenerateHash(HttpPostedFileBase file)
{
string directory = Path.Combine(HttpContext.Current.Server.MapPath("~/GoogleDriveFiles"),
Path.GetFileName(file.FileName));
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(directory))
{
var hash = md5.ComputeHash(stream);
var data = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
}
//file Upload to the 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));
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 System.IO.FileStream(path, System.IO.FileMode.Open))
{
request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
request.Fields = "id";
request.Upload();
}
}
}
//Download file from Google Drive by fileId.
public static string DownloadGoogleFile(string fileId)
{
DriveService service = GetService();
string FolderPath = System.Web.HttpContext.Current.Server.MapPath("/GoogleDriveFiles/");
FilesResource.GetRequest request = service.Files.Get(fileId);
string FileName = request.Execute().Name;
string FilePath = System.IO.Path.Combine(FolderPath, FileName);
MemoryStream stream1 = new MemoryStream();
// Add a handler which will be notified on progress changes.
// It will notify on each chunk download and when the
// download is completed or failed.
request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
{
switch (progress.Status)
{
case DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
SaveStream(stream1, FilePath);
break;
}
case DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
request.Download(stream1);
return FilePath;
}
// file save to server path
private static void SaveStream(MemoryStream stream, string FilePath)
{
using (System.IO.FileStream file = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite))
{
stream.WriteTo(file);
}
}
//Delete file from the Google drive
public static void DeleteFile(GoogleDriveFiles files)
{
DriveService service = GetService();
try
{
// Initial validation.
if (service == null)
throw new ArgumentNullException("service");
if (files == null)
throw new ArgumentNullException(files.Id);
// Make the request.
service.Files.Delete(files.Id).Execute();
}
catch (Exception ex)
{
throw new Exception("Request Files.Delete failed.", ex);
}
}
}
}
Last edited by a moderator: