Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
- class Program
- {
- static async Task Main(string[] args)
- {
- // Console.WriteLine("Masukkan path lengkap file .zip yang ingin diupload:");
- string filePath = "CMU_1.svs";
- //Console.WriteLine("Masukkan token keamanan:");
- //string securityToken = Console.ReadLine();
- await UploadFile(filePath);
- Console.WriteLine("Proses upload selesai.");
- Console.ReadLine();
- }
- static async Task UploadFile(string filePath)
- {
- string fileName = Path.GetFileName(filePath);
- long chunkSize = 1024 * 1024; // Ukuran chunk (dalam byte)
- string uploadID = "";
- string endPoint = "https://api2.neurabot.ai/api/v2/slides/chunks/";
- string endPointUpload = "https://api2.neurabot.ai/api/v2/slides/upload/";
- string securityToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjg1NDQ4MDU0LCJqdGkiOiJiZDA5MjBlOWExY2U0NmVjYTJlZmQwNzIyNmUyMDQ3MiIsInVzZXJfaWQiOjg1Mn0.oJM7_ugCS1fe4SXHxqjpzhN2lqaZXJNJKw8z5Ncw_Zs";
- using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
- {
- using (HttpClient httpClient = new HttpClient())
- {
- httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", securityToken);
- int offset = 0;
- long fileSize = fileStream.Length;
- long bytesToRead = fileSize;
- int numChunks = (int)(fileSize / chunkSize);
- long remainingBytes = fileSize % chunkSize;
- Console.WriteLine("File Size : " + fileSize);
- if (remainingBytes > 0)
- numChunks++;
- for (int chunkIndex = 0; chunkIndex < numChunks; chunkIndex++)
- {
- long currentChunkSize = (chunkIndex == numChunks - 1 && remainingBytes > 0) ? remainingBytes : chunkSize;
- Console.WriteLine("Current Chunk Size : " + currentChunkSize);
- byte[] buffer = new byte[currentChunkSize];
- Console.WriteLine("Offset : " + offset);
- Console.WriteLine("Buffer : " + buffer.Length);
- long seek = fileStream.Seek(offset, SeekOrigin.Begin);
- int bytesRead = fileStream.Read(buffer, 0, (int)currentChunkSize);
- string contentRange = $"bytes {offset}-{offset + (int)currentChunkSize - 1}/{fileSize}";
- ByteArrayContent byteContent = new ByteArrayContent(buffer);
- MultipartFormDataContent formData = new MultipartFormDataContent();
- formData.Add(byteContent, "the_file", fileName);
- formData.Headers.Add("Content-Range", contentRange);
- formData.Headers.Add("X-Requested-With", "XMLHttpRequest");
- if (chunkIndex > 0)
- {
- formData.Add(new StringContent(uploadID), "upload_id");
- }
- HttpResponseMessage response = await httpClient.PostAsync(endPoint, formData);
- var responseContent = await response.Content.ReadAsStringAsync();
- Console.WriteLine($"{responseContent}");
- var responseObject = JsonConvert.DeserializeObject<dynamic>(responseContent);
- Console.WriteLine("===============");
- if (response.IsSuccessStatusCode)
- {
- if (chunkIndex == 0)
- {
- uploadID = responseObject.upload_id;
- }
- //offset = Convert.ToInt32(responseObject.offset);
- bytesToRead -= bytesRead;
- // upload complate chunk
- Console.WriteLine(uploadID);
- Console.WriteLine(responseObject);
- Console.WriteLine($"Chunk {chunkIndex + 1}/{numChunks} berhasil diunggah.");
- }
- else
- {
- Console.WriteLine(responseObject);
- Console.WriteLine($"Gagal mengunggah chunk {chunkIndex + 1}/{numChunks}. Status: {response.StatusCode}");
- }
- offset += bytesRead;
- }
- if (uploadID != ""){
- var payload = new Dictionary<string, object> {
- { "upload_id", $"{uploadID}"},
- //{"lab_id", 18233} //optional
- };
- string jsonPayload = JsonConvert.SerializeObject(payload);
- HttpContent content = new StringContent(jsonPayload, System.Text.Encoding.UTF8, "application/json");
- HttpResponseMessage response_upload = await httpClient.PostAsync(endPointUpload, content);
- var content_response = await response_upload.Content.ReadAsStringAsync();
- Console.WriteLine(content_response);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement