Пример #1
0
            private void UploadVideoParts(string videoPath, Models.API.v5.UploadVideo.Upload upload)
            {
                if (!File.Exists(videoPath))
                {
                    throw new Exceptions.API.BadParameterException($"The provided path for a video upload does not appear to be value: {videoPath}");
                }
                var videoInfo = new FileInfo(videoPath);

                if (videoInfo.Length >= MAX_VIDEO_SIZE)
                {
                    throw new Exceptions.API.BadParameterException($"The provided file was too large (larger than 10gb). File size: {videoInfo.Length}");
                }

                const long size24Mb = 25165824;
                var        fileSize = videoInfo.Length;

                if (fileSize > size24Mb)
                {
                    // Split file into fragments if file size exceeds maximum fragment size
                    using (var fs = new FileStream(videoPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        var finalChunkSize = fileSize % size24Mb;
                        var parts          = (fileSize - finalChunkSize) / size24Mb + 1;
                        for (var currentPart = 1; currentPart <= parts; currentPart++)
                        {
                            byte[] chunk;
                            if (currentPart == parts)
                            {
                                chunk = new byte[finalChunkSize];
                                fs.Read(chunk, 0, (int)finalChunkSize);
                            }
                            else
                            {
                                chunk = new byte[size24Mb];
                                fs.Read(chunk, 0, (int)size24Mb);
                            }
                            Api.PutBytes($"{upload.Url}?part={currentPart}&upload_token={upload.Token}", chunk);
                            System.Threading.Thread.Sleep(1000);
                        }
                    }
                }
                else
                {
                    // Upload entire file at once if small enough
                    var file = File.ReadAllBytes(videoPath);
                    Api.PutBytes($"{upload.Url}?part=1&upload_token={upload.Token}", file);
                }
            }
Пример #2
0
 private async Task CompleteVideoUpload(Models.API.v5.UploadVideo.Upload upload, string accessToken)
 {
     await Api.PostAsync($"{upload.Url}/complete?upload_token={upload.Token}", null, null, accessToken);
 }