public MainWindow(ILogger logger) { InitializeComponent(); this.logger = logger; this.stashContainer = new StashContainer(); this.CurrentlyOpenFileName = string.Empty; this.DataContext = this; // temp var stash = new FileStash() { Description = "asdasdqefwefsvsv", Name = "test stash" }; stash.Files.Add(new StashedFile() { Description = "asdasdererr2wwccs", LastRestorePath = "c:\\temp.txt", Name = "temp.txt", SourcePath = "c:\\temp.txt" }); stash.Files.Add(StashedFile.FromFileInfo(new System.IO.FileInfo(@"c:\Users\noti\Dropbox\Projects\FileStasher\FileStasher\packages.config"))); stash.Files.Add(StashedFile.FromFileInfo(new System.IO.FileInfo(@"c:\Users\noti\Dropbox\Projects\FileStasher\FileStasher\FileStasher.csproj"))); this.stashContainer.Stashes.Add(stash); this.stashContainer.Add(); this.CurrentlyOpenFileName = "c:\\temp.txt"; }
public bool SaveEncryptedFile(FilePartCache cache, string outFile) { if (string.IsNullOrEmpty(cache.Container)) { throw new Exception("The container must be set"); } var tenant = GetTenant(cache.TenantID); try { using (var q = new WriterLock(cache.TenantID, cache.Container + "|" + cache.FileName)) { //Create engine var tenantKey = tenant.Key.Decrypt(MasterKey, IV); using (var engine = new FileEngine(MasterKey, tenantKey, cache.TenantID, IV)) { using (var context = new gFileSystemEntities(ConfigHelper.ConnectionString)) { this.RemoveFile(tenant.UniqueKey, cache.Container, cache.FileName); var containerItem = GetContainer(tenant, cache.Container); var fiCipher = new FileInfo(outFile); var stash = new FileStash { Path = cache.FileName, TenantID = tenant.TenantID, Size = cache.Size, StorageSize = fiCipher.Length, ContainerId = containerItem.ContainerId, CrcPlain = cache.CRC, IsCompressed = false, FileCreatedTime = cache.CreatedTime, FileModifiedTime = cache.ModifiedTime, UniqueKey = cache.ID, }; context.AddItem(stash); context.SaveChanges(); //Move the cipher file to storage var destFile = GetFilePath(tenant.UniqueKey, containerItem.UniqueKey, stash); File.Move(outFile, destFile); } } return(true); } } catch (Exception ex) { Logger.LogError(ex); throw; } }
/// <summary> /// Removes a <see cref="FileStash"/> from the collection /// </summary> /// <param name="stash">The <see cref="FileStash"/> to remove</param> public void Remove(FileStash stash) { if (stash == null) { return; } if (this.Container.Stashes.Contains(stash)) { this.Container.Stashes.Remove(stash); } }
/// <summary> /// Updates a <see cref="FileStash"/> in the collection /// </summary> /// <param name="stash">The <see cref="FileStash"/> to update</param> public void Update(FileStash stash) { if (stash == null) { return; } var val = this.Container.Stashes.Where(s => s.Id == stash.Id).SingleOrDefault(); if (val != null) { val = stash; } }
private string GetFilePath(Guid tenantID, Guid containerID, FileStash stash) { try { return(Path.Combine(ConfigHelper.StorageFolder, tenantID.ToString(), containerID.ToString(), stash.UniqueKey.ToString())); } catch (Exception ex) { throw; } }
/// <summary> /// Adds a <see cref="FileStash"/> to the current collection /// </summary> /// <param name="stash">THe <see cref="FileStash"/> to add</param> public void Add(FileStash stash) { if (stash == null) { return; } if (!this.Container.Stashes.Contains(stash)) { this.Container.Stashes.Add(stash); } else { this.Update(stash); } }
/// <summary> /// Retrieves a file from storage for a tenant in the specified container /// using the filenme as the lookup key /// </summary> /// <param name="tenantID"></param> /// <param name="container"></param> /// <param name="fileName"></param> /// <returns></returns> public System.IO.Stream GetFile(Guid tenantID, string container, string fileName) { if (string.IsNullOrEmpty(container)) { throw new Exception("The container must be set"); } var tenant = GetTenant(tenantID); try { using (var q = new ReaderLock(tenantID, container + "|" + fileName)) { FileStash stash = null; using (var context = new gFileSystemEntities(ConfigHelper.ConnectionString)) { stash = context.FileStash .Include(x => x.Container) .FirstOrDefault(x => x.TenantID == tenant.TenantID && x.Path == fileName && x.Container.Name == container); //There is no file so return null if (stash == null) { return(null); } } //Create engine var tenantKey = tenant.Key.Decrypt(MasterKey, IV); string cipherFile = null; using (var engine = new FileEngine(MasterKey, tenantKey, tenantID, IV)) { engine.WorkingFolder = ConfigHelper.WorkFolder; cipherFile = GetFilePath(tenant.UniqueKey, stash.Container.UniqueKey, stash); return(engine.GetFileStream(cipherFile)); } //if (stash.IsCompressed) //{ // var unzipFile = plainText + ".uz"; // if (FileUtilities.UnzipFile(plainText, unzipFile)) // { // FileUtilities.WipeFile(plainText); // File.Move(unzipFile, plainText); // } //} //var crc = FileUtilities.FileCRC(plainText); ////Verify that the file is the same as when it was saved //if (crc != stash.CrcPlain) // throw new Exception("The file is corrupted!"); //return plainText; } } catch (Exception ex) { Logger.LogError(ex); throw; } }