Last active
December 20, 2019 19:43
-
-
Save BaileyMillerSSI/ccd53f68ec2ad097c97ad0b23daef1e1 to your computer and use it in GitHub Desktop.
Added example program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Helpers | |
{ | |
public static async Task<ObjectId> UploadFile(this GridFSBucket bucket, string localPath ,IProgress<BasicProgress> progress) | |
{ | |
byte[] buffer = new byte[255000]; | |
ObjectId id; | |
using (var localStream = File.OpenRead(localPath)) | |
using (var uploadStream = await bucket.OpenUploadStreamAsync(Path.GetFileName(localPath))) | |
{ | |
id = uploadStream.Id; | |
int read; | |
while ((read = await localStream.ReadAsync(buffer, 0, buffer.Length)) > 0) | |
{ | |
if (progress != null) | |
{ | |
progress.Report(new BasicProgress(localStream.Position, localStream.Length, id, localPath, ProgressType.Upload)); | |
} | |
await uploadStream.WriteAsync(buffer, 0, read); | |
if (progress != null) | |
{ | |
progress.Report(new BasicProgress(localStream.Position, localStream.Length, id, localPath, ProgressType.Upload)); | |
} | |
} | |
if (progress != null) | |
{ | |
progress.Report(new BasicProgress(localStream.Position, localStream.Length, id, localPath, ProgressType.Complete)); | |
} | |
} | |
return id; | |
} | |
public static async Task DownloadFile(this GridFSBucket bucket, ObjectId id, string localPath, IProgress<BasicProgress> progress) | |
{ | |
byte[] buffer = new byte[1024]; | |
Directory.CreateDirectory(Path.GetDirectoryName(localPath)); | |
using (var localStream = File.Create(localPath)) | |
using (var downloadStream = await bucket.OpenDownloadStreamAsync(id)) | |
{ | |
int read; | |
while ((read = await downloadStream.ReadAsync(buffer, 0, buffer.Length)) > 0) | |
{ | |
await localStream.WriteAsync(buffer, 0, read); | |
if (progress != null) | |
{ | |
progress.Report(new BasicProgress(localStream.Position, downloadStream.Length, id, localPath, ProgressType.Download)); | |
} | |
} | |
if (progress != null) | |
{ | |
progress.Report(new BasicProgress(localStream.Position, localStream.Length, id, localPath, ProgressType.Complete)); | |
} | |
} | |
} | |
} | |
public enum ProgressType | |
{ | |
Upload, | |
Download, | |
Complete | |
} | |
public class BasicProgress | |
{ | |
public ProgressType Type { get; } | |
public FileInfo FileInfo { get; } | |
public ObjectId Id { get; set; } | |
public long TotalSize { get; } | |
public long CurrentSize { get; } | |
public double Percentage | |
{ | |
get => ((double)CurrentSize / (double)TotalSize) * 100; | |
} | |
public string Display { get => $"{RoundedPercentage(2)}%"; } | |
public double RoundedPercentage(int decimalCount) | |
{ | |
return Math.Round(Percentage, decimalCount); | |
} | |
public BasicProgress(long current, long total, ObjectId id, string path, ProgressType type) | |
{ | |
this.CurrentSize = current; | |
this.TotalSize = total; | |
this.Id = id; | |
this.FileInfo = new FileInfo(path); | |
this.Type = type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
const string MongoDBConnectionString = ""; | |
static async Task Main(string[] args) | |
{ | |
try | |
{ | |
var progress = new Progress<BasicProgress>(); | |
progress.ProgressChanged += Progress_ProgressChanged; | |
MongoClient dbClient = new MongoClient(MongoDBConnectionString); | |
var database = dbClient.GetDatabase("Files"); | |
var bucket = new GridFSBucket(database); | |
var file = @"D:\OneDrive\Documents\CIT368-01\Presentation_1080.mp4"; | |
Console.WriteLine("Uploading File"); | |
var id = await bucket.UploadFile(file, progress); | |
Console.WriteLine("Downloading File"); | |
await bucket.DownloadFile(id, $@"D:\OneDrive\Desktop\Test{Path.GetExtension(file)}", progress); | |
Console.WriteLine("Cleaning up"); | |
await bucket.DeleteAsync(id); | |
} | |
catch (Exception er) | |
{ | |
} | |
} | |
private static void Progress_ProgressChanged(object sender, BasicProgress e) | |
{ | |
switch (e.Type) | |
{ | |
case ProgressType.Upload: | |
Console.Title = $"Uploading from {e.FileInfo.Name} to '{e.Id}' | {e.Display}"; | |
break; | |
case ProgressType.Download: | |
Console.Title = $"Downloading from '{e.Id}' to {e.FileInfo.FullName} | {e.Display}"; | |
break; | |
default: | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment