Example #1
0
        // photos.updateProfilePhoto#eef579a0 id:InputPhoto crop:InputPhotoCrop = UserProfilePhoto;
        // photos.uploadProfilePhoto#d50f9c88 file:InputFile caption:string geo_point:InputGeoPoint crop:InputPhotoCrop = photos.Photo;
        // photos.deletePhotos#87cf7f2f id:Vector<InputPhoto> = Vector<long>;
        // photos.getUserPhotos#b7ee553c user_id:InputUser offset:int max_id:int limit:int = photos.Photos;

        #endregion

        #region Upload

        // upload.saveFilePart#b304a621 file_id:long file_part:int bytes:bytes = Bool;
        public async Task <bool> SaveFilePart(long fileId, int filePart, byte[] bytes)
        {
            var request = new SaveFilePartRequest(fileId, filePart, bytes, 0, bytes.Length);

            await SendRpcRequest(request);

            return(request.done);
        }
Example #2
0
        public async Task <InputFileConstructor> UploadFile(string name, Stream content)
        {
            var buffer = new byte[65536];
            var fileId = BitConverter.ToInt64(Helpers.GenerateRandomBytes(8), 0);

            int partsCount = 0;
            int bytesRead;

            while ((bytesRead = content.Read(buffer, 0, buffer.Length)) > 0)
            {
                var request = new SaveFilePartRequest(fileId, partsCount, buffer, 0, bytesRead);
                await SendRpcRequest(request);

                partsCount++;

                if (request.done == false)
                {
                    throw new InvalidOperationException($"Failed to upload file({name}) part: {partsCount})");
                }
            }

            var md5Checksum = string.Empty;

            if (content.CanSeek)
            {
                content.Position = 0;

                using (var md5 = MD5.Create())
                {
                    var hash       = md5.ComputeHash(content);
                    var hashResult = new StringBuilder(hash.Length * 2);

                    foreach (byte b in hash)
                    {
                        hashResult.Append(b.ToString("x2"));
                    }

                    md5Checksum = hashResult.ToString();
                }
            }

            return(new InputFileConstructor(fileId, partsCount, name, md5Checksum));
        }