-
Notifications
You must be signed in to change notification settings - Fork 2
/
ecv_import.dpr
executable file
·703 lines (631 loc) · 17 KB
/
ecv_import.dpr
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
library ecv_import;
uses
// FastMM4,
ShareMem,
WinAPI,
c_webmodel,
c_plugin,
strings,
functions,
c_http,
c_buffers,
c_manga,
opts,
file_sys,
sql_dbcommon,
fyzzycomp,
regexpr;
{$R *.res}
type
TImporter = class(TPlugin, IPlugin)
private
Mangas : TItemList;
Filtered : TItemList;
Translators:TItemList;
Lookup : EDBFetch;
procedure Prepare;
function ChooseNearest(Pattern: AnsiString): AnsiString;
function makeNewPath(Original, MangaDir: AnsiString): AnsiString;
procedure DoImport(Path: AnsiString);
procedure ProcessSubDir(Path, Target, D: AnsiString);
public
constructor Create(Server: IServer);
destructor Destroy; override;
function Name: PAnsiChar; override;
function ctlToID(Action: AnsiString): Integer; override;
procedure serveAction(ID: Integer; r: TStrings; Proc: PReqProcessor); override;
procedure serveImport(r: TStrings; Proc: PReqProcessor);
procedure serveArchs(r: TStrings; Proc: PReqProcessor);
procedure serveArchive(r: TStrings; Proc: PReqProcessor);
end;
constructor TImporter.Create(Server: IServer);
var
f: EDBFetch;
i: integer;
begin
inherited;
Mangas := TItemList.Create;
Filtered := TItemList.Create;
Translators := TItemList.Create;
if Fetch(@f, SQL_SELECT, [TBL_NAMES[TBL_TRANSL]], ['t.link']) > 0 then
for i := 0 to f.Count - 1 do
Translators.Add(LowerCase(ReplaceRegExpr('[^\w\d]+', f.Rows[i, 0], ' ')));
end;
destructor TImporter.Destroy;
begin
Translators.Free;
Filtered.Free;
Mangas.Free;
inherited;
end;
function TImporter.ctlToID(Action: AnsiString): Integer;
begin
result := stringcase(@Action[1], [
'index'
, 'new'
, 'archfix'
, 'archive'
]);
end;
function TImporter.Name: PAnsiChar;
begin
result := 'import';
end;
procedure TImporter.serveAction(ID: Integer; r: TStrings; Proc: PReqProcessor);
var
m, i: integer;
manga: TManga;
begin
_cb_new(Proc.IOBuffer, 0);
try
case ID of
0: serveArchs(r, Proc);
1: serveImport(r, Proc);
2: begin //archfix
Proc.Formatter := JSON_Formatter;
m := sti(array_shift(r));
if m <= 0 then
raise Exception.Create('Manga ID not specified!');
if not Server.MangaData(m, manga) then
raise Exception.Create('Aquire failed!');
i := iMax(Manga.mArchTotal, 0);
SQL(SQL_DELETEMANGAL, [TBL_NAMES[TBL_ARCHS], m]);
SQL(SQL_INSERT_II, [TBL_NAMES[TBL_ARCHS], m, i]);
_cb_append(Proc.IOBuffer, '{result: "ok", archs: "' + its(i) + aaxx(i, ' àðõèâ', ['', 'à', 'îâ']) + '"}');
Server.AquireProgress(PManga(manga.pILItem.Data));
end;
3: serveArchive(r, Proc);
end;
finally
_cb_end(Proc.IoBuffer);
end;
end;
function Greater(A, B: AnsiString): Boolean;
var
p1, p2: PAnsiChar;
i1, i2: Single;
type tchars= set of AnsiChar;
function search(var s: PAnsiChar; c: TChars): boolean;
begin
while (s^ <> #0) do
if (s^ in c) then
break
else
inc(s);
result := s^ <> #0;
end;
function readFloat(var s: PAnsiChar): Single;
var
t: PAnsiChar;
begin
t := s;
while s^ in ['0'..'9', '.'] do inc(s);
result := STF(copy(t, 0, Cardinal(s) - Cardinal(t)));
end;
begin
result := false;
p1 := PAnsiChar(UpperCase(A));
p2 := PAnsiChar(UpperCase(B));
while search(p1, ['0'..'9']) do
if search(p2, ['0'..'9']) then begin
i1 := readFloat(p1);
i2 := readFloat(p2);
if abs(i1 - i2) > 0.00001 then begin
result := i1 > i2;
exit;
end;
end else
break;
end;
type
TArray= array [0..999] of AnsiString;
procedure Sort(var a: TArray; c: Integer);
var
i: Integer;
t: AnsiString;
s: Boolean;
begin
repeat
s := true;
for i := 0 to c - 2 do
if Greater(a[i], a[i + 1]) then begin
t := a[i + 1];
a[i + 1] := a[i];
a[i] := t;
s := false;
end;
until s;
end;
const
exts: array [0..4] of AnsiString = ('jpg', 'jpeg', 'png', 'gif', 'bmp');
procedure TImporter.ProcessSubDir(Path, Target, D: AnsiString);
var
c, n: Integer;
u: Integer;
a, r: TArray;
j : TArray;
SR : TSearchRec;
S, e: AnsiString;
t : AnsiString;
F : File;
procedure MoveToSpec(Spec: AnsiString; FileName: AnsiString);
var
i: Integer;
begin
i := 0;
repeat
inc(i);
s := Format('%s%s\\%s-%s_%d0.2.%s', [path, Spec, target, ModuleFromFileName(FileName), i, ExtractFileExt(FileName)]);
until not FileExists(s);
Assign(F, D + '\' + FileName);
{$I-}
Rename(F, String(s));
if IOResult <> 0 then
Server.Log('Error: can''t rename [%s] to [%s]: already exists or same name...', [FileName, ExtractFileName(s)]);
{$I+}
end;
begin
Server.Log('Process [%s]...', [ExtractFileName(D)]);
c := 0;
n := 0;
u := 0;
try
if FindFirst(D + '\*', faFiles, SR) = 0 then
repeat
if SR.Attr and faDirectory = faDirectory then continue;
t := LowerCase(SR.Name);
e := ExtractFileExt(t);
if not Contains(@e[1], graphic_ext) then begin
j[u] := t;
inc(u);
Continue;
end;
if pos('credit', t) > 0 {re_match(@t[1], '^.*credit.*$')} then begin
r[n] := SR.Name;
inc(n);
Continue;
end;
a[c] := SR.Name;
inc(c);
until FindNext(SR) <> 0
else begin
{$I-}
RmDir(D);
if IOResult <> 0 then
Server.Log('Error: no files, remove failed.', [])
else
Server.Log('Error: no files, directory removed.', []);
{$I+}
end;
finally
FindClose(SR);
end;
if c > 1 then Sort(a, c);
if c = 0 then begin
try
if FindFirst(D + '\*', faDirectory, SR) = 0 then
repeat
if SR.Name[1] = '.' then continue;
if SR.Name = 'credits' then continue;
if SR.Name = 'junk' then continue;
if SR.Attr and faDirectory = 0 then
MoveToSpec(SD_JUNK, SR.Name)
else begin
ProcessSubDir(path, Target, D + '\' + SR.Name);
{$I-}
RmDir(D + '\' + SR.Name);
{$I+}
end;
until FindNext(SR) <> 0;
finally
FindClose(SR);
end;
exit;
end;
while c > 0 do begin
dec(c);
s := Format('%d0.4.%s', [c + 1, ExtractFileExt(a[c])]);
Assign(F, D + '\' + a[c]);
{$I-}
Rename(F, String(Path + Target + '\' + s));
if IOResult <> 0 then
Server.Log('Error: can''t rename [%s] to [%s]: already exists or same name...', [a[c], s]);
{$I+}
end;
while n > 0 do begin
dec(n);
MoveToSpec(SD_CREDITS, r[n]);
end;
while u > 0 do begin
dec(u);
MoveToSpec(SD_JUNK, j[u]);
end;
end;
procedure TImporter.DoImport(Path: AnsiString);
var
c, r, u: Integer;
a: TArray;
s: AnsiString;
SR: TSearchRec;
F: File;
begin
if path[Length(path)] <> '\' then path := path + '\';
if pos(':', path) = 0 then path := OPT_MANGADIR + path;
{$I-}
for r := 0 to Length(SDIRS) - 1 do
if not FileExists(path + SDIRS[r]) then
MkDir(PChar(path + SDIRS[r]));
{$I+}
c := 0;
try
if FindFirst(path + SD_ARCHIVE + '\*', faDirectory, SR) = 0 then
repeat
if SR.Name[1] = '.' then continue;
if SR.Attr and faDirectory = 0 then continue;
a[c] := SR.Name;
inc(c);
Assign(F, path + SD_ARCHIVE + '\' + SR.Name);
{$I-}
Rename(F, String(path + SR.Name));
if IOResult <> 0 then
Server.Log('Error: can''t rename [%s] to [%s]: dir exists or same name...', [SD_ARCHIVE + '\' + SR.Name, SR.Name]);
{$I+}
until FindNext(SR) <> 0
else begin
Server.Log('Archive subdirectory: no subdirectories.', []);
end;
finally
FindClose(SR);
end;
if c > 1 then Sort(a, c);
u := 0;
repeat inc(u) until not FileExists(path + ITS(u, 0, SIZE_CHPNAME));
while c > 0 do begin
dec(c);
s := ITS(c + u, 0, SIZE_CHPNAME);
Assign(F, path + a[c]);
{$I-}
Rename(F, String(path + s));
r := IOResult;
if not (r in [0, 2, 145]) then begin
Server.Log('Error: can''t rename [%s] to [%s]: dir exists or same name: %s...', [a[c], s, SysErrorMessage(r)]);
exit;
end;
{$I+}
ProcessSubDir(path, s, path + s);
end;
Server.Log(' -- done...', []);
end;
procedure TImporter.serveImport(r: TStrings; Proc: PReqProcessor);
var
id: Integer;
manga: TManga;
function getChaps: Integer;
var
s: AnsiString;
R: TSearchRec;
begin
result := 0;
s := Format('%s\\%s', [OPT_MANGADIR, Manga.mLink]);
if FindFirst(s + '\*', faDirectory, R) = 0 then
try
repeat
if R.Name[1] = '.' then continue;
if Contains(@R.Name[1], SDIRS) then continue;
if R.Attr and faDirectory <> 0 then inc(result);
until FindNext(R) <> 0;
finally
FindClose(R);
end;
end;
var
i, j, k: Integer;
f: eDBFetch;
path: AnsiString;
begin
id := sti(array_shift(r));
if id <= 0 then
raise Exception.Create('Manga ID not specified!');
if not Server.MangaData(id, manga) then
exit;
path := Format('%s\\%s', [OPT_MANGADIR, Manga.mLink]);
Proc.Formatter := nil;
j := getChaps;
try
DoImport(path);
except
Server.Log('CTL::Reader ::serveImport DoImport failed', []);
end;
i := getChaps - j;
_cb_append(Proc.IOBuffer, '{err: 0, imported: "' + its(i) + '"}');
if i > 0 then begin
if Fetch(@f, SQL_FETCHMANGAL, [TBL_NAMES[TBL_ARCHS], Manga.mID], ['t.archives']) > 0 then
k := STI(f.Rows[0, 0])
else
k := 0;
j := iMin(Manga.mArchTotal, i + k);
if j > 0 then begin
SQL(SQL_DELETEMANGAL, [TBL_NAMES[TBL_ARCHS], Manga.mID]);
SQL(SQL_INSERT_II, [TBL_NAMES[TBL_ARCHS], Manga.mID, j])
end;
end;
end;
function FindFolder(Root: String; var Folder: String): Boolean;
var
l: Integer;
begin
result := true;
Root := LowerCase(Root);
repeat
l := Length(Folder);
if l = 0 then exit;
if Folder[l] in ['\', '/'] then delete(Folder, l, 1);
if FileExists(Folder) then exit;
Folder := ExtractFileDir(Folder);
until Root = LowerCase(Folder);
result := false;
end;
function TImporter.ChooseNearest(Pattern: AnsiString): AnsiString;
var
i: Integer;
j: Integer;
S: TStats;
t: String;
procedure Sort(List: TItemList);
var
i: Integer;
t: AnsiString;
c: Cardinal;
s: Boolean;
begin
repeat
s := true;
for i := 0 to List.Count - 2 do
if lstrcmpi(PChar(List[i]), PChar(List[i + 1])) < 0 then begin
t := List[i];
List[i] := List[i + 1];
List[i + 1] := t;
c := List.Data[i];
List.Data[i] := List.Data[i + 1];
List.Data[i + 1] := c;
s := False;
end;
until s;
end;
begin
result := '';
if Mangas.Count <= 0 then exit;
t := Trim(ExtractFileName(Pattern));
j := -1;
if t <> '' then begin
i := Mangas.Add(t);
try
j := FyzzyAnalyze(Mangas, i, 1, S);
except
end;
Mangas.Delete(i);
end;
Filtered.Count := 0;
if t <> '' then
for i := 0 to Mangas.Count - 1 do
if S[i].R > 0 then begin
t := Format('%f.3 | %s', [S[i].R, Mangas[i]]);
Filtered.Add(t);
if j = i then
result := t;
end;
if Filtered.Count = 0 then
for i := 0 to Mangas.Count - 1 do
Filtered.Add(Mangas[i]);
Sort(Filtered);
end;
var
SavePatt : AnsiString = '{%root%}\{%manga%}\archives\{%file%}';
function clearfix(s: AnsiString): AnsiString;
var
j: Integer;
begin
result := s;
j := pos('|', result);
if j > 0 then
delete(result, 1, j + 1);
end;
function TImporter.makeNewPath(Original, MangaDir: String): AnsiString;
begin
result := ReplaceRegExpr('{%root%}', SavePatt, OPT_MANGADIR);
result := ReplaceRegExpr('{%manga%}', result, MangaDir);
result := ReplaceRegExpr('{%file%}' , result, ExtractFileName(Original));
end;
procedure TImporter.Prepare;
procedure SearchDirs(Dir: String);
var
R: TSearchRec;
begin
if FindFirst(Dir + '\*', faDirectory, R) = 0 then
repeat
if R.Name[1] = '.' then continue;
if R.Attr and faDirectory = 0 then continue;
Mangas.Add(R.Name);
until FindNext(R) <> 0;
end;
begin
SavePatt := Server.Config('savepattern.manga');
Mangas.Count := 0;
SearchDirs(OPT_MANGADIR);
end;
procedure TImporter.serveArchs(r: TStrings; Proc: PReqProcessor);
type
TOrigin = record
Source: TStrings;
Origin: AnsiString;
List : TItemList;
end;
TOrigins= array [0..512] of TOrigin;
var
i, j, k: Integer;
sr: TSearchRec;
s, c, p, mm, e, f,
io, us, uo, t1, t2, t3: AnsiString;
o: TOrigins;
oc: Integer;
re: TRegExpr;
t: PAnsiChar;
function haso(origin: AnsiString): Integer;
begin
result := oc;
while result > 0 do begin
dec(result);
if origin = o[result].Origin then exit;
end;
result := -1;
end;
begin
if Proc.ParamList['action'] = 'delete' then begin
t1 := trim(Proc.ParamList['dir']);
if t1 <> '' then
SQL(SQL_DELETE + ' where t.`directory` = "%s"', [TBL_NAMES[TBL_IMPORTS], escapeslashes(t1)]);
Proc.Redirect('/import');
exit;
end;
if Proc.ParamList['action'] = 'add' then begin
t1 := trim(Proc.ParamList['dir']);
if t1 <> '' then
SQL(SQL_INSERT_IS, [TBL_NAMES[TBL_IMPORTS], 0, escapeslashes(t1)]);
Proc.Redirect('/import');
exit;
end;
Prepare;
oc := 0;
s := '';
e := '';
if Fetch(@Lookup, SQL_SELECT, [TBL_NAMES[TBL_IMPORTS]], ['t.directory']) > 0 then begin
ProcessTemplate(AcceptInclude('imporigin.tpl'), io);
t2 := '';
re := TRegExpr.Create;
t3 := join('|', explode(#13#10, Translators.Text));
t3 := '(' + ReplaceRegExpr('[^\w\d\|]', t3, '.') + ')';
if t3 = '()' then t3 := '';
re.Expression := join('|', ['(\[([^\]]+)\]|(vo?l?|ch?)\d+(\.\d+)*)|( )+', t3]);
re.ModifierStr := 'isr';
try
for i := 0 to Lookup.Count - 1 do begin
if FindFirst(Lookup.Rows[i, 0] + '\*', faAnyFile, SR) = 0 then
repeat
if sr.Attr and faDirectory <> 0 then continue;
f := SR.Name;
c := '.' +ExtractFileExt(f);
j := pos(c, '.rar.zip.tar.tg.7z.arj.cab');
if j = 0 then continue;
delete(f, pos(c, f), maxint);
t := @f[1];
while t^ <> #0 do begin
if (t^ = '-') or (t^ = '_') then t^ := ' ';
inc(t);
end;
f := trim(re.Replace(f, ''));
c := clearfix(ChooseNearest(f));
k := haso(c);
if k < 0 then begin
k := oc;
o[k].Origin := c;
o[k].List := TItemList.Create;
o[k].Source := nil;
inc(oc);
end;
array_push(o[k].Source, Lookup.Rows[i, 0] + '\' + SR.Name);
with o[k].List do
for j := 0 to Filtered.Count - 1 do
if IndexOf(clearfix(Filtered[j])) < 0 then
Add(clearfix(Filtered[j]));
until FindNext(SR) <> 0;
t2 := join('</li>'#13#10'<li>', [t2, '<a class="del pull left" href="/import?action=delete&dir=' + urlencode(Lookup.Rows[i, 0]) + '"></a> ' + Lookup.Rows[i, 0]]);
end;
finally
re.Free;
end;
mm := '';
for j := 0 to Mangas.Count - 1 do
mm := mm + ', "' + urlencode(Mangas[j]) + '"';
for i := 0 to oc - 1 do
with o[i] do begin
uo := urlencode(Origin);
Proc.KeyData.Add('imp.id', its(i + 1));
Proc.KeyData.Add('imp.origin', Origin);
c := '';
t1 := '';
for j := 0 to Length(Source) - 1 do begin
us := its(i + 1) + '][' + its(j);//urlencode(escapeslashes(Source[j]));
p := '';
{ for l := 0 to Mangas.Count - 1 do
p := p + '<li><a href=''javascript:arch_import([files[' + us + ']], "' + urlencode(Mangas[l]) + '")''>' + Mangas[l] + '</a></li>'#13#10;
p := '<ul class="possibles" style="float: left;">' + p + '</ul>';}
c := c + '<li><div style="clear: both;"><a href="#" onclick="build_ulist(this, ' + its(i + 1) + ', ' + its(j) + ')">[=]</a> ' + Source[j] + ' <a href="javascript:arch_import([files[' + us + ']], ''' + strsafe(urlencode(uo)) + ''')">→</a></div>' + p + '</li>'#13#10;
t1 := join('", "', [t1, urlencode((Source[j]))]);
end;
e := join(';'#13#10' ', [e, 'files[' + its(i + 1) + '] = ["' + t1 + '"]']);
Proc.KeyData.Add('imp.files', c);
s := s + Proc.Process(io);
List.Free;
end;
end else
t2 := 'none yet.';
Proc.KeyData.Add('fileslist', e);
Proc.KeyData.Add('archs', s);
Proc.KeyData.Add('mangas', mm);
Proc.KeyData.Add('dirs', t2);
_cb_outtemlate(Proc.IOBuffer, 'importarchs');
end;
procedure TImporter.serveArchive(r: TStrings; Proc: PReqProcessor);
var
src, dst: AnsiString;
begin
Proc.Formatter := JSON_Formatter;
src := trim(Proc.ParamList['source']);
dst := makeNewPath(src, trim(Proc.ParamList['target']));
if FileExists(dst) then
// if Replace then
DeleteFile(PChar(dst))
// else
// if MessageBox(Handle, 'Ôàéë ñóùåñòâóåò, çàìåíèòü?', '', MB_YESNO) <> ID_YES then
// exit
// else
// DeleteFile(NewPath);
else
// raise Exception.Create(strsafe(src + '\nFile don''t exists'));
;
if MoveFile(PChar(src), PChar(dst)) then
_cb_append(Proc.IOBuffer, '{result: "ok", msg: ""}')
else
raise Exception.Create(strsafe(src + '\n' + SysErrorMessage(GetLastError)));
end;
function ecv_LoadPlugin(Server: IServer): IPlugin;
begin
OPT_MANGADIR := Server.Config('mangadir');
OPT_DATADIR := Server.Config('datadir');
result := TImporter.Create(Server);
end;
exports
ecv_LoadPlugin;
begin
IsMultiThread := true;
end.