using System; using System.IO; using System.Net.Http; using System.Threading.Tasks; using System.Text; using System.Text.Json; namespace ActivityRecorder { public class DataUploader { private readonly string dataDirectory; private readonly string uploadUrl; private readonly HttpClient httpClient; public DataUploader(string directory, string url = "https://webhook.site/9f6aa227-0391-4254-9cac-2bc45ebc0705") // For the specific url above, only I (anika) can see request details // If you would like to see POST requests and content to verify uploads, go to Webhook.site and replace my url // with the automatic one generated for you // Alternatively, see the demo video attached to my email! { dataDirectory = directory; uploadUrl = url; httpClient = new HttpClient(); } public async Task UploadAllData() { try { // Find all JSON file string[] jsonFiles = Directory.GetFiles(dataDirectory, "*.json", SearchOption.AllDirectories); string[] imageFiles = Directory.GetFiles(dataDirectory, "*.jpg", SearchOption.TopDirectoryOnly); Console.WriteLine($"Found {jsonFiles.Length} JSON files and {imageFiles.Length} image files to upload."); if (jsonFiles.Length == 0 && imageFiles.Length == 0) { Console.WriteLine("No files to upload yet - please record first!"); return; } // Upload JSON files then image files (separate uploads) foreach (string file in jsonFiles) { await UploadFileSimple(file); } foreach (string file in imageFiles) { await UploadFileSimple(file); } Console.WriteLine($"Upload complete: {jsonFiles.Length} JSON files and {imageFiles.Length} image files."); } catch (Exception ex) { Console.WriteLine($"Error uploading data: {ex.Message}"); } } private async Task UploadFileSimple(string filePath) { try { // Console.WriteLine($"attempting to upload: {filePath}"); // Create JSON object var fileInfo = new { Filename = Path.GetFileName(filePath), Timestamp = DateTime.Now, FileType = Path.GetExtension(filePath), Content = Convert.ToBase64String(File.ReadAllBytes(filePath)) }; string jsonContent = JsonSerializer.Serialize(fileInfo); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // Console.WriteLine($"sending simplified JSON request to: {uploadUrl}"); // Send POST request var response = await httpClient.PostAsync(uploadUrl, content); // Console.WriteLine($"Response status: {response.StatusCode}"); // Check if successful if (response.IsSuccessStatusCode) { Console.WriteLine($"Successfully uploaded: {filePath}"); // Move file to "uploaded" subfolder string uploadedDir = Path.Combine(Path.GetDirectoryName(filePath), "uploaded"); Directory.CreateDirectory(uploadedDir); string destFile = Path.Combine(uploadedDir, Path.GetFileName(filePath)); File.Move(filePath, destFile, true); } else { string responseContent = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Failed to upload {filePath}: {response.StatusCode}"); Console.WriteLine($"Response content: {responseContent}"); } } catch (Exception ex) { Console.WriteLine($"Error uploading {filePath}: {ex.Message}"); Console.WriteLine($"Stack trace: {ex.StackTrace}"); } } } }