47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using MongoDB.Driver.GridFS;
|
|
using System.IO;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Firelance
|
|
{
|
|
public class FirelanceGFS : IFlcMongoDBConnecton
|
|
{
|
|
private GridFSBucket gridFSBucket;
|
|
public void SetDatabaseInstance(IMongoDatabase db)
|
|
{
|
|
gridFSBucket = new GridFSBucket(db, new GridFSBucketOptions
|
|
{
|
|
BucketName = Collections.SaveGamesBucket
|
|
});
|
|
}
|
|
|
|
public async Task<string> UploadFile(string fileName, string location)
|
|
{
|
|
ObjectId fileId;
|
|
using (var fs = new FileStream(location, FileMode.Open))
|
|
{
|
|
fileId = await gridFSBucket.UploadFromStreamAsync(fileName, fs);
|
|
}
|
|
|
|
return fileId.ToString();
|
|
}
|
|
|
|
public async Task<bool> DownloadFile(ObjectId id, string saveLocation)
|
|
{
|
|
using (var fs = new FileStream(saveLocation, FileMode.Create))
|
|
{
|
|
await gridFSBucket.DownloadToStreamAsync(id, fs);
|
|
}
|
|
|
|
return VerifyDownloadedFile(saveLocation);
|
|
}
|
|
|
|
public async Task DeleteFile(ObjectId id) { await gridFSBucket.DeleteAsync(id); }
|
|
|
|
private bool VerifyDownloadedFile(string path) { return File.Exists(path); }
|
|
}
|
|
}
|