mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Upload Helper
This commit is contained in:
parent
23e647e81c
commit
79b02b9295
120
TLSharp.Core/Requests/UploadHelper.cs
Normal file
120
TLSharp.Core/Requests/UploadHelper.cs
Normal file
|
|
@ -0,0 +1,120 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TeleSharp.TL;
|
||||||
|
using TeleSharp.TL.Upload;
|
||||||
|
namespace TLSharp.Core.Requests
|
||||||
|
{
|
||||||
|
public class UploadHelper
|
||||||
|
{
|
||||||
|
public static async Task<TLAbsInputFile> Uploader(string name,StreamReader reader,TelegramClient client)
|
||||||
|
{
|
||||||
|
if (reader.BaseStream.Length < 10 * 1024 * 1024)
|
||||||
|
return await SmallFileUpload(name, reader, client);
|
||||||
|
else
|
||||||
|
return await BigFileUpload(name, reader, client);
|
||||||
|
}
|
||||||
|
private static async Task<TLInputFile> SmallFileUpload(string name, StreamReader reader, TelegramClient client)
|
||||||
|
{
|
||||||
|
var file = new byte[reader.BaseStream.Length];
|
||||||
|
reader.BaseStream.Read(file, 0, (int)reader.BaseStream.Length);
|
||||||
|
string hash;
|
||||||
|
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
|
||||||
|
{
|
||||||
|
hash = Convert.ToBase64String(md5.ComputeHash(file));
|
||||||
|
}
|
||||||
|
reader = null;
|
||||||
|
var stream = new MemoryStream(file);
|
||||||
|
Queue<byte[]> parts = new Queue<byte[]>();
|
||||||
|
while (!(stream.Position == stream.Length))
|
||||||
|
{
|
||||||
|
if ((stream.Length - stream.Position) > 512 *1024)
|
||||||
|
{
|
||||||
|
byte[] temp = new byte[512];
|
||||||
|
stream.Read(temp, 0, 512 * 1024);
|
||||||
|
parts.Enqueue(temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
byte[] temp = new byte[512];
|
||||||
|
stream.Read(temp, 0, (int)(stream.Length - stream.Position));
|
||||||
|
parts.Enqueue(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stream = null;
|
||||||
|
GC.Collect();
|
||||||
|
int partnumber = 0;
|
||||||
|
long file_id = BitConverter.ToInt64(RandomByteArray(8), 0);
|
||||||
|
while (parts.Count != 0)
|
||||||
|
{
|
||||||
|
var part = parts.Dequeue();
|
||||||
|
TLRequestSaveFilePart save = new TLRequestSaveFilePart();
|
||||||
|
save.file_id = file_id;
|
||||||
|
save.file_part = partnumber;
|
||||||
|
save.bytes = part;
|
||||||
|
await client.SendRequestAsync<bool>(save);
|
||||||
|
partnumber++;
|
||||||
|
}
|
||||||
|
TLInputFile returnFile = new TLInputFile();
|
||||||
|
returnFile.id = file_id;
|
||||||
|
returnFile.name = name;
|
||||||
|
returnFile.parts = parts.Count;
|
||||||
|
returnFile.md5_checksum = hash;
|
||||||
|
return returnFile;
|
||||||
|
}
|
||||||
|
private static async Task<TLInputFileBig> BigFileUpload(string name, StreamReader reader, TelegramClient client)
|
||||||
|
{
|
||||||
|
var file = new byte[reader.BaseStream.Length];
|
||||||
|
reader.BaseStream.Read(file, 0, (int)reader.BaseStream.Length);
|
||||||
|
reader = null;
|
||||||
|
var stream = new MemoryStream(file);
|
||||||
|
Queue<byte[]> parts = new Queue<byte[]>();
|
||||||
|
while (!(stream.Position == stream.Length))
|
||||||
|
{
|
||||||
|
if ((stream.Length - stream.Position) > 512 * 1024)
|
||||||
|
{
|
||||||
|
byte[] temp = new byte[512];
|
||||||
|
stream.Read(temp, 0, 512 * 1024);
|
||||||
|
parts.Enqueue(temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
byte[] temp = new byte[512];
|
||||||
|
stream.Read(temp, 0, (int)(stream.Length - stream.Position));
|
||||||
|
parts.Enqueue(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stream = null;
|
||||||
|
GC.Collect();
|
||||||
|
int partnumber = 0;
|
||||||
|
long file_id = BitConverter.ToInt64(RandomByteArray(8), 0);
|
||||||
|
while (parts.Count != 0)
|
||||||
|
{
|
||||||
|
var part = parts.Dequeue();
|
||||||
|
TLRequestSaveBigFilePart save = new TLRequestSaveBigFilePart();
|
||||||
|
save.file_id = file_id;
|
||||||
|
save.file_part = partnumber;
|
||||||
|
save.bytes = part;
|
||||||
|
save.file_total_parts = parts.Count;
|
||||||
|
await client.SendRequestAsync<bool>(save);
|
||||||
|
partnumber++;
|
||||||
|
}
|
||||||
|
TLInputFileBig returnFile = new TLInputFileBig();
|
||||||
|
returnFile.id = file_id;
|
||||||
|
returnFile.name = name;
|
||||||
|
returnFile.parts = parts.Count;
|
||||||
|
return returnFile;
|
||||||
|
}
|
||||||
|
private static byte[] RandomByteArray(int count)
|
||||||
|
{
|
||||||
|
var temp = new byte[count];
|
||||||
|
Random random = new Random();
|
||||||
|
random.NextBytes(temp);
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,87 +1,88 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
<ProjectGuid>{400D2544-1CC6-4D8A-A62C-2292D9947A16}</ProjectGuid>
|
<ProjectGuid>{400D2544-1CC6-4D8A-A62C-2292D9947A16}</ProjectGuid>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
<RootNamespace>TLSharp.Core</RootNamespace>
|
<RootNamespace>TLSharp.Core</RootNamespace>
|
||||||
<AssemblyName>TLSharp.Core</AssemblyName>
|
<AssemblyName>TLSharp.Core</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
<DebugType>full</DebugType>
|
<DebugType>full</DebugType>
|
||||||
<Optimize>false</Optimize>
|
<Optimize>false</Optimize>
|
||||||
<OutputPath>bin\Debug\</OutputPath>
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<DebugType>pdbonly</DebugType>
|
<DebugType>pdbonly</DebugType>
|
||||||
<Optimize>true</Optimize>
|
<Optimize>true</Optimize>
|
||||||
<OutputPath>bin\Release\</OutputPath>
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
<DefineConstants>TRACE</DefineConstants>
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Ionic.ZLib, Version=2.0.0.14, Culture=neutral, processorArchitecture=MSIL">
|
<Reference Include="Ionic.ZLib, Version=2.0.0.14, Culture=neutral, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\MarkerMetro.Unity.Ionic.Zlib.2.0.0.14\lib\net35\Ionic.ZLib.dll</HintPath>
|
<HintPath>..\packages\MarkerMetro.Unity.Ionic.Zlib.2.0.0.14\lib\net35\Ionic.ZLib.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
<Reference Include="System.Net.Http" />
|
<Reference Include="System.Net.Http" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Auth\Authenticator.cs" />
|
<Compile Include="Auth\Authenticator.cs" />
|
||||||
<Compile Include="Auth\Step1_PQRequest.cs" />
|
<Compile Include="Auth\Step1_PQRequest.cs" />
|
||||||
<Compile Include="Auth\Step2_DHExchange.cs" />
|
<Compile Include="Auth\Step2_DHExchange.cs" />
|
||||||
<Compile Include="Auth\Step3_CompleteDHExchange.cs" />
|
<Compile Include="Auth\Step3_CompleteDHExchange.cs" />
|
||||||
<Compile Include="MTProto\Crypto\AES.cs" />
|
<Compile Include="MTProto\Crypto\AES.cs" />
|
||||||
<Compile Include="MTProto\Crypto\AuthKey.cs" />
|
<Compile Include="MTProto\Crypto\AuthKey.cs" />
|
||||||
<Compile Include="MTProto\Crypto\BigInteger.cs" />
|
<Compile Include="MTProto\Crypto\BigInteger.cs" />
|
||||||
<Compile Include="MTProto\Crypto\Crc32.cs" />
|
<Compile Include="MTProto\Crypto\Crc32.cs" />
|
||||||
<Compile Include="MTProto\Crypto\Factorizator.cs" />
|
<Compile Include="MTProto\Crypto\Factorizator.cs" />
|
||||||
<Compile Include="MTProto\Crypto\MD5Digest.cs" />
|
<Compile Include="MTProto\Crypto\MD5Digest.cs" />
|
||||||
<Compile Include="MTProto\Crypto\RSA.cs" />
|
<Compile Include="MTProto\Crypto\RSA.cs" />
|
||||||
<Compile Include="MTProto\Crypto\Salt.cs" />
|
<Compile Include="MTProto\Crypto\Salt.cs" />
|
||||||
<Compile Include="MTProto\Serializers.cs" />
|
<Compile Include="MTProto\Serializers.cs" />
|
||||||
<Compile Include="Network\MtProtoPlainSender.cs" />
|
<Compile Include="Network\MtProtoPlainSender.cs" />
|
||||||
<Compile Include="Network\MtProtoSender.cs" />
|
<Compile Include="Network\MtProtoSender.cs" />
|
||||||
<Compile Include="Network\TcpMessage.cs" />
|
<Compile Include="Network\TcpMessage.cs" />
|
||||||
<Compile Include="Network\TcpTransport.cs" />
|
<Compile Include="Network\TcpTransport.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Requests\AckRequest.cs" />
|
<Compile Include="Requests\AckRequest.cs" />
|
||||||
<Compile Include="Session.cs" />
|
<Compile Include="Requests\UploadHelper.cs" />
|
||||||
<Compile Include="TelegramClient.cs" />
|
<Compile Include="Session.cs" />
|
||||||
<Compile Include="Utils\Helpers.cs" />
|
<Compile Include="TelegramClient.cs" />
|
||||||
</ItemGroup>
|
<Compile Include="Utils\Helpers.cs" />
|
||||||
<ItemGroup>
|
</ItemGroup>
|
||||||
<None Include="packages.config" />
|
<ItemGroup>
|
||||||
</ItemGroup>
|
<None Include="packages.config" />
|
||||||
<ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectReference Include="..\TeleSharp.TL\TeleSharp.TL.csproj">
|
<ItemGroup>
|
||||||
<Project>{d6144517-91d2-4880-86df-e9ff5d7f383a}</Project>
|
<ProjectReference Include="..\TeleSharp.TL\TeleSharp.TL.csproj">
|
||||||
<Name>TeleSharp.TL</Name>
|
<Project>{d6144517-91d2-4880-86df-e9ff5d7f383a}</Project>
|
||||||
</ProjectReference>
|
<Name>TeleSharp.TL</Name>
|
||||||
</ItemGroup>
|
</ProjectReference>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
</ItemGroup>
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
Other similar extension points exist, see Microsoft.Common.targets.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
<Target Name="BeforeBuild">
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
</Target>
|
<Target Name="BeforeBuild">
|
||||||
<Target Name="AfterBuild">
|
</Target>
|
||||||
</Target>
|
<Target Name="AfterBuild">
|
||||||
-->
|
</Target>
|
||||||
|
-->
|
||||||
</Project>
|
</Project>
|
||||||
Loading…
Reference in a new issue