// upload public async Task <InputFile> UploadFile(string filename, Stream stream, FileUploadProcessHandler handler) { TLApi api = await session.GetFileSessionMain(); long fileId = Helpers.GenerateRandomLong(); MD5 hash = new MD5(); if (stream.Length < 128 * 1024) { handler(0.0f); byte[] data = new byte[stream.Length]; stream.Read(data, 0, (int)stream.Length); bool result = await api.upload_saveFilePart(fileId, 0, data); //while(result != true) { // result = await api.upload_saveFilePart(fileId, 0, data); //} hash.Update(data); handler(1.0f); return(TL.inputFile(fileId, 1, filename, hash.FinalString())); } bool big = stream.Length > 10 * 1024 * 1024; float allStreamLength = stream.Length; int chunkSize = 128 * 1024; int chunkCount = (int)(stream.Length / chunkSize); int lastChunkSize = (int)(stream.Length - chunkSize * chunkCount); int allChunksCount = chunkCount + (lastChunkSize != 0 ? 1 : 0); for (int i = 0; i < chunkCount; i++) { handler((float)i * (float)chunkSize / allStreamLength); byte[] data = new byte[chunkSize]; stream.Read(data, 0, chunkSize); bool result = big ? await api.upload_saveBigFilePart(fileId, i, allChunksCount, data) : await api.upload_saveFilePart(fileId, i, data); //while(result != true) { // result = await api.upload_saveFilePart(fileId, i, data); //} hash.Update(data); } if (lastChunkSize != 0) { handler((float)chunkCount * (float)chunkSize / allStreamLength); byte[] lastChunkData = new byte[lastChunkSize]; stream.Read(lastChunkData, 0, lastChunkSize); bool lastChunkResult = big ? await api.upload_saveBigFilePart(fileId, chunkCount, allChunksCount, lastChunkData) : await api.upload_saveFilePart(fileId, chunkCount, lastChunkData); //while(lastChunkResult != true) { // lastChunkResult = await api.upload_saveFilePart(fileId, chunkCount, lastChunkData); //} hash.Update(lastChunkData); } handler(1.0f); return(TL.inputFile(fileId, allChunksCount, filename, hash.FinalString())); }