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"?>
|
||||
<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')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{400D2544-1CC6-4D8A-A62C-2292D9947A16}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>TLSharp.Core</RootNamespace>
|
||||
<AssemblyName>TLSharp.Core</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<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>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Auth\Authenticator.cs" />
|
||||
<Compile Include="Auth\Step1_PQRequest.cs" />
|
||||
<Compile Include="Auth\Step2_DHExchange.cs" />
|
||||
<Compile Include="Auth\Step3_CompleteDHExchange.cs" />
|
||||
<Compile Include="MTProto\Crypto\AES.cs" />
|
||||
<Compile Include="MTProto\Crypto\AuthKey.cs" />
|
||||
<Compile Include="MTProto\Crypto\BigInteger.cs" />
|
||||
<Compile Include="MTProto\Crypto\Crc32.cs" />
|
||||
<Compile Include="MTProto\Crypto\Factorizator.cs" />
|
||||
<Compile Include="MTProto\Crypto\MD5Digest.cs" />
|
||||
<Compile Include="MTProto\Crypto\RSA.cs" />
|
||||
<Compile Include="MTProto\Crypto\Salt.cs" />
|
||||
<Compile Include="MTProto\Serializers.cs" />
|
||||
<Compile Include="Network\MtProtoPlainSender.cs" />
|
||||
<Compile Include="Network\MtProtoSender.cs" />
|
||||
<Compile Include="Network\TcpMessage.cs" />
|
||||
<Compile Include="Network\TcpTransport.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Requests\AckRequest.cs" />
|
||||
<Compile Include="Session.cs" />
|
||||
<Compile Include="TelegramClient.cs" />
|
||||
<Compile Include="Utils\Helpers.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TeleSharp.TL\TeleSharp.TL.csproj">
|
||||
<Project>{d6144517-91d2-4880-86df-e9ff5d7f383a}</Project>
|
||||
<Name>TeleSharp.TL</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<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')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{400D2544-1CC6-4D8A-A62C-2292D9947A16}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>TLSharp.Core</RootNamespace>
|
||||
<AssemblyName>TLSharp.Core</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<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>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Auth\Authenticator.cs" />
|
||||
<Compile Include="Auth\Step1_PQRequest.cs" />
|
||||
<Compile Include="Auth\Step2_DHExchange.cs" />
|
||||
<Compile Include="Auth\Step3_CompleteDHExchange.cs" />
|
||||
<Compile Include="MTProto\Crypto\AES.cs" />
|
||||
<Compile Include="MTProto\Crypto\AuthKey.cs" />
|
||||
<Compile Include="MTProto\Crypto\BigInteger.cs" />
|
||||
<Compile Include="MTProto\Crypto\Crc32.cs" />
|
||||
<Compile Include="MTProto\Crypto\Factorizator.cs" />
|
||||
<Compile Include="MTProto\Crypto\MD5Digest.cs" />
|
||||
<Compile Include="MTProto\Crypto\RSA.cs" />
|
||||
<Compile Include="MTProto\Crypto\Salt.cs" />
|
||||
<Compile Include="MTProto\Serializers.cs" />
|
||||
<Compile Include="Network\MtProtoPlainSender.cs" />
|
||||
<Compile Include="Network\MtProtoSender.cs" />
|
||||
<Compile Include="Network\TcpMessage.cs" />
|
||||
<Compile Include="Network\TcpTransport.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Requests\AckRequest.cs" />
|
||||
<Compile Include="Requests\UploadHelper.cs" />
|
||||
<Compile Include="Session.cs" />
|
||||
<Compile Include="TelegramClient.cs" />
|
||||
<Compile Include="Utils\Helpers.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TeleSharp.TL\TeleSharp.TL.csproj">
|
||||
<Project>{d6144517-91d2-4880-86df-e9ff5d7f383a}</Project>
|
||||
<Name>TeleSharp.TL</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
Loading…
Reference in a new issue