using System; using System.Collections.Generic; using System.IO; using System.Linq; using DWSIM.Interfaces; using LiteDB; namespace DWSIM.FileStorage { /// /// File Database Provider for DWSIM, based on LiteDB. /// public class FileDatabaseProvider : IFileDatabaseProvider { private MemoryStream DBMem; private LiteDatabase DB; /// /// /// /// public LiteDatabase GetDatabaseObject() { return DB; } private static MemoryStream ReadStream(string path) { using (var temp = new MemoryStream(File.ReadAllBytes(path))) { var ms = new MemoryStream(); temp.CopyTo(ms); return ms; } } private bool IsDBLoaded = false; /// /// Checks if the database is loaded. /// public bool IsDatabaseLoaded { get { return IsDBLoaded; } } /// /// Checks if the given file exists in the database. /// /// Filename to search. /// True if the file exists. public bool CheckIfExists(string filename) { var file = DB.FileStorage.FindById(Path.GetFileName(filename)); return file != null; } /// /// Exports the embedded database to a file. /// /// Filename to export the database to. public void ExportDatabase(string filepath) { DB.Checkpoint(); File.WriteAllBytes(filepath, DBMem.ToArray()); } /// /// Exports a file/item from the database to a file in the filesystem. /// /// file item to export. /// Path with filename to save the item to. public void ExportFile(string filename, string exportpath) { var file = DB.FileStorage.FindById(Path.GetFileName(filename)); if (file != null) { if (!File.Exists(exportpath)) file.SaveAs(exportpath); } else { throw new Exception("File not found"); } } /// /// Returns a stored image file as a System.Drawing.Image object. /// /// Image file name in the database. /// A System.Drawing.Image object. public System.Drawing.Image GetFileAsImage(string filename) { var fname = Path.GetFileName(filename); var file = DB.FileStorage.FindById(fname); if (file != null) { using (var ms = new MemoryStream()) { file.CopyTo(ms); ms.Position = 0; var image = System.Drawing.Image.FromStream(ms); return image; } } else { return null; } } /// /// Returns a stored image file as an Eto.Drawing.Bitmap object. /// /// Image file name in the database. /// An Eto.Drawing.Bitmap object. public Eto.Drawing.Bitmap GetFileAsEtoBitmap(string filename) { var fname = Path.GetFileName(filename); var file = DB.FileStorage.FindById(fname); if (file != null) { using (var ms = new MemoryStream()) { file.CopyTo(ms); ms.Position = 0; var image = new Eto.Drawing.Bitmap(ms); return image; } } else { return null; } } /// /// Returns the file contents as plain text. /// /// File/Item name in the database. /// The file contents as plain text. public string GetFileAsText(string filename) { var file = DB.FileStorage.FindById(Path.GetFileName(filename)); if (file != null) { var tmpfile = DWSIM.SharedClasses.Utility.GetTempFileName(); file.SaveAs(tmpfile); var text = File.ReadAllText(tmpfile); File.Delete(tmpfile); return text; } else { return null; } } /// /// Gets a list of all files in the database. /// /// public List GetFiles() { var files = DB.FileStorage.FindAll(); return files.Select(f => f.Filename).ToList(); } /// /// Gets the contents of a file as a memory stream. /// /// File/Item name /// A MemoryStream object containing the item. public MemoryStream GetFileStream(string filename) { var file = DB.FileStorage.FindById(Path.GetFileName(filename)); if (file != null) { var ms = new MemoryStream(); file.CopyTo(ms); ms.Position = 0; return ms; } else { return null; } } /// /// For internal use only. /// /// public void LoadDatabase(string dbpath) { ReleaseDatabase(); DBMem = ReadStream(dbpath); DBMem.Position = 0; DB = new LiteDatabase(DBMem); IsDBLoaded = true; } /// /// Stores a file in the database. /// /// Path of the file to import. public void PutFile(string filepath) { DB.FileStorage.Upload(Path.GetFileName(filepath), filepath); DB.Checkpoint(); } /// /// Stores a file in the database. /// /// Path of the file to import. /// Internal name of the stored file. public void PutFile(string filepath, string internalfilename) { DB.FileStorage.Upload(internalfilename, filepath); DB.Checkpoint(); } /// /// Stores a file in the database. /// /// /// public void PutFile(Stream stream, string filename) { DB.FileStorage.Upload(filename, filename, stream); DB.Checkpoint(); } /// /// For internal use only. /// public void ReleaseDatabase() { if (DB != null) DB?.Dispose(); if (DBMem != null) DBMem?.Dispose(); IsDBLoaded = false; } /// /// Removes a file from the database. /// /// File/Item name. public void DeleteFile(string filename) { var file = DB.FileStorage.FindById(Path.GetFileName(filename)); if (file != null) { DB.FileStorage.Delete(file.Id); DB.Rebuild(); DB.Checkpoint(); } else { throw new Exception("File not found"); } } /// /// For internal use only. /// public void CreateDatabase() { ReleaseDatabase(); DBMem = new MemoryStream(); DB = new LiteDatabase(DBMem); IsDBLoaded = true; } /// /// Gets the database size in kilobytes. /// /// DB size in KB. public int GetSizeinKB() { return (int)DBMem.Length / 1024; } /// /// /// /// /// public void ExportFile(string filename, MemoryStream stream) { var file = DB.FileStorage.FindById(Path.GetFileName(filename)); if (file != null) { file.CopyTo(stream); stream.Position = 0; } else { throw new Exception("File not found"); } } } }