コード例 #1
0
        public Task Transfer(FileResult metadata)
        {
            return(Task.Run(() =>
            {
                metadata.DestPath = Path.Combine(_config.Dest, Guid.NewGuid().ToString("N")) + metadata.Extension;

                try
                {
                    using (var file = File.Open(metadata.SrcPath, FileMode.Open))
                        using (var dest = File.Open(metadata.DestPath, FileMode.CreateNew))
                            using (var sha1 = SHA1.Create())
                            {
                                //TODO
                                //Check if file stream is cause 2 reads instead of one
                                //Wrap in memory stream if it is
                                file.CopyTo(dest);
                                file.Position = 0;

                                var hash = sha1.ComputeHash(file);
                                var sb = new StringBuilder(hash.Length * 2);

                                foreach (var b in hash)
                                {
                                    sb.Append(b.ToString("X2"));
                                }

                                metadata.Sha1 = sb.ToString();

                                metadata.TransferComplete = DateTime.Now;
                                _resultFactory.AddResult(metadata);
                            }

                    File.Delete(metadata.SrcPath);
                }
                catch (Exception ex)
                {
                    _resultFactory.AddError(new ErrorResult {
                        CurrentFile = metadata.SrcPath, ErrorMessage = ex.Message
                    });
                }
            }));
        }