Exemplo n.º 1
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;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize a file for upload
        /// </summary>
        /// <returns>Token that is used to append data in chunk</returns>
        public FileDataInfo SendFileStart(FileInformation block)
        {
            if (block == null)
            {
                throw new Exception("Fie information not set");
            }
            if (block.TenantID == Guid.Empty)
            {
                throw new Exception("Invalid Tenant ID");
            }
            if (string.IsNullOrEmpty(block.Container))
            {
                throw new Exception("Invalid container");
            }
            if (string.IsNullOrEmpty(block.FileName))
            {
                throw new Exception("Invalid file name");
            }
            if (block.Size < 0)
            {
                throw new Exception("Invalid file size");
            }

            byte[] tenantKey = null;
            using (var fm = new FileManager())
            {
                var tenant = fm.GetTenant(block.TenantID);
                tenantKey = tenant.Key.Decrypt(fm.MasterKey, fm.IV);
            }

            lock (_fileUploadCache)
            {
                var cache = new FilePartCache
                {
                    TenantID     = block.TenantID,
                    Container    = block.Container,
                    FileName     = block.FileName,
                    CRC          = block.CRC,
                    Size         = block.Size,
                    Index        = 0,
                    CreatedTime  = block.CreatedTime,
                    ModifiedTime = block.ModifiedTime,
                    TenantKey    = tenantKey,
                };

                //Add to part cache
                if (_fileUploadCache.Contains(cache.Key))
                {
                    throw new Exception("File concurrency error");
                }
                try
                {
                    using (var fm = new FileManager())
                    {
                        fm.RemoveFile(block.TenantID, block.Container, block.FileName);
                    }

                    _fileUploadCache.Add(cache.Key);
                    _fileUploadPartCache.TryAdd(cache.ID, cache);

                    using (var fm = new FileManager())
                    {
                        var header = new FileHeader
                        {
                            DataKey          = cache.OneTimeKey,
                            EncryptedDataKey = cache.OneTimeKey.Encrypt(cache.TenantKey, FileEngine.DefaultIVector),
                            TenantKey        = cache.TenantKey,
                        };

                        //Create the data folder
                        var dataPath = Path.Combine(ConfigHelper.WorkFolder, cache.ID.ToString());
                        Directory.CreateDirectory(dataPath);
                        cache.TempDataFile  = Path.Combine(dataPath, "out");
                        cache.EncryptStream = Extensions.OpenEncryptStream(cache.TempDataFile, fm.IV, header);
                    }

                    return(new FileDataInfo
                    {
                        Token = cache.ID,
                        Size = cache.Size,
                        CreatedTime = block.CreatedTime,
                        ModifiedTime = block.ModifiedTime,
                    });
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                    throw;
                }
            }
        }