-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
1051 lines (903 loc) · 41.6 KB
/
Program.cs
File metadata and controls
1051 lines (903 loc) · 41.6 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace PackageManagerServer
{
class Program
{
private const string PACKAGES_DIR = "packages";
private const string DB_FILE = "package_db.json";
private const int PORT = 5005;
private static Dictionary<string, Dictionary<string, PackageVersionInfo>> packageDb;
private static HttpListener listener;
private static bool isRunning = true;
static async Task Main(string[] args)
{
Console.WriteLine("Package Manager Server");
Console.WriteLine("======================");
// Create packages directory if it doesn't exist
if (!Directory.Exists(PACKAGES_DIR))
{
Directory.CreateDirectory(PACKAGES_DIR);
Console.WriteLine($"Created packages directory: {PACKAGES_DIR}");
}
// Initialize package database
InitializeDatabase();
// Add sample packages if database is empty
if (packageDb.Count == 0)
{
AddSamplePackages();
}
// Start HTTP server
listener = new HttpListener();
listener.Prefixes.Add($"http://localhost:{PORT}/");
listener.Start();
Console.WriteLine($"Server started at http://localhost:{PORT}/");
Console.WriteLine("Press Ctrl+C to stop the server");
// Set up console cancellation
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
isRunning = false;
Console.WriteLine("Shutting down server...");
};
// Handle requests
while (isRunning)
{
try
{
HttpListenerContext context = await listener.GetContextAsync();
_ = ProcessRequestAsync(context);
}
catch (Exception ex)
{
if (isRunning) // Only log if not shutting down
{
Console.WriteLine($"Error handling request: {ex.Message}");
}
}
}
// Clean up
listener.Stop();
Console.WriteLine("Server stopped");
}
private static void InitializeDatabase()
{
if (File.Exists(DB_FILE))
{
string json = File.ReadAllText(DB_FILE);
packageDb = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, PackageVersionInfo>>>(json);
Console.WriteLine($"Loaded package database with {packageDb.Count} packages");
}
else
{
packageDb = new Dictionary<string, Dictionary<string, PackageVersionInfo>>();
SaveDatabase();
Console.WriteLine("Created new package database");
}
}
private static void SaveDatabase()
{
var options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize(packageDb, options);
File.WriteAllText(DB_FILE, json);
}
private static void AddSamplePackages()
{
var samplePackages = new List<(string name, string version, string description)>
{
("sample-lib", "1.0.0", "A sample library for demonstration purposes"),
("data-utils", "2.1.3", "Utilities for data processing and analysis"),
("web-framework", "0.9.5", "Lightweight web framework for applications")
};
foreach (var pkg in samplePackages)
{
// Create package directory
string pkgDir = Path.Combine(PACKAGES_DIR, pkg.name);
if (!Directory.Exists(pkgDir))
{
Directory.CreateDirectory(pkgDir);
}
// Create a simple zip file
string today = DateTime.Now.ToString("yyyyMMdd");
string zipFilename = $"{pkg.name}-{pkg.version}-{today}.zip";
string zipPath = Path.Combine(pkgDir, zipFilename);
// Create a simple zip with a README
using (var zipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
var readmeEntry = zipArchive.CreateEntry("README.md");
using (var writer = new StreamWriter(readmeEntry.Open()))
{
writer.WriteLine($"# {pkg.name} v{pkg.version}");
writer.WriteLine();
writer.WriteLine(pkg.description);
}
}
// Update database
if (!packageDb.ContainsKey(pkg.name))
{
packageDb[pkg.name] = new Dictionary<string, PackageVersionInfo>();
}
packageDb[pkg.name][pkg.version] = new PackageVersionInfo
{
Description = pkg.description,
FilePath = zipPath,
UploadDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
}
SaveDatabase();
Console.WriteLine("Added sample packages to the database");
}
private static async Task ProcessRequestAsync(HttpListenerContext context)
{
string path = context.Request.Url.AbsolutePath;
string method = context.Request.HttpMethod;
Console.WriteLine($"Request: {method} {path}");
try
{
if (path == "/" && method == "GET")
{
await ServeHomepageAsync(context);
}
else if (path.StartsWith("/api/search") && method == "GET")
{
await SearchPackagesAsync(context);
}
else if (path.StartsWith("/api/details/") && method == "GET")
{
await GetPackageDetailsAsync(context);
}
else if (path.StartsWith("/api/download/") && method == "GET")
{
await DownloadPackageAsync(context);
}
else if (path == "/api/upload" && method == "POST")
{
await UploadPackageAsync(context);
}
else if (path == "/api/upload-json" && method == "POST")
{
await UploadPackageJsonAsync(context);
}
else if (path == "/api/upload-chunk" && method == "POST")
{
await UploadChunkAsync(context);
}
else
{
// Not found
context.Response.StatusCode = 404;
await WriteJsonResponseAsync(context, new { error = "Not found" });
}
}
catch (Exception ex)
{
Console.WriteLine($"Error processing request: {ex.Message}");
context.Response.StatusCode = 500;
await WriteJsonResponseAsync(context, new { error = "Internal server error", message = ex.Message });
}
}
private static async Task UploadChunkAsync(HttpListenerContext context)
{
Console.WriteLine("Chunk upload request received");
// Read the request body
string requestBody;
using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
{
requestBody = await reader.ReadToEndAsync();
}
try
{
// Parse the JSON
var chunkData = JsonSerializer.Deserialize<ChunkUploadRequest>(requestBody);
// Validate required fields
if (string.IsNullOrEmpty(chunkData.name) ||
string.IsNullOrEmpty(chunkData.version) ||
string.IsNullOrEmpty(chunkData.chunkData) ||
chunkData.chunkIndex < 0 ||
chunkData.totalChunks <= 0)
{
context.Response.StatusCode = 400;
await WriteJsonResponseAsync(context, new
{
success = false,
message = "Missing or invalid chunk data"
});
return;
}
// Create temp directory for chunks if it doesn't exist
string tempDir = Path.Combine(PACKAGES_DIR, "temp", chunkData.name, chunkData.version);
Directory.CreateDirectory(tempDir);
// Save this chunk
string chunkPath = Path.Combine(tempDir, $"chunk_{chunkData.chunkIndex}.bin");
byte[] chunkBytes = Convert.FromBase64String(chunkData.chunkData);
File.WriteAllBytes(chunkPath, chunkBytes);
Console.WriteLine($"Saved chunk {chunkData.chunkIndex + 1}/{chunkData.totalChunks} for {chunkData.name} v{chunkData.version}");
// Check if all chunks are received
if (chunkData.chunkIndex == chunkData.totalChunks - 1)
{
// All chunks received, combine them
await CombineChunksAsync(chunkData.name, chunkData.version, chunkData.description, chunkData.totalChunks, tempDir);
}
await WriteJsonResponseAsync(context, new
{
success = true,
message = $"Chunk {chunkData.chunkIndex + 1}/{chunkData.totalChunks} received"
});
}
catch (Exception ex)
{
Console.WriteLine($"Error in chunk upload: {ex.Message}");
context.Response.StatusCode = 500;
await WriteJsonResponseAsync(context, new { success = false, message = $"Server error: {ex.Message}" });
}
}
private static async Task CombineChunksAsync(string packageName, string version, string description, int totalChunks, string tempDir)
{
try
{
Console.WriteLine($"Combining {totalChunks} chunks for {packageName} v{version}");
// Create package directory if it doesn't exist
string pkgDir = Path.Combine(PACKAGES_DIR, packageName);
if (!Directory.Exists(pkgDir))
{
Directory.CreateDirectory(pkgDir);
}
// Create zip file with version and date in the name
string today = DateTime.Now.ToString("yyyyMMdd");
string zipFilename = $"{packageName}-{version}-{today}.zip";
string zipPath = Path.Combine(pkgDir, zipFilename);
// Combine all chunks
using (var outputStream = new FileStream(zipPath, FileMode.Create))
{
for (int i = 0; i < totalChunks; i++)
{
string chunkPath = Path.Combine(tempDir, $"chunk_{i}.bin");
byte[] chunkData = File.ReadAllBytes(chunkPath);
await outputStream.WriteAsync(chunkData, 0, chunkData.Length);
// Delete the chunk after it's been added to the combined file
File.Delete(chunkPath);
}
}
// Update database
if (!packageDb.ContainsKey(packageName))
{
packageDb[packageName] = new Dictionary<string, PackageVersionInfo>();
}
packageDb[packageName][version] = new PackageVersionInfo
{
Description = description,
FilePath = zipPath,
UploadDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
// Save database immediately
SaveDatabase();
Console.WriteLine($"Successfully combined chunks and added {packageName} v{version} to database");
// Clean up temp directory
Directory.Delete(tempDir, true);
}
catch (Exception ex)
{
Console.WriteLine($"Error combining chunks: {ex.Message}");
throw;
}
}
private static async Task ServeHomepageAsync(HttpListenerContext context)
{
string html = @"
<!DOCTYPE html>
<html>
<head>
<title>Package Manager</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
h1 { color: #333; }
.search-container { margin: 20px 0; }
input[type=""text""] { padding: 8px; width: 70%; }
button { padding: 8px 15px; background: #4CAF50; color: white; border: none; cursor: pointer; }
.package { border: 1px solid #ddd; padding: 15px; margin: 10px 0; border-radius: 5px; }
.package h3 { margin-top: 0; }
.upload-form { margin: 30px 0; padding: 15px; background: #f5f5f5; border-radius: 5px; }
</style>
</head>
<body>
<h1>Package Manager</h1>
<div class=""search-container"">
<input type=""text"" id=""searchInput"" placeholder=""Search packages..."">
<button onclick=""searchPackages()"">Search</button>
</div>
<div id=""results""></div>
<div class=""upload-form"">
<h2>Upload New Package</h2>
<form id=""uploadForm"" enctype=""multipart/form-data"">
<div>
<label for=""packageName"">Package Name:</label>
<input type=""text"" id=""packageName"" name=""packageName"" required>
</div>
<div>
<label for=""version"">Version:</label>
<input type=""text"" id=""version"" name=""version"" required>
</div>
<div>
<label for=""description"">Description:</label>
<textarea id=""description"" name=""description"" rows=""4"" cols=""50"" required></textarea>
</div>
<div>
<label for=""packageFile"">Package File:</label>
<input type=""file"" id=""packageFile"" name=""packageFile"" required>
</div>
<button type=""button"" onclick=""uploadPackage()"">Upload</button>
</form>
</div>
<script>
function searchPackages() {
const query = document.getElementById('searchInput').value;
fetch(`/api/search?q=${query}`)
.then(response => response.json())
.then(data => {
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = '';
if (data.length === 0) {
resultsDiv.innerHTML = '<p>No packages found.</p>';
return;
}
data.forEach(pkg => {
const pkgDiv = document.createElement('div');
pkgDiv.className = 'package';
pkgDiv.innerHTML = `
<h3>${pkg.name} (${pkg.version})</h3>
<p>${pkg.description}</p>
<p>Uploaded: ${pkg.upload_date}</p>
<button onclick=""downloadPackage('${pkg.name}', '${pkg.version}')"">Download</button>
<button onclick=""getDetails('${pkg.name}')"">Details</button>
`;
resultsDiv.appendChild(pkgDiv);
});
})
.catch(error => console.error('Error:', error));
}
function downloadPackage(name, version) {
window.location.href = `/api/download/${name}/${version}`;
}
function getDetails(name) {
fetch(`/api/details/${name}`)
.then(response => response.json())
.then(data => {
alert(JSON.stringify(data, null, 2));
})
.catch(error => console.error('Error:', error));
}
function uploadPackage() {
const form = document.getElementById('uploadForm');
const formData = new FormData();
formData.append('name', document.getElementById('packageName').value);
formData.append('version', document.getElementById('version').value);
formData.append('description', document.getElementById('description').value);
formData.append('file', document.getElementById('packageFile').files[0]);
fetch('/api/upload', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Package uploaded successfully!');
form.reset();
} else {
alert('Error: ' + data.message);
}
})
.catch(error => console.error('Error:', error));
}
</script>
</body>
</html>
";
byte[] buffer = Encoding.UTF8.GetBytes(html);
context.Response.ContentType = "text/html";
context.Response.ContentLength64 = buffer.Length;
await context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
context.Response.Close();
}
private static async Task SearchPackagesAsync(HttpListenerContext context)
{
string query = "";
if (context.Request.QueryString["q"] != null)
{
query = context.Request.QueryString["q"].ToLower();
}
var results = new List<PackageSearchResult>();
foreach (var pkgEntry in packageDb)
{
string pkgName = pkgEntry.Key;
var versions = pkgEntry.Value;
if (query == "" || pkgName.ToLower().Contains(query) ||
versions.Any(v => v.Value.Description.ToLower().Contains(query)))
{
foreach (var versionEntry in versions)
{
results.Add(new PackageSearchResult
{
Name = pkgName,
Version = versionEntry.Key,
Description = versionEntry.Value.Description,
UploadDate = versionEntry.Value.UploadDate
});
}
}
}
await WriteJsonResponseAsync(context, results);
}
private static async Task GetPackageDetailsAsync(HttpListenerContext context)
{
string path = context.Request.Url.AbsolutePath;
string packageName = path.Substring("/api/details/".Length);
if (packageDb.ContainsKey(packageName))
{
await WriteJsonResponseAsync(context, packageDb[packageName]);
}
else
{
context.Response.StatusCode = 404;
await WriteJsonResponseAsync(context, new { error = "Package not found" });
}
}
private static async Task DownloadPackageAsync(HttpListenerContext context)
{
string path = context.Request.Url.AbsolutePath;
string[] parts = path.Substring("/api/download/".Length).Split('/');
if (parts.Length != 2)
{
context.Response.StatusCode = 400;
await WriteJsonResponseAsync(context, new { error = "Invalid request format" });
return;
}
// URL decode the package name
string requestedPackageName = WebUtility.UrlDecode(parts[0]);
string requestedVersion = WebUtility.UrlDecode(parts[1]);
Console.WriteLine($"Download request for package: '{requestedPackageName}', version: '{requestedVersion}'");
// Debug: List all packages in the database
Console.WriteLine("Available packages in database:");
foreach (var pkg in packageDb)
{
Console.WriteLine($"- {pkg.Key}");
foreach (var ver in pkg.Value)
{
Console.WriteLine($" - {ver.Key} => {ver.Value.FilePath}");
}
}
// Try to find a matching package name (case-insensitive and partial match)
string matchedPackageName = null;
// First try exact match (case-insensitive)
matchedPackageName = packageDb.Keys
.FirstOrDefault(k => string.Equals(k, requestedPackageName, StringComparison.OrdinalIgnoreCase));
// If no exact match, try contains match
if (matchedPackageName == null)
{
matchedPackageName = packageDb.Keys
.FirstOrDefault(k => k.IndexOf(requestedPackageName, StringComparison.OrdinalIgnoreCase) >= 0 ||
requestedPackageName.IndexOf(k, StringComparison.OrdinalIgnoreCase) >= 0);
}
// If still no match, try word matching (e.g., "Rifle" would match "Pro Rifle Pack")
if (matchedPackageName == null)
{
string[] requestedWords = requestedPackageName.Split(new[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var pkgName in packageDb.Keys)
{
string[] pkgWords = pkgName.Split(new[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries);
// Check if any word in the requested name matches any word in the package name
if (requestedWords.Any(rw => pkgWords.Any(pw => string.Equals(rw, pw, StringComparison.OrdinalIgnoreCase))))
{
matchedPackageName = pkgName;
break;
}
}
}
if (matchedPackageName != null)
{
Console.WriteLine($"Found matching package: '{matchedPackageName}'");
// Check if the version exists (case-insensitive)
string matchedVersion = packageDb[matchedPackageName].Keys
.FirstOrDefault(v => string.Equals(v, requestedVersion, StringComparison.OrdinalIgnoreCase));
if (matchedVersion != null)
{
string filePath = packageDb[matchedPackageName][matchedVersion].FilePath;
if (File.Exists(filePath))
{
Console.WriteLine($"Sending file: {filePath}");
byte[] fileData = File.ReadAllBytes(filePath);
context.Response.ContentType = "application/zip";
context.Response.ContentLength64 = fileData.Length;
context.Response.AddHeader("Content-Disposition", $"attachment; filename=\"{Path.GetFileName(filePath)}\"");
await context.Response.OutputStream.WriteAsync(fileData, 0, fileData.Length);
context.Response.Close();
return;
}
else
{
Console.WriteLine($"File not found: {filePath}");
context.Response.StatusCode = 404;
await WriteJsonResponseAsync(context, new { error = "Package file not found on disk" });
return;
}
}
else
{
Console.WriteLine($"Version not found: {requestedVersion}");
// Try to find the most recent version if the exact version wasn't found
if (packageDb[matchedPackageName].Count > 0)
{
// Get the most recent version based on upload date
var mostRecentVersion = packageDb[matchedPackageName]
.OrderByDescending(v => v.Value.UploadDate)
.First();
string filePath = mostRecentVersion.Value.FilePath;
if (File.Exists(filePath))
{
Console.WriteLine($"Sending most recent version instead: {mostRecentVersion.Key}");
byte[] fileData = File.ReadAllBytes(filePath);
context.Response.ContentType = "application/zip";
context.Response.ContentLength64 = fileData.Length;
context.Response.AddHeader("Content-Disposition", $"attachment; filename=\"{Path.GetFileName(filePath)}\"");
await context.Response.OutputStream.WriteAsync(fileData, 0, fileData.Length);
context.Response.Close();
return;
}
}
context.Response.StatusCode = 404;
await WriteJsonResponseAsync(context, new { error = "Version not found" });
return;
}
}
else
{
Console.WriteLine($"No matching package found for: {requestedPackageName}");
context.Response.StatusCode = 404;
await WriteJsonResponseAsync(context, new { error = "Package not found" });
return;
}
}
private static async Task UploadPackageAsync(HttpListenerContext context)
{
Console.WriteLine("Upload request received");
if (!context.Request.ContentType.StartsWith("multipart/form-data"))
{
context.Response.StatusCode = 400;
await WriteJsonResponseAsync(context, new { success = false, message = "Expected multipart/form-data" });
return;
}
// Parse multipart form data
var formData = await ParseMultipartFormDataAsync(context.Request);
// Get form fields
string name = formData.ContainsKey("name") ? formData["name"].ToString() : null;
string version = formData.ContainsKey("version") ? formData["version"].ToString() : null;
string description = formData.ContainsKey("description") ? formData["description"].ToString() : null;
byte[] fileData = formData.ContainsKey("file") ? (byte[])formData["file"] : null;
Console.WriteLine($"Processing upload: {name}, {version}, {description}, File size: {(fileData?.Length ?? 0)} bytes");
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(version) ||
string.IsNullOrEmpty(description) || fileData == null || fileData.Length == 0)
{
context.Response.StatusCode = 400;
await WriteJsonResponseAsync(context, new
{
success = false,
message = $"Missing required fields: name={!string.IsNullOrEmpty(name)}, version={!string.IsNullOrEmpty(version)}, description={!string.IsNullOrEmpty(description)}, file={fileData != null && fileData.Length > 0}"
});
return;
}
try
{
// Create package directory if it doesn't exist
string pkgDir = Path.Combine(PACKAGES_DIR, name);
if (!Directory.Exists(pkgDir))
{
Directory.CreateDirectory(pkgDir);
}
// Create zip file with version and date in the name
string today = DateTime.Now.ToString("yyyyMMdd");
string zipFilename = $"{name}-{version}-{today}.zip";
string zipPath = Path.Combine(pkgDir, zipFilename);
// Save the uploaded file
File.WriteAllBytes(zipPath, fileData);
Console.WriteLine($"File saved to {zipPath}");
// Update database
if (!packageDb.ContainsKey(name))
{
packageDb[name] = new Dictionary<string, PackageVersionInfo>();
}
packageDb[name][version] = new PackageVersionInfo
{
Description = description,
FilePath = zipPath,
UploadDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
// Save database immediately
SaveDatabase();
Console.WriteLine($"Database updated for {name} {version}");
await WriteJsonResponseAsync(context, new { success = true });
}
catch (Exception ex)
{
Console.WriteLine($"Error in upload: {ex.Message}");
context.Response.StatusCode = 500;
await WriteJsonResponseAsync(context, new { success = false, message = $"Server error: {ex.Message}" });
}
}
private static async Task UploadPackageJsonAsync(HttpListenerContext context)
{
Console.WriteLine("JSON Upload request received");
if (context.Request.ContentType != "application/json")
{
context.Response.StatusCode = 400;
await WriteJsonResponseAsync(context, new { success = false, message = "Expected application/json content type" });
return;
}
// Read the request body
string requestBody;
using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
{
requestBody = await reader.ReadToEndAsync();
}
Console.WriteLine($"Received JSON data of length: {requestBody.Length}");
try
{
// Parse the JSON
var uploadData = JsonSerializer.Deserialize<UploadJsonRequest>(requestBody);
// Validate required fields
if (string.IsNullOrEmpty(uploadData.name) ||
string.IsNullOrEmpty(uploadData.version) ||
string.IsNullOrEmpty(uploadData.description) ||
string.IsNullOrEmpty(uploadData.fileData))
{
context.Response.StatusCode = 400;
await WriteJsonResponseAsync(context, new
{
success = false,
message = $"Missing required fields: name={!string.IsNullOrEmpty(uploadData.name)}, " +
$"version={!string.IsNullOrEmpty(uploadData.version)}, " +
$"description={!string.IsNullOrEmpty(uploadData.description)}, " +
$"fileData={!string.IsNullOrEmpty(uploadData.fileData)}"
});
return;
}
// Decode the base64 file data
byte[] fileData;
try
{
fileData = Convert.FromBase64String(uploadData.fileData);
Console.WriteLine($"Decoded file data, size: {fileData.Length} bytes");
}
catch (Exception ex)
{
context.Response.StatusCode = 400;
await WriteJsonResponseAsync(context, new { success = false, message = $"Invalid base64 file data: {ex.Message}" });
return;
}
// Create package directory if it doesn't exist
string pkgDir = Path.Combine(PACKAGES_DIR, uploadData.name);
if (!Directory.Exists(pkgDir))
{
Directory.CreateDirectory(pkgDir);
}
// Create zip file with version and date in the name
string today = DateTime.Now.ToString("yyyyMMdd");
string zipFilename = $"{uploadData.name}-{uploadData.version}-{today}.zip";
string zipPath = Path.Combine(pkgDir, zipFilename);
// Save the uploaded file
File.WriteAllBytes(zipPath, fileData);
Console.WriteLine($"File saved to {zipPath}");
// Update database
if (!packageDb.ContainsKey(uploadData.name))
{
packageDb[uploadData.name] = new Dictionary<string, PackageVersionInfo>();
}
packageDb[uploadData.name][uploadData.version] = new PackageVersionInfo
{
Description = uploadData.description,
FilePath = zipPath,
UploadDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
// Save database immediately
SaveDatabase();
Console.WriteLine($"Database updated for {uploadData.name} {uploadData.version}");
// Debug: Show current database state
Console.WriteLine("Current database state:");
foreach (var pkg in packageDb)
{
Console.WriteLine($"- {pkg.Key}");
foreach (var ver in pkg.Value)
{
Console.WriteLine($" - {ver.Key} => {ver.Value.FilePath}");
}
}
await WriteJsonResponseAsync(context, new { success = true });
}
catch (JsonException ex)
{
Console.WriteLine($"JSON parsing error: {ex.Message}");
context.Response.StatusCode = 400;
await WriteJsonResponseAsync(context, new { success = false, message = $"Invalid JSON format: {ex.Message}" });
}
catch (Exception ex)
{
Console.WriteLine($"Error in upload: {ex.Message}");
context.Response.StatusCode = 500;
await WriteJsonResponseAsync(context, new { success = false, message = $"Server error: {ex.Message}" });
}
}
private static async Task<Dictionary<string, object>> ParseMultipartFormDataAsync(HttpListenerRequest request)
{
var formData = new Dictionary<string, object>();
try
{
// Get the boundary from the content type
string boundary = GetBoundary(request.ContentType);
if (string.IsNullOrEmpty(boundary))
{
Console.WriteLine("Could not find boundary in content type");
return formData;
}
boundary = "--" + boundary;
// Read the entire request body
byte[] requestBytes;
using (var memoryStream = new MemoryStream())
{
await request.InputStream.CopyToAsync(memoryStream);
requestBytes = memoryStream.ToArray();
}
// Convert to string for header parsing
string requestData = Encoding.UTF8.GetString(requestBytes);
// Split by boundary
string[] parts = requestData.Split(new[] { boundary }, StringSplitOptions.None);
foreach (string part in parts)
{
if (string.IsNullOrEmpty(part) || part == "--\r\n" || part == "--")
continue;
// Find the headers section
int headerEnd = part.IndexOf("\r\n\r\n");
if (headerEnd < 0)
continue;
string headers = part.Substring(0, headerEnd);
// Extract name from headers
int nameStart = headers.IndexOf("name=\"") + 6;
if (nameStart < 6)
continue;
int nameEnd = headers.IndexOf("\"", nameStart);
if (nameEnd < 0)
continue;
string name = headers.Substring(nameStart, nameEnd - nameStart);
// Check if this is a file
bool isFile = headers.Contains("filename=\"");
string filename = "";
if (isFile)
{
int filenameStart = headers.IndexOf("filename=\"") + 10;
if (filenameStart >= 10)
{
int filenameEnd = headers.IndexOf("\"", filenameStart);
if (filenameEnd > 0)
{
filename = headers.Substring(filenameStart, filenameEnd - filenameStart);
}
}
}
// Get content start position in bytes
int contentStartPos = Encoding.UTF8.GetByteCount(part.Substring(0, headerEnd + 4));
// Get content end position
int contentEndPos;
if (part.EndsWith("\r\n"))
{
contentEndPos = requestBytes.Length - Encoding.UTF8.GetByteCount("\r\n");
}
else
{
contentEndPos = requestBytes.Length;
}
// Extract content
if (isFile && !string.IsNullOrEmpty(filename))
{
// For files, extract binary data
int contentLength = part.Length - headerEnd - 4;
if (contentLength > 0)
{
byte[] fileData = new byte[contentLength];
Array.Copy(requestBytes, contentStartPos, fileData, 0, contentLength);
formData[name] = fileData;
Console.WriteLine($"Extracted file '{filename}' with {fileData.Length} bytes");
}
}
else
{
// For text fields
string content = part.Substring(headerEnd + 4);
if (content.EndsWith("\r\n"))
{
content = content.Substring(0, content.Length - 2);
}
formData[name] = content;
Console.WriteLine($"Extracted field '{name}' with value '{content}'");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error parsing multipart form data: {ex.Message}");
}
return formData;
}
private static string GetBoundary(string contentType)
{
if (string.IsNullOrEmpty(contentType))
return null;
int index = contentType.IndexOf("boundary=");
if (index < 0)
return null;
return contentType.Substring(index + 9);
}
private static async Task WriteJsonResponseAsync(HttpListenerContext context, object data)
{
string json = JsonSerializer.Serialize(data);
byte[] buffer = Encoding.UTF8.GetBytes(json);
context.Response.ContentType = "application/json";
context.Response.ContentLength64 = buffer.Length;
await context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
context.Response.Close();
}
}
public class PackageVersionInfo
{
[JsonPropertyName("description")]
public string Description { get; set; }
[JsonPropertyName("file_path")]
public string FilePath { get; set; }
[JsonPropertyName("upload_date")]
public string UploadDate { get; set; }
}
public class PackageSearchResult
{
[JsonPropertyName("name")]
public string Name { get; set; }