public FileStash GetFileInfo(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)) { using (var context = new gFileSystemEntities(ConfigHelper.ConnectionString)) { return(context.FileStash .FirstOrDefault(x => x.TenantID == tenant.TenantID && x.Path == fileName && x.Container.Name == container)); } } } catch (Exception ex) { Logger.LogError(ex); throw; } }
/// <summary> /// Get a list of all containers for the specified tenant. /// The start pattern can be used to narrow the search based on start of the name /// </summary> /// <param name="tenantID"></param> /// <param name="startPattern"></param> /// <returns></returns> public List <string> GetContainerList(Guid tenantID, string startPattern = null) { try { using (var q = new ReaderLock(tenantID, "")) { if (string.IsNullOrEmpty(startPattern)) { startPattern = null; } else { startPattern = startPattern.Replace("*", string.Empty); } var tenant = GetTenant(tenantID); using (var context = new gFileSystemEntities(ConfigHelper.ConnectionString)) { return(context.Container .Where(x => x.TenantId == tenant.TenantID && (startPattern == null || x.Name.StartsWith(startPattern))) .Select(x => x.Name) .ToList()); } } } catch (Exception ex) { Logger.LogError(ex); throw; } }
/// <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; } }