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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
|
package XXV::MODULES::MUSIC;
use strict;
use Tools;
use File::Basename;
use File::Path;
use File::Find;
$SIG{CHLD} = 'IGNORE';
# This module method must exist for XXV
# ------------------
sub module {
# ------------------
my $self = shift || return error('No object defined!');
my $args = {
Name => 'MUSIC',
Prereq => {
# 'DBI' => 'Database independent interface for Perl ',
# 'DBD::mysql' => 'MySQL driver for the Perl5 Database Interface (DBI)',
'MP3::Icecast' => 'Generate Icecast streams, as well as M3U and PLSv2 playlists',
'MP3::Info' => 'Manipulate / fetch info from MP3 audio files ',
'CGI' => 'Simple Common Gateway Interface Class',
'LWP::Simple' => 'get, head, getprint, getstore, mirror - Procedural LWP interface',
'Net::Amazon' => 'Framework for accessing amazon.com via SOAP and XML/HTTP',
'Net::Amazon::Request::Artist' => 'Class for submitting Artist requests',
},
Description => gettext('This module managed music files.'),
Status => sub{ $self->status(@_) },
Preferences => {
active => {
description => gettext('Activate this service'),
default => 'y',
type => 'confirm',
required => gettext('This is required!'),
},
path => {
description => gettext('Directory with the music files'),
default => '/music',
type => 'dir',
required => gettext('This is required!'),
},
port => {
description => gettext('Port to listen for icecast clients.'),
default => 8100,
type => 'integer',
required => gettext('This is required!'),
},
Interface => {
description => gettext('Local interface to bind service'),
default => '0.0.0.0',
type => 'host',
required => gettext('This is required!'),
},
proxy => {
description => gettext('Proxy URL to music server. e.g. (http://vdr/xxv) Please remember you must write the port to icecast server in your proxy configuration!'),
default => '',
type => 'string',
},
clients => {
description => gettext('Maximum clients to connect at the same time'),
default => 5,
type => 'integer',
required => gettext('This is required!'),
},
coverimages => {
description => gettext('Common directory for cover images'),
default => '/var/cache/xxv/cover',
type => 'dir',
required => gettext('This is required!'),
},
muggle => {
description => gettext('DSN for muggle database'),
default => 'DBI:mysql:database=GiantDisc;host=localhost;port=3306',
type => 'string',
check => sub{
my $value = shift;
if($value ne $self->{muggle}) {
$self->{mdbh}->disconnect() if($self->{mdbh});
$self->{mdbh} = &connectDB(
$value,
main::getGeneralConfig->{USR},
main::getGeneralConfig->{PWD},
$self->{charset}
);
}
return $value;
},
},
mugglei => {
description => sprintf(gettext("Path of command '%s'"),'mugglei'),
default => 'mugglei',
type => 'file',
},
AmazonToken => {
description => gettext('Access Key ID to gathering cover images (a 20-character, alphanumeric sequence). Please sign up at http://aws.amazon.com'),
default => '',
type => 'string',
},
AmazonSecretKey => {
description => gettext('Secret Access Key to gathering cover images (a 40-character sequence). Please sign up at http://aws.amazon.com'),
default => '',
type => 'string',
},
},
Commands => {
mrefresh => {
description => gettext('Rereading of the music directory.'),
short => 'mr',
callback => sub{ $self->refresh(@_) },
Level => 'admin',
DenyClass => 'mlist',
},
mcovers => {
description => gettext('Download album covers.'),
short => 'mc',
callback => sub{ $self->getcovers(@_) },
Level => 'admin',
DenyClass => 'mlist',
},
mplay => {
description => gettext("Play music file 'fid'"),
short => 'mp',
callback => sub{ $self->play(@_) },
DenyClass => 'stream',
},
mplaylist => {
description => gettext("Get a m3u playlist for 'fid'"),
short => 'm3',
callback => sub{ $self->playlist(@_) },
DenyClass => 'stream',
binary => 'nocache'
},
mlist => {
description => gettext("Shows music 'dir'"),
short => 'ml',
callback => sub{ $self->list(@_) },
DenyClass => 'mlist',
},
msearch => {
description => gettext("Search music 'txt'"),
short => 'ms',
callback => sub{ $self->search(@_) },
DenyClass => 'mlist',
},
mcoverimage => {
description => gettext('Show album covers.'),
short => 'mi',
callback => sub{ $self->coverimage(@_) },
DenyClass => 'mlist',
binary => 'cache'
},
mgetfile => {
description => gettext("Get music file 'fid'"),
short => 'mg',
callback => sub{ $self->getfile(@_) },
DenyClass => 'mlist',
binary => 'cache'
},
msuggest => {
hidden => 'yes',
callback => sub{ $self->suggest(@_) },
DenyClass => 'mlist',
},
},
};
return $args;
}
# ------------------
sub new {
# ------------------
my($class, %attr) = @_;
my $self = {};
bless($self, $class);
$self->{charset} = delete $attr{'-charset'};
if($self->{charset} eq 'UTF-8'){
eval 'use utf8';
}
# paths
$self->{paths} = delete $attr{'-paths'};
# who am I
$self->{MOD} = $self->module;
# all configvalues to $self without parents (important for ConfigModule)
map {
$self->{$_} = $attr{'-config'}->{$self->{MOD}->{Name}}->{$_};
$self->{$_} = $self->{MOD}->{Preferences}->{$_}->{default} unless($self->{$_});
} keys %{$self->{MOD}->{Preferences}};
# Try to use the Requirments
map {
eval "use $_";
if($@) {
my $m = (split(/ /, $_))[0];
return panic("\nCouldn't load perl module: $m\nPlease install this module on your system:\nperl -MCPAN -e 'install $m'");
}
} keys %{$self->{MOD}->{Prereq}};
# read the DB Handle
$self->{dbh} = delete $attr{'-dbh'};
# The Initprocess
my $erg = $self->_init or return error('Problem to initialize modul!');
return $self;
}
# ------------------
sub _init {
# ------------------
my $self = shift || return error('No object defined!');
return 1
if($self->{active} eq 'n');
#create an instance to find all files below /usr/local/mp3
$self->{ICE} = MP3::Icecast->new();
# $self->{ICE}->recursive(1);
# Use "file::find" & "add_file" instead of use "add_directory"
# avoid dead of modul via link-loops like cd /mp3; ln -s foo ../mp3
# $self->{ICE}->add_directory($self->{path});
find( {
wanted => sub{
if($File::Find::name =~ /\.mp3$/sig) { # Lookup for *.mp3
if(-r $File::Find::name) {
$self->{ICE}->add_file($File::Find::name)
} else {
lg "Permissions deny, couldn't read : $File::Find::name";
}
}
},
follow => 1,
follow_skip => 2
},
$self->{path}
);
$self->{SOCK} = IO::Socket::INET->new(
LocalPort => $self->{port}, #standard Icecast port
LocalAddr => $self->{Interface},
Listen => $self->{clients},
Proto => 'tcp',
Reuse => 1,
Timeout => 3600
) or return error("Couldn't create socket: $!");;
my $channels;
Event->io(
fd => $self->{SOCK},
prio => -1, # -1 very hard ... 6 very low
cb => sub {
# accept client
my $client = $self->{SOCK}->accept;
panic "Couldn't connect to new icecast client." and return unless $client;
$client->autoflush;
# make "channel" number
my $channel=++$channels;
# install a communicator
Event->io(
fd => $client,
prio => -1, # -1 very hard ... 6 very low
poll => 'r',
cb => sub {
my $watcher = shift;
# report
lg(sprintf("Talking on icecast channel %d", $channel));
# read new line and report it
my $handle=$watcher->w->fd;
my $data = $self->parseRequest($handle);
my $files = $self->handleInput($data);
unless(ref $files eq 'ARRAY') {
$watcher->w->cancel;
$client->close();
undef $watcher;
return 1;
}
$self->stream($files, $client);
$watcher->w->cancel;
undef $watcher;
$client->close;
},
);
# report
lg(sprintf("Open new icecast channel %d", $channel));
},
);
main::after(sub{
$self->{mdbh} = &connectDB(
$self->{muggle},
main::getGeneralConfig->{USR},
main::getGeneralConfig->{PWD},
$self->{charset}
);
unless($self->{mdbh}) {
unless($self->{dbh}) {
panic("Session to database is'nt connected");
return 0;
}
debug("Database 'GiantDisc' not found! Fallback to own internal music table!");
my $version = 26; # Must be increment if rows of table changed
# this tables hasen't handmade user data,
# therefore old table could dropped if updated rows
if(!tableUpdated($self->{dbh},'MUSIC',$version,1)) {
return 0;
}
$self->{dbh}->do(qq|
CREATE TABLE IF NOT EXISTS MUSIC (
Id int(11) unsigned auto_increment NOT NULL,
FILE text NOT NULL,
ARTIST varchar(128) default 'unknown',
ALBUM varchar(128) default 'unknown',
TITLE varchar(128) default 'unknown',
COMMENT varchar(128),
TRACKNUM varchar(10) default '0',
YEAR smallint(4) unsigned,
GENRE varchar(128),
BITRATE smallint(4) unsigned,
FREQUENCY varchar(4),
SECS int (11) NOT NULL,
PRIMARY KEY (ID)
) COMMENT = '$version'
|);
$self->{fields} = fields($self->{dbh}, 'SELECT SQL_CACHE * from MUSIC');
# Read File to Database, if the DB empty and Musicdir exists
$self->refresh()
unless($self->{dbh}->selectrow_arrayref("SELECT SQL_CACHE count(*) from MUSIC")->[0]);
}
return 1;
}, "MUSIC: Connect to database ...");
1;
}
# ------------------
sub refresh {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift;
my $config = shift;
my $dbh = ($self->{mdbh} ? $self->{mdbh} : $self->{dbh});
if( ref $console and not -d $self->{path} ) {
my $errmsg = sprintf(gettext("Directory of the music files '%s' not found"), $self->{path});
error($errmsg);
$console->err($errmsg);
$console->link({
text => gettext("Back to music list"),
url => "?cmd=mlist",
}) if($console->typ eq 'HTML');
return;
}
if($self->{mugglei} and $self->{mdbh}) {
my $usr = main::getGeneralConfig->{USR};
my $pwd = main::getGeneralConfig->{PWD};
my $host = (split(/ /, $dbh->{'mysql_hostinfo'}))[0];
# /usr/local/bin/mugglei -h 127.0.0.1 -c -u xpix -w xpix97 -t /NAS/Music .
my $command = sprintf('%s -h %s -z -c -u %s -w %s -t %s . 2>&1',
$self->{mugglei}, lc($host), $usr, $pwd, $self->{path});
lg sprintf("Execute: cd '%s';%s",$self->{path},$command);
chdir($self->{path});
my @erg = (`$command`);
if( ref $console) {
$console->message(gettext("Reread the music files ..."));
$console->link({
text => gettext("Back to music list"),
url => "?cmd=mlist",
}) if($console->typ eq 'HTML');
}
undef $self->{GENRES}; # delete genres cache
return 1;
}
my $waiter;
# Show waiter, early as is possible
if(ref $console && $console->typ eq 'HTML') {
$waiter = $console->wait(gettext("Get information from music files ..."), 0, 1000, 'no');
}
lg('Please wait! I search for new Musicfiles!');
#create an instance to find all files below /usr/local/mp3
$self->{ICE} = MP3::Icecast->new();
$self->{ICE}->recursive(1);
$self->{ICE}->add_directory($self->{path});
$self->{CACHE} = {};
my $data = $dbh->selectall_hashref("SELECT SQL_CACHE ID, FILE from MUSIC", 'FILE');
my @files = $self->{ICE}->files;
lg sprintf('Found %d music files !', scalar @files);
return unless(scalar @files);
if( ref $console and not scalar @files ) {
# last call of waiter
$waiter->end() if(ref $waiter);
$console->start() if(ref $waiter);
$console->err(gettext("No music files found!"));
$console->link({
text => gettext("Back to music list"),
url => "?cmd=mlist",
}) if($console->typ eq 'HTML');
return;
}
# Adjust waiter max value now.
$waiter->max(scalar @files)
if(ref $waiter);
my $c = 0;
my $new = 0;
foreach my $file (@files) {
++$c;
$waiter->next($c)
if(ref $waiter);
next if(delete $data->{$file});
my $info = MP3::Info->new($file);
$new++
if($self->insert($info));
}
foreach my $f (sort keys %$data) {
unless(-e $f) {
$dbh->do(sprintf('DELETE FROM MUSIC WHERE ID = %lu', $data->{$f}->{ID}));
}
}
# last call of waiter
$waiter->end() if(ref $waiter);
if(ref $console) {
$console->start()
if(ref $waiter);
my $msg = sprintf(gettext("%d new music files in database saved and %d non exists entries deleted!"), $new, scalar keys %$data);
$console->message($msg);
lg $msg;
$console->link({
text => gettext("Back to music list"),
url => "?cmd=mlist",
}) if($console->typ eq 'HTML');
}
}
# ------------------
sub play {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $data = shift || return error('No data defined!');
debug sprintf('Call play%s',
( $console->{USER} && $console->{USER}->{Name} ? sprintf(' from user: %s', $console->{USER}->{Name}) : "" )
);
$console->player("?cmd=mplaylist&data=${data}");
}
# ------------------
sub playlist {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $data = shift || return error('No data defined!');
my $dbh = ($self->{mdbh} ? $self->{mdbh} : $self->{dbh});
my $host = main::getModule('STREAM')->{host} || main::getModule('STATUS')->IP;
my $output;
foreach my $id (split('_', $data)) {
my $data;
if($self->{mdbh}) {
$data = $dbh->selectrow_hashref("SELECT SQL_CACHE * from tracks where id = '$id'");
} else {
$data = $dbh->selectrow_hashref("SELECT SQL_CACHE * from MUSIC where ID = '$id'");
}
next unless($data);
$output .= "#EXTM3U\r\n" unless($output);
my $file;
my $proxy = $self->{proxy};
$proxy =~ s/^\s+//; # no leading white space
$proxy =~ s/\s+$//; # no trailing white space
if(length($proxy)) {
$file = sprintf('%s/?cmd=play&data=%s&field=id', $proxy, $id);
} else {
$file = sprintf('http://%s:%lu/?cmd=play&data=%s&field=%s', $host, $self->{port}, $id, ($self->{mdbh} ? 'id' : 'ID'));
}
if($self->{mdbh}) {
$output .= sprintf("#EXTINF:%d,%s - %s (%s)\r\n",$data->{'length'},$data->{title},$data->{artist},$data->{sourceid});
} else {
$output .= sprintf("#EXTINF:%d,%s - %s (%s)\r\n",$data->{SECS},$data->{TITLE},$data->{ARTIST},$data->{ALBUM});
}
$output .= sprintf("%s\r\n", $file);
}
if($output && $console->typ eq 'HTML') {
$console->{nopack} = 1;
my $arg;
$arg->{'attachment'} = "playlist.m3u";
$arg->{'Content-Length'} = length($output);
$console->out($output, "audio/x-mpegurl", %{$arg} );
} else {
$console->err(gettext("Sorry, playback is'nt supported"));
}
}
# ------------------
sub search {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $text = shift;
unless($text) {
error("No text to search defined! Please use msearch 'text'");
return $self->list($console, $config);
} else {
return $self->list($console, $config, "search:".$text);
}
}
# ------------------
sub list {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $param = shift;
my $dbh = $self->{mdbh} ? $self->{mdbh} : $self->{dbh};
unless($dbh) {
error ("Couldn't connect to database");
return $console->err(gettext("Couldn't connect to database"));
}
unless($param) {
if($self->{mdbh}) {
my $eg = $dbh->selectrow_arrayref('SELECT cddbid from album order by artist,title limit 1');
unless($eg) {
error sprintf("Couldn't execute query: %s.",$dbh->errstr);
return $console->err($dbh->errstr);
}
$param = sprintf('cddbid:%s', $eg->[0]);
} else {
my $eg = $dbh->selectrow_arrayref('SELECT SQL_CACHE ALBUM from MUSIC order by ARTIST,TITLE limit 1');
unless($eg) {
error sprintf("Couldn't execute query: %s.",$dbh->errstr);
return $console->err($dbh->errstr);
}
$param = sprintf('album:%s', $eg->[0]);
}
}
my @field = split(':',$param);
my $typ = $field[0];
shift @field;
my $text = join(':',@field);
my $search = '';
my $term;
if($typ eq 'search') {
if($self->{mdbh}) {
my $query = buildsearch("album.artist,tracks.artist,album.title,tracks.title,album.covertxt",$text);
$search = $query->{query};
foreach(@{$query->{term}}) { push(@{$term},$_); }
foreach(@{$query->{term}}) { push(@{$term},$_); } #double for UNION
} else {
my $query = buildsearch("ALBUM,ARTIST,TITLE,COMMENT",$text);
$search = $query->{query};
foreach(@{$query->{term}}) { push(@{$term},$_); }
}
} else {
# assign xxv tables to giantdisc table name
my $translate = {
cddbid => 'cddbid',
artist => 'artist',
album => 'title',
genre => 'genre1',
title => 'title',
year => 'year'
};
my $t;
if($typ eq 'all') {
} elsif($typ eq 'genre') {
$t = ($self->{mdbh} ? 'tracks.'.$translate->{$typ} : uc($typ));
# caching genres
$self->{GENRES} = $dbh->selectall_hashref('SELECT * from genre', 'id')
if($self->{mdbh} && !$self->{GENRES});
$text = $self->{GENRES}->{$text}->{id} if($self->{mdbh});
} elsif($typ eq 'year') {
$t = ($self->{mdbh} ? 'tracks.'.$translate->{$typ} : uc($typ));
} elsif($typ eq 'album') {
$t = ($self->{mdbh} ? 'album.'.$translate->{$typ} : uc($typ));
} elsif($typ eq 'cddbid') {
$t = ($self->{mdbh} ? 'album.'.$translate->{$typ} : uc($typ));
} else {
$t = ($self->{mdbh} ? 'tracks.'.$translate->{$typ} : uc($typ));
}
if($typ eq 'all') {
$search = '1';
} elsif($typ eq 'genre' && $self->{mdbh}) {
$search = sprintf("%s LIKE ?", $t); #?%
push(@{$term},$text.'%');
} else {
$search = sprintf("%s RLIKE ?", $t); #%?%
push(@{$term},$text);
push(@{$term},$text) if($self->{mdbh});
}
}
my %f = (
'Id' => gettext('Service'),
'Artist' => gettext('Artist'),
'Album' => gettext('Album'),
'Title' => gettext('Title'),
'Tracknum' => gettext('Number of track'),
'Year' => gettext('Year'),
'Length' => gettext('Length')
);
my $sql;
if($self->{mdbh}) {
$sql = qq|
SELECT
tracks.id as \'$f{'Id'}\',
tracks.artist as \'$f{'Artist'}\',
album.title as \'$f{'Album'}\',
tracks.title as \'$f{'Title'}\',
tracks.tracknb as \'$f{'Tracknum'}\',
tracks.year as \'$f{'Year'}\',
IF(tracks.length >= 3600,SEC_TO_TIME(tracks.length),DATE_FORMAT(FROM_UNIXTIME(tracks.length), '%i:%s')) as \'$f{'Length'}\',
genre.genre as __GENRE,
album.covertxt as __COMMENT
FROM
tracks, album, genre
WHERE
tracks.sourceid = album.cddbid and
tracks.genre1 = genre.id and
$search
|;
$sql .= qq|
UNION
SELECT
tracks.id as \'$f{'Id'}\',
tracks.artist as \'$f{'Artist'}\',
album.title as \'$f{'Album'}\',
tracks.title as \'$f{'Title'}\',
tracks.tracknb as \'$f{'Tracknum'}\',
tracks.year as \'$f{'Year'}\',
IF(tracks.length >= 3600,SEC_TO_TIME(tracks.length),DATE_FORMAT(FROM_UNIXTIME(tracks.length), '%i:%s')) as \'$f{'Length'}\',
"" as __GENRE,
album.covertxt as __COMMENT
FROM
tracks, album
WHERE
tracks.sourceid = album.cddbid and
tracks.genre1 = 'NULL' and
$search
| if($typ ne 'genre');
$sql .= qq|
ORDER BY
\'$f{'Album'}\',
\'$f{'Tracknum'}\'
|;
} else {
$sql = qq|
SELECT
ID as \'$f{'Id'}\',
ARTIST as \'$f{'Artist'}\',
ALBUM as \'$f{'Album'}\',
TITLE as \'$f{'Title'}\',
TRACKNUM as \'$f{'Tracknum'}\',
YEAR as \'$f{'Year'}\',
IF(SECS >= 3600,SEC_TO_TIME(SECS),DATE_FORMAT(FROM_UNIXTIME(SECS), '%i:%s')) as \'$f{'Length'}\',
GENRE as __GENRE,
COMMENT as __COMMENT
FROM
MUSIC
WHERE
1 AND
$search
ORDER BY
FILE
|;
}
my $rows;
my $sth;
my $limit = $console->{cgi} && $console->{cgi}->param('limit') ? CORE::int($console->{cgi}->param('limit')) : 0;
if($limit > 0) {
# Query total count of rows
my $rsth = $dbh->prepare($sql);
$rsth->execute(@{$term})
or return error sprintf("Couldn't execute query: %s.",$rsth->errstr);
$rows = $rsth->rows;
if($rows <= $limit) {
$sth = $rsth;
} else {
# Add limit query
if($console->{cgi}->param('start')) {
$sql .= " LIMIT " . CORE::int($console->{cgi}->param('start'));
$sql .= "," . $limit;
} else {
$sql .= " LIMIT " . $limit;
}
}
}
unless($sth) {
$sth = $dbh->prepare($sql);
$sth->execute(@{$term})
or return error sprintf("Couldn't execute query: %s.",$sth->errstr);
$rows = $sth->rows unless($rows);
}
my $fields = $sth->{'NAME'};
my $erg = $sth->fetchall_arrayref();
if($console->typ ne 'AJAX') {
unshift(@$erg, $fields);
}
my $info = {
rows => $rows
};
if($console->typ eq 'HTML') {
$info->{albums} = ($self->{mdbh} ? $self->GroupArray('title', 'album', 'cddbid') : $self->GroupArray('ALBUM'));
$info->{artists} = ($self->{mdbh} ? $self->GroupArray('artist', 'tracks', 'id'): $self->GroupArray('ARTIST'));
$info->{genres} = $self->GenreArray();
$info->{getCover} = sub{ return $self->_findcoverfromcache(@_, 'relative') };
$console->setCall('mlist');
}
$console->table($erg, $info);
}
# ------------------
sub handleInput {
# ------------------
my $self = shift || return error('No object defined!');
my $data = shift || return error('No request defined!');
my $cgi = CGI->new( $data->{Query} );
my $ucmd = $cgi->param('cmd') || 'play';
my $ufield = $cgi->param('field') || ($self->{mdbh} ? 'id' : 'ID');
my $udata = $cgi->param('data') || '*';
my $dbh = ($self->{mdbh} ? $self->{mdbh} : $self->{dbh});
return 0
if(!$dbh);
$dbh->{InactiveDestroy} = 1;
my $ldbh = $dbh->clone();
my $files;
if($ucmd eq 'play' and $ufield and my @search = split(',',$udata)) {
$files = $self->field2path($ldbh, $ufield, \@search);
} else {
return error "I don't understand this command '$ucmd'";
}
return $files;
}
# ------------------
sub field2path {
# ------------------
my $self = shift || return error('No object defined!');
my $dbh = shift || return error('No dbh defined!');
my $field = shift || return error('No field defined!');
my $data = shift || return error('No data defined!');
my $pathfield;
my $sql;
map {$_ = $dbh->quote($_)} @$data;
if($self->{mdbh}) {
$pathfield = 'mp3file';
$sql = sprintf "SELECT SQL_CACHE %s, %s from tracks", $pathfield, $field;
} else {
$pathfield = 'FILE';
$sql = sprintf "SELECT SQL_CACHE %s, %s from MUSIC", $pathfield, $field;
}
$sql .= sprintf " where %s in (%s)", $field, join(',', @$data)
if($data->[0] ne '*');
my $ret = $dbh->selectall_hashref($sql, $pathfield);
my @files = sort keys %$ret;
return \@files;
}
# ------------------
sub insert {
# ------------------
my $self = shift || return error('No object defined!');
my $data = shift || return 0;
my @setdata;
foreach my $name (keys %$data) {
next unless(grep($name eq $_, @{$self->{fields}}));
push(@setdata, sprintf("%s=%s", $name, $self->{dbh}->quote($data->{$name})));
}
# MD5(File) as ID
my $sql = sprintf('INSERT INTO MUSIC SET %s', join(', ', @setdata));
$self->{dbh}->do( $sql );
return 1;
}
# ------------------
sub stream {
# ------------------
my $self = shift || return error('No object defined!');
my $files = shift || return error('No file defined!');
my $client = shift || return error('No client defined!');
my %seen = ();
my @uniqu = grep { ! $seen{$_} ++ } @$files;
defined(my $child = fork()) or die "Couldn't fork: $!";
if($child == 0) {
$self->{SOCK}->close;
$self->{dbh}->{InactiveDestroy} = 1;
if($self->{mdbh}) {
$self->{mdbh}->{InactiveDestroy} = 1;
}
foreach my $file (@uniqu) {
$file = $self->{path} . "/" . $file
if($self->{mdbh});
debug sprintf('Stream file "%s"',$file);
my $erg = $self->{ICE}->stream($file,0,$client)
|| last;
}
exit 0;
}
}
# ------------------
sub parseRequest {
# ------------------
my $self = shift || return error('No object defined!');
my $hdl = shift || return error('No request defined!');
my ($Req, $size) = getFromSocket($hdl);
if(ref $Req eq 'ARRAY' and $Req->[0] =~ /^GET (\/[\w\.\/-\:]*)([\?[\w=&\.\+\%-\:\!]*]*)[\#\d ]+HTTP\/1.\d$/) {
my $data = {};
($data->{Request}, $data->{Query}) = ($1, $2 ? substr($2, 1, length($2)) : undef);
# parse header
foreach my $line (@$Req) {
if($line =~ /Referer: (.*)/) {
$data->{Referer} = $1;
}
if($line =~ /Host: (.*)/) {
$data->{HOST} = $1;
}
if($line =~ /Authorization: basic (.*)/i) {
($data->{username}, $data->{password}) = split(":", MIME::Base64::decode_base64($1), 2);
}
if($line =~ /User-Agent: (.*)/i) {
$data->{http_useragent} = $1;
}
}
# Log like Apache Format ip, resolved hostname, user, method request, status, bytes, referer, useragent
lg sprintf('%s - %s "%s %s%s" %s %s "%s" "%s"',
getip($hdl),
$data->{username} ? $data->{username} : "-",
"GET", #$data->{Method},
$data->{Request} ? $data->{Request} : "",
$data->{Query} ? "?" . $data->{Query} : "",
"-", #$console->{'header'},
"-", #$console->{'sendbytes'},
$data->{Referer} ? $data->{Referer} : "-",
"-" #$data->{http_useragent} ? $data->{http_useragent} : ""
);
return $data;
} else {
error sprintf(" Unknown Request : %s", join("\n", @$Req));
return undef;
}
}
# ------------------
sub GroupArray {
# ------------------
my $self = shift || return error('No object defined!');
my $field = shift || return undef;
my $table = shift;
my $idfield = shift;
my $search = shift;
my $limitquery = shift;
my $dbh = ($self->{mdbh} ? $self->{mdbh} : $self->{dbh});
my $where = '';
$where = sprintf("WHERE %s LIKE '%%%%%s%%%%'",$field, $search)
if($search);
my $limit = '';
$limit = sprintf("LIMIT %s",$limitquery)
if($limitquery && $limitquery > 0);
my $sql;
if($self->{mdbh}) {
$sql = sprintf('SELECT SQL_CACHE %s, %s from %s %s group by %s order by %s %s', $field, $idfield, $table, $where, $field, $field, $limit);
} else {
$sql = sprintf('SELECT SQL_CACHE %s, ID from MUSIC %s group by %s order by %s %s %s ', $field, $where, $field, $field, $limit);
}
my $erg = $dbh->selectall_arrayref($sql);
return $erg;
}
# ------------------
sub GenreArray {
# ------------------
my $self = shift || return error('No object defined!');
my $dbh = ($self->{mdbh} ? $self->{mdbh} : $self->{dbh});
my $sql;
if($self->{mdbh}) {
$sql = "SELECT SQL_CACHE genre, genre.id as id from genre,tracks where genre.id = tracks.genre1 group by id order by id";
} else {
my $field = 'genre';
$sql = sprintf('SELECT SQL_CACHE %s, %s from MUSIC group by %s order by %s', $field, $field, $field, $field);
}
my $erg = $dbh->selectall_arrayref($sql);
return $erg;
}
# ------------------
sub status {
# ------------------
my $self = shift || return error('No object defined!');
my $lastReportTime = shift || 0;
return
if($self->{active} eq 'n');
my $report = {};
if($self->{mdbh}) {
$report->{FILE} = $self->{mdbh}->selectrow_arrayref('SELECT SQL_CACHE count(*) from tracks')->[0];
$report->{ALBUM} = $self->{mdbh}->selectrow_arrayref('SELECT SQL_CACHE count(*) from album')->[0];
my $d = $self->{mdbh}->selectall_arrayref('SELECT SQL_CACHE artist from tracks group by artist');
$report->{ARTIST} = scalar @$d;
$d = $self->{mdbh}->selectall_arrayref('SELECT SQL_CACHE genre1 from tracks group by genre1');
$report->{GENRE} = scalar @$d;
} else {
foreach my $field (qw/FILE ALBUM ARTIST GENRE/) {
my $data = $self->GroupArray($field);
$report->{$field} = scalar @$data;
}
}
return {
message => sprintf(gettext('Music database contains %d entries with %d albums from %d artists in %d genres'),
$report->{FILE}, $report->{ALBUM},$report->{ARTIST}, $report->{GENRE}),
};
}
# ------------------
sub _storecover {
# ------------------
my $self = shift || return error('No object defined!');
my $image = shift || return 0;
my $target = shift;
# Avoid empty hash
if($image && ref $image eq 'HASH') {
my $hash = $image;
$image = undef;
foreach my $i (keys %$hash) {
$image = $hash->{$i};
last;
}
}
if($image) {
lg sprintf("Try to get cover %s", $image);
return 1 if(is_success(getstore($image, $target)));
}
return 0;
}
# ------------------
sub getcovers {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift;
my $config = shift;
my $force = shift;
my $dbh = ($self->{mdbh} ? $self->{mdbh} : $self->{dbh});
return $console->error(gettext('No Amazon Web Service (AWS) access key identifiers token exists. Please sign up at http://aws.amazon.com .'))
unless($self->{AmazonToken} && $self->{AmazonSecretKey});
$self->{Amazon} = Net::Amazon->new(
token => $self->{AmazonToken}
,secret_key => $self->{AmazonSecretKey}
) unless($self->{Amazon});
return error('No valid Amazon token exists. Please sign up at http://aws.amazon.com')
unless($self->{Amazon});
debug sprintf('Call getcovers%s',
( $console->{USER} && $console->{USER}->{Name} ? sprintf(' from user: %s', $console->{USER}->{Name}) : "" )
);
my $waiter = $console->wait(gettext("Please wait, search for new covers ..."),0,1000,'no')
if(ref $console);
unless(-d $self->{coverimages}) {
mkpath($self->{coverimages}) or error "Couldn't mkpath $self->{coverimages} : $!";
lg sprintf('mkdir path "%s"',
$self->{coverimages}
);
}
my $rob = main::getModule('ROBOT')
or return error('No ROBOT Module installed!');
$rob->saveRobot('coverimage', sub{
my $artist = shift || return 0, "Missing artist";
my $album = shift || return 0, "Missing album";
my $year = shift || 0;
my $target = shift || return 0, "Missing target";
my $current = shift || 0;
my $msg = sprintf(gettext("Lookup for cover from '%s-%s'"), $artist,$album);
lg $msg;
# Anzeige der ProcessBar
$waiter->next($current,undef, $msg) if(ref $waiter);
my $req = Net::Amazon::Request::Artist->new(
artist => $artist,
);
my $resp = $self->{Amazon}->request($req);
$album =~ s/[^[:alnum:]]//sig;
$artist =~ s/[^[:alnum:]]//sig;
my $image;
foreach my $item ($resp->properties) {
next unless($item->can('album'));
my $ialbum = $item->album();
$ialbum =~ s/[^[:alnum:]]//sig;
next unless($item->can('artist'));
my $iartist = $item->artist();
$iartist =~ s/[^[:alnum:]]//sig;
if($ialbum =~ /$album/i
and $iartist =~ /$artist/i) {
$image = $item->ImageUrlMedium()
if($item->can('ImageUrlMedium'));
last if($image && $self->_storecover($image,$target));
$image = $item->ImageUrlLarge()
if($item->can('ImageUrlLarge'));
last if($image && $self->_storecover($image,$target));
$image = $item->ImageUrlSmall()
if($item->can('ImageUrlSmall'));
last if($image && $self->_storecover($image,$target));
}
}
return 1;
});
my $erg;
if($self->{mdbh}) {
$erg = $dbh->selectall_hashref('SELECT SQL_CACHE DISTINCT t.id as ID,t.mp3file as FILE, a.artist as ARTIST, a.title as ALBUM, t.year as YEAR from album as a, tracks as t where a.cddbid = t.sourceid group by a.title', 'ID');
} else {
$erg = $dbh->selectall_hashref('SELECT SQL_CACHE DISTINCT Id as ID, FILE, ARTIST, ALBUM, YEAR from MUSIC group by ALBUM', 'ID');
}
my $current = 0;
foreach my $id (sort keys %$erg) {
my $e = $erg->{$id};
my $file = sprintf('%s/%s', $self->{path}, $e->{FILE});
my $target = $self->_findcover($file,$e->{ARTIST},$e->{ALBUM});
next if($target and -e $target and not $force);
my $dest = $self->_findcoverfromcache($e->{ALBUM},$e->{ARTIST});
$rob->register('coverimage', $e->{ARTIST}, $e->{ALBUM}, $e->{YEAR}, $dest, ++$current);
}
# Adjust waiter max value now.
$waiter->max($current || 1)
if(ref $waiter);
if(ref $waiter and $current) {
$waiter->endcallback(
sub{
if(ref $console) {
$console->start();
$console->message(my $msg = gettext("New covers search was successfully!"));
lg sprintf($msg);
$console->link({
text => gettext("Back to music list"),
url => "?cmd=mlist",
}) if($console->typ eq 'HTML');
$console->footer();
}
}
);
}
if(ref $waiter and not $current) {
$waiter->endcallback(
sub{
if(ref $console) {
$console->start();
$console->message(gettext("It is not necessary to look for new covers because already all albums possess cover!"));
$console->link({
text => gettext("Back to music list"),
url => "?cmd=mlist",
}) if($console->typ eq 'HTML');
$console->footer();
}
}
);
lg sprintf('All covers exists!');
}
# Start Robots
$rob->start( 'coverimage', sub{ $waiter->end if(ref $waiter and $current); } );
return $erg;
}
# ------------------
sub _findcoverfromcache {
# ------------------
my $self = shift || return error('No object defined!');
my $album = shift || return error('No album defined!');
my $artist = shift || 0;
my $typ = shift || 'absolute';
my $absolute;
my $relative;
if($artist) {
$absolute = sprintf('%s/%s-%s.jpg', $self->{coverimages}, $self->unique($artist), $self->unique($album));
$relative = sprintf('/coverimages/%s-%s.jpg', $self->unique($artist), $self->unique($album));
} else {
$absolute = sprintf('%s/%s.jpg', $self->{coverimages}, $self->unique($album));
$relative = sprintf('/coverimages/%s.jpg', $self->unique($album));
}
return $absolute
if($typ eq 'absolute');
return $relative
if(-r $absolute);
lg sprintf("Don't find cover for %s - %s, as file %s",$artist,$album,$absolute);
return undef;
}
# ------------------
sub unique {
# ------------------
my $self = shift || return error('No object defined!');
my $text = shift || return '';
$text =~ s/[^0-9a-z]//sig;
return $text;
}
# ------------------
sub _findcover {
# ------------------
my $self = shift || return error('No object defined!');
my $file = shift || return error('No file defined!');
my $artist = shift;
my $album = shift;
my $coverimage;
my $directory = dirname($file);
if($self->{coverimages} && -d $self->{coverimages}) {
my $cache = $self->_findcoverfromcache($album,$artist);
$coverimage = $cache
if($cache && -r $cache);
}
if(!$coverimage && -d $directory) {
my @images = [];
find(
{
wanted => sub{
if($File::Find::name =~ /\.jpg$|\.jpeg$|\.gif$|\.png/sig) { # Lookup for images
if(-r $File::Find::name) {
push(@images,$File::Find::name)
} else {
lg "Permissions deny, couldn't read : $File::Find::name";
}
}
},
follow => 1,
follow_skip => 2
},
$directory
);
# An image in the same directory as the song, named like the song but with the
# song extension replaced with the image format extension
# e.g. test.mp3 -> test.jpg
my $song = basename($file);
$song =~ s/([\)\(\-\?\+\*\[\]\{\}])/\\$1/g; # Replace regex groupsymbols "),(,-,?,+,*,[,],{,}"
$song =~ s/([\/])/\./g; # Replace splash
$song =~ s/(.*)\.mp3$/$1./ig;
my @f = grep { /$song/i } @images;
$coverimage = $f[0]
if(scalar @f > 0 && -r $f[0]);
if(!$coverimage && $artist) {
$artist =~ s/([\)\(\-\?\+\*\[\]\{\}])/\\$1/g; # Replace regex groupsymbols "),(,-,?,+,*,[,],{,}"
$artist =~ s/([\/])/\./g; # Replace splash
@f = grep { /\/$artist\./i } @images;
$coverimage = $f[0]
if(scalar @f > 0 && -r $f[0]);
}
if(!$coverimage && $album) {
$album =~ s/([\)\(\-\?\+\*\[\]\{\}])/\\$1/g; # Replace regex groupsymbols "),(,-,?,+,*,[,],{,}"
$album =~ s/([\/])/\./g; # Replace splash
@f = grep { /\/$album\./i } @images;
$coverimage = $f[0]
if(scalar @f > 0 && -r $f[0]);
}
# An image named "cover" with the image format extension in the same directory
# as the song (album cover).
# e.g. cover.gif
if(!$coverimage) {
@f = grep { /\/cover\./i } @images;
$coverimage = $f[0]
if(scalar @f > 0 && -r $f[0]);
}
# An image named "artist" with the image format extension in the parent
# directory of the song (artist image).
# e.g. artist.png
if(!$coverimage) {
@f = grep { /\/artist\./i } @images;
$coverimage = $f[0]
if(scalar @f > 0 && -r $f[0]);
}
# An image named "album" with the image format extension in the parent
# directory of the song (album image).
# e.g. album.png
if(!$coverimage) {
@f = grep { /\/album\./i } @images;
$coverimage = $f[0]
if(scalar @f > 0 && -r $f[0]);
}
# An image named "background" with the image format extension in the base
# directory of the MP3 source.
if(!$coverimage) {
@f = grep { /\/background\./i } @images;
$coverimage = $f[0]
if(scalar @f > 0 && -r $f[0]);
}
}
return $coverimage;
}
# ------------------
sub coverimage {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $data = shift || return error('No data defined!');
my $dbh = ($self->{mdbh} ? $self->{mdbh} : $self->{dbh});
if($dbh) {
my $sql;
my @id = split('_',$data);
my $coverimage;
map {$_ = $dbh->quote($_)} @id;
if($self->{mdbh}) {
$sql = sprintf qq|
SELECT SQL_CACHE id, mp3file as file,
tracks.artist as artist,
album.title as album
from tracks, album
where tracks.sourceid = album.cddbid
and id in (%s)|, join(',', @id);
} else {
$sql = sprintf qq|
SELECT SQL_CACHE ID as id,
FILE as file,
ARTIST as artist,
ALBUM as album
from MUSIC
where id in (%s)|, join(',', @id);
}
my $ret = $dbh->selectrow_hashref($sql);
if($ret && $ret->{'id'})
{
my $file = sprintf('%s/%s', $self->{path}, $ret->{'file'});
$coverimage = $self->_findcover($file,$ret->{'artist'},$ret->{'album'});
}
if($console->typ eq 'HTML') {
if($coverimage) {
$console->datei($coverimage);
} else {
my $HTTPD = main::getModule('HTTPD');
my $nocover = sprintf('%s/%s/images/nocover', $HTTPD->{paths}->{HTMLDIR}, $HTTPD->{HtmlRoot});
if(-r $nocover . ".png") {
$console->datei($nocover . ".png");
}
elsif(-r $nocover . ".gif") {
$console->datei($nocover . ".gif");
} else {
$nocover = sprintf('%s/default/images/nocover', $HTTPD->{paths}->{HTMLDIR});
if(-r $nocover . ".png") {
$console->datei($nocover . ".png");
} else {
$console->datei($nocover . ".gif");
}
}
}
}
return 1;
}
$console->err(gettext("Sorry, images for cover is'nt supported"));
return 0;
}
# ------------------
sub getfile {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $data = shift || return error('No data defined!');
my $dbh = ($self->{mdbh} ? $self->{mdbh} : $self->{dbh});
if($dbh) {
my $sql;
my @id = split('_',$data);
map {$_ = $dbh->quote($_)} @id;
if($self->{mdbh}) {
$sql = sprintf qq|
SELECT SQL_CACHE id, mp3file as file from tracks
where id in (%s)|, join(',', @id);
} else {
$sql = sprintf qq|
SELECT SQL_CACHE ID as id, FILE as file from MUSIC
where id in (%s)|, join(',', @id);
}
my $ret = $dbh->selectrow_hashref($sql);
if($ret
&& $ret->{'id'}
&& $ret->{'file'}
&& $console->typ eq 'HTML') {
$console->datei(sprintf('%s/%s', $self->{path}, $ret->{'file'}));
return 1;
}
}
$console->err(gettext("Sorry, couldn't get file."));
return 0;
}
# ------------------
sub suggest {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $search = shift;
my $params = shift;
if(exists $params->{get}) {
my $result;
$result = ($self->{mdbh} ? $self->GroupArray('title', 'album', 'cddbid',$search, 25) : $self->GroupArray('ALBUM',undef,undef,$search, 25))
if($params->{get} eq 'album');
$result = ($self->{mdbh} ? $self->GroupArray('artist', 'tracks', 'id',$search, 25): $self->GroupArray('ARTIST',undef,undef,$search, 25))
if($params->{get} eq 'artist');
$result = ($self->{mdbh} ? $self->GroupArray('title', 'tracks', 'id',$search, 25): $self->GroupArray('TITLE',undef,undef,$search, 25))
if($params->{get} eq 'title');
$console->table($result)
if(ref $console && $result);
}
}
1;
|