forked from nanoboard/nanoboard
/
DbApiHandler.cs
2139 lines (1886 loc) · 101 KB
/
DbApiHandler.cs
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.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Linq;
using NDB;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using nboard;
using nbpack;
using System.Drawing.Imaging; //make ImageFormat.Png working, to save bmp as PNG...
using fractalgen;
using captcha; //get random captcha or solve this
namespace NServer
{
/*
Gives access to various DB and server functions, mainly related to reading/writing posts.
*/
class DbApiHandler : IRequestHandler
{
private PostDb _db;
private Dictionary<string, Func<string,string,HttpResponse>> _handlers;
private Dictionary<string, Func<string,byte[],HttpResponse>> _handlers_bin;
private bool is_lite = false; //True, when this is request on lite-server, or false, if this is request on full-server.
public DbApiHandler(PostDb db, bool lite = false, bool SetAllowReput = false, bool SetByPassValidation = false)
{
_db = db;
_handlers = new Dictionary<string, Func<string, string, HttpResponse>>();
_handlers_bin = new Dictionary<string, Func<string, byte[], HttpResponse>>();
is_lite = lite; allowReput = SetAllowReput; bypassValidation = SetByPassValidation; //set this once, for current Instance.
if(lite == false){ //all actions allowed for full server
// filling handlers dictionary with actions that will be called with (request address, request content) args:
_handlers["get"] = GetPostByHash;
_handlers["GetDbAsJSON"] = GetDbAsJSON;
_handlers["captcha-index"] = GetCaptchaAsJsonByIndex;
_handlers["getlastn"] = GetLastNPosts; //from client 3.1 //get last n posts in thread, or last 3 threads in category, to show this.
_handlers["delete"] = DeletePost;
_handlers["add"] = AddPost;
_handlers["download-posts"] = Download_Posts; //download posts by url, from another nanodb server.
_handlers["upload-posts"] = Upload_Posts; //upload posts to another nanodb server, like Bitmessage-retranslation.
_handlers["upload-post"] = Upload_Post; //upload posts to another nanodb server, like Bitmessage-retranslation.
_handlers["addmany"] = AddPosts;
_handlers["readd"] = ReAddPost;
_handlers["replies"] = GetReplies;
//get posts by list with hashes.
//posts can be parsed from responce
//GET-query (bytesize limit): http://127.0.0.1:7346/api/getposts/f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35
//POST-query: http://127.0.0.1:7346/api/getposts/POST/ with value "f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35"
_handlers["getposts"] = GetPosts; //get list of specified posts. Return array with posts in JSON.
_handlers["count"] = GetPostCount;
// _handlers["count"] = GetPostCount; //on lite-server, show number of posts, without "deleted_once"-posts.
_handlers["nget"] = GetNthPost;
//GET: http://127.0.0.1:7346/api/prange/?fromto=0-20 - JSON response with array of posts
//POST: http://127.0.0.1:7346/api/prange/0-20/ - array with posts
//GET: http://127.0.0.1:7346/api/prange/?fromto=0-20&only_hashes=only_hashes - array with hashes only.
//POST: http://127.0.0.1:7346/api/prange/0-20/ string="only_hashes" - array with hashes only.
_handlers["prange"] = GetPresentRange;
_handlers["pcount"] = GetPresentCount;
_handlers["search"] = Search;
_handlers["paramset"] = ParamSet;
_handlers["paramget"] = ParamGet; //get-set (if uncommented). On lite-server get only. And need to make not all params to get.
_handlers["params"] = Params;
_handlers["find-thread"] = FindThread;
_handlers["threadsize"] = ThreadSize;
//GET and POST parameters available. http://127.0.0.1:7346/api/getposts/POST/OnlyRAM|save_files|10 (last value - is limit for connectoins)
_handlers["png-collect"] = PngCollect;
_handlers["download-png"] = DownloadPNG;
//posts from queue can be packed, using link:
//http://127.0.0.1:7346/api/png-create/f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35
//or using POST-query to http://127.0.0.1:7346/api/png-create/
//with value "f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35"
_handlers["png-create"] = PngCreate; //create png with post from queue or not (depending from parameters).
_handlers["run-nbpack"] = Run_nbpack_with_params; //Run NBPack.cs with parameters, specified in url.
//URL must contains parameters, joined with comma, after EncodeURIComponent();
_handlers["convert-to-PNG"] = ConvertToPNG; //Upload custom picture, convert this to PNG and save in the folder default folder "download/generated" or "containers"
_handlers["png-collect-avail"] = (a,b)=>new HttpResponse(_collectAvail ? StatusCode.Ok : StatusCode.NotFound, _collectAvail ? "Finished." : "Collect...");
_handlers["png-create-avail"] = (a,b)=>new HttpResponse(_createAvail ? StatusCode.Ok : StatusCode.NotFound, _createAvail ? "Finished." : "Creating PNG...");
_handlers["get_reports"] = GetReports;
_handlers["delete_reports"] = DeleteReportsForPostHash;
_handlers["undelete_post"] = UndeletePost; //undelete post, after this was been "deleted_once" on full-server: http://127.0.0.1:7346/api/undelete_post/{POST_HASH}
_handlers["random_captcha"] = RandomCaptcha; //GET random captcha or solve this (POST)
_handlers ["echo-string"] = EchoString; //return the same string, utf-8
_handlers ["echo-binary-string"] = EchoBinaryString; //return bytes of binary string
_handlers_bin ["echo-bytes"] = EchoBytes; //return bytes back
}
else if(lite == true){ //not all actions allowed for lite-server to run this by another anonymous users.
/*
to make able anonymous to create PNG, need to enable params, paramget (places), createPNG, and pnt-create-avail.
and also generate fractal, and make able to save this locally, using base64 dataURL, or blob-object.
Now, this all just is not available, and maybe this will be available in future.
*/
// filling handlers dictionary with actions that will be called with (request address, request content) args:
_handlers["get"] = GetPostByHash;
_handlers["GetDbAsJSON"] = GetDbAsJSON;
_handlers["captcha-index"] = GetCaptchaAsJsonByIndex;
_handlers["getlastn"] = GetLastNPosts; //from client 3.1 //get last n posts in thread, or last 3 threads in category, to show this.
// _handlers["delete"] = DeletePost;
//Another anonymous can delete all another posts, locally, and send reports for the posts to delete this from the server, by admin.
//This is strongly not recommended, because Article 19 of "Universal Declaration of Human Rights (UDHR)",
//but sometimes need to delete posts, for example, after attack server by using wipe-attack by using intensive shitposting.
// Warning!
// If anyone is not happy with the perversed level of censorship with some admin,
// anyone can raise his own server, and talk about anything there, himself, and with his friends.
_handlers["report"] = ReportPost;
_handlers["add"] = AddPost; //when bypassValidation == true, posts without captcha can be accepted on lite-server. enable allowReput to use this instead "readd"
// _handlers["download-posts"] = Download_Posts; //disabled for lite-server, because this posts downoloaded in database on full-server.
_handlers["upload-posts"] = Upload_Posts; //This option is enabled for lite-server. Posts can be uploaded. PostDb.cs: public bool PutPost(Post p, bool allowReput = false, bool bypassValidation = false).
_handlers["upload-post"] = Upload_Post; //This option is enabled for lite-server. Post can be uploaded. PostDb.cs: public bool PutPost(Post p, bool allowReput = false, bool bypassValidation = false).
_handlers["addmany"] = AddPosts; //posts without captcha added only when bypassValidation == true;
// _handlers["readd"] = ReAddPost; //disable this on lite-server to don't allow add deleted posts, and restore the posts with wipe. Use just add, when allowReput enabled on the full-server.
_handlers["replies"] = GetReplies;
//get posts by list with hashes.
//posts can be parsed from responce
//GET-query (bytesize limit): http://127.0.0.1:7346/api/getposts/f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35
//POST-query: http://127.0.0.1:7346/api/getposts/POST/ with value "f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35"
_handlers["getposts"] = GetPosts; //get list of specified posts. Return array with posts in JSON.
_handlers["count"] = GetPostCount;
_handlers["nget"] = GetNthPost;
//GET: http://127.0.0.1:7346/api/prange/?fromto=0-20 - JSON response with array of posts
//POST: http://127.0.0.1:7346/api/prange/0-20/ - array with posts
//GET: http://127.0.0.1:7346/api/prange/?fromto=0-20&only_hashes=only_hashes - array with hashes only.
//POST: http://127.0.0.1:7346/api/prange/0-20/ string="only_hashes" - array with hashes only.
_handlers["prange"] = GetPresentRange;
_handlers["pcount"] = GetPresentCount;
_handlers["search"] = Search;
// _handlers["paramset"] = ParamSet;
_handlers["paramget"] = ParamGet_lite; //this can be available for lite-server, to see places and send container there.
// _handlers["params"] = Params; //some params can be available on lite-server, for example, params and paramget. To see places and send container there.
_handlers["find-thread"] = FindThread;
_handlers["threadsize"] = ThreadSize;
//GET and POST parameters available. http://127.0.0.1:7346/api/getposts/POST/OnlyRAM|save_files|10 (last value - is limit for connectoins)
// _handlers["png-collect"] = PngCollect;
_handlers["download-png"] = DownloadPNG;
//posts from queue can be packed, using link:
//http://127.0.0.1:7346/api/png-create/f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35
//or using POST-query to http://127.0.0.1:7346/api/png-create/
//with value "f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35"
_handlers["png-create"] = PngCreate; //create png with post from queue or not (depending from parameters). //This can be available on lite-server to create PNG, but need to make able to save this not in the server-folder, but in browser.
_handlers["run-nbpack"] = Run_nbpack_with_params; //Run NBPack.cs with parameters, specified in url.
//URL must contains parameters, joined with comma, after EncodeURIComponent();
_handlers["convert-to-PNG"] = ConvertToPNG; //Upload custom picture, convert this to PNG and save it the "download/generated/" folder.
// _handlers["png-collect-avail"] = (a,b)=>new HttpResponse(_collectAvail ? StatusCode.Ok : StatusCode.NotFound, _collectAvail ? "Finished." : "Collect...");
_handlers["png-create-avail"] = (a,b)=>new HttpResponse(_createAvail ? StatusCode.Ok : StatusCode.NotFound, _createAvail ? "Finished." : "Creating PNG...");
_handlers["random_captcha"] = RandomCaptcha; //GET random captcha or solve this (POST)
// _handlers ["echo-string"] = EchoString; //return the same string, utf-8
// _handlers ["echo-binary-string"] = EchoBinaryString; //return bytes of binary string encoded with iso-8859-1
// _handlers_bin ["echo-bytes"] = EchoBytes; //return bytes back
}
Set_Timer_To_Delete_Generated_Images();
}
private static bool allowReput = false;
private static bool bypassValidation = false;
/*//true if need to make bypassValidation for posts in containers, while png-collect is processing.*/
//echo string, in any encoding.
private HttpResponse EchoString(string stringGET = null, string stringPOST = null){
string data = param_GET_POST(stringGET, stringPOST);
return new HttpResponse(StatusCode.Ok, data);
}
//echo binary string, as binary response
private HttpResponse EchoBinaryString(string stringGET = null, string stringPOST = null){
string data = param_GET_POST(stringGET, stringPOST);
//Console.WriteLine("EchoBinaryString: data.Length: "+data.Length);
System.Text.Encoding encoding = null;
if(isLatin1String(data)){
encoding = System.Text.Encoding.GetEncoding("iso-8859-1");
}
else{
encoding = System.Text.Encoding.UTF8;
}
//Console.WriteLine("DbApiHandler. EchoBinaryString: data.Length: "+data.Length+", encoding: "+encoding);
return new HttpResponse(StatusCode.Ok, data, MimeType.Mime("App", encoding), true, encoding); //just return back, the binary string with bytes, in HTTP response.
//"true", means response is binary, even if this is a string.
}
//echo bytes from binary request.
private HttpResponse EchoBytes(string path = null, byte[] bytes = null){
//Console.WriteLine("EchoBytes: bytes.Length: "+bytes.Length+", bytes: "+BitConverter.ToString( bytes ).Replace("-", string.Empty));
try{
return new HttpResponse(StatusCode.Ok, bytes, MimeType.Mime("App")); //just return binary data in HTTP response.
}catch(Exception ex){
Console.WriteLine(ex);
return new HttpResponse(StatusCode.Ok, ex.Message, MimeType.Mime("Html")); //just return binary data in HTTP response.
}
}
//C#: check is string "iso-8859-1"-encoded or not (true/false)
public static bool isLatin1String(string str){
lock (_lockObj)
{
return (
Regex.Match(
str //for each char in str
, @"[^\u0000-\u00FF]" //try to match not latin1-char, and stop
, RegexOptions.None
)
.Length //then take Length of result
) == 0 //and compare this to 0
; //true/false (non-latin character was not found or was found)
}
}
// JavaScript:
//function isLatin1String(str){return (str.match(/[^\u0000-\u00FF]/) === null);} //check is string "iso-8859-1"-encoded or not (true/false)
/*
This method return string with parameters, from request parameters.
It return first or second parameter of request query (if second is available).
*/
private string param_GET_POST(string GET = null, string POST = null){
lock (_lockObj){
if( (POST == null) || (POST == "")){ //if second parameter is empty
// return GET; //return first parameter from GET-query, as string with parameters.
return Uri.UnescapeDataString(GET); //Try to unescape (make decodeURIComponent), if this param was been escaped by encodeURIComponent. Else return GET as is.
}
else{
return POST; //return second parameter from POST-query, as string with parameters.
}
}
}
private string SearchUp(string hash, string categoriesHash, string previousHash)
{
var p = _db.GetPost(hash);
if (p == null)
{
return null;
}
if (p.replyto == categoriesHash)
{
return previousHash;
}
previousHash = p.hash;
hash = p.replyto;
return SearchUp(hash, categoriesHash, previousHash);
}
private HttpResponse FindThread(string postHash, string categoriesHash)
{
var prev = postHash;
var result = SearchUp(postHash, categoriesHash, prev);
return new HttpResponse(result == null ? StatusCode.NotFound : StatusCode.Ok, result ?? "");
}
public static Dictionary<string, string> GetParamValue(string GET_STRING){
Dictionary<string, string> KeyValue = new Dictionary<string, string>();
string[] parameters = GET_STRING.Split('&');
for(var i = 0; i<parameters.Length; i++){
//add this into dictionary
KeyValue.Add(
Uri.UnescapeDataString( (parameters[i]).Split('=')[0] )
, Uri.UnescapeDataString( (parameters[i]).Split('=')[1] )
);
}
//and return dictionary.
return KeyValue;
}
private HttpResponse RandomCaptcha(string GET = "", string POST = ""){
string response = "";
if(POST == ""){
response =
@"<form action=""/api/random_captcha"" method=""post"">"+
captcha.Captcha.AddInputsOfRandomCaptchaToSomeForm()
+@"<button type=""submit"">Solve</button></form>"
;
}else{
Console.WriteLine("POST: "+POST);
Dictionary<string, string> ParamValue = GetParamValue(POST);
string RandomCaptchaGuid = ParamValue["RandomCaptchaGuid"] ;
string RandomCaptchaGuess = ParamValue["RandomCaptchaGuess"] ;
bool result = captcha.Captcha.CheckAnswer(RandomCaptchaGuid, RandomCaptchaGuess);
Console.WriteLine("result: "+result);
response = result.ToString();
}
return new HttpResponse(StatusCode.Ok, response, "text/html");
}
private HttpResponse GetCaptchaAsJsonByIndex(string GET = "", string POST = ""){
string CaptchaIndex = param_GET_POST(GET, POST); //string with queue_and_image, joined with comma. Default value is null, and this string is empty.
string response = captcha.Captcha.GetCaptchaAsJsonByIndex(CaptchaIndex);
return new HttpResponse(StatusCode.Ok, response);
}
private HttpResponse DownloadPNG(string GET = "", string POST = "") //default queue value is null
{
string params_ = param_GET_POST(GET, POST); //string with queue_and_image, joined with comma. Default value is null, and this string is empty.
string [] p = params_.Split('|');
var obj_Aggregator = new Aggregator(new string [] {p[0], "collect_using_RAM", "save_files", "1" /*max_connections*/});
return new HttpResponse(StatusCode.Ok, obj_Aggregator.DownloadPNG(p, is_lite));
}
private bool _collectAvail = true;
private bool _createAvail = true;
private HttpResponse PngCollect(string GET="", string POST = "")
{
string parameters = param_GET_POST(GET, POST); // string parameters = Uri.UnescapeDataString(param_GET_POST(GET, POST));
Console.WriteLine("\nPngCollect was been runned, wait the end of collect images...");
Console.WriteLine("parameters: " + parameters);
string [] _params_ = (parameters!="") ? parameters.Split('|') : new string[0];
if(allowReput == true){_params_ = _params_.Concat(new string[] { "allowReput" }).ToArray();}if(bypassValidation == true){_params_ = _params_.Concat(new string[] { "bypassValidation" }).ToArray();}
_collectAvail = false;
AggregatorMain.Run(_params_);
ThreadPool.QueueUserWorkItem(o =>
{
while(AggregatorMain.Running)
{
Thread.Sleep(1000);
}
_collectAvail = true;
});
return new HttpResponse(StatusCode.Ok, "");
}
/*
private void ParseContainers()
{
NBPackMain.Main(new []{"-a",
"http://"
+ Configurator.Instance.GetValue("ip", "127.0.0.1")
+ ":"
+ Configurator.Instance.GetValue("port", "7346"),
Configurator.Instance.GetValue("password", "nano")});
}
*/
//params in one string, joined with comma, after JavaScript EncodeURIComponent().
//See NBPack.cs guide, about it.
private HttpResponse Run_nbpack_with_params(string GET = "", string POST = "")
{
//Console.WriteLine("GET {0}, POST {1}", GET, POST);
string parameters = param_GET_POST(GET, POST); //string parameters = Uri.UnescapeDataString(param_GET_POST(GET, POST)); //string with queue_and_image, joined with comma. Default value is null, and this string is empty.
string[] p = parameters.Split('|');
Console.Write("Start: nbpack");
for(int i=0; i<p.Length;i++){
if( //if dataURL
p[i].IndexOf("data:") != -1
&& p[i].IndexOf("base64,")!= -1
&& nbpack.NBPackMain.IsBase64Encoded(p[i].Split(',')[1])
){
Console.Write(" {0}", p[i].Substring(0, 40) + "...");
}else{
Console.Write(" {0}", p[i]);
}
}
Console.Write("\n");
_createAvail = false;
ThreadPool.QueueUserWorkItem(o =>
{
NBPackMain.Main(p);
_createAvail = true;
});
string status;
if(p.Length<=2) {status = "Specify parameters, after encodeURIcomponent, in the end of URL.\n" +
"Example: http://127.0.0.1:7346/api/run-nbpack/-u%2Cdownload%2Fcontainer_PNG.png%2Cnano3%2Cdownload%2Fcontainer_posts.json";}
else if(p[0]=="-u" && p.Length == 3){
status = JsonConvert.SerializeObject(nbpack.NBPackMain.Unpack(p[1], p[2])); //return posts in JSON-response.
//e.g.: http://127.0.0.1:7346/api/run-nbpack/-u%2Cdownload%2Fcontainer_PNG.png%2Cnano3
}
else{status = "OK";}
Console.WriteLine("Done.");
return new HttpResponse(StatusCode.Ok, status);
}
public static string notifications_with_filename = "";
//posts from queue can be packed, using link:
//http://127.0.0.1:7346/api/png-create/f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35
//or using POST-query to http://127.0.0.1:7346/api/png-create/
//with value "f682830a470200d738d32c69e6c2b8a4,cd94a3d60f2f521806abebcd3dc3f549,bdd4b5fc1b3a933367bc6830fef72a35"
private HttpResponse PngCreate(string GET = "", string POST = "") //default queue value is null
{
string queue_and_image = param_GET_POST(GET, POST); //string with queue_and_image, joined with comma. Default value is null, and this string is empty.
_createAvail = false;
ThreadPool.QueueUserWorkItem(o =>
{
NBPackMain.Main(new []
{"-g",
(
(is_lite == true)
? "CreatePNG_on_lite_server"
: (
"http://"
+ Configurator.Instance.GetValue("ip", "127.0.0.1")
+ ":"
+ Configurator.Instance.GetValue("port", "7346")
)
)
,
Configurator.Instance.GetValue("password", Configurator.DefaultPass),
queue_and_image //add queue_and_image as parameter
});
_createAvail = true;
});
while(_createAvail!=true){
Thread.Sleep(1000);
}
Thread.Sleep(5000);
HttpResponse result = new HttpResponse(StatusCode.Ok, notifications_with_filename);//return new HttpResponse(StatusCode.Ok, "PNG successfully created.");
notifications_with_filename = "";
return result;
}
public static Dictionary<string, DateTime> datetime_of_generated_images = new Dictionary<string, DateTime>();
public static System.Timers.Timer timer_to_delete;
public static double milliseconds_to_delete_generated_images = 1800000; /*(30 minutes is maximum, to store images in "download/generated", and "download/created")*/
private void Set_Timer_To_Delete_Generated_Images()
{
timer_to_delete = new System.Timers.Timer(milliseconds_to_delete_generated_images);
timer_to_delete.Elapsed += Delete_Old_Generated_Files;
timer_to_delete.AutoReset = false;
timer_to_delete.Enabled = true;
}
private void Delete_Old_Generated_Files(Object source, System.Timers.ElapsedEventArgs e)
{
if(datetime_of_generated_images.Count == 0){
timer_to_delete.Stop();
foreach(System.IO.FileInfo file in ((System.IO.DirectoryInfo) new DirectoryInfo("download"+Path.DirectorySeparatorChar+"generated")).GetFiles()){file.Delete();}
foreach(System.IO.FileInfo file in ((System.IO.DirectoryInfo) new DirectoryInfo("download"+Path.DirectorySeparatorChar+"created")).GetFiles()){file.Delete();}
}
else{
timer_to_delete.Start();
foreach(KeyValuePair<string, DateTime> generated_image in datetime_of_generated_images){
TimeSpan file_stored_time = (DateTime.Now).Subtract(generated_image.Value);
if(TimeSpan.Compare(file_stored_time, TimeSpan.FromMilliseconds( milliseconds_to_delete_generated_images )) >= 0){
if(
File.Exists(generated_image.Key)
){
( (System.IO.FileInfo) new FileInfo( generated_image.Key ) ).Delete();
datetime_of_generated_images.Remove(generated_image.Key);
}
}
}
}
}
private HttpResponse generate_PNG(string query, string pathway = "")//?generate,1920,1080
{
//Console.WriteLine("args[0] = {0}, == \"\"", args[0], (args[0]==""));
int width = 0; int height = 0; int bitlength = 0;
//if pathway not specified, and if this was been request on lite-server, use "download/generated"-folder, to let them download it.
//Else - "containers"-folder, for full-server, or using the specified pathway.
pathway = ((pathway=="") ? ( (is_lite == true) ? "download"+Path.DirectorySeparatorChar+"generated" : "containers" ) : pathway );
string [] splitted;
string fractalgen_result = "";
splitted = query.Split('|');
string error = "Too many parameters.\nUse comma, as delimiter" +
"\"../api/convert-to-PNG/?generate_PNG,PNG_WIDTH,PNG_HEIGHT\"," +
"or \"../api/convert-to-PNG/generate_PNG/generate_PNG,bitlength_to_pack\"";
if(splitted.Length>5){
return new HttpResponse(StatusCode.BadRequest, error);
}
else if(splitted.Length==5 && splitted[1]=="fractalgen"){
fractalgen_result = fractalgen.Program.Main_(new string[]{/*splitted[2],*/ pathway, splitted[3], splitted[4]});
}
else if(splitted.Length==4 && splitted[1]=="fractalgen"){
fractalgen_result = fractalgen.Program.Main_(new string[]{ /*add this*/pathway, splitted[2], splitted[3]});
}
else if(splitted.Length==3 && splitted[1]=="fractalgen"){
fractalgen_result = fractalgen.Program.Main_(new string[]{splitted[2]});
}
else if(splitted.Length==2 && splitted[1]=="fractalgen"){
fractalgen_result = fractalgen.Program.Main_(new string[]{});
}if(fractalgen_result != ""){if(pathway == "download"+Path.DirectorySeparatorChar+"generated"){timer_to_delete.Start();}
string [] splitted_result = fractalgen_result.Split(new string[]{"Saved as: "}, StringSplitOptions.None); datetime_of_generated_images[splitted_result[1]] = DateTime.Now;
fractalgen_result = fractalgen_result+"<br><a href=\"../"
+ (splitted_result[1]).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
+ "\" target=\"_blank\">Open \""
+ (splitted_result[1]).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
+ "\" as image</a>,<br><a href=\"../"
+ (splitted_result[1]).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
+ "\" target=\"_blank\" download=\""
+ splitted_result[1].Split(Path.DirectorySeparatorChar).Last()
+ "\">Download "
+ splitted_result[1].Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Split(Path.AltDirectorySeparatorChar).Last()
+ " as file</a>"
;
return new HttpResponse(StatusCode.Ok, fractalgen_result);
}
else if(splitted.Length==4){
/*pathway = ((is_lite == true) ? pathway : splitted[1]); // use default pathway for to prevent change it, and make DDoS, by send many requests, and save many files... */
width=nbpack.NBPackMain.parse_number(splitted[2]);
height=nbpack.NBPackMain.parse_number(splitted[3]);}
else if(splitted.Length==3){width=nbpack.NBPackMain.parse_number(splitted[1]); height=nbpack.NBPackMain.parse_number(splitted[2]);}
else if(splitted.Length==2){
bitlength = nbpack.NBPackMain.parse_number(splitted[1]);
int side = 0;
if(bitlength!=0){
side = (int)Math.Ceiling((decimal)Math.Sqrt(((bitlength*8)/3)+32)); //side of square, from bitlength of message to pack
width = side; height = side;
}
}else{
return new HttpResponse(StatusCode.Ok, ("Specify PNG size to generate.\nUse comma, as delimiter: \n" +
"../api/convert-to-PNG/generate_PNG,PNG_WIDTH,PNG_HEIGHT\"," +
"or ../api/convert-to-PNG/?generate_PNG,bitlength_to_pack")
);
}
Console.WriteLine("Start generate PNG: width = " + width + ", heigth = " + height + ", bitlength = " + bitlength);
//after width and height values are defined...
Bitmap bmp = new Bitmap(width, height); //bitmap
Random rand = new Random(); //random number
for (int y = 0; y < height; y++) //create random pixels
{
for (int x = 0; x < width; x++)
{
//int a = rand.Next(256); //generate random ARGB value
int r = rand.Next(256); //Just generate RGB
int g = rand.Next(256);
int b = rand.Next(256);
//bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b)); //set ARGB value
bmp.SetPixel(x, y, Color.FromArgb(r, g, b)); //Just RGB.
}
}if(pathway == "download"+Path.DirectorySeparatorChar+"generated"){timer_to_delete.Start();}
var random_guid = Guid.NewGuid().ToString(); datetime_of_generated_images[(pathway + Path.DirectorySeparatorChar + random_guid + ".png")] = DateTime.Now;
bmp.Save( pathway + Path.DirectorySeparatorChar + random_guid + ".png", ImageFormat.Png); //png ImageFormat
return new
HttpResponse(
StatusCode.Ok,
(
"Random PNG generated successfully. Width = " + width + ", height = " + height + ".\n" +
"Generated image saved to: "+(pathway + Path.DirectorySeparatorChar + random_guid + ".png")+
"<br>"+
"<a href=\""+
(("../"+pathway + "/"+ random_guid + ".png").Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar))+
"\""+
"target=\"_blank\">"+
"Open \""+
(("../"+pathway + "/" + random_guid + ".png").Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar))+
"\" as image"+
"</a>,"+
"<br>"+
"<a href=\""+
(("../"+pathway + "/"+ random_guid + ".png").Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar))+
"\" target=\"_blank\" download=\""+
(random_guid + ".png")+
"\">"+
"Download \""+
(random_guid + ".png")
+"\" as file."+
"</a>"
)
);
}
private HttpResponse ConvertToPNG(string dataURL_GET = "", string dataURL_POST = "") //default queue value is null
{
//Console.WriteLine("first {0}, second {1}", dataURL_GET.Substring(0, (dataURL_GET.Length>40)?40:dataURL_GET.Length), dataURL_POST.Substring(0, (dataURL_POST.Length>40)?40:dataURL_POST.Length));
//test split string
//input string with parameters for png-create
//output - splitted and extracted values...
// int from_last_posts = 0;
string dataURL = "";
// string queue = ""; //string with hashes of posts, separated with comma.
// int from_queue = 0;
string queue_image_params = "";
// if( dataURL_GET.Trim()=="" && dataURL_POST.Trim()=="" ) {queue_image_params = "";}
// else if( dataURL_POST.Trim()=="" && dataURL_GET.Trim()!="" ) {queue_image_params = dataURL_GET;}
// else if( dataURL_POST.Trim()!="" && dataURL_GET.Trim()=="" ) {queue_image_params = dataURL_POST;}
// else if( dataURL_POST.Trim()!="" && dataURL_GET.Trim()!="" ) {
// if( dataURL_GET.IndexOf("%2F") != -1 || dataURL_GET.IndexOf("%2f") != -1 ) {queue_image_params = dataURL_GET;}
// else if(
// dataURL_POST.IndexOf("%2F") != -1 || dataURL_POST.IndexOf("%2f") != -1 ||
// dataURL_POST.IndexOf("\n") != -1 || dataURL_POST.IndexOf("%2Fn")!=-1 ||
// dataURL_POST.IndexOf("%2fn")!=-1
// ) {queue_image_params = dataURL_POST;}
// else {
// //Console.WriteLine("ConvertToPNG: 1. another case...");
// }
// }else {
// //Console.WriteLine("ConvertToPNG: 2. another case...{0}");
// }
//if string send using GET-query this must to be escaped.
queue_image_params = param_GET_POST(dataURL_GET, dataURL_POST); // queue_image_params = Uri.UnescapeDataString(queue_image_params); //Unescape this. "%2F" or "%2f" replaced to "/".
var splitted = queue_image_params.Split('|');
string pathway = ( (is_lite == true) ? "download"+Path.DirectorySeparatorChar+"generated" : "containers" ); //if this was been request on lite-server, use "download"-folder, to let them download it. Else - "containers"-folder.
bool exists = System.IO.Directory.Exists(pathway); if(!exists){System.IO.Directory.CreateDirectory(pathway);} if(pathway == "download"+Path.DirectorySeparatorChar+"generated"){timer_to_delete.Start();}
for(int i=0;i<splitted.Length;i++){
if(splitted[i]==""){continue;} //if empty string - continue. String is empty if '\n' at last of post-content.
if( splitted[i].IndexOf("data:") != -1
&& splitted[i].IndexOf("base64,")!= -1
&& nbpack.NBPackMain.IsBase64Encoded(splitted[i].Split(',')[1])
) {dataURL = splitted[i];} //if dataURL specified
else{
//pathway = ((is_lite) ? pathway : splitted[i]); //just folder name, like "download/generated" or "containers"-folder.
//Don't set it for request on lite-server, and use default predefined folder, to prevent change this and make DDoS, and StackOverFlow, by sending many requests, to save many files.
}
}
if(queue_image_params.IndexOf("generate_PNG")!=-1){return generate_PNG((string)queue_image_params, (string)pathway);}
if(dataURL==""){dataURL="No_dataURL_specified_for_source_image";} //if string is null, this is empty string
//if(queue.Trim().Length == 0){queue = "";} //if trimmed queue value length == 0, leave this empty
//Console.WriteLine(
// "from_last_posts = {0}\n"
// + "dataURL = {1}\n"
// + "queue = {2}\n"
// + "from_queue = {3}\n"
// ,
// from_last_posts
// ,dataURL.Substring(0, ( (dataURL.Length>40) ?40: dataURL.Length ) )
// ,queue
// ,from_queue
//);
if(
dataURL.IndexOf("data:") != -1
&& dataURL.IndexOf("base64,") != -1
&& nbpack.NBPackMain.IsBase64Encoded(dataURL.Split(',')[1])
){
Console.WriteLine("Custom image uploaded and dataURL found.");
//create bitmap from dataURL, and save this as PNG-file to Upload folder.
var base64Data = Regex.Match(dataURL, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
var binData = Convert.FromBase64String(base64Data);
string random_guid = Guid.NewGuid().ToString();
using (var stream = new MemoryStream(binData))
{
Image bmp = new Bitmap(stream);
bmp.Save(pathway + Path.DirectorySeparatorChar + random_guid + ".png", ImageFormat.Png);
Console.WriteLine("Saved to \""+(pathway + Path.DirectorySeparatorChar + random_guid + ".png")+"\"");
bmp.Dispose();
}datetime_of_generated_images[(pathway + Path.DirectorySeparatorChar + random_guid + ".png")] = DateTime.Now;
return new HttpResponse(
StatusCode.Ok
,
(
"Saved to: \""+(pathway + Path.DirectorySeparatorChar + random_guid + ".png")
+"\", <br>"
+"<a href=\""+("../" + pathway + "/" + random_guid + ".png").Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
+"\" target=\"_blank\">"+ "Open \""+("../" + pathway + "/" + random_guid + ".png").Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
+"\" as image"+ "</a>, <br>"
+ "<a href=\""+("../" + pathway + "/" + random_guid + ".png").Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
+"\" target=\"_blank\" download=\""+(random_guid + ".png")+"\">"+ "Download \""+(random_guid + ".png")+"\" as file."+ "</a>"
)
)
;
}
else{
Console.WriteLine("DataURL not found. dataURL = {0}", dataURL.Substring(0, (dataURL.Length>40)?40:dataURL.Length));
return new HttpResponse(StatusCode.Ok, ("dataURL: " + dataURL + "\n\nAfter encodeURIComponent(dataURL), you can send this HERE \nas GET parameter or POST (for big images);"));
}
}
// example: prange/90-10 - gets not deleted posts from 91 to 100 (skip 90, take 10)
private HttpResponse GetPresentRange(string GET = null, string POST = null)
{
// Console.WriteLine("DbApiHandler.cs. GetPresentRange. \n\n\n\n\n ");
string arguments = param_GET_POST(GET, POST); //get string with arguments
string[] splitted = arguments.Split('-'); //split this string by "-" and put arguments to array.
//define default values:
int skip = 0; //skip posts
int count = 0; //number posts to get this
string only_hashes = null; //can contains two values, if need to return hashes
bool append_text = false; //append text "post_was_deleted", //and "post_is_reported" to base64 of post message or not? (false by default)
bool show_deleted = false; //is need to show deleted posts? (false by default)
NDB.Post[] posts; //result if need to return array with posts Post[]
string[] posts_hashes; //result if "only_hashes" array with strings string[]
if(splitted.Length < 2){ //if lesser than 2 arguments specified
return new HttpResponse(StatusCode.BadRequest, "DbApiHandler.cs. GetPresentRange. Invalid number of arguments. splitted.Length: "+splitted.Length); //return error.
}//else, continue...
//parse numbers from strings
skip = nbpack.NBPackMain.parse_number(splitted[0]); //skip
count = nbpack.NBPackMain.parse_number(splitted[1]); //count
if(splitted.Length == 2){ //if only 2 arguments specified
posts = _db.RangePresent(skip, count, append_text, show_deleted); //get array with posts by running method from PostDb.cs, with default arguments.
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(posts)); //and return this as JSON.
}//else, continue...
if(splitted.Contains("append_text")){ //if need to append text, and this string found in argumets
append_text = true; //set it to true.
}
if(splitted.Contains("show_deleted")){ //if need to show deleted posts, and this string found in aruments
show_deleted = true; //set it to true
}
if(splitted.Contains("only_hashes")){ //if need to return only hashes of the posts, and this string found in arguments.
only_hashes = "only_hashes"; //set this value in this string
posts_hashes = _db.RangePresent_string(skip, count, only_hashes, show_deleted); //run another method from NBPack.cs, with defined earlier arguments
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(posts_hashes)); //and return hashes of the posts, as JSON.
}
else if(splitted.Contains("hashes_with_bytelength")){ //if need to append bytelength to hashes, and this string was found in arguments
only_hashes = "hashes_with_bytelength"; //set this value in this string
posts_hashes = _db.RangePresent_string(skip, count, only_hashes, show_deleted); //run another method from NBPack.cs, with defined earlier arguments
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(posts_hashes)); //and return the hashes with bytelength, as JSON.
}else{ //else, if no need to return hashes.
posts = _db.RangePresent(skip, count, append_text, show_deleted); //get the array with posts with defined earlier artuments.
//Console.WriteLine("posts.Length"+posts.Length);
string response = "";
try{
response = JsonConvert.SerializeObject(posts); //hm... 1681 posts not return in browser... 1680 posts - return good, as text... No any catch.
//Console.WriteLine("response.Length: "+response.Length+", response.Substring(response.Length-100): "+response.Substring(response.Length-100));
return new HttpResponse(StatusCode.Ok, response); //and return result as JSON. Warning... 1681 posts not returned good in JSON...
}catch (Exception ex){
return new HttpResponse(StatusCode.BadRequest, ex.ToString()); //return Exception
}
}
}
//download all posts as readable json-file, with Formatting.Indented: http://127.0.0.1:7346/api/GetDbAsJSON
private HttpResponse GetDbAsJSON(string GET = null, string POST = null){
try{
int total_posts = _db.GetPresentCount(); //Get number of posts in DataBase
NDB.Post[] posts = _db.RangePresent(0, total_posts, false, false); //get array with posts by running method from PostDb.cs, with default arguments.
foreach(NDB.Post post in posts){ //for each post
post.message = post.message.FromB64(); //See /DataBase/PostValidator.cs. Decode post.message from Base64 or leave this as is, and escape json special characters.
}
//This file can be uploaded in database, by using '\pages\Upload_posts_from_JSON_file.html'
File.WriteAllText(@"download\All_Posts_in_DataBase.json", JsonConvert.SerializeObject(posts, Formatting.Indented)); //write serialized JSON in file.
return new HttpResponse(StatusCode.Ok, ""
+ "File \"All_Posts_in_DataBase.json\" was been updated sussessfully!<br>"
+ "<a href=\"../../download/All_Posts_in_DataBase.json\""
+ "download=\"All_Posts_in_DataBase.json\""
+ "title=\"This file can be uploaded in database by using \'\\pages\\Upload_posts_from_JSON_file.html\'\">"
+ "Download \"All_Posts_in_DataBase.json\"!"
+ "</a>"
,
MimeType.Mime("Html")
); //and return the link to download it.
}
catch(Exception ex){
Console.WriteLine(ex);
return new ErrorHandler(StatusCode.NotFound, ex.Message).Handle(null);
}
}
private HttpResponse ParamGet(string addr = null, string content = null)
{
if((content == null || content == "") && addr.IndexOf('?')!=-1){
string[] splitted = addr.Split('?');
addr = splitted[0];
content = Uri.UnescapeDataString(splitted[1]);
}
return new HttpResponse(StatusCode.Ok, Configurator.Instance.GetValue(addr, content)); //get param or set default.
}
/*
get only allowed params for lite-server.
*/
private HttpResponse ParamGet_lite(string GET = null, string POST = null)
{
var param = param_GET_POST(GET, POST);
// parameters, disabled to show on lite-server:
string[] unallowed_params = new string [] {
"places", //places - temporary disabled, and can be available, after PNG-collect working on lite-server.
"captcha_pack_file",
"Proxy_List",
"Services_Returns_External_IP",
"ip",
"port",
"password",
"no_tcp_delay",
"check_version_update",
"show_timestamps",
"instant_retranslation",
"detect_URLs",
"show_deleted",
"post_offset_in_tree_px",
"post_delete_timeout",
"use_spam_filter",
"spam_filter",
"remind_if_updates_exists",
"post_count_notification_time",
"check_updates_every_hours",
"Download_Timeout_Sec",
"last_update",
"original_captcha_sha256",
"last_another_repo_version",
"captcha_url",
"collect_memory_limit_to_wait",
}
;
if (!Configurator.Instance.HasValue(param) || unallowed_params.Contains(param))
{
return new ErrorHandler(StatusCode.NotFound, "No such param.").Handle(null);
}
return new HttpResponse(StatusCode.Ok, Configurator.Instance.GetValue(param, "")); //get param only, because this paramget API-call on public lite-server is opened for anonymous.
}
// returns available params in a JSON array
private HttpResponse Params(string addr, string content)
{
var @params = Configurator.Instance.GetParams();
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(@params));
}
private HttpResponse ParamSet(string addr, string content)
{
if(content == null && addr.IndexOf('?')!=-1){ //if second parameter from POST-query not defined, try parse first parameter from GET-query
string[] splitted = addr.Split('?');
addr = splitted[0];
content = Uri.UnescapeDataString(splitted[1]);
}
// Console.WriteLine("DbApiHandler.cs. ParamSet. addr: "+addr+", content: "+content); //show two parameters
if(addr!=null && content!=null){
Configurator.Instance.SetValue(addr, content);
return new HttpResponse(StatusCode.Ok, "Ok");
}
else{
return new HttpResponse(StatusCode.BadRequest, "DbApiHandler.cs. ParamSet. Invalid request. Value cann't be null: param:"+addr+", value:"+content);
}
}
private HttpResponse GetPostByHash(string GET = null, string POST = null)
{
string arguments = param_GET_POST(GET, POST);
string[] splitted = arguments.Split('-');
string hash = splitted[0];
bool appendText = ((splitted.Length == 2)? bool.Parse(splitted[1]) : false);
var post = _db.GetPost(hash, appendText);
if (post == null)
{
return new ErrorHandler(StatusCode.NotFound, "No such post.").Handle(null);
}
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(post));
}
private HttpResponse GetLastNPosts(string GET = null, string POST = null) //from client 3.1
{
string arguments = param_GET_POST(GET, POST);
string[] splitted = arguments.Split('-');
string hash = splitted[0];
string n = splitted[1];
bool appendText = ((splitted.Length >= 3)? bool.Parse(splitted[2]) : false);
bool fast = ((splitted.Length >= 4)? bool.Parse(splitted[3]) : false);
// Console.WriteLine("GetLastNPosts. hash: "+hash+", n: "+n+", appendText: "+appendText+", fast: "+fast);
List<NDB.Post> posts;
try
{
posts = _db.GetLastNAnswers(hash, nbpack.NBPackMain.parse_number(n), appendText, fast); //now ok.
}
catch(Exception e)
{
return new ErrorHandler(StatusCode.BadRequest, e.Message).Handle(null);
}
if (posts == null)
{
return new ErrorHandler(StatusCode.NotFound, "No such post.").Handle(null);
}
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(posts));
}
// includes deleted
private HttpResponse GetNthPost(string n, string notUsed = null)
{
//var post = _db.GetNthPost(int.Parse(n));
var post = _db.GetNthPost(nbpack.NBPackMain.parse_number(n));
if (post == null)
{
return new ErrorHandler(StatusCode.NotFound, "No such post.").Handle(null);
}
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(post));
}
// returns count of all posts including deleted
private HttpResponse GetPostCount(string notUsed1, string notUsed = null)
{
return new HttpResponse(StatusCode.Ok, _db.GetPostCount().ToString());
}
// returns recursive replies count for specified post
private HttpResponse ThreadSize(string hash, string notUsed = null)
{
return new HttpResponse(StatusCode.Ok, _db.GetThreadSize(hash).ToString());
}
// returns count of not deleted posts
private HttpResponse GetPresentCount(string notUsed1, string notUsed = null)
{
return new HttpResponse(StatusCode.Ok, _db.GetPresentCount().ToString());
}
// returns array of posts with messages including searchString, search avoids [img=..] tag contents.
private HttpResponse Search(string GET = null, string POST = null) //notUsed contains base64 encoded searchString, when POST-query sent.
{
try{
string searchString = param_GET_POST(GET, POST);
string [] splitted = searchString.Split('|');
searchString = splitted[0].FromB64();
Console.WriteLine("searchString: \""+searchString+"\"");
var found = new List<NDB.Post>();
int limit = 500;
// Console.WriteLine("DbApiHandler.cs. Search. int limit: "+limit); //maybe this change limit in MaxConsecZeros
if(splitted.Length==2){
limit = nbpack.NBPackMain.parse_number(splitted[1]);
}
for (int i = _db.GetPostCount() - 1; i >= 0; i--)
{
var post = _db.GetNthPost(i);
if (post == null){
continue;
}
var msg = ( //maybe this need to delete
(post.message.StartsWith("post_was_deleted"))// || post.message.StartsWith("post_is_reported"))
? post.message.Substring(16)
: post.message
)
.FromB64();
if (msg.Contains("[img="))
{
//msg = Regex.Replace(msg, "\\[img=[A-Za-z0-9+=/]{4,64512}\\]", "");
msg = Regex.Replace(msg, "\\[img=[A-Za-z0-9+=/]{4,}\\]", ""); //remove limit, because pictures from Karasiq nanoboard can be larger.
}
//if (msg.Contains(searchString))
if (msg.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase)!=-1)
{
found.Add(post);
if (found.Count >= limit) break;
}
}
return new HttpResponse(StatusCode.Ok, found.Count == 0 ? "[]" : JsonConvert.SerializeObject(found.ToArray()));
}
catch (Exception ex){
Console.WriteLine(ex);
return new HttpResponse(StatusCode.BadRequest, "[]");
}
}
private HttpResponse GetReplies(string GET = null, string POST = null)
{
string arguments = param_GET_POST(GET, POST);
string[] splitted = arguments.Split('-');
string hash = splitted[0];
bool appendText = ((splitted.Length == 2) ? bool.Parse(splitted[1]) : false);
var replies = _db.GetReplies(hash, appendText);
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(replies));
}