-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploader.cs
More file actions
115 lines (95 loc) · 4.17 KB
/
Copy pathUploader.cs
File metadata and controls
115 lines (95 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.IO.Compression;
using System.Text;
using System.Text.Json;
namespace PasswordAPIAssessment
{
public class Uploader
{
public void CreateZipAndUpload(string uploadUrl, string cvPath, string projectRoot)
{
// Define the path for the zip file to be created
string zipPath = Path.Combine(projectRoot, "submission.zip");
// List of required .cs files to include in the zip
string[] requiredCsFiles = new[]
{
"Program.cs",
"Uploader.cs"
};
Console.WriteLine($"Upload URL: {uploadUrl}");
// Create zip archive
using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Create))
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
// Add CV file to the zip
archive.CreateEntryFromFile(cvPath, Path.GetFileName(cvPath));
// Add dict.txt if it exists
string dictPath = Path.Combine(projectRoot, "dict.txt");
if (File.Exists(dictPath))
archive.CreateEntryFromFile(dictPath, "dict.txt");
// Add required .cs files to the zip
foreach (string fileName in requiredCsFiles)
{
string filePath = Path.Combine(projectRoot, fileName);
if (File.Exists(filePath))
archive.CreateEntryFromFile(filePath, Path.GetFileName(filePath));
}
}
// Verify that all required files are in the zip
if (!VerifyZip(zipPath, requiredCsFiles, Path.GetFileName(cvPath)))
{
Console.WriteLine("\n❌ ZIP verification failed.");
return;
}
Console.WriteLine("\n✅ ZIP verified. Uploading...");
// Read the zip as bytes and convert to Base64 string
byte[] fileBytes = File.ReadAllBytes(zipPath);
string base64Zip = Convert.ToBase64String(fileBytes);
// Create the payload object to send in the request
var payload = new
{
Data = base64Zip,
Name = "Simbongile",
Surname = "Dyi",
Email = "nwabisamxa@gmail.com"
};
// Serialize payload to JSON
string json = JsonSerializer.Serialize(payload);
// Send HTTP POST request
using HttpClient client = new HttpClient();
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(uploadUrl, content).Result;
// Handle response
if (response.IsSuccessStatusCode)
{
Console.WriteLine("✅ Upload successful!");
}
else
{
Console.WriteLine($"❌ Upload failed: {response.StatusCode}");
string responseBody = response.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Upload error response body: {responseBody}");
}
}
// Verify the contents of the created zip file
private bool VerifyZip(string zipPath, string[] requiredCsFiles, string cvFileName)
{
bool hasDict = false;
bool hasCV = false;
HashSet<string> foundCsFiles = new();
using (ZipArchive zip = ZipFile.OpenRead(zipPath))
{
foreach (var entry in zip.Entries)
{
if (entry.FullName.Equals("dict.txt", StringComparison.OrdinalIgnoreCase))
hasDict = true;
if (entry.FullName.Equals(cvFileName, StringComparison.OrdinalIgnoreCase))
hasCV = true;
if (requiredCsFiles.Contains(entry.FullName))
foundCsFiles.Add(entry.FullName);
}
}
// Return true only if all required files are found
return hasDict && hasCV && foundCsFiles.Count == requiredCsFiles.Length;
}
}
}