Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- static async Task Main(string[] args)
- {
- Console.WriteLine("Enter the full path of file you want to upload:");
- string filePath = Console.ReadLine();
- // await Login("jionglelam@phaostech.com", "Admin!234");
- await Login("indarto@neurabot.ai", "Admin!234!");
- await UploadFile(filePath);
- Console.WriteLine("The uploading process is complete.");
- Console.ReadLine();
- }
- static string accessToken = "";
- // static string base_url = "http://localhost:8000/api/v2/";
- static string base_url = "https://api2.neurabot.ai/api/v2/";
- static async Task Login(string email, string password)
- {
- string url = base_url +"auth/login/";
- var payload = new Dictionary<string, object>
- {
- ["email"] = email,
- ["password"] = password
- };
- var payloadJson = JsonConvert.SerializeObject(payload);
- var content = new StringContent(payloadJson, Encoding.UTF8, "application/json");
- var request = new HttpRequestMessage(HttpMethod.Post, url);
- request.Content = content;
- //if (!string.IsNullOrEmpty(accessToken))
- // request.Headers.Add("Authorization", "Token " + accessToken);
- using (HttpClient httpClient = new HttpClient())
- {
- var response = await httpClient.SendAsync(request);
- var responseString = await response.Content.ReadAsStringAsync();
- if (!response.IsSuccessStatusCode)
- {
- Console.WriteLine("Login Failed");
- }
- dynamic jsonResponse = JsonConvert.DeserializeObject(responseString);
- accessToken = jsonResponse.data.access_token;
- if (!string.IsNullOrEmpty(accessToken)) { Console.WriteLine("Logged in!"); }
- }
- }
- static async Task UploadFile(string filePath)
- {
- string fileName = Path.GetFileName(filePath);
- long chunkSize = 5000000; // Ukuran chunk (dalam byte)
- string uploadID = "";
- string endPoint = base_url + "slides/chunks/"; //endpoint for chunk file
- string endPointUpload = base_url + "slides/upload/dzi/"; //endpoint for complate upload file dzi
- string securityToken = accessToken;
- Console.WriteLine("Security Token : " + securityToken);
- 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();
- 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;
- Console.WriteLine(responseObject);
- Console.WriteLine($"Chunk {chunkIndex + 1}/{numChunks} uploaded successfully.");
- }
- else
- {
- Console.WriteLine(responseObject);
- Console.WriteLine($"Failed to upload 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