コード例 #1
0
        public void Upload(string filePath,
                           string name,
                           IEnumerable <string> keywords,
                           string comment,
                           DigitalSignature digitalSignature,
                           int priority)
        {
            lock (this.ThisLock)
            {
                UploadItem item = new UploadItem();

                item.State                = UploadState.ComputeHash;
                item.Type                 = UploadType.Upload;
                item.Rank                 = 1;
                item.FilePath             = filePath;
                item.CompressionAlgorithm = CompressionAlgorithm.Xz;
                item.CryptoAlgorithm      = CryptoAlgorithm.Aes256;
                item.CorrectionAlgorithm  = CorrectionAlgorithm.ReedSolomon8;
                item.HashAlgorithm        = HashAlgorithm.Sha256;
                item.DigitalSignature     = digitalSignature;
                item.Seed                 = new Seed();
                item.Seed.Name            = name;
                item.Seed.Keywords.AddRange(keywords);
                item.Seed.CreationTime = DateTime.UtcNow;
                item.Seed.Comment      = comment;
                item.BlockLength       = 1024 * 1024 * 1;
                item.Priority          = priority;

                _settings.UploadItems.Add(item);
                _ids.Add(_id, item);

                _id++;
            }
        }
コード例 #2
0
        private void CheckState(UploadItem item)
        {
            lock (this.ThisLock)
            {
                foreach (var key in item.UploadKeys.ToArray())
                {
                    if (!_connectionsManager.IsUploadWaiting(key))
                    {
                        item.UploadedKeys.Add(key);
                        item.UploadKeys.Remove(key);
                    }
                }

                if (item.State == UploadState.Uploading)
                {
                    if (item.UploadKeys.Count == 0)
                    {
                        item.State = UploadState.Completed;

                        _settings.UploadedSeeds.Add(item.Seed.Clone());
                    }
                }
            }
        }
コード例 #3
0
        private void CheckState(UploadItem item)
        {
            lock (_thisLock)
            {
                if (item.State == UploadState.Uploading)
                {
                    foreach (var key in item.UploadKeys.ToArray())
                    {
                        if (!_connectionsManager.IsUploadWaiting(key))
                        {
                            item.UploadedKeys.Add(key);
                            item.UploadKeys.Remove(key);
                        }
                    }

                    if (item.UploadKeys.Count == 0)
                    {
                        item.State = UploadState.Completed;

                        _settings.UploadedSeeds.Add(item.Seed.Clone());
                    }
                }
            }
        }
コード例 #4
0
        public void Upload(string filePath,
            string name,
            IEnumerable<string> keywords,
            DigitalSignature digitalSignature,
            int priority)
        {
            lock (_thisLock)
            {
                var item = new UploadItem();

                item.State = UploadState.ComputeHash;
                item.Type = UploadType.Upload;
                item.Name = name;
                item.Keywords.AddRange(keywords);
                item.CreationTime = DateTime.UtcNow;
                item.Depth = 1;
                item.FilePath = filePath;
                item.CompressionAlgorithm = CompressionAlgorithm.Xz;
                item.CryptoAlgorithm = CryptoAlgorithm.Aes256;
                item.CorrectionAlgorithm = CorrectionAlgorithm.ReedSolomon8;
                item.HashAlgorithm = HashAlgorithm.Sha256;
                item.DigitalSignature = digitalSignature;
                item.BlockLength = 1024 * 1024 * 1;
                item.Priority = priority;

                _settings.UploadItems.Add(item);
                _idManager.Add(item);
            }
        }
コード例 #5
0
        public void Share(string filePath,
            string name,
            IEnumerable<string> keywords,
            DigitalSignature digitalSignature,
            int priority)
        {
            lock (_thisLock)
            {
                if (_settings.UploadItems
                    .Where(n => n.Type == UploadType.Share)
                    .Any(n => n.FilePath == filePath)) return;

                if (_cacheManager.Contains(filePath)) return;

                var item = new UploadItem();

                item.State = UploadState.ComputeHash;
                item.Type = UploadType.Share;
                item.Name = name;
                item.CreationTime = DateTime.UtcNow;
                item.Keywords.AddRange(keywords);
                item.Depth = 1;
                item.FilePath = filePath;
                item.CompressionAlgorithm = CompressionAlgorithm.Xz;
                item.CryptoAlgorithm = CryptoAlgorithm.Aes256;
                item.CorrectionAlgorithm = CorrectionAlgorithm.ReedSolomon8;
                item.HashAlgorithm = HashAlgorithm.Sha256;
                item.DigitalSignature = digitalSignature;
                item.BlockLength = 1024 * 1024 * 1;
                item.Priority = priority;

                _settings.UploadItems.Add(item);

                int id = _idManager.Add(item);
                _shareIdLink.Add(filePath, id);
            }
        }
コード例 #6
0
        public void Share(string filePath,
            string name,
            IEnumerable<string> keywords,
            string comment,
            DigitalSignature digitalSignature,
            int priority)
        {
            lock (this.ThisLock)
            {
                UploadItem item = new UploadItem();

                item.State = UploadState.ComputeHash;
                item.Type = UploadType.Share;
                item.Rank = 1;
                item.FilePath = filePath;
                item.CompressionAlgorithm = CompressionAlgorithm.Xz;
                item.CryptoAlgorithm = CryptoAlgorithm.Aes256;
                item.CorrectionAlgorithm = CorrectionAlgorithm.ReedSolomon8;
                item.HashAlgorithm = HashAlgorithm.Sha256;
                item.DigitalSignature = digitalSignature;
                item.Seed = new Seed();
                item.Seed.Name = name;
                item.Seed.Keywords.AddRange(keywords);
                item.Seed.CreationTime = DateTime.UtcNow;
                item.Seed.Comment = comment;
                item.BlockLength = 1024 * 1024 * 1;
                item.Priority = priority;

                _settings.UploadItems.Add(item);
                _ids.Add(_id, item);

                List<int> idList = null;

                if (!_shareLink.TryGetValue(filePath, out idList))
                {
                    idList = new List<int>();
                    _shareLink[filePath] = idList;
                }

                idList.Add(_id);

                _id++;
            }
        }
コード例 #7
0
        private void EncodeManagerThread()
        {
            for (; ;)
            {
                Thread.Sleep(1000 * 1);
                if (this.EncodeState == ManagerState.Stop)
                {
                    return;
                }

                UploadItem item = null;

                try
                {
                    lock (this.ThisLock)
                    {
                        if (_settings.UploadItems.Count > 0)
                        {
                            item = _settings.UploadItems
                                   .Where(n => n.State == UploadState.ComputeHash || n.State == UploadState.Encoding || n.State == UploadState.ParityEncoding)
                                   .Where(n => n.Priority != 0)
                                   .OrderBy(n => - n.Priority)
                                   .Where(n => !_workingPaths.Contains(n.FilePath))
                                   .FirstOrDefault();

                            if (item != null)
                            {
                                _workingPaths.Add(item.FilePath);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    return;
                }

                if (item == null)
                {
                    continue;
                }

                try
                {
                    if (item.Type == UploadType.Upload)
                    {
                        if (item.Groups.Count == 0 && item.Keys.Count == 0)
                        {
                            item.State = UploadState.Encoding;

                            KeyCollection keys      = null;
                            byte[]        cryptoKey = null;

                            try
                            {
                                using (FileStream stream = new FileStream(item.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                                    using (ProgressStream hashProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                    {
                                        isStop = (this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));

                                        item.EncodingBytes = Math.Min(readSize, stream.Length);
                                    }, 1024 * 1024, true))
                                        using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                        {
                                            isStop = (this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));

                                            item.EncodingBytes = Math.Min(readSize, stream.Length);
                                        }, 1024 * 1024, true))
                                        {
                                            item.EncodeBytes = stream.Length;
                                            item.Seed.Length = stream.Length;

                                            if (item.Seed.Length == 0)
                                            {
                                                throw new InvalidOperationException("Stream Length");
                                            }

                                            item.State = UploadState.ComputeHash;

                                            if (item.HashAlgorithm == HashAlgorithm.Sha256)
                                            {
                                                cryptoKey = Sha256.ComputeHash(hashProgressStream);
                                            }

                                            stream.Seek(0, SeekOrigin.Begin);
                                            item.EncodingBytes = 0;

                                            item.State = UploadState.Encoding;
                                            keys       = _cacheManager.Encoding(encodingProgressStream, item.CompressionAlgorithm, item.CryptoAlgorithm, cryptoKey, item.BlockLength, item.HashAlgorithm);
                                        }
                            }
                            catch (StopIoException)
                            {
                                continue;
                            }

                            lock (this.ThisLock)
                            {
                                foreach (var key in keys)
                                {
                                    item.UploadKeys.Add(key);
                                    item.LockedKeys.Add(key);
                                }

                                item.EncodingBytes = 0;
                                item.EncodeBytes   = 0;

                                item.CryptoKey = cryptoKey;
                                item.Keys.AddRange(keys);
                            }
                        }
                        else if (item.Groups.Count == 0 && item.Keys.Count == 1)
                        {
                            lock (this.ThisLock)
                            {
                                item.Seed.Rank = item.Rank;
                                item.Seed.Key  = item.Keys[0];
                                item.Keys.Clear();

                                item.Seed.CompressionAlgorithm = item.CompressionAlgorithm;

                                item.Seed.CryptoAlgorithm = item.CryptoAlgorithm;
                                item.Seed.CryptoKey       = item.CryptoKey;

                                if (item.DigitalSignature != null)
                                {
                                    item.Seed.CreateCertificate(item.DigitalSignature);
                                }

                                item.UploadKeys.Add(item.Seed.Key);

                                foreach (var key in item.UploadKeys)
                                {
                                    _connectionsManager.Upload(key);
                                }

                                this.CheckState(item);

                                _cacheManager.SetSeed(item.Seed.Clone(), item.Indexes.Select(n => n.Clone()).ToArray());
                                item.Indexes.Clear();

                                foreach (var key in item.LockedKeys)
                                {
                                    _cacheManager.Unlock(key);
                                }

                                item.LockedKeys.Clear();

                                item.State = UploadState.Uploading;
                            }
                        }
                        else if (item.Keys.Count > 0)
                        {
                            item.State = UploadState.ParityEncoding;

                            item.EncodeBytes = item.Groups.Sum(n =>
                            {
                                long sumLength = 0;

                                for (int i = 0; i < n.InformationLength; i++)
                                {
                                    if (_cacheManager.Contains(n.Keys[i]))
                                    {
                                        sumLength += (long)_cacheManager.GetLength(n.Keys[i]);
                                    }
                                }

                                return(sumLength);
                            }) + item.Keys.Sum(n =>
                            {
                                if (_cacheManager.Contains(n))
                                {
                                    return((long)_cacheManager.GetLength(n));
                                }

                                return(0);
                            });

                            var   length = Math.Min(item.Keys.Count, 128);
                            var   keys   = new KeyCollection(item.Keys.Take(length));
                            Group group  = null;

                            try
                            {
                                group = _cacheManager.ParityEncoding(keys, item.HashAlgorithm, item.BlockLength, item.CorrectionAlgorithm, (object state2) =>
                                {
                                    return(this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));
                                });
                            }
                            catch (StopException)
                            {
                                continue;
                            }

                            lock (this.ThisLock)
                            {
                                foreach (var key in group.Keys.Skip(group.InformationLength))
                                {
                                    item.UploadKeys.Add(key);
                                    item.LockedKeys.Add(key);
                                }

                                item.Groups.Add(group);

                                item.EncodingBytes = item.Groups.Sum(n =>
                                {
                                    long sumLength = 0;

                                    for (int i = 0; i < n.InformationLength; i++)
                                    {
                                        if (_cacheManager.Contains(n.Keys[i]))
                                        {
                                            sumLength += (long)_cacheManager.GetLength(n.Keys[i]);
                                        }
                                    }

                                    return(sumLength);
                                });

                                item.Keys.RemoveRange(0, length);
                            }
                        }
                        else if (item.Groups.Count > 0 && item.Keys.Count == 0)
                        {
                            item.State = UploadState.Encoding;

                            var index = new Index();
                            index.Groups.AddRange(item.Groups);
                            index.CompressionAlgorithm = item.CompressionAlgorithm;
                            index.CryptoAlgorithm      = item.CryptoAlgorithm;
                            index.CryptoKey            = item.CryptoKey;

                            item.Indexes.Add(index);

                            byte[]        cryptoKey = null;
                            KeyCollection keys      = null;

                            try
                            {
                                using (var stream = index.Export(_bufferManager))
                                    using (ProgressStream hashProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                    {
                                        isStop = (this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));

                                        item.EncodingBytes = Math.Min(readSize, stream.Length);
                                    }, 1024 * 1024, true))
                                        using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                        {
                                            isStop = (this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));

                                            item.EncodingBytes = Math.Min(readSize, stream.Length);
                                        }, 1024 * 1024, true))
                                        {
                                            item.EncodeBytes = stream.Length;

                                            item.State = UploadState.ComputeHash;

                                            if (item.HashAlgorithm == HashAlgorithm.Sha256)
                                            {
                                                cryptoKey = Sha256.ComputeHash(hashProgressStream);
                                            }

                                            stream.Seek(0, SeekOrigin.Begin);
                                            item.EncodingBytes = 0;

                                            item.State = UploadState.Encoding;
                                            keys       = _cacheManager.Encoding(encodingProgressStream, item.CompressionAlgorithm, item.CryptoAlgorithm, cryptoKey, item.BlockLength, item.HashAlgorithm);
                                        }
                            }
                            catch (StopIoException)
                            {
                                continue;
                            }

                            lock (this.ThisLock)
                            {
                                foreach (var key in keys)
                                {
                                    item.UploadKeys.Add(key);
                                    item.LockedKeys.Add(key);
                                }

                                item.EncodingBytes = 0;
                                item.EncodeBytes   = 0;

                                item.CryptoKey = cryptoKey;
                                item.Keys.AddRange(keys);
                                item.Rank++;
                                item.Groups.Clear();
                            }
                        }
                    }
                    else if (item.Type == UploadType.Share)
                    {
                        if (item.Groups.Count == 0 && item.Keys.Count == 0)
                        {
                            item.State = UploadState.ComputeHash;

                            KeyCollection keys = null;

                            try
                            {
                                using (FileStream stream = new FileStream(item.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                                    using (ProgressStream hashProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                    {
                                        isStop = (this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));

                                        item.EncodingBytes = Math.Min(readSize, stream.Length);
                                    }, 1024 * 1024, true))
                                    {
                                        item.EncodeBytes = stream.Length;
                                        item.Seed.Length = stream.Length;

                                        if (item.Seed.Length == 0)
                                        {
                                            throw new InvalidOperationException("Stream Length");
                                        }

                                        keys = _cacheManager.Share(hashProgressStream, stream.Name, item.HashAlgorithm, item.BlockLength);
                                    }
                            }
                            catch (StopIoException)
                            {
                                continue;
                            }

                            if (keys.Count == 1)
                            {
                                lock (this.ThisLock)
                                {
                                    item.EncodingBytes = 0;
                                    item.EncodeBytes   = 0;

                                    item.Keys.Add(keys[0]);

                                    item.State = UploadState.Encoding;
                                }
                            }
                            else
                            {
                                var groups = new List <Group>();

                                for (int i = 0, remain = keys.Count; 0 < remain; i++, remain -= 256)
                                {
                                    var tempKeys = keys.GetRange(i * 256, Math.Min(remain, 256));

                                    Group group = new Group();
                                    group.CorrectionAlgorithm = CorrectionAlgorithm.None;
                                    group.InformationLength   = tempKeys.Count;
                                    group.BlockLength         = item.BlockLength;
                                    group.Length = tempKeys.Sum(n => (long)_cacheManager.GetLength(n));
                                    group.Keys.AddRange(tempKeys);

                                    groups.Add(group);
                                }

                                lock (this.ThisLock)
                                {
                                    item.EncodingBytes = 0;
                                    item.EncodeBytes   = 0;

                                    foreach (var key in keys)
                                    {
                                        item.UploadKeys.Add(key);
                                    }

                                    item.Groups.AddRange(groups);

                                    item.State = UploadState.Encoding;
                                }
                            }
                        }
                        else if (item.Groups.Count == 0 && item.Keys.Count == 1)
                        {
                            lock (this.ThisLock)
                            {
                                item.Seed.Rank = item.Rank;
                                item.Seed.Key  = item.Keys[0];
                                item.Keys.Clear();

                                if (item.Rank != 1)
                                {
                                    item.Seed.CompressionAlgorithm = item.CompressionAlgorithm;

                                    item.Seed.CryptoAlgorithm = item.CryptoAlgorithm;
                                    item.Seed.CryptoKey       = item.CryptoKey;
                                }

                                if (item.DigitalSignature != null)
                                {
                                    item.Seed.CreateCertificate(item.DigitalSignature);
                                }

                                item.UploadKeys.Add(item.Seed.Key);

                                foreach (var key in item.UploadKeys)
                                {
                                    _connectionsManager.Upload(key);
                                }

                                this.CheckState(item);

                                _cacheManager.SetSeed(item.Seed.Clone(), item.FilePath, item.Indexes.Select(n => n.Clone()).ToArray());
                                item.Indexes.Clear();

                                foreach (var key in item.LockedKeys)
                                {
                                    _cacheManager.Unlock(key);
                                }

                                item.LockedKeys.Clear();

                                item.State = UploadState.Uploading;
                            }
                        }
                        else if (item.Keys.Count > 0)
                        {
                            item.State = UploadState.ParityEncoding;

                            item.EncodeBytes = item.Groups.Sum(n =>
                            {
                                long sumLength = 0;

                                for (int i = 0; i < n.InformationLength; i++)
                                {
                                    if (_cacheManager.Contains(n.Keys[i]))
                                    {
                                        sumLength += (long)_cacheManager.GetLength(n.Keys[i]);
                                    }
                                }

                                return(sumLength);
                            }) + item.Keys.Sum(n =>
                            {
                                if (_cacheManager.Contains(n))
                                {
                                    return((long)_cacheManager.GetLength(n));
                                }

                                return(0);
                            });

                            var   length = Math.Min(item.Keys.Count, 128);
                            var   keys   = new KeyCollection(item.Keys.Take(length));
                            Group group  = null;

                            try
                            {
                                group = _cacheManager.ParityEncoding(keys, item.HashAlgorithm, item.BlockLength, item.CorrectionAlgorithm, (object state2) =>
                                {
                                    return(this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));
                                });
                            }
                            catch (StopException)
                            {
                                continue;
                            }

                            lock (this.ThisLock)
                            {
                                foreach (var key in group.Keys.Skip(group.InformationLength))
                                {
                                    item.UploadKeys.Add(key);
                                    item.LockedKeys.Add(key);
                                }

                                item.Groups.Add(group);

                                item.EncodingBytes = item.Groups.Sum(n =>
                                {
                                    long sumLength = 0;

                                    for (int i = 0; i < n.InformationLength; i++)
                                    {
                                        if (_cacheManager.Contains(n.Keys[i]))
                                        {
                                            sumLength += (long)_cacheManager.GetLength(n.Keys[i]);
                                        }
                                    }

                                    return(sumLength);
                                });

                                item.Keys.RemoveRange(0, length);
                            }
                        }
                        else if (item.Groups.Count > 0 && item.Keys.Count == 0)
                        {
                            item.State = UploadState.Encoding;

                            var index = new Index();
                            index.Groups.AddRange(item.Groups);

                            if (item.Rank != 1)
                            {
                                index.CompressionAlgorithm = item.CompressionAlgorithm;

                                index.CryptoAlgorithm = item.CryptoAlgorithm;
                                index.CryptoKey       = item.CryptoKey;

                                item.Indexes.Add(index);
                            }

                            byte[]        cryptoKey = null;
                            KeyCollection keys      = null;

                            try
                            {
                                using (var stream = index.Export(_bufferManager))
                                    using (ProgressStream hashProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                    {
                                        isStop = (this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));

                                        item.EncodingBytes = Math.Min(readSize, stream.Length);
                                    }, 1024 * 1024, true))
                                        using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                        {
                                            isStop = (this.EncodeState == ManagerState.Stop || !_settings.UploadItems.Contains(item));

                                            item.EncodingBytes = Math.Min(readSize, stream.Length);
                                        }, 1024 * 1024, true))
                                        {
                                            item.EncodeBytes = stream.Length;

                                            item.State = UploadState.ComputeHash;

                                            if (item.HashAlgorithm == HashAlgorithm.Sha256)
                                            {
                                                cryptoKey = Sha256.ComputeHash(hashProgressStream);
                                            }

                                            stream.Seek(0, SeekOrigin.Begin);
                                            item.EncodingBytes = 0;

                                            item.State = UploadState.Encoding;
                                            keys       = _cacheManager.Encoding(encodingProgressStream, item.CompressionAlgorithm, item.CryptoAlgorithm, cryptoKey, item.BlockLength, item.HashAlgorithm);
                                        }
                            }
                            catch (StopIoException)
                            {
                                continue;
                            }

                            lock (this.ThisLock)
                            {
                                foreach (var key in keys)
                                {
                                    item.UploadKeys.Add(key);
                                    item.LockedKeys.Add(key);
                                }

                                item.EncodingBytes = 0;
                                item.EncodeBytes   = 0;

                                item.CryptoKey = cryptoKey;
                                item.Keys.AddRange(keys);
                                item.Rank++;

                                item.Groups.Clear();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    item.State = UploadState.Error;

                    Log.Error(e);
                }
                finally
                {
                    _workingPaths.Remove(item.FilePath);
                }
            }
        }