예제 #1
0
        private Container GetContainer(Tenant tenant, string name)
        {
            try
            {
                using (var context = new gFileSystemEntities(ConfigHelper.ConnectionString))
                {
                    var retval = context.Container
                                 .Where(x => x.TenantId == tenant.TenantID && x.Name == name)
                                 .FirstOrDefault();

                    var containerItem = context.Container
                                        .Where(x => x.TenantId == tenant.TenantID && x.Name == name)
                                        .FirstOrDefault();

                    if (containerItem == null)
                    {
                        containerItem = new Container {
                            TenantId = tenant.TenantID, Name = name
                        };
                        context.AddItem(containerItem);
                        context.SaveChanges();
                        Directory.CreateDirectory(Path.Combine(ConfigHelper.StorageFolder, tenant.UniqueKey.ToString(), containerItem.UniqueKey.ToString()));
                    }

                    return(containerItem);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #2
0
        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;
            }
        }