Statistics
| Branch: | Tag: | Revision:

dvbd / utils / cgi / media.cgi @ 723fa73a

History | View | Annotate | Download (1.87 KB)

1
#!/usr/bin/perl
2
use CGI;
3
use POSIX 'setsid';
4
use URI::Escape;
5
use HTML::Entities;
6
use File::Find;
7
use Date::Format;
8
use sort 'stable';
9

    
10
%catalogues = ("avi" => "/mnt/media2/avi", 
11
	       "recordings" => "/mnt/media1/recordings");
12

    
13
$q = new CGI;
14
print $q->header;
15

    
16
$cataloguename = $q->param('catalogue');
17
$catalogue = $catalogues{$cataloguename};
18
if (!defined($cataloguename) || !defined($catalogue)) {
19
    print "<pre>Unrecognised catalogue $cataloguename (edit media.cgi)</pre>\n";
20
    exit 1;
21
}
22

    
23
if (defined($q->param('play'))) {
24
    # Play a particular movie
25

    
26
    $file = $q->param('play');
27
    $file =~ s/;//g;
28
    $file =~ s/^[\/.]*//g;
29

    
30
    $movie = $catalogue . "/" . $file;
31
    print "<pre>Play movie $movie...\n</pre>";
32
    $SIG{'CHLD'}="IGNORE";
33
    if (fork() == 0) {
34
	daemonize();
35
	exec("/home/jak/bin/playmovie", $movie);
36
    }
37
} else {
38
    # Get available movies
39
    @files = ();
40
    find({ wanted => sub {
41
	push @files, $File::Find::name
42
	    if (/\.(avi|mpg|wmv)/ && -f $File::Find::name);
43
	$File::Find::prune = 1 
44
	    if (/\.AppleDouble/);
45
    } }, $catalogue );
46

    
47
    # Sort
48
    @files = sort { -M $a <=> -M $b } @files;
49

    
50
    $prefixlength = length($catalogue) + 1;
51
    foreach $file (@files) {
52
	$shortfile = substr($file, $prefixlength);
53
	$playurl = "?catalogue=" . uri_escape($cataloguename) . "&play=" . uri_escape($shortfile);
54
	$playanchor = "<a href=\"$playurl\">" . encode_entities($shortfile) . "</a>";
55
	print "<tt>" . ctime((stat $file)[9]) . "</tt> $playanchor<br>";
56
    }
57
}
58

    
59

    
60
sub daemonize {
61
    chdir '/'               or die "Can't chdir to /: $!";
62
    open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
63
    open STDOUT, '>/dev/null'
64
		   or die "Can't write to /dev/null: $!";
65
    defined(my $pid = fork) or die "Can't fork: $!";
66
    exit if $pid;
67
    setsid                  or die "Can't start a new session: $!";
68
    open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
69
}
70