package joyproxy; use 5.018; use strict; use warnings; use utf8; use open qw (:std :utf8); use Fcntl; use HTTP::Tiny; use URI::URL; use URI::Escape qw (uri_unescape); use Exporter qw (import); use version; our $VERSION = qw (1.1.0); our @EXPORT_OK = qw (joyproxy joyurl); sub joyproxy ($) { my $str = shift; chomp $str; # formal url check if ($str !~ /^img\d+\.reactor\.cc/xmsg) { return ('500', 'text/plain', 'This is not reactor video'); } my ($file, $filesize) = __dlfunc ("http://$str"); if (defined $file) { my $buf = ''; if (open my $VID, '<', $file) { binmode $VID; my $readlen = read ($VID, $buf, $filesize); close $VID; if (defined $readlen) { if ($readlen != $filesize) { warn "Unable to read $file readlen does not match filesize: $readlen vs $filesize"; return ('500', 'text/plain', "Unable to read file in temporary location\n"); } } else { warn "Unable to read $file: $!"; return ('500', 'text/plain', "Unable to read file in temporary location: $!\n"); } if ($file =~ /\.mp4$/xmsgi) { return ('200', 'video/mp4', $buf); } elsif ($file =~ /\.webm$/xmsgi) { return ('200', 'video/webm', $buf); } else { return ('200', 'video/mpeg', $buf); } } else { if (-f $file) { unlink $file; } return ('500', 'text/plain', "Unable to open file in temporary location: $!\n"); } } else { return ('500', 'text/plain', "Unable to get file from remote source!\n"); } } sub joyurl (@) { my $str = shift; my $host = shift; # TODO: handle error here $str = uri_unescape $str; if (length ($str) < 60) { $str = ''; } else { $str = substr $str, 14; } if ($str =~ /^img\d+\.reactor\.cc/xmsg) { # img1.reactor.cc/pics/post/webm/видосик.webm my @url = split /\//msx, $str; if (($url[3] eq 'webm' || $url[3] eq 'mp4') && ($url[4] =~ /\.webm$/xmsg || $url[4] =~ /\.mp4$/xmsg)) { # we prefer mp4, right?, so my $fname; $fname = substr ($url[4], 0, -5) if (substr ($url[4], -5, 6) eq '.webm'); $fname = substr ($url[4], 0, -4) if (substr ($url[4], -4, 4) eq '.mp4'); $str = sprintf ( 'https://%s/joyproxy/%s/%s/%s/mp4/%s.mp4', $host, $url[0], $url[1], $url[2], $fname ); $str = __urlencode ($str); } else { $str = ''; } } else { $str = ''; } my $msg = << "EOL";