<commit_before>﻿using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using YaR.MailRuCloud.Api.XTSSharp;
using YaR.WebDavMailRu.CloudStore.XTSSharp;

namespace YaR.MailRuCloud.Api.Base
{
    public class UploadStreamFabric
    {
        private readonly MailRuCloud _cloud;

        public UploadStreamFabric(MailRuCloud cloud)
        {
            _cloud = cloud;
        }

        public async Task<Stream> Create(File file, FileUploadedDelegate onUploaded = null, bool discardEncryption = false)
        {
            var folder = await _cloud.GetItem(file.Path, MailRuCloud.ItemType.Folder) as Folder;
            if (null == folder)
                throw new DirectoryNotFoundException(file.Path);

            if (folder.CryptRequired && !discardEncryption)
            {
                var key1 = new byte[32];
                var key2 = new byte[32];
                Array.Copy(Encoding.ASCII.GetBytes("01234567890123456789012345678900zzzzzzzzzzzzzzzzzzzzzz"), key1, 32);
                Array.Copy(Encoding.ASCII.GetBytes("01234567890123456789012345678900zzzzzzzzzzzzzzzzzzzzzz"), key2, 32);
                var xts = XtsAes256.Create(key1, key2);

                file.ServiceInfo.CryptInfo = new CryptInfo
                {
                    PublicKey = key2,
                    AlignBytes = (uint) (XTSWriteOnlyStream.BlockSize - file.Size % XTSWriteOnlyStream.BlockSize)
                };

                var size = file.Size % XTSWriteOnlyStream.BlockSize == 0
                    ? file.Size.DefaultValue
                    : (file.Size / XTSWriteOnlyStream.BlockSize + 1) * XTSWriteOnlyStream.BlockSize;

                var ustream = new SplittedUploadStream(file.FullPath, _cloud, size, false, file.ServiceInfo.CryptInfo);
                if (onUploaded != null) ustream.FileUploaded += onUploaded;
                var encustream = new XTSWriteOnlyStream(ustream, xts, XTSWriteOnlyStream.DefaultSectorSize);

                return encustream;
            }

            var stream = new SplittedUploadStream(file.FullPath, _cloud, file.Size);<commit_msg>#22 fixed wrong align mark in filenames<commit_after>﻿using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using YaR.MailRuCloud.Api.XTSSharp;
using YaR.WebDavMailRu.CloudStore.XTSSharp;

namespace YaR.MailRuCloud.Api.Base
{
    public class UploadStreamFabric
    {
        private readonly MailRuCloud _cloud;

        public UploadStreamFabric(MailRuCloud cloud)
        {
            _cloud = cloud;
        }

        public async Task<Stream> Create(File file, FileUploadedDelegate onUploaded = null, bool discardEncryption = false)
        {
            var folder = await _cloud.GetItem(file.Path, MailRuCloud.ItemType.Folder) as Folder;
            if (null == folder)
                throw new DirectoryNotFoundException(file.Path);

            if (folder.CryptRequired && !discardEncryption)
            {
                var key1 = new byte[32];
                var key2 = new byte[32];
                Array.Copy(Encoding.ASCII.GetBytes("01234567890123456789012345678900zzzzzzzzzzzzzzzzzzzzzz"), key1, 32);
                Array.Copy(Encoding.ASCII.GetBytes("01234567890123456789012345678900zzzzzzzzzzzzzzzzzzzzzz"), key2, 32);
                var xts = XtsAes256.Create(key1, key2);

                file.ServiceInfo.CryptInfo = new CryptInfo
                {
                    PublicKey = key2,
                    AlignBytes = (uint) (file.Size % XTSWriteOnlyStream.BlockSize != 0 ? XTSWriteOnlyStream.BlockSize - file.Size % XTSWriteOnlyStream.BlockSize : 0)
                };

                var size = file.Size % XTSWriteOnlyStream.BlockSize == 0
                    ? file.Size.DefaultValue
                    : (file.Size / XTSWriteOnlyStream.BlockSize + 1) * XTSWriteOnlyStream.BlockSize;

                var ustream = new SplittedUploadStream(file.FullPath, _cloud, size, false, file.ServiceInfo.CryptInfo);
                if (onUploaded != null) ustream.FileUploaded += onUploaded;
                var encustream = new XTSWriteOnlyStream(ustream, xts, XTSWriteOnlyStream.DefaultSectorSize);

                return encustream;
            }

            var stream = new SplittedUploadStream(file.FullPath, _cloud, file.Size);
{'fraction_non_alphanumeric': 0.08379888268156424, 'fraction_numerical': 0.04003724394785847, 'mean_word_length': 2.247921390778534, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5446414', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1204, 'n_words': 245}