Add joyproxy prototype
This commit is contained in:
103
lib/joyproxy.pm
Normal file
103
lib/joyproxy.pm
Normal file
@@ -0,0 +1,103 @@
|
||||
package joyproxy;
|
||||
|
||||
use strict;
|
||||
use warnings "all";
|
||||
use lib qw(./lib ./vendor_perl);
|
||||
use Fcntl qw(O_RDONLY O_WRONLY);
|
||||
use HTTP::Tiny;
|
||||
use URI::URL;
|
||||
|
||||
use Exporter qw(import);
|
||||
use vars qw/$VERSION/;
|
||||
$VERSION = "1.0";
|
||||
our @EXPORT = qw(joyproxy);
|
||||
|
||||
|
||||
sub joyproxy ($) {
|
||||
my $str = shift;
|
||||
chomp($str);
|
||||
|
||||
return ('500', 'text/plain', 'This is not reactor video') if ($str !~ /^img1\.reactor\.cc/);
|
||||
|
||||
my ($file, $filesize) = __dlfunc("http://$str");
|
||||
|
||||
if (defined($file)) {
|
||||
my $buf = '';
|
||||
|
||||
if (open(VID, '<', $file)) {
|
||||
read(VID, $buf, $filesize);
|
||||
close VID;
|
||||
|
||||
if ($file =~ /\.mp4/i) {
|
||||
return ('200', 'video/mp4', $buf);
|
||||
} elsif ($file =~ /\.webm/i) {
|
||||
return ('200', 'video/webm', $buf);
|
||||
} else {
|
||||
return ('200', 'video/mpeg', $buf);
|
||||
}
|
||||
} else {
|
||||
unlink $file if (-f $file);
|
||||
return ('500', 'text/plain', "Unable to open file in temporary loaction: $!\n");
|
||||
}
|
||||
} else {
|
||||
return ('500', 'text/plain', "Unable to get file from remote source!\n");
|
||||
}
|
||||
}
|
||||
|
||||
sub __urlencode($) {
|
||||
my $str = shift;
|
||||
my $urlobj = url $str;
|
||||
$str = $urlobj->as_string;
|
||||
undef $urlobj;
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
||||
sub __dlfunc($) {
|
||||
my $url = shift;
|
||||
|
||||
if (! -d "/tmp/joyproxy") {
|
||||
warn "no /tmp/joyproxy";
|
||||
return (undef, undef) unless (mkdir("/tmp/joyproxy"));
|
||||
}
|
||||
|
||||
my @tmparray = split(/\//, $url);
|
||||
my $file = $tmparray[@tmparray - 1];
|
||||
@tmparray = -1; undef @tmparray;
|
||||
$file = "/tmp/joyproxy/" . $file;
|
||||
$url = __urlencode($url);
|
||||
|
||||
my $http = HTTP::Tiny->new();
|
||||
my $response = $http->mirror(
|
||||
$url,
|
||||
$file,
|
||||
{
|
||||
headers => {
|
||||
"Accept" => '*/*',
|
||||
"Accept-Encoding" => 'identity;q=1, *;q=0',
|
||||
"Accept-Language" => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
|
||||
"Range" => 'bytes=0-',
|
||||
"Referer" => 'http://old.reactor.cc/all',
|
||||
"User-Agent" => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if ($response->{success}) {
|
||||
my $filesize = (stat($file))[7];
|
||||
|
||||
if ($filesize > 10485760) {
|
||||
unlink $file;
|
||||
return (undef, undef);
|
||||
} else {
|
||||
return ($file, $filesize);
|
||||
}
|
||||
} else {
|
||||
return (undef, undef);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
|
||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||
41
myapi-joyproxy.psgi
Normal file
41
myapi-joyproxy.psgi
Normal file
@@ -0,0 +1,41 @@
|
||||
use strict;
|
||||
use warnings "all";
|
||||
|
||||
# my plugins
|
||||
use lib qw(./lib ./vendor_perl ./vendor_perl/lib/perl5);
|
||||
use conf;
|
||||
use joyproxy;
|
||||
|
||||
my $CONF = loadConf();
|
||||
|
||||
if ($CONF->{api}->{prefix} eq '/') { $CONF->{api}->{prefix} = ''; }
|
||||
my $prefix = $CONF->{api}->{prefix};
|
||||
|
||||
my $app = sub {
|
||||
my $env = shift;
|
||||
|
||||
my $msg = "Your Opinion is very important for us, please stand by.\n";
|
||||
my $status = '404';
|
||||
my $content = 'text/plain';
|
||||
|
||||
if ($env->{PATH_INFO} =~ /$prefix\/joyproxy\/(.+)/) {
|
||||
my $joyproxyurl = $1;
|
||||
($status, $content, $msg) = ('400', $content, "Bad Request?\n");
|
||||
|
||||
if (($joyproxyurl =~ /\.mp4/i) or ($joyproxyurl =~ /\.webm/i)) {
|
||||
($status, $content, $msg) = joyproxy($joyproxyurl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return [
|
||||
$status,
|
||||
[ 'Content-Type' => $content, 'Content-Length' => length($msg) ],
|
||||
[ $msg ],
|
||||
];
|
||||
};
|
||||
|
||||
|
||||
__END__
|
||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||
Reference in New Issue
Block a user